001/*
002 * (C) Copyright 2011 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 *     matic
018 */
019package org.nuxeo.ecm.webengine.samples;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.Produces;
024import javax.ws.rs.WebApplicationException;
025import javax.ws.rs.core.Response;
026
027import org.nuxeo.ecm.webengine.model.Access;
028import org.nuxeo.ecm.webengine.model.WebObject;
029import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
030import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
031import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
032
033/**
034 * Web Engine Samples Root
035 * <p>
036 * This demonstrates how to define the entry point for a WebEngine module.
037 * <p>
038 * The module entry points are regular JAX-RS resources with an additional @WebObject annotation. This annotation is
039 * mainly used to specify the resource name. A Web Module is declared in the MANIFEST.MF using the directive
040 * {code}NuxeoWebModule{/code}. You can also configure a Web Module using a module.xml file located in the module root
041 * directory. This file can be used to define: root resources (as we've seen in the previous example), links, media type
042 * IDs random extensions to other extension points;
043 * 
044 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
045 * @author Stephane Lacoin (aka matic)
046 */
047@WebObject(type = "Root", administrator = Access.GRANT)
048@Path("samples")
049@Produces("text/html;charset=UTF-8")
050public class SamplesRoot extends ModuleRoot {
051
052    @GET
053    public Object doGet() {
054        return getTemplate("index.ftl");
055    }
056
057    @Path("hello")
058    public Object doGetHello() {
059        return newObject("Hello");
060    }
061
062    @Path("templating")
063    public Object doGetTemplating() {
064        return newObject("Templating");
065    }
066
067    @Path("basics")
068    public Object doGetObjects() {
069        return newObject("Basics");
070    }
071
072    @Path("documents")
073    public Object doGetBrowser() {
074        return newObject("Documents");
075    }
076
077    @Override
078    public Object handleError(WebApplicationException e) {
079        if (e instanceof WebSecurityException) {
080            // display a login page
081            return Response.status(401).entity(getTemplate("error/error_401.ftl")).build();
082        } else if (e instanceof WebResourceNotFoundException) {
083            return Response.status(404).entity(getTemplate("error/error_404.ftl")).build();
084        } else {
085            // not interested in that exception - use default handling
086            return super.handleError(e);
087        }
088    }
089}