View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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: 1.5 $ $Date: 2004/02/25 00:01:04 $
34   */
35  
36  public class ServletWebContext extends WebContext {
37  
38  
39      // ------------------------------------------------------------ Constructors
40  
41  
42      /***
43       * <p>Construct an uninitialized {@link ServletWebContext} instance.</p>
44       */
45      public ServletWebContext() {
46  
47          ;
48  
49      }
50  
51  
52      /***
53       * <p>Construct a {@link ServletWebContext} instance that is initialized
54       * with the specified Servlet API objects.</p>
55       *
56       * @param context The <code>ServletContext</code> for this web application
57       * @param request The <code>HttpServletRequest</code> for this request
58       * @param response The <code>HttpServletResponse</code> for this request
59       */
60      public ServletWebContext(ServletContext context,
61                               HttpServletRequest request,
62                               HttpServletResponse response) {
63  
64          initialize(context, request, response);
65  
66      }
67  
68  
69      // ------------------------------------------------------ Instance Variables
70  
71  
72      /***
73       * <p>The lazily instantiated <code>Map</code> of application scope
74       * attributes.</p>
75       */
76      private Map applicationScope = null;
77  
78  
79      /***
80       * <p>The <code>ServletContext</code> for this web application.</p>
81       */
82      protected ServletContext context = null;
83  
84  
85      /***
86       * <p>The lazily instantiated <code>Map</code> of header name-value
87       * combinations (immutable).</p>
88       */
89      private Map header = null;
90  
91  
92      /***
93       * <p>The lazily instantitated <code>Map</code> of header name-values
94       * combinations (immutable).</p>
95       */
96      private Map headerValues = null;
97  
98  
99      /***
100      * <p>The lazily instantiated <code>Map</code> of context initialization
101      * parameters.</p>
102      */
103     private Map initParam = null;
104 
105 
106     /***
107      * <p>The lazily instantiated <code>Map</code> of request
108      * parameter name-value.</p>
109      */
110     private Map param = null;
111 
112 
113     /***
114      * <p>The lazily instantiated <code>Map</code> of request
115      * parameter name-values.</p>
116      */
117     private Map paramValues = null;
118 
119 
120     /***
121      * <p>The <code>HttpServletRequest</code> for this request.</p>
122      */
123     protected HttpServletRequest request = null;
124 
125 
126     /***
127      * <p>The lazily instantiated <code>Map</code> of request scope
128      * attributes.</p>
129      */
130     private Map requestScope = null;
131 
132 
133     /***
134      * <p>The <code>HttpServletResponse</code> for this request.</p>
135      */
136     protected HttpServletResponse response = null;
137 
138 
139     /***
140      * <p>The lazily instantiated <code>Map</code> of session scope
141      * attributes.</p>
142      */
143     private Map sessionScope = null;
144 
145 
146     // ---------------------------------------------------------- Public Methods
147 
148 
149     /***
150      * <p>Return the {@link ServletContext} for this context.</p>
151      */
152     public ServletContext getContext() {
153 
154     return (this.context);
155 
156     }
157 
158 
159     /***
160      * <p>Return the {@link HttpServletRequest} for this context.</p>
161      */
162     public HttpServletRequest getRequest() {
163 
164     return (this.request);
165 
166     }
167 
168 
169     /***
170      * <p>Return the {@link HttpServletResponse} for this context.</p>
171      */
172     public HttpServletResponse getResponse() {
173 
174     return (this.response);
175 
176     }
177 
178 
179     /***
180      * <p>Initialize (or reinitialize) this {@link ServletWebContext} instance
181      * for the specified Servlet API objects.</p>
182      *
183      * @param context The <code>ServletContext</code> for this web application
184      * @param request The <code>HttpServletRequest</code> for this request
185      * @param response The <code>HttpServletResponse</code> for this request
186      */
187     public void initialize(ServletContext context,
188                            HttpServletRequest request,
189                            HttpServletResponse response) {
190 
191         // Save the specified Servlet API object references
192         this.context = context;
193         this.request = request;
194         this.response = response;
195 
196         // Perform other setup as needed
197 
198     }
199 
200 
201     /***
202      * <p>Release references to allocated resources acquired in
203      * <code>initialize()</code> of via subsequent processing.  After this
204      * method is called, subsequent calls to any other method than
205      * <code>initialize()</code> will return undefined results.</p>
206      */
207     public void release() {
208 
209         // Release references to allocated collections
210         applicationScope = null;
211         header = null;
212         headerValues = null;
213         initParam = null;
214         param = null;
215         paramValues = null;
216         requestScope = null;
217         sessionScope = null;
218 
219         // Release references to Servlet API objects
220         context = null;
221         request = null;
222         response = null;
223 
224     }
225 
226 
227 
228     // ------------------------------------------------------ WebContext Methods
229 
230 
231     public Map getApplicationScope() {
232 
233         if ((applicationScope == null) && (context != null)) {
234             applicationScope = new ServletApplicationScopeMap(context);
235         }
236         return (applicationScope);
237 
238     }
239 
240 
241     public Map getHeader() {
242 
243         if ((header == null) && (request != null)) {
244             header = new ServletHeaderMap(request);
245         }
246         return (header);
247 
248     }
249 
250 
251     public Map getHeaderValues() {
252 
253         if ((headerValues == null) && (request != null)) {
254             headerValues = new ServletHeaderValuesMap(request);
255         }
256         return (headerValues);
257 
258     }
259 
260 
261     public Map getInitParam() {
262 
263         if ((initParam == null) && (context != null)) {
264             initParam = new ServletInitParamMap(context);
265         }
266         return (initParam);
267 
268     }
269 
270 
271     public Map getParam() {
272 
273         if ((param == null) && (request != null)) {
274             param = new ServletParamMap(request);
275         }
276         return (param);
277 
278     }
279 
280 
281     public Map getParamValues() {
282 
283         if ((paramValues == null) && (request != null)) {
284             paramValues = new ServletParamValuesMap(request);
285         }
286         return (paramValues);
287 
288     }
289 
290 
291     public Map getRequestScope() {
292 
293         if ((requestScope == null) && (request != null)) {
294             requestScope = new ServletRequestScopeMap(request);
295         }
296         return (requestScope);
297 
298     }
299 
300 
301     public Map getSessionScope() {
302 
303         if ((sessionScope == null) && (request != null)) {
304             sessionScope = new ServletSessionScopeMap(request.getSession());
305         }
306         return (sessionScope);
307 
308     }
309 
310 
311 
312 }