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 javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.PathParam;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.webengine.model.WebObject;
027import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
028import org.nuxeo.runtime.transaction.TransactionHelper;
029
030/**
031 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
032 */
033@WebObject(type = "templateRoot")
034@Path("/templates")
035public class RootResource extends ModuleRoot {
036
037    @GET
038    public String index() {
039        String sid = getContext().getCoreSession().toString();
040        return "ok :sid =" + sid;
041    }
042
043    protected CoreSession getCoreSession() {
044        TransactionHelper.startTransaction();
045
046        return getContext().getCoreSession();
047    }
048
049    @GET
050    @Path("ping")
051    public String getPong() {
052        return "pong";
053    }
054
055    @Path("templates")
056    public Object getTemplates() {
057        return getContext().newObject("templateResource");
058    }
059
060    @Path("docs")
061    public Object getDocs() {
062        return getContext().newObject("templateBasedResource");
063    }
064
065    @Path("template/{id}")
066    public Object getTemplates(@PathParam(value = "id") String id) {
067        return getContext().newObject("templateResource", id);
068    }
069
070    @Path("doc/{id}")
071    public Object getDocs(@PathParam(value = "id") String id) {
072        return getContext().newObject("templateBasedResource", id);
073    }
074
075}