001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package org.apache.commons.el;
057:
058: import java.util.ArrayList;
059: import java.util.Collections;
060: import java.util.Date;
061: import java.util.Enumeration;
062: import java.util.HashMap;
063: import java.util.List;
064: import java.util.Map;
065: import javax.servlet.ServletContext;
066: import javax.servlet.http.Cookie;
067: import javax.servlet.http.HttpServletRequest;
068: import javax.servlet.jsp.PageContext;
069:
070: /**
071: *
072: * <p>This class is used to generate the implicit Map and List objects
073: * that wrap various elements of the PageContext. It also returns the
074: * correct implicit object for a given implicit object name.
075: *
076: * @author Nathan Abramson - Art Technology Group
077: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
078: **/
079:
080: public class ImplicitObjects {
081: //-------------------------------------
082: // Constants
083: //-------------------------------------
084:
085: static final String sAttributeName = "org.apache.taglibs.standard.ImplicitObjects";
086:
087: //-------------------------------------
088: // Member variables
089: //-------------------------------------
090:
091: PageContext mContext;
092: Map mPage;
093: Map mRequest;
094: Map mSession;
095: Map mApplication;
096: Map mParam;
097: Map mParams;
098: Map mHeader;
099: Map mHeaders;
100: Map mInitParam;
101: Map mCookie;
102:
103: //-------------------------------------
104: /**
105: *
106: * Constructor
107: **/
108: public ImplicitObjects(PageContext pContext) {
109: mContext = pContext;
110: }
111:
112: //-------------------------------------
113: /**
114: *
115: * Finds the ImplicitObjects associated with the PageContext,
116: * creating it if it doesn't yet exist.
117: **/
118: public static ImplicitObjects getImplicitObjects(
119: PageContext pContext) {
120: ImplicitObjects objs = (ImplicitObjects) pContext.getAttribute(
121: sAttributeName, PageContext.PAGE_SCOPE);
122: if (objs == null) {
123: objs = new ImplicitObjects(pContext);
124: pContext.setAttribute(sAttributeName, objs,
125: PageContext.PAGE_SCOPE);
126: }
127: return objs;
128: }
129:
130: //-------------------------------------
131: /**
132: *
133: * Returns the Map that "wraps" page-scoped attributes
134: **/
135: public Map getPageScopeMap() {
136: if (mPage == null) {
137: mPage = createPageScopeMap(mContext);
138: }
139: return mPage;
140: }
141:
142: //-------------------------------------
143: /**
144: *
145: * Returns the Map that "wraps" request-scoped attributes
146: **/
147: public Map getRequestScopeMap() {
148: if (mRequest == null) {
149: mRequest = createRequestScopeMap(mContext);
150: }
151: return mRequest;
152: }
153:
154: //-------------------------------------
155: /**
156: *
157: * Returns the Map that "wraps" session-scoped attributes
158: **/
159: public Map getSessionScopeMap() {
160: if (mSession == null) {
161: mSession = createSessionScopeMap(mContext);
162: }
163: return mSession;
164: }
165:
166: //-------------------------------------
167: /**
168: *
169: * Returns the Map that "wraps" application-scoped attributes
170: **/
171: public Map getApplicationScopeMap() {
172: if (mApplication == null) {
173: mApplication = createApplicationScopeMap(mContext);
174: }
175: return mApplication;
176: }
177:
178: //-------------------------------------
179: /**
180: *
181: * Returns the Map that maps parameter name to a single parameter
182: * values.
183: **/
184: public Map getParamMap() {
185: if (mParam == null) {
186: mParam = createParamMap(mContext);
187: }
188: return mParam;
189: }
190:
191: //-------------------------------------
192: /**
193: *
194: * Returns the Map that maps parameter name to an array of parameter
195: * values.
196: **/
197: public Map getParamsMap() {
198: if (mParams == null) {
199: mParams = createParamsMap(mContext);
200: }
201: return mParams;
202: }
203:
204: //-------------------------------------
205: /**
206: *
207: * Returns the Map that maps header name to a single header
208: * values.
209: **/
210: public Map getHeaderMap() {
211: if (mHeader == null) {
212: mHeader = createHeaderMap(mContext);
213: }
214: return mHeader;
215: }
216:
217: //-------------------------------------
218: /**
219: *
220: * Returns the Map that maps header name to an array of header
221: * values.
222: **/
223: public Map getHeadersMap() {
224: if (mHeaders == null) {
225: mHeaders = createHeadersMap(mContext);
226: }
227: return mHeaders;
228: }
229:
230: //-------------------------------------
231: /**
232: *
233: * Returns the Map that maps init parameter name to a single init
234: * parameter values.
235: **/
236: public Map getInitParamMap() {
237: if (mInitParam == null) {
238: mInitParam = createInitParamMap(mContext);
239: }
240: return mInitParam;
241: }
242:
243: //-------------------------------------
244: /**
245: *
246: * Returns the Map that maps cookie name to the first matching
247: * Cookie in request.getCookies().
248: **/
249: public Map getCookieMap() {
250: if (mCookie == null) {
251: mCookie = createCookieMap(mContext);
252: }
253: return mCookie;
254: }
255:
256: //-------------------------------------
257: // Methods for generating wrapper maps
258: //-------------------------------------
259: /**
260: *
261: * Creates the Map that "wraps" page-scoped attributes
262: **/
263: public static Map createPageScopeMap(PageContext pContext) {
264: final PageContext context = pContext;
265: return new EnumeratedMap() {
266: public Enumeration enumerateKeys() {
267: return context
268: .getAttributeNamesInScope(PageContext.PAGE_SCOPE);
269: }
270:
271: public Object getValue(Object pKey) {
272: if (pKey instanceof String) {
273: return context.getAttribute((String) pKey,
274: PageContext.PAGE_SCOPE);
275: } else {
276: return null;
277: }
278: }
279:
280: public boolean isMutable() {
281: return true;
282: }
283: };
284: }
285:
286: //-------------------------------------
287: /**
288: *
289: * Creates the Map that "wraps" request-scoped attributes
290: **/
291: public static Map createRequestScopeMap(PageContext pContext) {
292: final PageContext context = pContext;
293: return new EnumeratedMap() {
294: public Enumeration enumerateKeys() {
295: return context
296: .getAttributeNamesInScope(PageContext.REQUEST_SCOPE);
297: }
298:
299: public Object getValue(Object pKey) {
300: if (pKey instanceof String) {
301: return context.getAttribute((String) pKey,
302: PageContext.REQUEST_SCOPE);
303: } else {
304: return null;
305: }
306: }
307:
308: public boolean isMutable() {
309: return true;
310: }
311: };
312: }
313:
314: //-------------------------------------
315: /**
316: *
317: * Creates the Map that "wraps" session-scoped attributes
318: **/
319: public static Map createSessionScopeMap(PageContext pContext) {
320: final PageContext context = pContext;
321: return new EnumeratedMap() {
322: public Enumeration enumerateKeys() {
323: return context
324: .getAttributeNamesInScope(PageContext.SESSION_SCOPE);
325: }
326:
327: public Object getValue(Object pKey) {
328: if (pKey instanceof String) {
329: return context.getAttribute((String) pKey,
330: PageContext.SESSION_SCOPE);
331: } else {
332: return null;
333: }
334: }
335:
336: public boolean isMutable() {
337: return true;
338: }
339: };
340: }
341:
342: //-------------------------------------
343: /**
344: *
345: * Creates the Map that "wraps" application-scoped attributes
346: **/
347: public static Map createApplicationScopeMap(PageContext pContext) {
348: final PageContext context = pContext;
349: return new EnumeratedMap() {
350: public Enumeration enumerateKeys() {
351: return context
352: .getAttributeNamesInScope(PageContext.APPLICATION_SCOPE);
353: }
354:
355: public Object getValue(Object pKey) {
356: if (pKey instanceof String) {
357: return context.getAttribute((String) pKey,
358: PageContext.APPLICATION_SCOPE);
359: } else {
360: return null;
361: }
362: }
363:
364: public boolean isMutable() {
365: return true;
366: }
367: };
368: }
369:
370: //-------------------------------------
371: /**
372: *
373: * Creates the Map that maps parameter name to single parameter
374: * value.
375: **/
376: public static Map createParamMap(PageContext pContext) {
377: final HttpServletRequest request = (HttpServletRequest) pContext
378: .getRequest();
379: return new EnumeratedMap() {
380: public Enumeration enumerateKeys() {
381: return request.getParameterNames();
382: }
383:
384: public Object getValue(Object pKey) {
385: if (pKey instanceof String) {
386: return request.getParameter((String) pKey);
387: } else {
388: return null;
389: }
390: }
391:
392: public boolean isMutable() {
393: return false;
394: }
395: };
396: }
397:
398: //-------------------------------------
399: /**
400: *
401: * Creates the Map that maps parameter name to an array of parameter
402: * values.
403: **/
404: public static Map createParamsMap(PageContext pContext) {
405: final HttpServletRequest request = (HttpServletRequest) pContext
406: .getRequest();
407: return new EnumeratedMap() {
408: public Enumeration enumerateKeys() {
409: return request.getParameterNames();
410: }
411:
412: public Object getValue(Object pKey) {
413: if (pKey instanceof String) {
414: return request.getParameterValues((String) pKey);
415: } else {
416: return null;
417: }
418: }
419:
420: public boolean isMutable() {
421: return false;
422: }
423: };
424: }
425:
426: //-------------------------------------
427: /**
428: *
429: * Creates the Map that maps header name to single header
430: * value.
431: **/
432: public static Map createHeaderMap(PageContext pContext) {
433: final HttpServletRequest request = (HttpServletRequest) pContext
434: .getRequest();
435: return new EnumeratedMap() {
436: public Enumeration enumerateKeys() {
437: return request.getHeaderNames();
438: }
439:
440: public Object getValue(Object pKey) {
441: if (pKey instanceof String) {
442: return request.getHeader((String) pKey);
443: } else {
444: return null;
445: }
446: }
447:
448: public boolean isMutable() {
449: return false;
450: }
451: };
452: }
453:
454: //-------------------------------------
455: /**
456: *
457: * Creates the Map that maps header name to an array of header
458: * values.
459: **/
460: public static Map createHeadersMap (PageContext pContext)
461: {
462: final HttpServletRequest request =
463: (HttpServletRequest) pContext.getRequest ();
464: return new EnumeratedMap ()
465: {
466: public Enumeration enumerateKeys ()
467: {
468: return request.getHeaderNames ();
469: }
470:
471: public Object getValue (Object pKey)
472: {
473: if (pKey instanceof String) {
474: // Drain the header enumeration
475: List l = new ArrayList ();
476: Enumeration enum = request.getHeaders ((String) pKey);
477: if (enum != null) {
478: while (enum.hasMoreElements ()) {
479: l.add (enum.nextElement ());
480: }
481: }
482: String [] ret = (String []) l.toArray (new String [l.size ()]);
483: return ret;
484: }
485: else {
486: return null;
487: }
488: }
489:
490: public boolean isMutable ()
491: {
492: return false;
493: }
494: };
495: }
496:
497: //-------------------------------------
498: /**
499: *
500: * Creates the Map that maps init parameter name to single init
501: * parameter value.
502: **/
503: public static Map createInitParamMap(PageContext pContext) {
504: final ServletContext context = pContext.getServletContext();
505: return new EnumeratedMap() {
506: public Enumeration enumerateKeys() {
507: return context.getInitParameterNames();
508: }
509:
510: public Object getValue(Object pKey) {
511: if (pKey instanceof String) {
512: return context.getInitParameter((String) pKey);
513: } else {
514: return null;
515: }
516: }
517:
518: public boolean isMutable() {
519: return false;
520: }
521: };
522: }
523:
524: //-------------------------------------
525: /**
526: *
527: * Creates the Map that maps cookie name to the first matching
528: * Cookie in request.getCookies().
529: **/
530: public static Map createCookieMap(PageContext pContext) {
531: // Read all the cookies and construct the entire map
532: HttpServletRequest request = (HttpServletRequest) pContext
533: .getRequest();
534: Cookie[] cookies = request.getCookies();
535: Map ret = new HashMap();
536: for (int i = 0; cookies != null && i < cookies.length; i++) {
537: Cookie cookie = cookies[i];
538: if (cookie != null) {
539: String name = cookie.getName();
540: if (!ret.containsKey(name)) {
541: ret.put(name, cookie);
542: }
543: }
544: }
545: return ret;
546: }
547:
548: //-------------------------------------
549: }
|