1/*2 * $Id: FreemarkerRequestUtil.java 1306435 2012-03-28 15:39:11Z nlebas $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.request.freemarker;
2223import org.apache.tiles.request.ApplicationContext;
24import org.apache.tiles.request.servlet.ServletUtil;
2526import freemarker.core.Environment;
27import freemarker.ext.servlet.FreemarkerServlet;
28import freemarker.ext.servlet.HttpRequestHashModel;
29import freemarker.ext.servlet.ServletContextHashModel;
30import freemarker.template.TemplateModelException;
3132/**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 */37publicfinalclassFreemarkerRequestUtil {
3839/**40 * Constructor.41 */42privateFreemarkerRequestUtil() {
43 }
4445/**46 * Returns the HTTP request hash model.47 *48 * @param env The current FreeMarker environment.49 * @return The request hash model.50 */51publicstatic HttpRequestHashModel getRequestHashModel(Environment env) {
52try {
53return (HttpRequestHashModel) env.getDataModel().get(
54 FreemarkerServlet.KEY_REQUEST);
55 } catch (TemplateModelException e) {
56thrownewNotAvailableFreemarkerServletException(
57"Exception got when obtaining the request hash model", e);
58 }
59 }
6061/**62 * Returns the servlet context hash model.63 *64 * @param env The current FreeMarker environment.65 * @return The servlet context hash model.66 */67publicstatic ServletContextHashModel getServletContextHashModel(
68 Environment env) {
69try {
70return (ServletContextHashModel) env.getDataModel().get(
71 FreemarkerServlet.KEY_APPLICATION);
72 } catch (TemplateModelException e) {
73thrownewNotAvailableFreemarkerServletException(
74"Exception got when obtaining the application hash model",
75 e);
76 }
77 }
7879/**80 * Returns the application context. It must be81 * first saved creating an {@link ApplicationContext} and using82 * {@link org.apache.tiles.request.ApplicationAccess#register(ApplicationContext)}.83 *84 * @param env The Freemarker environment.85 * @return The86 */87publicstaticApplicationContext getApplicationContext(
88 Environment env) {
89returnServletUtil90 .getApplicationContext(getServletContextHashModel(env)
91 .getServlet().getServletContext());
92 }
9394 }