1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.web.servlet;
17
18
19 import java.util.Map;
20 import javax.servlet.ServletContext;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import org.apache.commons.chain.web.WebContext;
24
25
26 /***
27 * <p>Concrete implementation of {@link WebContext} suitable for use in
28 * Servlets and JSP pages. The abstract methods are mapped to the appropriate
29 * collections of the underlying servlet context, request, and response
30 * instances that are passed to the constructor (or the initialize method).</p>
31 *
32 * @author Craig R. McClanahan
33 * @version $Revision: 412789 $ $Date: 2006-06-08 17:19:14 +0100 (Thu, 08 Jun 2006) $
34 */
35
36 public class ServletWebContext extends WebContext {
37
38
39
40
41
42 /***
43 * <p>Construct an uninitialized {@link ServletWebContext} instance.</p>
44 */
45 public ServletWebContext() {
46 }
47
48
49 /***
50 * <p>Construct a {@link ServletWebContext} instance that is initialized
51 * with the specified Servlet API objects.</p>
52 *
53 * @param context The <code>ServletContext</code> for this web application
54 * @param request The <code>HttpServletRequest</code> for this request
55 * @param response The <code>HttpServletResponse</code> for this request
56 */
57 public ServletWebContext(ServletContext context,
58 HttpServletRequest request,
59 HttpServletResponse response) {
60
61 initialize(context, request, response);
62
63 }
64
65
66
67
68
69 /***
70 * <p>The lazily instantiated <code>Map</code> of application scope
71 * attributes.</p>
72 */
73 private Map applicationScope = null;
74
75
76 /***
77 * <p>The <code>ServletContext</code> for this web application.</p>
78 */
79 protected ServletContext context = null;
80
81
82 /***
83 * <p>The lazily instantiated <code>Map</code> of header name-value
84 * combinations (immutable).</p>
85 */
86 private Map header = null;
87
88
89 /***
90 * <p>The lazily instantitated <code>Map</code> of header name-values
91 * combinations (immutable).</p>
92 */
93 private Map headerValues = null;
94
95
96 /***
97 * <p>The lazily instantiated <code>Map</code> of context initialization
98 * parameters.</p>
99 */
100 private Map initParam = null;
101
102
103 /***
104 * <p>The lazily instantiated <code>Map</code> of cookies.</p>
105 */
106 private Map cookieValues = null;
107
108
109 /***
110 * <p>The lazily instantiated <code>Map</code> of request
111 * parameter name-value.</p>
112 */
113 private Map param = null;
114
115
116 /***
117 * <p>The lazily instantiated <code>Map</code> of request
118 * parameter name-values.</p>
119 */
120 private Map paramValues = null;
121
122
123 /***
124 * <p>The <code>HttpServletRequest</code> for this request.</p>
125 */
126 protected HttpServletRequest request = null;
127
128
129 /***
130 * <p>The lazily instantiated <code>Map</code> of request scope
131 * attributes.</p>
132 */
133 private Map requestScope = null;
134
135
136 /***
137 * <p>The <code>HttpServletResponse</code> for this request.</p>
138 */
139 protected HttpServletResponse response = null;
140
141
142 /***
143 * <p>The lazily instantiated <code>Map</code> of session scope
144 * attributes.</p>
145 */
146 private Map sessionScope = null;
147
148
149
150
151
152 /***
153 * <p>Return the {@link ServletContext} for this context.</p>
154 *
155 * @return The <code>ServletContext</code> for this context.
156 */
157 public ServletContext getContext() {
158
159 return (this.context);
160
161 }
162
163
164 /***
165 * <p>Return the {@link HttpServletRequest} for this context.</p>
166 *
167 * @return The <code>HttpServletRequest</code> for this context.
168 */
169 public HttpServletRequest getRequest() {
170
171 return (this.request);
172
173 }
174
175
176 /***
177 * <p>Return the {@link HttpServletResponse} for this context.</p>
178 *
179 * @return The <code>HttpServletResponse</code> for this context.
180 */
181 public HttpServletResponse getResponse() {
182
183 return (this.response);
184
185 }
186
187
188 /***
189 * <p>Initialize (or reinitialize) this {@link ServletWebContext} instance
190 * for the specified Servlet API objects.</p>
191 *
192 * @param context The <code>ServletContext</code> for this web application
193 * @param request The <code>HttpServletRequest</code> for this request
194 * @param response The <code>HttpServletResponse</code> for this request
195 */
196 public void initialize(ServletContext context,
197 HttpServletRequest request,
198 HttpServletResponse response) {
199
200
201 this.context = context;
202 this.request = request;
203 this.response = response;
204
205
206
207 }
208
209
210 /***
211 * <p>Release references to allocated resources acquired in
212 * <code>initialize()</code> of via subsequent processing. After this
213 * method is called, subsequent calls to any other method than
214 * <code>initialize()</code> will return undefined results.</p>
215 */
216 public void release() {
217
218
219 applicationScope = null;
220 header = null;
221 headerValues = null;
222 initParam = null;
223 param = null;
224 paramValues = null;
225 cookieValues = null;
226 requestScope = null;
227 sessionScope = null;
228
229
230 context = null;
231 request = null;
232 response = null;
233
234 }
235
236
237
238
239
240
241 /***
242 * See the {@link WebContext}'s Javadoc.
243 *
244 * @return Application scope Map.
245 */
246 public Map getApplicationScope() {
247
248 if ((applicationScope == null) && (context != null)) {
249 applicationScope = new ServletApplicationScopeMap(context);
250 }
251 return (applicationScope);
252
253 }
254
255
256 /***
257 * See the {@link WebContext}'s Javadoc.
258 *
259 * @return Header values Map.
260 */
261 public Map getHeader() {
262
263 if ((header == null) && (request != null)) {
264 header = new ServletHeaderMap(request);
265 }
266 return (header);
267
268 }
269
270
271 /***
272 * See the {@link WebContext}'s Javadoc.
273 *
274 * @return Header values Map.
275 */
276 public Map getHeaderValues() {
277
278 if ((headerValues == null) && (request != null)) {
279 headerValues = new ServletHeaderValuesMap(request);
280 }
281 return (headerValues);
282
283 }
284
285
286 /***
287 * See the {@link WebContext}'s Javadoc.
288 *
289 * @return Initialization parameter Map.
290 */
291 public Map getInitParam() {
292
293 if ((initParam == null) && (context != null)) {
294 initParam = new ServletInitParamMap(context);
295 }
296 return (initParam);
297
298 }
299
300
301 /***
302 * See the {@link WebContext}'s Javadoc.
303 *
304 * @return Request parameter Map.
305 */
306 public Map getParam() {
307
308 if ((param == null) && (request != null)) {
309 param = new ServletParamMap(request);
310 }
311 return (param);
312
313 }
314
315
316 /***
317 * See the {@link WebContext}'s Javadoc.
318 *
319 * @return Request parameter Map.
320 */
321 public Map getParamValues() {
322
323 if ((paramValues == null) && (request != null)) {
324 paramValues = new ServletParamValuesMap(request);
325 }
326 return (paramValues);
327
328 }
329
330
331 /***
332 * See the {@link WebContext}'s Javadoc.
333 *
334 * @return Map of Cookies.
335 * @since Chain 1.1
336 */
337 public Map getCookies() {
338
339 if ((cookieValues == null) && (request != null)) {
340 cookieValues = new ServletCookieMap(request);
341 }
342 return (cookieValues);
343
344 }
345
346
347 /***
348 * See the {@link WebContext}'s Javadoc.
349 *
350 * @return Request scope Map.
351 */
352 public Map getRequestScope() {
353
354 if ((requestScope == null) && (request != null)) {
355 requestScope = new ServletRequestScopeMap(request);
356 }
357 return (requestScope);
358
359 }
360
361
362 /***
363 * See the {@link WebContext}'s Javadoc.
364 *
365 * @return Session scope Map.
366 */
367 public Map getSessionScope() {
368
369 if ((sessionScope == null) && (request != null)) {
370 sessionScope = new ServletSessionScopeMap(request);
371 }
372 return (sessionScope);
373
374 }
375
376
377
378 }