1/*2 * $Id: JspUtil.java 797765 2009-07-25 13:20:26Z apetrelli $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 */2122package org.apache.tiles.jsp.context;
2324import java.io.IOException;
25import java.io.StringWriter;
26import java.util.HashMap;
27import java.util.Map;
2829import javax.servlet.jsp.JspContext;
30import javax.servlet.jsp.JspException;
31import javax.servlet.jsp.PageContext;
32import javax.servlet.jsp.tagext.JspFragment;
3334import org.apache.tiles.ArrayStack;
35import org.apache.tiles.TilesContainer;
36import org.apache.tiles.access.TilesAccess;
37import org.apache.tiles.impl.NoSuchContainerException;
38import org.apache.tiles.jsp.taglib.TilesJspException;
39import org.apache.tiles.servlet.context.ServletUtil;
40import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
4243/***44 * Utility class for working within a Jsp environment.45 *46 * @version $Rev: 797765 $ $Date: 2009-07-25 15:20:26 +0200 (sab, 25 lug 2009) $47 */48publicfinalclassJspUtil {
4950/***51 * The name of the attribute that will contain the compose stack.52 */53publicstaticfinal String COMPOSE_STACK_ATTRIBUTE_NAME = "org.apache.tiles.template.COMPOSE_STACK";
5455/***56 * Maps scope names to their constants.57 */58privatestaticfinal Map<String, Integer> SCOPES =
59new HashMap<String, Integer>();
6061static {
62 JspUtil.SCOPES.put("page", PageContext.PAGE_SCOPE);
63 JspUtil.SCOPES.put("request", PageContext.REQUEST_SCOPE);
64 JspUtil.SCOPES.put("session", PageContext.SESSION_SCOPE);
65 JspUtil.SCOPES.put("application", PageContext.APPLICATION_SCOPE);
66 }
6768/***69 * Constructor, private to avoid instantiation.70 */71privateJspUtil() {
72 }
7374/***75 * Returns true if forced include of the result is needed.76 *77 * @param context The page context.78 * @return If <code>true</code> the include operation must be forced.79 * @since 2.0.680 */81publicstaticboolean isForceInclude(JspContext context) {
82 Boolean retValue = (Boolean) context.getAttribute(
83 ServletUtil.FORCE_INCLUDE_ATTRIBUTE_NAME,
84 PageContext.REQUEST_SCOPE);
85return retValue != null && retValue.booleanValue();
86 }
8788/***89 * Sets the option that enables the forced include of the response.90 *91 * @param context The page context.92 * @param forceInclude If <code>true</code> the include operation must be93 * forced.94 * @since 2.0.695 */96publicstaticvoid setForceInclude(JspContext context, boolean forceInclude) {
97 Boolean retValue = Boolean.valueOf(forceInclude);
98 context.setAttribute(
99 ServletUtil.FORCE_INCLUDE_ATTRIBUTE_NAME,
100 retValue, PageContext.REQUEST_SCOPE);
101 }
102103/***104 * Returns the default Tiles container.105 *106 * @param context The page context to use.107 * @return The default Tiles container.108 * @since 2.1.2109 */110publicstatic TilesContainer getContainer(JspContext context) {
111return getContainer(context, TilesAccess.CONTAINER_ATTRIBUTE);
112 }
113114/***115 * Returns a specific Tiles container.116 *117 * @param context The page context to use.118 * @param key The key under which the container is stored. If null, the119 * default container will be returned.120 * @return The requested Tiles container.121 * @since 2.1.2122 */123publicstatic TilesContainer getContainer(JspContext context, String key) {
124if (key == null) {
125 key = TilesAccess.CONTAINER_ATTRIBUTE;
126 }
127return (TilesContainer) context.getAttribute(key,
128 PageContext.APPLICATION_SCOPE);
129 }
130131/***132 * Configures the default container to be used in the application.133 *134 * @param context The page context object to use.135 * @param container The container object to set.136 * @since 2.1.2137 */138publicstaticvoid setContainer(JspContext context,
139 TilesContainer container) {
140 setContainer(context, container, TilesAccess.CONTAINER_ATTRIBUTE);
141 }
142143/***144 * Configures the container to be used in the application.145 *146 * @param context The page context object to use.147 * @param container The container object to set.148 * @param key The key under which the container will be stored.149 * @since 2.1.2150 */151publicstaticvoid setContainer(JspContext context,
152 TilesContainer container, String key) {
153 Logger log = LoggerFactory.getLogger(ServletUtil.class);
154if (key == null) {
155 key = TilesAccess.CONTAINER_ATTRIBUTE;
156 }
157158if (container == null) {
159if (log.isInfoEnabled()) {
160 log.info("Removing TilesContext for context: " + context.getClass().getName());
161 }
162 context.removeAttribute(key, PageContext.APPLICATION_SCOPE);
163 }
164if (container != null && log.isInfoEnabled()) {
165 log.info("Publishing TilesContext for context: " + context.getClass().getName());
166 }
167 context.setAttribute(key, container, PageContext.APPLICATION_SCOPE);
168 }
169170/***171 * Sets the current container to use in web pages.172 *173 * @param context The page context to use.174 * @param key The key under which the container is stored.175 * @since 2.1.0176 */177publicstaticvoid setCurrentContainer(JspContext context, String key) {
178 TilesContainer container = getContainer(context, key);
179if (container != null) {
180 context.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
181 container, PageContext.REQUEST_SCOPE);
182 } else {
183thrownew NoSuchContainerException("The container with the key '"184 + key + "' cannot be found");
185 }
186 }
187188/***189 * Sets the current container to use in web pages.190 *191 * @param context The page context to use.192 * @param container The container to use as the current container.193 * @since 2.1.0194 */195publicstaticvoid setCurrentContainer(JspContext context,
196 TilesContainer container) {
197if (container != null) {
198 context.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
199 container, PageContext.REQUEST_SCOPE);
200 } else {
201thrownew NoSuchContainerException("The container cannot be null");
202 }
203 }
204205/***206 * Returns the current container that has been set, or the default one.207 *208 * @param context The page context to use.209 * @return The current Tiles container to use in web pages.210 * @since 2.1.0211 */212publicstatic TilesContainer getCurrentContainer(JspContext context) {
213 TilesContainer container = (TilesContainer) context.getAttribute(
214 ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
215 PageContext.REQUEST_SCOPE);
216if (container == null) {
217 container = getContainer(context);
218 context.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
219 container, PageContext.REQUEST_SCOPE);
220 }
221222return container;
223 }
224225/***226 * Returns the compose stack, that is used by the tags to compose227 * definitions, attributes, etc.228 *229 * @param context The page context.230 * @return The compose stack.231 * @since 2.2.0232 */233 @SuppressWarnings("unchecked")
234publicstatic ArrayStack<Object> getComposeStack(JspContext context) {
235 ArrayStack<Object> composeStack = (ArrayStack<Object>) context.getAttribute(
236 COMPOSE_STACK_ATTRIBUTE_NAME, PageContext.REQUEST_SCOPE);
237if (composeStack == null) {
238 composeStack = new ArrayStack<Object>();
239 context.setAttribute(COMPOSE_STACK_ATTRIBUTE_NAME, composeStack,
240 PageContext.REQUEST_SCOPE);
241 }
242return composeStack;
243 }
244245/***246 * Converts the scope name into its corresponding PageContext constant value.247 *248 * @param scopeName Can be "page", "request", "session", or "application" in any249 * case.250 * @return The constant representing the scope (ie. PageContext.REQUEST_SCOPE).251 * @throws TilesJspException if the scopeName is not a valid name.252 * @since 2.2.0253 */254publicstaticint getScope(String scopeName) throws TilesJspException {
255if (scopeName == null) {
256return PageContext.PAGE_SCOPE;
257 }
258259 Integer scope = JspUtil.SCOPES.get(scopeName.toLowerCase());
260261if (scope == null) {
262thrownewTilesJspException("Unable to retrieve the scope "263 + scopeName);
264 }
265266return scope;
267 }
268269/***270 * Evaluates the fragment (invokes it with a null Writer) if not null.271 *272 * @param fragment The fragment to evaluate.273 * @throws JspException If the fragment invocation fails.274 * @throws IOException If the fragment invocation fails.275 * @since 2.2.0276 */277publicstaticvoid evaluateFragment(JspFragment fragment) throws JspException, IOException {
278if (fragment != null) {
279 fragment.invoke(null);
280 }
281 }
282283/***284 * Evaluates the fragment and returns its content as a string.285 *286 * @param fragment The fragment to evaluate.287 * @return The fragment evaluated as a string.288 * @throws JspException If the fragment invocation fails.289 * @throws IOException If the fragment invocation fails with an I/O error.290 * @since 2.2.0291 */292publicstatic String evaluateFragmentAsString(JspFragment fragment) throws JspException, IOException {
293 String body = null;
294if (fragment != null) {
295 StringWriter writer = new StringWriter();
296try {
297 fragment.invoke(writer);
298 } finally {
299 writer.close();
300 }
301 body = writer.toString();
302 }
303return body;
304 }
305 }