This project has retired. For details please refer to its Attic page.
FreemarkerRenderer xref
View Javadoc

1   /*
2    * $Id: FreemarkerRenderer.java 1306435 2012-03-28 15:39:11Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles.request.freemarker.render;
23  
24  import java.io.IOException;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.tiles.request.Request;
31  import org.apache.tiles.request.freemarker.FreemarkerRequestException;
32  import org.apache.tiles.request.render.CannotRenderException;
33  import org.apache.tiles.request.render.Renderer;
34  import org.apache.tiles.request.servlet.ExternalWriterHttpServletResponse;
35  import org.apache.tiles.request.servlet.ServletRequest;
36  
37  /**
38   * FreeMarker renderer for rendering FreeMarker templates as Tiles attributes.
39   * It is only usable under a Servlet environment, because it uses
40   * {@link AttributeValueFreemarkerServlet} internally to forward the request.<br/>
41   * To initialize it correctly, call {@link #setParameter(String, String)} for all the
42   * parameters that you want to set, and then call {@link #commit()}.
43   *
44   * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
45   */
46  public class FreemarkerRenderer implements Renderer {
47  
48      /**
49       * The servlet that is used to forward the request to.
50       */
51      private AttributeValueFreemarkerServlet servlet;
52  
53      /**
54       * Constructor.
55       *
56       * @param servlet The servlet to use.
57       */
58      public FreemarkerRenderer(AttributeValueFreemarkerServlet servlet) {
59          this.servlet = servlet;
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public void render(String path, Request request) throws IOException {
65          if (path == null) {
66              throw new CannotRenderException("Cannot dispatch a null path");
67          }
68          ServletRequest servletRequest = org.apache.tiles.request.servlet.ServletUtil.getServletRequest(request);
69          HttpServletRequest httpRequest = servletRequest.getRequest();
70          HttpServletResponse httpResponse = servletRequest.getResponse();
71          servlet.setValue(path);
72          try {
73              servlet.doGet(httpRequest,
74                      new ExternalWriterHttpServletResponse(httpResponse,
75                              request.getPrintWriter()));
76          } catch (ServletException e) {
77              throw new FreemarkerRequestException("Exception when rendering a FreeMarker attribute", e);
78          }
79      }
80  
81      /** {@inheritDoc} */
82      public boolean isRenderable(String path, Request request) {
83          return path != null && path.startsWith("/") && path.endsWith(".ftl");
84      }
85  }