001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.chain.web.servlet;
018
019
020 import java.util.Map;
021 import javax.servlet.ServletContext;
022 import javax.servlet.http.HttpServletRequest;
023 import javax.servlet.http.HttpServletResponse;
024 import org.apache.commons.chain.web.WebContext;
025
026
027 /**
028 * <p>Concrete implementation of {@link WebContext} suitable for use in
029 * Servlets and JSP pages. The abstract methods are mapped to the appropriate
030 * collections of the underlying servlet context, request, and response
031 * instances that are passed to the constructor (or the initialize method).</p>
032 *
033 * @author Craig R. McClanahan
034 * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
035 */
036
037 public class ServletWebContext extends WebContext {
038
039
040 // ------------------------------------------------------------ Constructors
041
042
043 /**
044 * <p>Construct an uninitialized {@link ServletWebContext} instance.</p>
045 */
046 public ServletWebContext() {
047 }
048
049
050 /**
051 * <p>Construct a {@link ServletWebContext} instance that is initialized
052 * with the specified Servlet API objects.</p>
053 *
054 * @param context The <code>ServletContext</code> for this web application
055 * @param request The <code>HttpServletRequest</code> for this request
056 * @param response The <code>HttpServletResponse</code> for this request
057 */
058 public ServletWebContext(ServletContext context,
059 HttpServletRequest request,
060 HttpServletResponse response) {
061
062 initialize(context, request, response);
063
064 }
065
066
067 // ------------------------------------------------------ Instance Variables
068
069
070 /**
071 * <p>The lazily instantiated <code>Map</code> of application scope
072 * attributes.</p>
073 */
074 private Map applicationScope = null;
075
076
077 /**
078 * <p>The <code>ServletContext</code> for this web application.</p>
079 */
080 protected ServletContext context = null;
081
082
083 /**
084 * <p>The lazily instantiated <code>Map</code> of header name-value
085 * combinations (immutable).</p>
086 */
087 private Map header = null;
088
089
090 /**
091 * <p>The lazily instantitated <code>Map</code> of header name-values
092 * combinations (immutable).</p>
093 */
094 private Map headerValues = null;
095
096
097 /**
098 * <p>The lazily instantiated <code>Map</code> of context initialization
099 * parameters.</p>
100 */
101 private Map initParam = null;
102
103
104 /**
105 * <p>The lazily instantiated <code>Map</code> of cookies.</p>
106 */
107 private Map cookieValues = null;
108
109
110 /**
111 * <p>The lazily instantiated <code>Map</code> of request
112 * parameter name-value.</p>
113 */
114 private Map param = null;
115
116
117 /**
118 * <p>The lazily instantiated <code>Map</code> of request
119 * parameter name-values.</p>
120 */
121 private Map paramValues = null;
122
123
124 /**
125 * <p>The <code>HttpServletRequest</code> for this request.</p>
126 */
127 protected HttpServletRequest request = null;
128
129
130 /**
131 * <p>The lazily instantiated <code>Map</code> of request scope
132 * attributes.</p>
133 */
134 private Map requestScope = null;
135
136
137 /**
138 * <p>The <code>HttpServletResponse</code> for this request.</p>
139 */
140 protected HttpServletResponse response = null;
141
142
143 /**
144 * <p>The lazily instantiated <code>Map</code> of session scope
145 * attributes.</p>
146 */
147 private Map sessionScope = null;
148
149
150 // ---------------------------------------------------------- Public Methods
151
152
153 /**
154 * <p>Return the {@link ServletContext} for this context.</p>
155 *
156 * @return The <code>ServletContext</code> for this context.
157 */
158 public ServletContext getContext() {
159
160 return (this.context);
161
162 }
163
164
165 /**
166 * <p>Return the {@link HttpServletRequest} for this context.</p>
167 *
168 * @return The <code>HttpServletRequest</code> for this context.
169 */
170 public HttpServletRequest getRequest() {
171
172 return (this.request);
173
174 }
175
176
177 /**
178 * <p>Return the {@link HttpServletResponse} for this context.</p>
179 *
180 * @return The <code>HttpServletResponse</code> for this context.
181 */
182 public HttpServletResponse getResponse() {
183
184 return (this.response);
185
186 }
187
188
189 /**
190 * <p>Initialize (or reinitialize) this {@link ServletWebContext} instance
191 * for the specified Servlet API objects.</p>
192 *
193 * @param context The <code>ServletContext</code> for this web application
194 * @param request The <code>HttpServletRequest</code> for this request
195 * @param response The <code>HttpServletResponse</code> for this request
196 */
197 public void initialize(ServletContext context,
198 HttpServletRequest request,
199 HttpServletResponse response) {
200
201 // Save the specified Servlet API object references
202 this.context = context;
203 this.request = request;
204 this.response = response;
205
206 // Perform other setup as needed
207
208 }
209
210
211 /**
212 * <p>Release references to allocated resources acquired in
213 * <code>initialize()</code> of via subsequent processing. After this
214 * method is called, subsequent calls to any other method than
215 * <code>initialize()</code> will return undefined results.</p>
216 */
217 public void release() {
218
219 // Release references to allocated collections
220 applicationScope = null;
221 header = null;
222 headerValues = null;
223 initParam = null;
224 param = null;
225 paramValues = null;
226 cookieValues = null;
227 requestScope = null;
228 sessionScope = null;
229
230 // Release references to Servlet API objects
231 context = null;
232 request = null;
233 response = null;
234
235 }
236
237
238
239 // ------------------------------------------------------ WebContext Methods
240
241
242 /**
243 * See the {@link WebContext}'s Javadoc.
244 *
245 * @return Application scope Map.
246 */
247 public Map getApplicationScope() {
248
249 if ((applicationScope == null) && (context != null)) {
250 applicationScope = new ServletApplicationScopeMap(context);
251 }
252 return (applicationScope);
253
254 }
255
256
257 /**
258 * See the {@link WebContext}'s Javadoc.
259 *
260 * @return Header values Map.
261 */
262 public Map getHeader() {
263
264 if ((header == null) && (request != null)) {
265 header = new ServletHeaderMap(request);
266 }
267 return (header);
268
269 }
270
271
272 /**
273 * See the {@link WebContext}'s Javadoc.
274 *
275 * @return Header values Map.
276 */
277 public Map getHeaderValues() {
278
279 if ((headerValues == null) && (request != null)) {
280 headerValues = new ServletHeaderValuesMap(request);
281 }
282 return (headerValues);
283
284 }
285
286
287 /**
288 * See the {@link WebContext}'s Javadoc.
289 *
290 * @return Initialization parameter Map.
291 */
292 public Map getInitParam() {
293
294 if ((initParam == null) && (context != null)) {
295 initParam = new ServletInitParamMap(context);
296 }
297 return (initParam);
298
299 }
300
301
302 /**
303 * See the {@link WebContext}'s Javadoc.
304 *
305 * @return Request parameter Map.
306 */
307 public Map getParam() {
308
309 if ((param == null) && (request != null)) {
310 param = new ServletParamMap(request);
311 }
312 return (param);
313
314 }
315
316
317 /**
318 * See the {@link WebContext}'s Javadoc.
319 *
320 * @return Request parameter Map.
321 */
322 public Map getParamValues() {
323
324 if ((paramValues == null) && (request != null)) {
325 paramValues = new ServletParamValuesMap(request);
326 }
327 return (paramValues);
328
329 }
330
331
332 /**
333 * See the {@link WebContext}'s Javadoc.
334 *
335 * @return Map of Cookies.
336 * @since Chain 1.1
337 */
338 public Map getCookies() {
339
340 if ((cookieValues == null) && (request != null)) {
341 cookieValues = new ServletCookieMap(request);
342 }
343 return (cookieValues);
344
345 }
346
347
348 /**
349 * See the {@link WebContext}'s Javadoc.
350 *
351 * @return Request scope Map.
352 */
353 public Map getRequestScope() {
354
355 if ((requestScope == null) && (request != null)) {
356 requestScope = new ServletRequestScopeMap(request);
357 }
358 return (requestScope);
359
360 }
361
362
363 /**
364 * See the {@link WebContext}'s Javadoc.
365 *
366 * @return Session scope Map.
367 */
368 public Map getSessionScope() {
369
370 if ((sessionScope == null) && (request != null)) {
371 sessionScope = new ServletSessionScopeMap(request);
372 }
373 return (sessionScope);
374
375 }
376
377
378
379 }