1 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 79 80 public class ImplicitObjects 81 { 82 86 static final String sAttributeName = 87 "org.apache.taglibs.standard.ImplicitObjects"; 88 89 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 110 public ImplicitObjects (PageContext pContext) 111 { 112 mContext = pContext; 113 } 114 115 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 141 public Map getPageScopeMap () 142 { 143 if (mPage == null) { 144 mPage = createPageScopeMap (mContext); 145 } 146 return mPage; 147 } 148 149 154 public Map getRequestScopeMap () 155 { 156 if (mRequest == null) { 157 mRequest = createRequestScopeMap (mContext); 158 } 159 return mRequest; 160 } 161 162 167 public Map getSessionScopeMap () 168 { 169 if (mSession == null) { 170 mSession = createSessionScopeMap (mContext); 171 } 172 return mSession; 173 } 174 175 180 public Map getApplicationScopeMap () 181 { 182 if (mApplication == null) { 183 mApplication = createApplicationScopeMap (mContext); 184 } 185 return mApplication; 186 } 187 188 194 public Map getParamMap () 195 { 196 if (mParam == null) { 197 mParam = createParamMap (mContext); 198 } 199 return mParam; 200 } 201 202 208 public Map getParamsMap () 209 { 210 if (mParams == null) { 211 mParams = createParamsMap (mContext); 212 } 213 return mParams; 214 } 215 216 222 public Map getHeaderMap () 223 { 224 if (mHeader == null) { 225 mHeader = createHeaderMap (mContext); 226 } 227 return mHeader; 228 } 229 230 236 public Map getHeadersMap () 237 { 238 if (mHeaders == null) { 239 mHeaders = createHeadersMap (mContext); 240 } 241 return mHeaders; 242 } 243 244 250 public Map getInitParamMap () 251 { 252 if (mInitParam == null) { 253 mInitParam = createInitParamMap (mContext); 254 } 255 return mInitParam; 256 } 257 258 264 public Map getCookieMap () 265 { 266 if (mCookie == null) { 267 mCookie = createCookieMap (mContext); 268 } 269 return mCookie; 270 } 271 272 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 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 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 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 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 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 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 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 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 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 598 public static Map createCookieMap (PageContext pContext) 599 { 600 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 } 618 | Popular Tags |