2
* The Apache Software License, Version 1.1
4
* Copyright (c) 1999 The Apache Software Foundation. All rights
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in
16
* the documentation and/or other materials provided with the
19
* 3. The end-user documentation included with the redistribution, if
20
* any, must include the following acknowlegement:
21
* "This product includes software developed by the
22
* Apache Software Foundation (http://www.apache.org/)."
23
* Alternately, this acknowlegement may appear in the software itself,
24
* if and wherever such third-party acknowlegements normally appear.
26
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
27
* Foundation" must not be used to endorse or promote products derived
28
* from this software without prior written permission. For written
29
* permission, please contact apache@apache.org.
31
* 5. Products derived from this software may not be called "Apache"
32
* nor may "Apache" appear in their names without prior written
33
* permission of the Apache Group.
35
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
* ====================================================================
49
* This software consists of voluntary contributions made by many
50
* individuals on behalf of the Apache Software Foundation. For more
51
* information on the Apache Software Foundation, please see
52
* <http://www.apache.org/>.
56
package org.apache.commons.el;
58
import java.util.ArrayList;
59
import java.util.Collections;
60
import java.util.Date;
61
import java.util.Enumeration;
62
import java.util.HashMap;
63
import java.util.List;
65
import javax.servlet.ServletContext;
66
import javax.servlet.http.Cookie;
67
import javax.servlet.http.HttpServletRequest;
68
import javax.servlet.jsp.PageContext;
72
* <p>This class is used to generate the implicit Map and List objects
73
* that wrap various elements of the PageContext. It also returns the
74
* correct implicit object for a given implicit object name.
76
* @author Nathan Abramson - Art Technology Group
77
* @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
80
public class ImplicitObjects
82
//-------------------------------------
84
//-------------------------------------
86
static final String sAttributeName =
87
"org.apache.taglibs.standard.ImplicitObjects";
89
//-------------------------------------
91
//-------------------------------------
105
//-------------------------------------
110
public ImplicitObjects (PageContext pContext)
115
//-------------------------------------
118
* Finds the ImplicitObjects associated with the PageContext,
119
* creating it if it doesn't yet exist.
121
public static ImplicitObjects getImplicitObjects (PageContext pContext)
123
ImplicitObjects objs =
125
pContext.getAttribute (sAttributeName,
126
PageContext.PAGE_SCOPE);
128
objs = new ImplicitObjects (pContext);
129
pContext.setAttribute (sAttributeName,
131
PageContext.PAGE_SCOPE);
136
//-------------------------------------
139
* Returns the Map that "wraps" page-scoped attributes
141
public Map getPageScopeMap ()
144
mPage = createPageScopeMap (mContext);
149
//-------------------------------------
152
* Returns the Map that "wraps" request-scoped attributes
154
public Map getRequestScopeMap ()
156
if (mRequest == null) {
157
mRequest = createRequestScopeMap (mContext);
162
//-------------------------------------
165
* Returns the Map that "wraps" session-scoped attributes
167
public Map getSessionScopeMap ()
169
if (mSession == null) {
170
mSession = createSessionScopeMap (mContext);
175
//-------------------------------------
178
* Returns the Map that "wraps" application-scoped attributes
180
public Map getApplicationScopeMap ()
182
if (mApplication == null) {
183
mApplication = createApplicationScopeMap (mContext);
188
//-------------------------------------
191
* Returns the Map that maps parameter name to a single parameter
194
public Map getParamMap ()
196
if (mParam == null) {
197
mParam = createParamMap (mContext);
202
//-------------------------------------
205
* Returns the Map that maps parameter name to an array of parameter
208
public Map getParamsMap ()
210
if (mParams == null) {
211
mParams = createParamsMap (mContext);
216
//-------------------------------------
219
* Returns the Map that maps header name to a single header
222
public Map getHeaderMap ()
224
if (mHeader == null) {
225
mHeader = createHeaderMap (mContext);
230
//-------------------------------------
233
* Returns the Map that maps header name to an array of header
236
public Map getHeadersMap ()
238
if (mHeaders == null) {
239
mHeaders = createHeadersMap (mContext);
244
//-------------------------------------
247
* Returns the Map that maps init parameter name to a single init
250
public Map getInitParamMap ()
252
if (mInitParam == null) {
253
mInitParam = createInitParamMap (mContext);
258
//-------------------------------------
261
* Returns the Map that maps cookie name to the first matching
262
* Cookie in request.getCookies().
264
public Map getCookieMap ()
266
if (mCookie == null) {
267
mCookie = createCookieMap (mContext);
272
//-------------------------------------
273
// Methods for generating wrapper maps
274
//-------------------------------------
277
* Creates the Map that "wraps" page-scoped attributes
279
public static Map createPageScopeMap (PageContext pContext)
281
final PageContext context = pContext;
282
return new EnumeratedMap ()
284
public Enumeration enumerateKeys ()
286
return context.getAttributeNamesInScope
287
(PageContext.PAGE_SCOPE);
290
public Object getValue (Object pKey)
292
if (pKey instanceof String) {
293
return context.getAttribute
295
PageContext.PAGE_SCOPE);
302
public boolean isMutable ()
309
//-------------------------------------
312
* Creates the Map that "wraps" request-scoped attributes
314
public static Map createRequestScopeMap (PageContext pContext)
316
final PageContext context = pContext;
317
return new EnumeratedMap ()
319
public Enumeration enumerateKeys ()
321
return context.getAttributeNamesInScope
322
(PageContext.REQUEST_SCOPE);
325
public Object getValue (Object pKey)
327
if (pKey instanceof String) {
328
return context.getAttribute
330
PageContext.REQUEST_SCOPE);
337
public boolean isMutable ()
344
//-------------------------------------
347
* Creates the Map that "wraps" session-scoped attributes
349
public static Map createSessionScopeMap (PageContext pContext)
351
final PageContext context = pContext;
352
return new EnumeratedMap ()
354
public Enumeration enumerateKeys ()
356
return context.getAttributeNamesInScope
357
(PageContext.SESSION_SCOPE);
360
public Object getValue (Object pKey)
362
if (pKey instanceof String) {
363
return context.getAttribute
365
PageContext.SESSION_SCOPE);
372
public boolean isMutable ()
379
//-------------------------------------
382
* Creates the Map that "wraps" application-scoped attributes
384
public static Map createApplicationScopeMap (PageContext pContext)
386
final PageContext context = pContext;
387
return new EnumeratedMap ()
389
public Enumeration enumerateKeys ()
391
return context.getAttributeNamesInScope
392
(PageContext.APPLICATION_SCOPE);
395
public Object getValue (Object pKey)
397
if (pKey instanceof String) {
398
return context.getAttribute
400
PageContext.APPLICATION_SCOPE);
407
public boolean isMutable ()
414
//-------------------------------------
417
* Creates the Map that maps parameter name to single parameter
420
public static Map createParamMap (PageContext pContext)
422
final HttpServletRequest request =
423
(HttpServletRequest) pContext.getRequest ();
424
return new EnumeratedMap ()
426
public Enumeration enumerateKeys ()
428
return request.getParameterNames ();
431
public Object getValue (Object pKey)
433
if (pKey instanceof String) {
434
return request.getParameter ((String) pKey);
441
public boolean isMutable ()
448
//-------------------------------------
451
* Creates the Map that maps parameter name to an array of parameter
454
public static Map createParamsMap (PageContext pContext)
456
final HttpServletRequest request =
457
(HttpServletRequest) pContext.getRequest ();
458
return new EnumeratedMap ()
460
public Enumeration enumerateKeys ()
462
return request.getParameterNames ();
465
public Object getValue (Object pKey)
467
if (pKey instanceof String) {
468
return request.getParameterValues ((String) pKey);
475
public boolean isMutable ()
482
//-------------------------------------
485
* Creates the Map that maps header name to single header
488
public static Map createHeaderMap (PageContext pContext)
490
final HttpServletRequest request =
491
(HttpServletRequest) pContext.getRequest ();
492
return new EnumeratedMap ()
494
public Enumeration enumerateKeys ()
496
return request.getHeaderNames ();
499
public Object getValue (Object pKey)
501
if (pKey instanceof String) {
502
return request.getHeader ((String) pKey);
509
public boolean isMutable ()
516
//-------------------------------------
519
* Creates the Map that maps header name to an array of header
522
public static Map createHeadersMap (PageContext pContext)
524
final HttpServletRequest request =
525
(HttpServletRequest) pContext.getRequest ();
526
return new EnumeratedMap ()
528
public Enumeration enumerateKeys ()
530
return request.getHeaderNames ();
533
public Object getValue (Object pKey)
535
if (pKey instanceof String) {
536
// Drain the header enumeration
537
List l = new ArrayList ();
538
Enumeration enum = request.getHeaders ((String) pKey);
540
while (enum.hasMoreElements ()) {
541
l.add (enum.nextElement ());
544
String [] ret = (String []) l.toArray (new String [l.size ()]);
552
public boolean isMutable ()
559
//-------------------------------------
562
* Creates the Map that maps init parameter name to single init
565
public static Map createInitParamMap (PageContext pContext)
567
final ServletContext context = pContext.getServletContext ();
568
return new EnumeratedMap ()
570
public Enumeration enumerateKeys ()
572
return context.getInitParameterNames ();
575
public Object getValue (Object pKey)
577
if (pKey instanceof String) {
578
return context.getInitParameter ((String) pKey);
585
public boolean isMutable ()
592
//-------------------------------------
595
* Creates the Map that maps cookie name to the first matching
596
* Cookie in request.getCookies().
598
public static Map createCookieMap (PageContext pContext)
600
// Read all the cookies and construct the entire map
601
HttpServletRequest request = (HttpServletRequest) pContext.getRequest ();
602
Cookie [] cookies = request.getCookies ();
603
Map ret = new HashMap ();
604
for (int i = 0; cookies != null && i < cookies.length; i++) {
605
Cookie cookie = cookies [i];
606
if (cookie != null) {
607
String name = cookie.getName ();
608
if (!ret.containsKey (name)) {
609
ret.put (name, cookie);
616
//-------------------------------------