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