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

1   /*
2    * $Id: FreemarkerRequestUtil.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  package org.apache.tiles.request.freemarker;
22  
23  import org.apache.tiles.request.ApplicationContext;
24  import org.apache.tiles.request.servlet.ServletUtil;
25  
26  import freemarker.core.Environment;
27  import freemarker.ext.servlet.FreemarkerServlet;
28  import freemarker.ext.servlet.HttpRequestHashModel;
29  import freemarker.ext.servlet.ServletContextHashModel;
30  import freemarker.template.TemplateModelException;
31  
32  /**
33   * Utilities to work with Freemarker requests.
34   *
35   * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
36   */
37  public final class FreemarkerRequestUtil {
38  
39      /**
40       * Constructor.
41       */
42      private FreemarkerRequestUtil() {
43      }
44  
45      /**
46       * Returns the HTTP request hash model.
47       *
48       * @param env The current FreeMarker environment.
49       * @return The request hash model.
50       */
51      public static HttpRequestHashModel getRequestHashModel(Environment env) {
52          try {
53              return (HttpRequestHashModel) env.getDataModel().get(
54                      FreemarkerServlet.KEY_REQUEST);
55          } catch (TemplateModelException e) {
56              throw new NotAvailableFreemarkerServletException(
57                      "Exception got when obtaining the request hash model", e);
58          }
59      }
60  
61      /**
62       * Returns the servlet context hash model.
63       *
64       * @param env The current FreeMarker environment.
65       * @return The servlet context hash model.
66       */
67      public static ServletContextHashModel getServletContextHashModel(
68              Environment env) {
69          try {
70              return (ServletContextHashModel) env.getDataModel().get(
71                      FreemarkerServlet.KEY_APPLICATION);
72          } catch (TemplateModelException e) {
73              throw new NotAvailableFreemarkerServletException(
74                      "Exception got when obtaining the application hash model",
75                      e);
76          }
77      }
78  
79      /**
80       * Returns the application context. It must be
81       * first saved creating an {@link ApplicationContext} and using
82       * {@link org.apache.tiles.request.ApplicationAccess#register(ApplicationContext)}.
83       *
84       * @param env The Freemarker environment.
85       * @return The
86       */
87      public static ApplicationContext getApplicationContext(
88              Environment env) {
89          return ServletUtil
90                  .getApplicationContext(getServletContextHashModel(env)
91                          .getServlet().getServletContext());
92      }
93  
94  }