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    protected void initialize(Object... args) {
048        if (args != null && args.length > 0) {
049            this.uuid = (String) args[0];
050        }
051    }
052
053    public TemplateResource(String value) {
054        this.uuid = value;
055    }
056
057    protected TemplateSourceDocument resolve() {
058        if (uuid != null) {
059            IdRef idRef = new IdRef(uuid);
060            DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
061            return doc.getAdapter(TemplateSourceDocument.class);
062        } else {
063            return null;
064        }
065    }
066
067    @GET
068    public Object get() {
069        TemplateSourceDocument source = resolve();
070        if (source == null) {
071            return getList();
072        } else {
073            StringBuffer sb = new StringBuffer();
074            sb.append(source.getId() + " - " + source.getLabel());
075            return sb.toString();
076        }
077    }
078
079    protected String getList() {
080
081        TemplateProcessorService tps = Framework.getService(TemplateProcessorService.class);
082        List<TemplateSourceDocument> sources = tps.getAvailableTemplates(getContext().getCoreSession(), null);
083
084        StringBuffer sb = new StringBuffer();
085        for (TemplateSourceDocument source : sources) {
086            sb.append(source.getId() + " - " + source.getName() + "-" + source.getLabel());
087            sb.append("\n");
088        }
089        return sb.toString();
090    }
091
092    @GET
093    @Path("resource/{resourceName}")
094    @Produces("*/*")
095    public Blob getResource(@PathParam(value = "resourceName") String resourceName) {
096        TemplateSourceDocument tmpl = resolve();
097        return getResource(tmpl, resourceName);
098    }
099
100    static Blob getResource(TemplateSourceDocument tmpl, String resourceName) {
101
102        BlobHolder bh = tmpl.getAdaptedDoc().getAdapter(BlobHolder.class);
103        if (bh != null) {
104            for (Blob blob : bh.getBlobs()) {
105                if (resourceName.equalsIgnoreCase(blob.getFilename())) {
106                    return blob;
107                }
108            }
109        }
110        return null;
111    }
112
113}