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