001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     matic
016 */
017package org.nuxeo.ecm.webengine.samples;
018
019import javax.ws.rs.GET;
020import javax.ws.rs.Path;
021import javax.ws.rs.Produces;
022import javax.ws.rs.WebApplicationException;
023import javax.ws.rs.core.Response;
024
025import org.nuxeo.ecm.webengine.model.Access;
026import org.nuxeo.ecm.webengine.model.WebObject;
027import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
028import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
029import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
030
031/**
032 * Web Engine Samples Root
033 * <p>
034 * This demonstrates how to define the entry point for a WebEngine module.
035 * <p>
036 * The module entry points are regular JAX-RS resources with an additional @WebObject annotation. This annotation is
037 * mainly used to specify the resource name. A Web Module is declared in the MANIFEST.MF using the directive
038 * {code}NuxeoWebModule{/code}. You can also configure a Web Module using a module.xml file located in the module root
039 * directory. This file can be used to define: root resources (as we've seen in the previous example), links, media type
040 * IDs random extensions to other extension points;
041 * 
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 * @author Stephane Lacoin (aka matic)
044 */
045@WebObject(type = "Root", administrator = Access.GRANT)
046@Path("samples")
047@Produces("text/html;charset=UTF-8")
048public class SamplesRoot extends ModuleRoot {
049
050    @GET
051    public Object doGet() {
052        return getTemplate("index.ftl");
053    }
054
055    @Path("hello")
056    public Object doGetHello() {
057        return newObject("Hello");
058    }
059
060    @Path("templating")
061    public Object doGetTemplating() {
062        return newObject("Templating");
063    }
064
065    @Path("basics")
066    public Object doGetObjects() {
067        return newObject("Basics");
068    }
069
070    @Path("documents")
071    public Object doGetBrowser() {
072        return newObject("Documents");
073    }
074
075    @Override
076    public Object handleError(WebApplicationException e) {
077        if (e instanceof WebSecurityException) {
078            // display a login page
079            return Response.status(401).entity(getTemplate("error/error_401.ftl")).build();
080        } else if (e instanceof WebResourceNotFoundException) {
081            return Response.status(404).entity(getTemplate("error/error_404.ftl")).build();
082        } else {
083            // not interested in that exception - use default handling
084            return super.handleError(e);
085        }
086    }
087}