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.xdocreport.jaxrs;
020
021import java.util.List;
022
023import javax.servlet.http.HttpServletRequest;
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.Produces;
027import javax.ws.rs.core.Context;
028import javax.ws.rs.core.MediaType;
029
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.template.api.TemplateProcessorService;
033import org.nuxeo.template.api.adapters.TemplateSourceDocument;
034
035/**
036 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
037 */
038public class ResourceService extends AbstractResourceService {
039
040    public ResourceService(CoreSession session) {
041        super(session);
042    }
043
044    @GET
045    @Path("name")
046    @Produces(MediaType.TEXT_PLAIN)
047    public String getName() {
048        return super.getName();
049    }
050
051    @Context
052    protected HttpServletRequest request;
053
054    public String getRoot() {
055        CoreSession session = getCoreSession();
056        TemplateProcessorService tps = Framework.getService(TemplateProcessorService.class);
057        List<TemplateSourceDocument> templates = tps.getAvailableTemplates(session, null);
058        StringBuffer sb = new StringBuffer();
059
060        sb.append("[");
061        for (TemplateSourceDocument t : templates) {
062            sb.append("{");
063            sb.append("\"label\":" + "\"" + t.getLabel() + "\",");
064            sb.append("\"name\":" + "\"" + t.getName() + "\",");
065            sb.append("\"id\":" + "\"" + t.getId() + "\"");
066            sb.append("},");
067        }
068
069        String result = sb.toString();
070        result = result.substring(0, result.length() - 2) + "]";
071
072        return result;
073    }
074}