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