001/*
002 * (C) Copyright 2006 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 *     Bogdan Stefanescu
018 */
019package org.nuxeo.ecm.webengine.samples;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.PathParam;
024import javax.ws.rs.Produces;
025import javax.ws.rs.core.Response;
026
027import org.nuxeo.ecm.webengine.model.WebObject;
028
029/**
030 * Templates sample. This demonstrates how to use template files to build client responses. JAX-RS provides a flexible
031 * mechanism to send responses based on the mime type that the client expects. To send a response to the client you
032 * simply return the Object you want as the response. JAX-RS engines will usually know how to render common Java objects
033 * like String, InputStream, File etc. If you need to output specific objects you need to register a custom
034 * MessageBodyWriter class. In JAX-RS you are not able to modify the HttpServletResponse object directly from a resource
035 * method. (add headers, cookies etc) Anything you may want to output must be returned from the resource method back to
036 * JAX-RS engine, and the engine will output it for you. This is a very good thing, even if for some people this
037 * approach may seem strange. You may ask yourself, ok cool, The response rendering is pretty well separated from the
038 * resource logic. But how can I modify response headers? In that case you must return a javax.ws.rs.Response that may
039 * be used to customize your response headers.
040 * <p>
041 * WebEngine is adding a new type of response objects: templates. Templates are freemarker based templates that can be
042 * used to render your objects depending on the request context. WebEngine is adding some cool extensions to freemarker
043 * templates that let you build your web site in a modular fashion. These extensions are called blocks. Blocks are
044 * dynamic template parts that can be extended or replaced using derived blocks. Using blocks, you can write a base
045 * template that may define the site layout (using blocks containing empty or generic content) and then write final
046 * <i>skins</i> for your layout by extending the base template and redefining blocks you are interested in. See the
047 * <i>skin</i> directory for template examples.
048 * <p>
049 * Templates are stored in files under the <i>skin</i> directory. Templates are always resolved relative to the
050 * <i>skin</i> directory, even if you are using absolute paths. The following variables are accessible from a template
051 * when rendered at rendering time:
052 * <ul>
053 * <li> {@code Context} - the WebContext instance
054 * <li> {@code Engine} - the WebEngine instance
055 * <li> {@code This} - the target Web Object.
056 * <li> {@code Root} - the root WebObject.
057 * <li> {@code Document} - the target Document if any otherwise null.
058 * <li> {@code Session} - the Repository Session. (aka Core Session)
059 * <li> {@code basePath} - the request base path (context path + servlet path)
060 * </ul>
061 * To render a template as a response you need to instantiate it and then return it from the resource method. The
062 * template will be processed by the corresponding MessageBodyWriter and rendered on the client stream.
063 *
064 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
065 */
066@WebObject(type = "Templating")
067@Produces("text/html;charset=UTF-8")
068public class TemplatingObject extends HelloObject {
069
070    /**
071     * Return the template index.ftl from 'skin' directory
072     */
073    @GET
074    @Path("index1")
075    public Object getIndex1() {
076        return getTemplate("index1.ftl");
077    }
078
079    /**
080     * Inject the variable 'name' in the template context and then return the template.
081     */
082    @GET
083    @Path("index1/{name}")
084    public Object getIndex1(@PathParam("name") String name) {
085        return getTemplate("index1.ftl").arg("name", name);
086    }
087
088    /**
089     * Render the index2 template
090     */
091    @GET
092    @Path("index2")
093    public Object getIndex2() {
094        return getTemplate("index2.ftl");
095    }
096
097    /**
098     * Example of using redirect. The redirect method inherited from DefaultModule is returning a Response object that
099     * is doing a redirect
100     */
101    @GET
102    @Path("redirect/{whereToRedirect}")
103    public Response doRedirect(@PathParam("whereToRedirect") String path) {
104        return redirect(ctx.getModulePath() + "/" + path);
105    }
106
107}