001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.el;
018    
019    import java.util.ArrayList;
020    import java.util.Enumeration;
021    import java.util.HashMap;
022    import java.util.List;
023    import java.util.Map;
024    
025    import javax.servlet.ServletContext;
026    import javax.servlet.http.Cookie;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.PageContext;
029    
030    /**
031     *
032     * <p>This class is used to generate the implicit Map and List objects
033     * that wrap various elements of the PageContext.  It also returns the
034     * correct implicit object for a given implicit object name.
035     * 
036     * @author Nathan Abramson - Art Technology Group
037     * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: mbenson $
038     **/
039    
040    public class ImplicitObjects
041    {
042      //-------------------------------------
043      // Constants
044      //-------------------------------------
045    
046      static final String sAttributeName = 
047        "org.apache.commons.el.ImplicitObjects";
048    
049      //-------------------------------------
050      // Member variables
051      //-------------------------------------
052    
053      PageContext mContext;
054      Map mPage;
055      Map mRequest;
056      Map mSession;
057      Map mApplication;
058      Map mParam;
059      Map mParams;
060      Map mHeader;
061      Map mHeaders;
062      Map mInitParam;
063      Map mCookie;
064    
065      //-------------------------------------
066      /**
067       *
068       * Constructor
069       **/
070      public ImplicitObjects (PageContext pContext)
071      {
072        mContext = pContext;
073      }
074    
075      //-------------------------------------
076      /**
077       *
078       * Finds the ImplicitObjects associated with the PageContext,
079       * creating it if it doesn't yet exist.
080       **/
081      public static ImplicitObjects getImplicitObjects (PageContext pContext)
082      {
083        ImplicitObjects objs = 
084          (ImplicitObjects)
085          pContext.getAttribute (sAttributeName,
086                                 PageContext.PAGE_SCOPE);
087        if (objs == null) {
088          objs = new ImplicitObjects (pContext);
089          pContext.setAttribute (sAttributeName,
090                                 objs,
091                                 PageContext.PAGE_SCOPE);
092        }
093        return objs;
094      }
095    
096      //-------------------------------------
097      /**
098       *
099       * Returns the Map that "wraps" page-scoped attributes
100       **/
101      public Map getPageScopeMap ()
102      {
103        if (mPage == null) {
104          mPage = createPageScopeMap (mContext);
105        }
106        return mPage;
107      }
108    
109      //-------------------------------------
110      /**
111       *
112       * Returns the Map that "wraps" request-scoped attributes
113       **/
114      public Map getRequestScopeMap ()
115      {
116        if (mRequest == null) {
117          mRequest = createRequestScopeMap (mContext);
118        }
119        return mRequest;
120      }
121    
122      //-------------------------------------
123      /**
124       *
125       * Returns the Map that "wraps" session-scoped attributes
126       **/
127      public Map getSessionScopeMap ()
128      {
129        if (mSession == null) {
130          mSession = createSessionScopeMap (mContext);
131        }
132        return mSession;
133      }
134    
135      //-------------------------------------
136      /**
137       *
138       * Returns the Map that "wraps" application-scoped attributes
139       **/
140      public Map getApplicationScopeMap ()
141      {
142        if (mApplication == null) {
143          mApplication = createApplicationScopeMap (mContext);
144        }
145        return mApplication;
146      }
147    
148      //-------------------------------------
149      /**
150       *
151       * Returns the Map that maps parameter name to a single parameter
152       * values.
153       **/
154      public Map getParamMap ()
155      {
156        if (mParam == null) {
157          mParam = createParamMap (mContext);
158        }
159        return mParam;
160      }
161    
162      //-------------------------------------
163      /**
164       *
165       * Returns the Map that maps parameter name to an array of parameter
166       * values.
167       **/
168      public Map getParamsMap ()
169      {
170        if (mParams == null) {
171          mParams = createParamsMap (mContext);
172        }
173        return mParams;
174      }
175    
176      //-------------------------------------
177      /**
178       *
179       * Returns the Map that maps header name to a single header
180       * values.
181       **/
182      public Map getHeaderMap ()
183      {
184        if (mHeader == null) {
185          mHeader = createHeaderMap (mContext);
186        }
187        return mHeader;
188      }
189    
190      //-------------------------------------
191      /**
192       *
193       * Returns the Map that maps header name to an array of header
194       * values.
195       **/
196      public Map getHeadersMap ()
197      {
198        if (mHeaders == null) {
199          mHeaders = createHeadersMap (mContext);
200        }
201        return mHeaders;
202      }
203    
204      //-------------------------------------
205      /**
206       *
207       * Returns the Map that maps init parameter name to a single init
208       * parameter values.
209       **/
210      public Map getInitParamMap ()
211      {
212        if (mInitParam == null) {
213          mInitParam = createInitParamMap (mContext);
214        }
215        return mInitParam;
216      }
217    
218      //-------------------------------------
219      /**
220       *
221       * Returns the Map that maps cookie name to the first matching
222       * Cookie in request.getCookies().
223       **/
224      public Map getCookieMap ()
225      {
226        if (mCookie == null) {
227          mCookie = createCookieMap (mContext);
228        }
229        return mCookie;
230      }
231    
232      //-------------------------------------
233      // Methods for generating wrapper maps
234      //-------------------------------------
235      /**
236       *
237       * Creates the Map that "wraps" page-scoped attributes
238       **/
239      public static Map createPageScopeMap (PageContext pContext)
240      {
241        final PageContext context = pContext;
242        return new EnumeratedMap ()
243          {
244            public Enumeration enumerateKeys () 
245            {
246              return context.getAttributeNamesInScope
247                (PageContext.PAGE_SCOPE);
248            }
249    
250            public Object getValue (Object pKey) 
251            {
252              if (pKey instanceof String) {
253                return context.getAttribute
254                  ((String) pKey, 
255                   PageContext.PAGE_SCOPE);
256              }
257              else {
258                return null;
259              }
260            }
261    
262            public boolean isMutable ()
263            {
264              return true;
265            }
266          };
267      }
268    
269      //-------------------------------------
270      /**
271       *
272       * Creates the Map that "wraps" request-scoped attributes
273       **/
274      public static Map createRequestScopeMap (PageContext pContext)
275      {
276        final PageContext context = pContext;
277        return new EnumeratedMap ()
278          {
279            public Enumeration enumerateKeys () 
280            {
281              return context.getAttributeNamesInScope
282                (PageContext.REQUEST_SCOPE);
283            }
284    
285            public Object getValue (Object pKey) 
286            {
287              if (pKey instanceof String) {
288                return context.getAttribute
289                  ((String) pKey, 
290                   PageContext.REQUEST_SCOPE);
291              }
292              else {
293                return null;
294              }
295            }
296    
297            public boolean isMutable ()
298            {
299              return true;
300            }
301          };
302      }
303    
304      //-------------------------------------
305      /**
306       *
307       * Creates the Map that "wraps" session-scoped attributes
308       **/
309      public static Map createSessionScopeMap (PageContext pContext)
310      {
311        final PageContext context = pContext;
312        return new EnumeratedMap ()
313          {
314            public Enumeration enumerateKeys () 
315            {
316              return context.getAttributeNamesInScope
317                (PageContext.SESSION_SCOPE);
318            }
319    
320            public Object getValue (Object pKey) 
321            {
322              if (pKey instanceof String) {
323                return context.getAttribute
324                  ((String) pKey, 
325                   PageContext.SESSION_SCOPE);
326              }
327              else {
328                return null;
329              }
330            }
331    
332            public boolean isMutable ()
333            {
334              return true;
335            }
336          };
337      }
338    
339      //-------------------------------------
340      /**
341       *
342       * Creates the Map that "wraps" application-scoped attributes
343       **/
344      public static Map createApplicationScopeMap (PageContext pContext)
345      {
346        final PageContext context = pContext;
347        return new EnumeratedMap ()
348          {
349            public Enumeration enumerateKeys () 
350            {
351              return context.getAttributeNamesInScope
352                (PageContext.APPLICATION_SCOPE);
353            }
354    
355            public Object getValue (Object pKey) 
356            {
357              if (pKey instanceof String) {
358                return context.getAttribute
359                  ((String) pKey, 
360                   PageContext.APPLICATION_SCOPE);
361              }
362              else {
363                return null;
364              }
365            }
366    
367            public boolean isMutable ()
368            {
369              return true;
370            }
371          };
372      }
373    
374      //-------------------------------------
375      /**
376       *
377       * Creates the Map that maps parameter name to single parameter
378       * value.
379       **/
380      public static Map createParamMap (PageContext pContext)
381      {
382        final HttpServletRequest request =
383          (HttpServletRequest) pContext.getRequest ();
384        return new EnumeratedMap ()
385          {
386            public Enumeration enumerateKeys () 
387            {
388              return request.getParameterNames ();
389            }
390    
391            public Object getValue (Object pKey) 
392            {
393              if (pKey instanceof String) {
394                return request.getParameter ((String) pKey);
395              }
396              else {
397                return null;
398              }
399            }
400    
401            public boolean isMutable ()
402            {
403              return false;
404            }
405          };
406      }
407    
408      //-------------------------------------
409      /**
410       *
411       * Creates the Map that maps parameter name to an array of parameter
412       * values.
413       **/
414      public static Map createParamsMap (PageContext pContext)
415      {
416        final HttpServletRequest request =
417          (HttpServletRequest) pContext.getRequest ();
418        return new EnumeratedMap ()
419          {
420            public Enumeration enumerateKeys () 
421            {
422              return request.getParameterNames ();
423            }
424    
425            public Object getValue (Object pKey) 
426            {
427              if (pKey instanceof String) {
428                return request.getParameterValues ((String) pKey);
429              }
430              else {
431                return null;
432              }
433            }
434    
435            public boolean isMutable ()
436            {
437              return false;
438            }
439          };
440      }
441    
442      //-------------------------------------
443      /**
444       *
445       * Creates the Map that maps header name to single header
446       * value.
447       **/
448      public static Map createHeaderMap (PageContext pContext)
449      {
450        final HttpServletRequest request =
451          (HttpServletRequest) pContext.getRequest ();
452        return new EnumeratedMap ()
453          {
454            public Enumeration enumerateKeys () 
455            {
456              return request.getHeaderNames ();
457            }
458    
459            public Object getValue (Object pKey) 
460            {
461              if (pKey instanceof String) {
462                return request.getHeader ((String) pKey);
463              }
464              else {
465                return null;
466              }
467            }
468    
469            public boolean isMutable ()
470            {
471              return false;
472            }
473          };
474      }
475    
476      //-------------------------------------
477      /**
478       *
479       * Creates the Map that maps header name to an array of header
480       * values.
481       **/
482      public static Map createHeadersMap (PageContext pContext)
483      {
484        final HttpServletRequest request =
485          (HttpServletRequest) pContext.getRequest ();
486        return new EnumeratedMap ()
487          {
488            public Enumeration enumerateKeys () 
489            {
490              return request.getHeaderNames ();
491            }
492    
493            public Object getValue (Object pKey) 
494            {
495              if (pKey instanceof String) {
496                // Drain the header enumeration
497                List l = new ArrayList ();
498                Enumeration e = request.getHeaders ((String) pKey);
499                if (e != null) {
500                  while (e.hasMoreElements ()) {
501                    l.add (e.nextElement ());
502                  }
503                }
504                String [] ret = (String []) l.toArray (new String [l.size ()]);
505                return ret;
506              }
507              else {
508                return null;
509              }
510            }
511    
512            public boolean isMutable ()
513            {
514              return false;
515            }
516          };
517      }
518    
519      //-------------------------------------
520      /**
521       *
522       * Creates the Map that maps init parameter name to single init
523       * parameter value.
524       **/
525      public static Map createInitParamMap (PageContext pContext)
526      {
527        final ServletContext context = pContext.getServletContext ();
528        return new EnumeratedMap ()
529          {
530            public Enumeration enumerateKeys () 
531            {
532              return context.getInitParameterNames ();
533            }
534    
535            public Object getValue (Object pKey) 
536            {
537              if (pKey instanceof String) {
538                return context.getInitParameter ((String) pKey);
539              }
540              else {
541                return null;
542              }
543            }
544    
545            public boolean isMutable ()
546            {
547              return false;
548            }
549          };
550      }
551    
552      //-------------------------------------
553      /**
554       *
555       * Creates the Map that maps cookie name to the first matching
556       * Cookie in request.getCookies().
557       **/
558      public static Map createCookieMap (PageContext pContext)
559      {
560        // Read all the cookies and construct the entire map
561        HttpServletRequest request = (HttpServletRequest) pContext.getRequest ();
562        Cookie [] cookies = request.getCookies ();
563        Map ret = new HashMap ();
564        for (int i = 0; cookies != null && i < cookies.length; i++) {
565          Cookie cookie = cookies [i];
566          if (cookie != null) {
567            String name = cookie.getName ();
568            if (!ret.containsKey (name)) {
569              ret.put (name, cookie);
570            }
571          }
572        }
573        return ret;
574      }
575    
576      //-------------------------------------
577    }