~ubuntu-branches/debian/sid/libcommons-el-java/sid

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/el/ImplicitObjects.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2003-07-01 17:04:27 UTC
  • Revision ID: james.westby@ubuntu.com-20030701170427-tiu89jnf1mggvln0
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 * Copyright (c) 1999 The Apache Software Foundation.  All rights 
 
5
 * reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer. 
 
13
 *
 
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
 
17
 *    distribution.
 
18
 *
 
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.
 
25
 *
 
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.
 
30
 *
 
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.
 
34
 *
 
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
 
46
 * SUCH DAMAGE.
 
47
 * ====================================================================
 
48
 *
 
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/>.
 
53
 *
 
54
 */ 
 
55
 
 
56
package org.apache.commons.el;
 
57
 
 
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;
 
64
import java.util.Map;
 
65
import javax.servlet.ServletContext;
 
66
import javax.servlet.http.Cookie;
 
67
import javax.servlet.http.HttpServletRequest;
 
68
import javax.servlet.jsp.PageContext;
 
69
 
 
70
/**
 
71
 *
 
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.
 
75
 * 
 
76
 * @author Nathan Abramson - Art Technology Group
 
77
 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
 
78
 **/
 
79
 
 
80
public class ImplicitObjects
 
81
{
 
82
  //-------------------------------------
 
83
  // Constants
 
84
  //-------------------------------------
 
85
 
 
86
  static final String sAttributeName = 
 
87
    "org.apache.taglibs.standard.ImplicitObjects";
 
88
 
 
89
  //-------------------------------------
 
90
  // Member variables
 
91
  //-------------------------------------
 
92
 
 
93
  PageContext mContext;
 
94
  Map mPage;
 
95
  Map mRequest;
 
96
  Map mSession;
 
97
  Map mApplication;
 
98
  Map mParam;
 
99
  Map mParams;
 
100
  Map mHeader;
 
101
  Map mHeaders;
 
102
  Map mInitParam;
 
103
  Map mCookie;
 
104
 
 
105
  //-------------------------------------
 
106
  /**
 
107
   *
 
108
   * Constructor
 
109
   **/
 
110
  public ImplicitObjects (PageContext pContext)
 
111
  {
 
112
    mContext = pContext;
 
113
  }
 
114
 
 
115
  //-------------------------------------
 
116
  /**
 
117
   *
 
118
   * Finds the ImplicitObjects associated with the PageContext,
 
119
   * creating it if it doesn't yet exist.
 
120
   **/
 
121
  public static ImplicitObjects getImplicitObjects (PageContext pContext)
 
122
  {
 
123
    ImplicitObjects objs = 
 
124
      (ImplicitObjects)
 
125
      pContext.getAttribute (sAttributeName,
 
126
                             PageContext.PAGE_SCOPE);
 
127
    if (objs == null) {
 
128
      objs = new ImplicitObjects (pContext);
 
129
      pContext.setAttribute (sAttributeName,
 
130
                             objs,
 
131
                             PageContext.PAGE_SCOPE);
 
132
    }
 
133
    return objs;
 
134
  }
 
135
 
 
136
  //-------------------------------------
 
137
  /**
 
138
   *
 
139
   * Returns the Map that "wraps" page-scoped attributes
 
140
   **/
 
141
  public Map getPageScopeMap ()
 
142
  {
 
143
    if (mPage == null) {
 
144
      mPage = createPageScopeMap (mContext);
 
145
    }
 
146
    return mPage;
 
147
  }
 
148
 
 
149
  //-------------------------------------
 
150
  /**
 
151
   *
 
152
   * Returns the Map that "wraps" request-scoped attributes
 
153
   **/
 
154
  public Map getRequestScopeMap ()
 
155
  {
 
156
    if (mRequest == null) {
 
157
      mRequest = createRequestScopeMap (mContext);
 
158
    }
 
159
    return mRequest;
 
160
  }
 
161
 
 
162
  //-------------------------------------
 
163
  /**
 
164
   *
 
165
   * Returns the Map that "wraps" session-scoped attributes
 
166
   **/
 
167
  public Map getSessionScopeMap ()
 
168
  {
 
169
    if (mSession == null) {
 
170
      mSession = createSessionScopeMap (mContext);
 
171
    }
 
172
    return mSession;
 
173
  }
 
174
 
 
175
  //-------------------------------------
 
176
  /**
 
177
   *
 
178
   * Returns the Map that "wraps" application-scoped attributes
 
179
   **/
 
180
  public Map getApplicationScopeMap ()
 
181
  {
 
182
    if (mApplication == null) {
 
183
      mApplication = createApplicationScopeMap (mContext);
 
184
    }
 
185
    return mApplication;
 
186
  }
 
187
 
 
188
  //-------------------------------------
 
189
  /**
 
190
   *
 
191
   * Returns the Map that maps parameter name to a single parameter
 
192
   * values.
 
193
   **/
 
194
  public Map getParamMap ()
 
195
  {
 
196
    if (mParam == null) {
 
197
      mParam = createParamMap (mContext);
 
198
    }
 
199
    return mParam;
 
200
  }
 
201
 
 
202
  //-------------------------------------
 
203
  /**
 
204
   *
 
205
   * Returns the Map that maps parameter name to an array of parameter
 
206
   * values.
 
207
   **/
 
208
  public Map getParamsMap ()
 
209
  {
 
210
    if (mParams == null) {
 
211
      mParams = createParamsMap (mContext);
 
212
    }
 
213
    return mParams;
 
214
  }
 
215
 
 
216
  //-------------------------------------
 
217
  /**
 
218
   *
 
219
   * Returns the Map that maps header name to a single header
 
220
   * values.
 
221
   **/
 
222
  public Map getHeaderMap ()
 
223
  {
 
224
    if (mHeader == null) {
 
225
      mHeader = createHeaderMap (mContext);
 
226
    }
 
227
    return mHeader;
 
228
  }
 
229
 
 
230
  //-------------------------------------
 
231
  /**
 
232
   *
 
233
   * Returns the Map that maps header name to an array of header
 
234
   * values.
 
235
   **/
 
236
  public Map getHeadersMap ()
 
237
  {
 
238
    if (mHeaders == null) {
 
239
      mHeaders = createHeadersMap (mContext);
 
240
    }
 
241
    return mHeaders;
 
242
  }
 
243
 
 
244
  //-------------------------------------
 
245
  /**
 
246
   *
 
247
   * Returns the Map that maps init parameter name to a single init
 
248
   * parameter values.
 
249
   **/
 
250
  public Map getInitParamMap ()
 
251
  {
 
252
    if (mInitParam == null) {
 
253
      mInitParam = createInitParamMap (mContext);
 
254
    }
 
255
    return mInitParam;
 
256
  }
 
257
 
 
258
  //-------------------------------------
 
259
  /**
 
260
   *
 
261
   * Returns the Map that maps cookie name to the first matching
 
262
   * Cookie in request.getCookies().
 
263
   **/
 
264
  public Map getCookieMap ()
 
265
  {
 
266
    if (mCookie == null) {
 
267
      mCookie = createCookieMap (mContext);
 
268
    }
 
269
    return mCookie;
 
270
  }
 
271
 
 
272
  //-------------------------------------
 
273
  // Methods for generating wrapper maps
 
274
  //-------------------------------------
 
275
  /**
 
276
   *
 
277
   * Creates the Map that "wraps" page-scoped attributes
 
278
   **/
 
279
  public static Map createPageScopeMap (PageContext pContext)
 
280
  {
 
281
    final PageContext context = pContext;
 
282
    return new EnumeratedMap ()
 
283
      {
 
284
        public Enumeration enumerateKeys () 
 
285
        {
 
286
          return context.getAttributeNamesInScope
 
287
            (PageContext.PAGE_SCOPE);
 
288
        }
 
289
 
 
290
        public Object getValue (Object pKey) 
 
291
        {
 
292
          if (pKey instanceof String) {
 
293
            return context.getAttribute
 
294
              ((String) pKey, 
 
295
               PageContext.PAGE_SCOPE);
 
296
          }
 
297
          else {
 
298
            return null;
 
299
          }
 
300
        }
 
301
 
 
302
        public boolean isMutable ()
 
303
        {
 
304
          return true;
 
305
        }
 
306
      };
 
307
  }
 
308
 
 
309
  //-------------------------------------
 
310
  /**
 
311
   *
 
312
   * Creates the Map that "wraps" request-scoped attributes
 
313
   **/
 
314
  public static Map createRequestScopeMap (PageContext pContext)
 
315
  {
 
316
    final PageContext context = pContext;
 
317
    return new EnumeratedMap ()
 
318
      {
 
319
        public Enumeration enumerateKeys () 
 
320
        {
 
321
          return context.getAttributeNamesInScope
 
322
            (PageContext.REQUEST_SCOPE);
 
323
        }
 
324
 
 
325
        public Object getValue (Object pKey) 
 
326
        {
 
327
          if (pKey instanceof String) {
 
328
            return context.getAttribute
 
329
              ((String) pKey, 
 
330
               PageContext.REQUEST_SCOPE);
 
331
          }
 
332
          else {
 
333
            return null;
 
334
          }
 
335
        }
 
336
 
 
337
        public boolean isMutable ()
 
338
        {
 
339
          return true;
 
340
        }
 
341
      };
 
342
  }
 
343
 
 
344
  //-------------------------------------
 
345
  /**
 
346
   *
 
347
   * Creates the Map that "wraps" session-scoped attributes
 
348
   **/
 
349
  public static Map createSessionScopeMap (PageContext pContext)
 
350
  {
 
351
    final PageContext context = pContext;
 
352
    return new EnumeratedMap ()
 
353
      {
 
354
        public Enumeration enumerateKeys () 
 
355
        {
 
356
          return context.getAttributeNamesInScope
 
357
            (PageContext.SESSION_SCOPE);
 
358
        }
 
359
 
 
360
        public Object getValue (Object pKey) 
 
361
        {
 
362
          if (pKey instanceof String) {
 
363
            return context.getAttribute
 
364
              ((String) pKey, 
 
365
               PageContext.SESSION_SCOPE);
 
366
          }
 
367
          else {
 
368
            return null;
 
369
          }
 
370
        }
 
371
 
 
372
        public boolean isMutable ()
 
373
        {
 
374
          return true;
 
375
        }
 
376
      };
 
377
  }
 
378
 
 
379
  //-------------------------------------
 
380
  /**
 
381
   *
 
382
   * Creates the Map that "wraps" application-scoped attributes
 
383
   **/
 
384
  public static Map createApplicationScopeMap (PageContext pContext)
 
385
  {
 
386
    final PageContext context = pContext;
 
387
    return new EnumeratedMap ()
 
388
      {
 
389
        public Enumeration enumerateKeys () 
 
390
        {
 
391
          return context.getAttributeNamesInScope
 
392
            (PageContext.APPLICATION_SCOPE);
 
393
        }
 
394
 
 
395
        public Object getValue (Object pKey) 
 
396
        {
 
397
          if (pKey instanceof String) {
 
398
            return context.getAttribute
 
399
              ((String) pKey, 
 
400
               PageContext.APPLICATION_SCOPE);
 
401
          }
 
402
          else {
 
403
            return null;
 
404
          }
 
405
        }
 
406
 
 
407
        public boolean isMutable ()
 
408
        {
 
409
          return true;
 
410
        }
 
411
      };
 
412
  }
 
413
 
 
414
  //-------------------------------------
 
415
  /**
 
416
   *
 
417
   * Creates the Map that maps parameter name to single parameter
 
418
   * value.
 
419
   **/
 
420
  public static Map createParamMap (PageContext pContext)
 
421
  {
 
422
    final HttpServletRequest request =
 
423
      (HttpServletRequest) pContext.getRequest ();
 
424
    return new EnumeratedMap ()
 
425
      {
 
426
        public Enumeration enumerateKeys () 
 
427
        {
 
428
          return request.getParameterNames ();
 
429
        }
 
430
 
 
431
        public Object getValue (Object pKey) 
 
432
        {
 
433
          if (pKey instanceof String) {
 
434
            return request.getParameter ((String) pKey);
 
435
          }
 
436
          else {
 
437
            return null;
 
438
          }
 
439
        }
 
440
 
 
441
        public boolean isMutable ()
 
442
        {
 
443
          return false;
 
444
        }
 
445
      };
 
446
  }
 
447
 
 
448
  //-------------------------------------
 
449
  /**
 
450
   *
 
451
   * Creates the Map that maps parameter name to an array of parameter
 
452
   * values.
 
453
   **/
 
454
  public static Map createParamsMap (PageContext pContext)
 
455
  {
 
456
    final HttpServletRequest request =
 
457
      (HttpServletRequest) pContext.getRequest ();
 
458
    return new EnumeratedMap ()
 
459
      {
 
460
        public Enumeration enumerateKeys () 
 
461
        {
 
462
          return request.getParameterNames ();
 
463
        }
 
464
 
 
465
        public Object getValue (Object pKey) 
 
466
        {
 
467
          if (pKey instanceof String) {
 
468
            return request.getParameterValues ((String) pKey);
 
469
          }
 
470
          else {
 
471
            return null;
 
472
          }
 
473
        }
 
474
 
 
475
        public boolean isMutable ()
 
476
        {
 
477
          return false;
 
478
        }
 
479
      };
 
480
  }
 
481
 
 
482
  //-------------------------------------
 
483
  /**
 
484
   *
 
485
   * Creates the Map that maps header name to single header
 
486
   * value.
 
487
   **/
 
488
  public static Map createHeaderMap (PageContext pContext)
 
489
  {
 
490
    final HttpServletRequest request =
 
491
      (HttpServletRequest) pContext.getRequest ();
 
492
    return new EnumeratedMap ()
 
493
      {
 
494
        public Enumeration enumerateKeys () 
 
495
        {
 
496
          return request.getHeaderNames ();
 
497
        }
 
498
 
 
499
        public Object getValue (Object pKey) 
 
500
        {
 
501
          if (pKey instanceof String) {
 
502
            return request.getHeader ((String) pKey);
 
503
          }
 
504
          else {
 
505
            return null;
 
506
          }
 
507
        }
 
508
 
 
509
        public boolean isMutable ()
 
510
        {
 
511
          return false;
 
512
        }
 
513
      };
 
514
  }
 
515
 
 
516
  //-------------------------------------
 
517
  /**
 
518
   *
 
519
   * Creates the Map that maps header name to an array of header
 
520
   * values.
 
521
   **/
 
522
  public static Map createHeadersMap (PageContext pContext)
 
523
  {
 
524
    final HttpServletRequest request =
 
525
      (HttpServletRequest) pContext.getRequest ();
 
526
    return new EnumeratedMap ()
 
527
      {
 
528
        public Enumeration enumerateKeys () 
 
529
        {
 
530
          return request.getHeaderNames ();
 
531
        }
 
532
 
 
533
        public Object getValue (Object pKey) 
 
534
        {
 
535
          if (pKey instanceof String) {
 
536
            // Drain the header enumeration
 
537
            List l = new ArrayList ();
 
538
            Enumeration enum = request.getHeaders ((String) pKey);
 
539
            if (enum != null) {
 
540
              while (enum.hasMoreElements ()) {
 
541
                l.add (enum.nextElement ());
 
542
              }
 
543
            }
 
544
            String [] ret = (String []) l.toArray (new String [l.size ()]);
 
545
            return ret;
 
546
          }
 
547
          else {
 
548
            return null;
 
549
          }
 
550
        }
 
551
 
 
552
        public boolean isMutable ()
 
553
        {
 
554
          return false;
 
555
        }
 
556
      };
 
557
  }
 
558
 
 
559
  //-------------------------------------
 
560
  /**
 
561
   *
 
562
   * Creates the Map that maps init parameter name to single init
 
563
   * parameter value.
 
564
   **/
 
565
  public static Map createInitParamMap (PageContext pContext)
 
566
  {
 
567
    final ServletContext context = pContext.getServletContext ();
 
568
    return new EnumeratedMap ()
 
569
      {
 
570
        public Enumeration enumerateKeys () 
 
571
        {
 
572
          return context.getInitParameterNames ();
 
573
        }
 
574
 
 
575
        public Object getValue (Object pKey) 
 
576
        {
 
577
          if (pKey instanceof String) {
 
578
            return context.getInitParameter ((String) pKey);
 
579
          }
 
580
          else {
 
581
            return null;
 
582
          }
 
583
        }
 
584
 
 
585
        public boolean isMutable ()
 
586
        {
 
587
          return false;
 
588
        }
 
589
      };
 
590
  }
 
591
 
 
592
  //-------------------------------------
 
593
  /**
 
594
   *
 
595
   * Creates the Map that maps cookie name to the first matching
 
596
   * Cookie in request.getCookies().
 
597
   **/
 
598
  public static Map createCookieMap (PageContext pContext)
 
599
  {
 
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);
 
610
        }
 
611
      }
 
612
    }
 
613
    return ret;
 
614
  }
 
615
 
 
616
  //-------------------------------------
 
617
}