001: package org.apache.velocity.tools.view.context;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Collections;
023: import java.util.Map;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import javax.servlet.http.HttpSession;
028: import javax.servlet.ServletContext;
029:
030: import org.apache.velocity.VelocityContext;
031: import org.apache.velocity.app.VelocityEngine;
032: import org.apache.velocity.context.Context;
033:
034: /**
035: * <p>Velocity context implementation specific to the Servlet environment.</p>
036: *
037: * <p>It provides the following special features:</p>
038: * <ul>
039: * <li>puts the request, response, session, and servlet context objects
040: * into the Velocity context for direct access, and keeps them
041: * read-only</li>
042: * <li>supports a read-only toolbox of view tools</li>
043: * <li>auto-searches servlet request attributes, session attributes and
044: * servlet context attribues for objects</li>
045: * </ul>
046: *
047: * <p>The {@link #internalGet(String key)} method implements the following search order
048: * for objects:</p>
049: * <ol>
050: * <li>toolbox</li>
051: * <li>servlet request, servlet response, servlet session, servlet context</li>
052: * <li>local hashtable of objects (traditional use)</li>
053: * <li>servlet request attribues, servlet session attribute, servlet context
054: * attributes</li>
055: * </ol>
056: *
057: * <p>The purpose of this class is to make it easy for web designer to work
058: * with Java servlet based web applications. They do not need to be concerned
059: * with the concepts of request, session or application attributes and the
060: * lifetime of objects in these scopes.</p>
061: *
062: * <p>Note that the put() method always puts objects into the local hashtable.
063: * </p>
064: *
065: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
066: * @author <a href="mailto:sidler@teamup.com">Gabe Sidler</a>
067: *
068: * @version $Id: ChainedContext.java 480849 2006-11-30 06:19:04Z nbubna $
069: */
070: public class ChainedContext extends VelocityContext implements
071: ViewContext {
072:
073: /* the current toolbox, request, response, and session */
074: private Map toolbox;
075: private HttpServletRequest request;
076: private HttpServletResponse response;
077: private HttpSession session;
078:
079: /* the servlet context */
080: private ServletContext application;
081:
082: /* the velocity engine being used */
083: private VelocityEngine velocity;
084:
085: /**
086: * @deprecated This will be removed after VelocityTools 1.3
087: */
088: public ChainedContext(Context ctx, HttpServletRequest request,
089: HttpServletResponse response, ServletContext application) {
090: this (ctx, null, request, response, application);
091: }
092:
093: public ChainedContext(VelocityEngine velocity,
094: HttpServletRequest request, HttpServletResponse response,
095: ServletContext application) {
096: this (null, velocity, request, response, application);
097: }
098:
099: public ChainedContext(Context ctx, VelocityEngine velocity,
100: HttpServletRequest request, HttpServletResponse response,
101: ServletContext application) {
102: super (null, ctx);
103:
104: this .velocity = velocity;
105: this .request = request;
106: this .response = response;
107: this .session = request.getSession(false);
108: this .application = application;
109: }
110:
111: /**
112: * <p>Sets the toolbox of view tools.</p>
113: *
114: * @param box toolbox of view tools
115: */
116: public void setToolbox(Map box) {
117: this .toolbox = box;
118: /* just in case the servlet toolbox manager
119: * had to create a new session to hold session tools
120: * let's make sure this context's session ref is current */
121: this .session = request.getSession(false);
122: }
123:
124: /**
125: * <p>Returns a read-only view of the toolbox {@link Map}
126: * for this context.</p>
127: * @since VelocityTools 1.3
128: * @return an unmodifiable version of the toolbox for this request
129: * or {@code null} if there is none
130: */
131: public Map getToolbox() {
132: if (this .toolbox != null) {
133: return Collections.unmodifiableMap(this .toolbox);
134: }
135: return null;
136: }
137:
138: /**
139: * <p>Looks up and returns the object with the specified key.</p>
140: * <p>See the class documentation for more details.</p>
141: *
142: * @param key the key of the object requested
143: * @return the requested object or null if not found
144: */
145: public Object internalGet(String key) {
146: Object o = null;
147:
148: /* search the toolbox */
149: if (toolbox != null) {
150: o = toolbox.get(key);
151: if (o != null) {
152: return o;
153: }
154: }
155:
156: /* make the four scopes of the Apocalypse Read only */
157: if (key.equals(REQUEST)) {
158: return request;
159: } else if (key.equals(RESPONSE)) {
160: return response;
161: } else if (key.equals(SESSION)) {
162: return session;
163: } else if (key.equals(APPLICATION)) {
164: return application;
165: }
166:
167: /* try the local hashtable */
168: o = super .internalGet(key);
169: if (o != null) {
170: return o;
171: }
172:
173: /* if not found, wander down the scopes... */
174: return getAttribute(key);
175: }
176:
177: /**
178: * <p>Searches for the named attribute in request, session (if valid),
179: * and application scope(s) in order and returns the value associated
180: * or null.</p>
181: *
182: * @since VelocityTools 1.1
183: */
184: public Object getAttribute(String key) {
185: Object o = request.getAttribute(key);
186: if (o == null) {
187: if (session != null) {
188: try {
189: o = session.getAttribute(key);
190: } catch (IllegalStateException ise) {
191: // Handle invalidated session state
192: o = null;
193: }
194: }
195:
196: if (o == null) {
197: o = application.getAttribute(key);
198: }
199: }
200: return o;
201: }
202:
203: /**
204: * <p>Returns the current servlet request.</p>
205: */
206: public HttpServletRequest getRequest() {
207: return request;
208: }
209:
210: /**
211: * <p>Returns the current servlet response.</p>
212: */
213: public HttpServletResponse getResponse() {
214: return response;
215: }
216:
217: /**
218: * <p>Returns the servlet context.</p>
219: */
220: public ServletContext getServletContext() {
221: return application;
222: }
223:
224: /**
225: * <p>Returns a reference to the Velocity context (this object).</p>
226: */
227: public Context getVelocityContext() {
228: return this ;
229: }
230:
231: /**
232: * <p>Returns a reference to the VelocityEngine.</p>
233: */
234: public VelocityEngine getVelocityEngine() {
235: return velocity;
236: }
237:
238: }
|