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.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    @Override
045    @GET
046    @Path("name")
047    @Produces(MediaType.TEXT_PLAIN)
048    public String getName() {
049        return super.getName();
050    }
051
052    @Context
053    protected HttpServletRequest request;
054
055    public String getRoot() {
056        CoreSession session = getCoreSession();
057        TemplateProcessorService tps = Framework.getService(TemplateProcessorService.class);
058        List<TemplateSourceDocument> templates = tps.getAvailableTemplates(session, null);
059        StringBuilder sb = new StringBuilder();
060
061        sb.append("[");
062        for (TemplateSourceDocument t : templates) {
063            sb.append("{")
064              .append("\"label\":")
065              .append("\"")
066              .append(t.getLabel())
067              .append("\",")
068              .append("\"name\":")
069              .append("\"")
070              .append(t.getName())
071              .append("\",")
072              .append("\"id\":")
073              .append("\"")
074              .append(t.getId())
075              .append("\"")
076              .append("},");
077        }
078
079        String result = sb.toString();
080        result = result.substring(0, result.length() - 2) + "]";
081
082        return result;
083    }
084}