001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.template.jaxrs;
020
021import java.util.List;
022
023import javax.ws.rs.GET;
024import javax.ws.rs.Path;
025import javax.ws.rs.PathParam;
026import javax.ws.rs.Produces;
027
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.webengine.model.WebObject;
033import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.template.api.TemplateProcessorService;
036import org.nuxeo.template.api.adapters.TemplateSourceDocument;
037
038@WebObject(type = "templateResource")
039public class TemplateResource extends DefaultObject {
040
041    protected String uuid;
042
043    public TemplateResource() {
044        uuid = null;
045    }
046
047    @Override
048    protected void initialize(Object... args) {
049        if (args != null && args.length > 0) {
050            this.uuid = (String) args[0];
051        }
052    }
053
054    public TemplateResource(String value) {
055        this.uuid = value;
056    }
057
058    protected TemplateSourceDocument resolve() {
059        if (uuid != null) {
060            IdRef idRef = new IdRef(uuid);
061            DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
062            return doc.getAdapter(TemplateSourceDocument.class);
063        } else {
064            return null;
065        }
066    }
067
068    @GET
069    public Object get() {
070        TemplateSourceDocument source = resolve();
071        if (source == null) {
072            return getList();
073        } else {
074            return source.getId() + " - " + source.getLabel();
075        }
076    }
077
078    protected String getList() {
079
080        TemplateProcessorService tps = Framework.getService(TemplateProcessorService.class);
081        List<TemplateSourceDocument> sources = tps.getAvailableTemplates(getContext().getCoreSession(), null);
082
083        StringBuilder sb = new StringBuilder();
084        for (TemplateSourceDocument source : sources) {
085            sb.append(source.getId())
086              .append(" - ")
087              .append(source.getName())
088              .append("-")
089              .append(source.getLabel())
090              .append("\n");
091        }
092        return sb.toString();
093    }
094
095    @GET
096    @Path("resource/{resourceName}")
097    @Produces("*/*")
098    public Blob getResource(@PathParam(value = "resourceName") String resourceName) {
099        TemplateSourceDocument tmpl = resolve();
100        return getResource(tmpl, resourceName);
101    }
102
103    static Blob getResource(TemplateSourceDocument tmpl, String resourceName) {
104
105        BlobHolder bh = tmpl.getAdaptedDoc().getAdapter(BlobHolder.class);
106        if (bh != null) {
107            for (Blob blob : bh.getBlobs()) {
108                if (resourceName.equalsIgnoreCase(blob.getFilename())) {
109                    return blob;
110                }
111            }
112        }
113        return null;
114    }
115
116}