View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.servlet;
18  
19  
20  import java.util.Map;
21  import javax.servlet.ServletContext;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import org.apache.commons.chain.web.WebContext;
25  
26  
27  /**
28   * <p>Concrete implementation of {@link WebContext} suitable for use in
29   * Servlets and JSP pages.  The abstract methods are mapped to the appropriate
30   * collections of the underlying servlet context, request, and response
31   * instances that are passed to the constructor (or the initialize method).</p>
32   *
33   * @author Craig R. McClanahan
34   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
35   */
36  
37  public class ServletWebContext extends WebContext {
38  
39  
40      // ------------------------------------------------------------ Constructors
41  
42  
43      /**
44       * <p>Construct an uninitialized {@link ServletWebContext} instance.</p>
45       */
46      public ServletWebContext() {
47      }
48  
49  
50      /**
51       * <p>Construct a {@link ServletWebContext} instance that is initialized
52       * with the specified Servlet API objects.</p>
53       *
54       * @param context The <code>ServletContext</code> for this web application
55       * @param request The <code>HttpServletRequest</code> for this request
56       * @param response The <code>HttpServletResponse</code> for this request
57       */
58      public ServletWebContext(ServletContext context,
59                               HttpServletRequest request,
60                               HttpServletResponse response) {
61  
62          initialize(context, request, response);
63  
64      }
65  
66  
67      // ------------------------------------------------------ Instance Variables
68  
69  
70      /**
71       * <p>The lazily instantiated <code>Map</code> of application scope
72       * attributes.</p>
73       */
74      private Map applicationScope = null;
75  
76  
77      /**
78       * <p>The <code>ServletContext</code> for this web application.</p>
79       */
80      protected ServletContext context = null;
81  
82  
83      /**
84       * <p>The lazily instantiated <code>Map</code> of header name-value
85       * combinations (immutable).</p>
86       */
87      private Map header = null;
88  
89  
90      /**
91       * <p>The lazily instantitated <code>Map</code> of header name-values
92       * combinations (immutable).</p>
93       */
94      private Map headerValues = null;
95  
96  
97      /**
98       * <p>The lazily instantiated <code>Map</code> of context initialization
99       * 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 }