Coverage Report - org.apache.commons.el.ImplicitObjects
 
Classes in this File Line Coverage Branch Coverage Complexity
ImplicitObjects
0%
0/66
0%
0/30
1.714
ImplicitObjects$1
0%
0/6
0%
0/2
1.714
ImplicitObjects$2
0%
0/6
0%
0/2
1.714
ImplicitObjects$3
0%
0/6
0%
0/2
1.714
ImplicitObjects$4
0%
0/6
0%
0/2
1.714
ImplicitObjects$5
0%
0/6
0%
0/2
1.714
ImplicitObjects$6
0%
0/6
0%
0/2
1.714
ImplicitObjects$7
0%
0/6
0%
0/2
1.714
ImplicitObjects$8
0%
0/12
0%
0/6
1.714
ImplicitObjects$9
0%
0/6
0%
0/2
1.714
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.el;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Enumeration;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 import javax.servlet.ServletContext;
 26  
 import javax.servlet.http.Cookie;
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 import javax.servlet.jsp.PageContext;
 29  
 
 30  
 /**
 31  
  *
 32  
  * <p>This class is used to generate the implicit Map and List objects
 33  
  * that wrap various elements of the PageContext.  It also returns the
 34  
  * correct implicit object for a given implicit object name.
 35  
  * 
 36  
  * @author Nathan Abramson - Art Technology Group
 37  
  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: mbenson $
 38  
  **/
 39  
 
 40  
 public class ImplicitObjects
 41  
 {
 42  
   //-------------------------------------
 43  
   // Constants
 44  
   //-------------------------------------
 45  
 
 46  
   static final String sAttributeName = 
 47  
     "org.apache.commons.el.ImplicitObjects";
 48  
 
 49  
   //-------------------------------------
 50  
   // Member variables
 51  
   //-------------------------------------
 52  
 
 53  
   PageContext mContext;
 54  
   Map mPage;
 55  
   Map mRequest;
 56  
   Map mSession;
 57  
   Map mApplication;
 58  
   Map mParam;
 59  
   Map mParams;
 60  
   Map mHeader;
 61  
   Map mHeaders;
 62  
   Map mInitParam;
 63  
   Map mCookie;
 64  
 
 65  
   //-------------------------------------
 66  
   /**
 67  
    *
 68  
    * Constructor
 69  
    **/
 70  
   public ImplicitObjects (PageContext pContext)
 71  0
   {
 72  0
     mContext = pContext;
 73  0
   }
 74  
 
 75  
   //-------------------------------------
 76  
   /**
 77  
    *
 78  
    * Finds the ImplicitObjects associated with the PageContext,
 79  
    * creating it if it doesn't yet exist.
 80  
    **/
 81  
   public static ImplicitObjects getImplicitObjects (PageContext pContext)
 82  
   {
 83  0
     ImplicitObjects objs = 
 84  
       (ImplicitObjects)
 85  
       pContext.getAttribute (sAttributeName,
 86  
                              PageContext.PAGE_SCOPE);
 87  0
     if (objs == null) {
 88  0
       objs = new ImplicitObjects (pContext);
 89  0
       pContext.setAttribute (sAttributeName,
 90  
                              objs,
 91  
                              PageContext.PAGE_SCOPE);
 92  
     }
 93  0
     return objs;
 94  
   }
 95  
 
 96  
   //-------------------------------------
 97  
   /**
 98  
    *
 99  
    * Returns the Map that "wraps" page-scoped attributes
 100  
    **/
 101  
   public Map getPageScopeMap ()
 102  
   {
 103  0
     if (mPage == null) {
 104  0
       mPage = createPageScopeMap (mContext);
 105  
     }
 106  0
     return mPage;
 107  
   }
 108  
 
 109  
   //-------------------------------------
 110  
   /**
 111  
    *
 112  
    * Returns the Map that "wraps" request-scoped attributes
 113  
    **/
 114  
   public Map getRequestScopeMap ()
 115  
   {
 116  0
     if (mRequest == null) {
 117  0
       mRequest = createRequestScopeMap (mContext);
 118  
     }
 119  0
     return mRequest;
 120  
   }
 121  
 
 122  
   //-------------------------------------
 123  
   /**
 124  
    *
 125  
    * Returns the Map that "wraps" session-scoped attributes
 126  
    **/
 127  
   public Map getSessionScopeMap ()
 128  
   {
 129  0
     if (mSession == null) {
 130  0
       mSession = createSessionScopeMap (mContext);
 131  
     }
 132  0
     return mSession;
 133  
   }
 134  
 
 135  
   //-------------------------------------
 136  
   /**
 137  
    *
 138  
    * Returns the Map that "wraps" application-scoped attributes
 139  
    **/
 140  
   public Map getApplicationScopeMap ()
 141  
   {
 142  0
     if (mApplication == null) {
 143  0
       mApplication = createApplicationScopeMap (mContext);
 144  
     }
 145  0
     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  0
     if (mParam == null) {
 157  0
       mParam = createParamMap (mContext);
 158  
     }
 159  0
     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  0
     if (mParams == null) {
 171  0
       mParams = createParamsMap (mContext);
 172  
     }
 173  0
     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  0
     if (mHeader == null) {
 185  0
       mHeader = createHeaderMap (mContext);
 186  
     }
 187  0
     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  0
     if (mHeaders == null) {
 199  0
       mHeaders = createHeadersMap (mContext);
 200  
     }
 201  0
     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  0
     if (mInitParam == null) {
 213  0
       mInitParam = createInitParamMap (mContext);
 214  
     }
 215  0
     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  0
     if (mCookie == null) {
 227  0
       mCookie = createCookieMap (mContext);
 228  
     }
 229  0
     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  0
     final PageContext context = pContext;
 242  0
     return new EnumeratedMap ()
 243  0
       {
 244  
         public Enumeration enumerateKeys () 
 245  
         {
 246  0
           return context.getAttributeNamesInScope
 247  
             (PageContext.PAGE_SCOPE);
 248  
         }
 249  
 
 250  
         public Object getValue (Object pKey) 
 251  
         {
 252  0
           if (pKey instanceof String) {
 253  0
             return context.getAttribute
 254  
               ((String) pKey, 
 255  
                PageContext.PAGE_SCOPE);
 256  
           }
 257  
           else {
 258  0
             return null;
 259  
           }
 260  
         }
 261  
 
 262  
         public boolean isMutable ()
 263  
         {
 264  0
           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  0
     final PageContext context = pContext;
 277  0
     return new EnumeratedMap ()
 278  0
       {
 279  
         public Enumeration enumerateKeys () 
 280  
         {
 281  0
           return context.getAttributeNamesInScope
 282  
             (PageContext.REQUEST_SCOPE);
 283  
         }
 284  
 
 285  
         public Object getValue (Object pKey) 
 286  
         {
 287  0
           if (pKey instanceof String) {
 288  0
             return context.getAttribute
 289  
               ((String) pKey, 
 290  
                PageContext.REQUEST_SCOPE);
 291  
           }
 292  
           else {
 293  0
             return null;
 294  
           }
 295  
         }
 296  
 
 297  
         public boolean isMutable ()
 298  
         {
 299  0
           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  0
     final PageContext context = pContext;
 312  0
     return new EnumeratedMap ()
 313  0
       {
 314  
         public Enumeration enumerateKeys () 
 315  
         {
 316  0
           return context.getAttributeNamesInScope
 317  
             (PageContext.SESSION_SCOPE);
 318  
         }
 319  
 
 320  
         public Object getValue (Object pKey) 
 321  
         {
 322  0
           if (pKey instanceof String) {
 323  0
             return context.getAttribute
 324  
               ((String) pKey, 
 325  
                PageContext.SESSION_SCOPE);
 326  
           }
 327  
           else {
 328  0
             return null;
 329  
           }
 330  
         }
 331  
 
 332  
         public boolean isMutable ()
 333  
         {
 334  0
           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  0
     final PageContext context = pContext;
 347  0
     return new EnumeratedMap ()
 348  0
       {
 349  
         public Enumeration enumerateKeys () 
 350  
         {
 351  0
           return context.getAttributeNamesInScope
 352  
             (PageContext.APPLICATION_SCOPE);
 353  
         }
 354  
 
 355  
         public Object getValue (Object pKey) 
 356  
         {
 357  0
           if (pKey instanceof String) {
 358  0
             return context.getAttribute
 359  
               ((String) pKey, 
 360  
                PageContext.APPLICATION_SCOPE);
 361  
           }
 362  
           else {
 363  0
             return null;
 364  
           }
 365  
         }
 366  
 
 367  
         public boolean isMutable ()
 368  
         {
 369  0
           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  0
     final HttpServletRequest request =
 383  
       (HttpServletRequest) pContext.getRequest ();
 384  0
     return new EnumeratedMap ()
 385  0
       {
 386  
         public Enumeration enumerateKeys () 
 387  
         {
 388  0
           return request.getParameterNames ();
 389  
         }
 390  
 
 391  
         public Object getValue (Object pKey) 
 392  
         {
 393  0
           if (pKey instanceof String) {
 394  0
             return request.getParameter ((String) pKey);
 395  
           }
 396  
           else {
 397  0
             return null;
 398  
           }
 399  
         }
 400  
 
 401  
         public boolean isMutable ()
 402  
         {
 403  0
           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  0
     final HttpServletRequest request =
 417  
       (HttpServletRequest) pContext.getRequest ();
 418  0
     return new EnumeratedMap ()
 419  0
       {
 420  
         public Enumeration enumerateKeys () 
 421  
         {
 422  0
           return request.getParameterNames ();
 423  
         }
 424  
 
 425  
         public Object getValue (Object pKey) 
 426  
         {
 427  0
           if (pKey instanceof String) {
 428  0
             return request.getParameterValues ((String) pKey);
 429  
           }
 430  
           else {
 431  0
             return null;
 432  
           }
 433  
         }
 434  
 
 435  
         public boolean isMutable ()
 436  
         {
 437  0
           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  0
     final HttpServletRequest request =
 451  
       (HttpServletRequest) pContext.getRequest ();
 452  0
     return new EnumeratedMap ()
 453  0
       {
 454  
         public Enumeration enumerateKeys () 
 455  
         {
 456  0
           return request.getHeaderNames ();
 457  
         }
 458  
 
 459  
         public Object getValue (Object pKey) 
 460  
         {
 461  0
           if (pKey instanceof String) {
 462  0
             return request.getHeader ((String) pKey);
 463  
           }
 464  
           else {
 465  0
             return null;
 466  
           }
 467  
         }
 468  
 
 469  
         public boolean isMutable ()
 470  
         {
 471  0
           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  0
     final HttpServletRequest request =
 485  
       (HttpServletRequest) pContext.getRequest ();
 486  0
     return new EnumeratedMap ()
 487  0
       {
 488  
         public Enumeration enumerateKeys () 
 489  
         {
 490  0
           return request.getHeaderNames ();
 491  
         }
 492  
 
 493  
         public Object getValue (Object pKey) 
 494  
         {
 495  0
           if (pKey instanceof String) {
 496  
             // Drain the header enumeration
 497  0
             List l = new ArrayList ();
 498  0
             Enumeration e = request.getHeaders ((String) pKey);
 499  0
             if (e != null) {
 500  0
               while (e.hasMoreElements ()) {
 501  0
                 l.add (e.nextElement ());
 502  
               }
 503  
             }
 504  0
             String [] ret = (String []) l.toArray (new String [l.size ()]);
 505  0
             return ret;
 506  
           }
 507  
           else {
 508  0
             return null;
 509  
           }
 510  
         }
 511  
 
 512  
         public boolean isMutable ()
 513  
         {
 514  0
           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  0
     final ServletContext context = pContext.getServletContext ();
 528  0
     return new EnumeratedMap ()
 529  0
       {
 530  
         public Enumeration enumerateKeys () 
 531  
         {
 532  0
           return context.getInitParameterNames ();
 533  
         }
 534  
 
 535  
         public Object getValue (Object pKey) 
 536  
         {
 537  0
           if (pKey instanceof String) {
 538  0
             return context.getInitParameter ((String) pKey);
 539  
           }
 540  
           else {
 541  0
             return null;
 542  
           }
 543  
         }
 544  
 
 545  
         public boolean isMutable ()
 546  
         {
 547  0
           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  0
     HttpServletRequest request = (HttpServletRequest) pContext.getRequest ();
 562  0
     Cookie [] cookies = request.getCookies ();
 563  0
     Map ret = new HashMap ();
 564  0
     for (int i = 0; cookies != null && i < cookies.length; i++) {
 565  0
       Cookie cookie = cookies [i];
 566  0
       if (cookie != null) {
 567  0
         String name = cookie.getName ();
 568  0
         if (!ret.containsKey (name)) {
 569  0
           ret.put (name, cookie);
 570  
         }
 571  
       }
 572  
     }
 573  0
     return ret;
 574  
   }
 575  
 
 576  
   //-------------------------------------
 577  
 }