1 16 17 18 19 package org.apache.taglibs.standard.lang.jstl; 20 21 22 23 import java.util.ArrayList ; 24 25 import java.util.Collections ; 26 27 import java.util.Date ; 28 29 import java.util.Enumeration ; 30 31 import java.util.HashMap ; 32 33 import java.util.List ; 34 35 import java.util.Map ; 36 37 import javax.servlet.ServletContext ; 38 39 import javax.servlet.http.Cookie ; 40 41 import javax.servlet.http.HttpServletRequest ; 42 43 import javax.servlet.jsp.PageContext ; 44 45 46 47 64 65 66 67 public class ImplicitObjects 68 69 { 70 71 73 75 77 78 79 static final String sAttributeName = 80 81 "org.apache.taglibs.standard.ImplicitObjects"; 82 83 84 85 87 89 91 92 93 PageContext mContext; 94 95 Map mPage; 96 97 Map mRequest; 98 99 Map mSession; 100 101 Map mApplication; 102 103 Map mParam; 104 105 Map mParams; 106 107 Map mHeader; 108 109 Map mHeaders; 110 111 Map mInitParam; 112 113 Map mCookie; 114 115 116 117 119 126 127 public ImplicitObjects (PageContext pContext) 128 129 { 130 131 mContext = pContext; 132 133 } 134 135 136 137 139 148 149 public static ImplicitObjects getImplicitObjects (PageContext pContext) 150 151 { 152 153 ImplicitObjects objs = 154 155 (ImplicitObjects) 156 157 pContext.getAttribute (sAttributeName, 158 159 PageContext.PAGE_SCOPE); 160 161 if (objs == null) { 162 163 objs = new ImplicitObjects (pContext); 164 165 pContext.setAttribute (sAttributeName, 166 167 objs, 168 169 PageContext.PAGE_SCOPE); 170 171 } 172 173 return objs; 174 175 } 176 177 178 179 181 188 189 public Map getPageScopeMap () 190 191 { 192 193 if (mPage == null) { 194 195 mPage = createPageScopeMap (mContext); 196 197 } 198 199 return mPage; 200 201 } 202 203 204 205 207 214 215 public Map getRequestScopeMap () 216 217 { 218 219 if (mRequest == null) { 220 221 mRequest = createRequestScopeMap (mContext); 222 223 } 224 225 return mRequest; 226 227 } 228 229 230 231 233 240 241 public Map getSessionScopeMap () 242 243 { 244 245 if (mSession == null) { 246 247 mSession = createSessionScopeMap (mContext); 248 249 } 250 251 return mSession; 252 253 } 254 255 256 257 259 266 267 public Map getApplicationScopeMap () 268 269 { 270 271 if (mApplication == null) { 272 273 mApplication = createApplicationScopeMap (mContext); 274 275 } 276 277 return mApplication; 278 279 } 280 281 282 283 285 294 295 public Map getParamMap () 296 297 { 298 299 if (mParam == null) { 300 301 mParam = createParamMap (mContext); 302 303 } 304 305 return mParam; 306 307 } 308 309 310 311 313 322 323 public Map getParamsMap () 324 325 { 326 327 if (mParams == null) { 328 329 mParams = createParamsMap (mContext); 330 331 } 332 333 return mParams; 334 335 } 336 337 338 339 341 350 351 public Map getHeaderMap () 352 353 { 354 355 if (mHeader == null) { 356 357 mHeader = createHeaderMap (mContext); 358 359 } 360 361 return mHeader; 362 363 } 364 365 366 367 369 378 379 public Map getHeadersMap () 380 381 { 382 383 if (mHeaders == null) { 384 385 mHeaders = createHeadersMap (mContext); 386 387 } 388 389 return mHeaders; 390 391 } 392 393 394 395 397 406 407 public Map getInitParamMap () 408 409 { 410 411 if (mInitParam == null) { 412 413 mInitParam = createInitParamMap (mContext); 414 415 } 416 417 return mInitParam; 418 419 } 420 421 422 423 425 434 435 public Map getCookieMap () 436 437 { 438 439 if (mCookie == null) { 440 441 mCookie = createCookieMap (mContext); 442 443 } 444 445 return mCookie; 446 447 } 448 449 450 451 453 455 457 464 465 public static Map createPageScopeMap (PageContext pContext) 466 467 { 468 469 final PageContext context = pContext; 470 471 return new EnumeratedMap () 472 473 { 474 475 public Enumeration enumerateKeys () 476 477 { 478 479 return context.getAttributeNamesInScope 480 481 (PageContext.PAGE_SCOPE); 482 483 } 484 485 486 487 public Object getValue (Object pKey) 488 489 { 490 491 if (pKey instanceof String ) { 492 493 return context.getAttribute 494 495 ((String ) pKey, 496 497 PageContext.PAGE_SCOPE); 498 499 } 500 501 else { 502 503 return null; 504 505 } 506 507 } 508 509 510 511 public boolean isMutable () 512 513 { 514 515 return true; 516 517 } 518 519 }; 520 521 } 522 523 524 525 527 534 535 public static Map createRequestScopeMap (PageContext pContext) 536 537 { 538 539 final PageContext context = pContext; 540 541 return new EnumeratedMap () 542 543 { 544 545 public Enumeration enumerateKeys () 546 547 { 548 549 return context.getAttributeNamesInScope 550 551 (PageContext.REQUEST_SCOPE); 552 553 } 554 555 556 557 public Object getValue (Object pKey) 558 559 { 560 561 if (pKey instanceof String ) { 562 563 return context.getAttribute 564 565 ((String ) pKey, 566 567 PageContext.REQUEST_SCOPE); 568 569 } 570 571 else { 572 573 return null; 574 575 } 576 577 } 578 579 580 581 public boolean isMutable () 582 583 { 584 585 return true; 586 587 } 588 589 }; 590 591 } 592 593 594 595 597 604 605 public static Map createSessionScopeMap (PageContext pContext) 606 607 { 608 609 final PageContext context = pContext; 610 611 return new EnumeratedMap () 612 613 { 614 615 public Enumeration enumerateKeys () 616 617 { 618 619 return context.getAttributeNamesInScope 620 621 (PageContext.SESSION_SCOPE); 622 623 } 624 625 626 627 public Object getValue (Object pKey) 628 629 { 630 631 if (pKey instanceof String ) { 632 633 return context.getAttribute 634 635 ((String ) pKey, 636 637 PageContext.SESSION_SCOPE); 638 639 } 640 641 else { 642 643 return null; 644 645 } 646 647 } 648 649 650 651 public boolean isMutable () 652 653 { 654 655 return true; 656 657 } 658 659 }; 660 661 } 662 663 664 665 667 674 675 public static Map createApplicationScopeMap (PageContext pContext) 676 677 { 678 679 final PageContext context = pContext; 680 681 return new EnumeratedMap () 682 683 { 684 685 public Enumeration enumerateKeys () 686 687 { 688 689 return context.getAttributeNamesInScope 690 691 (PageContext.APPLICATION_SCOPE); 692 693 } 694 695 696 697 public Object getValue (Object pKey) 698 699 { 700 701 if (pKey instanceof String ) { 702 703 return context.getAttribute 704 705 ((String ) pKey, 706 707 PageContext.APPLICATION_SCOPE); 708 709 } 710 711 else { 712 713 return null; 714 715 } 716 717 } 718 719 720 721 public boolean isMutable () 722 723 { 724 725 return true; 726 727 } 728 729 }; 730 731 } 732 733 734 735 737 746 747 public static Map createParamMap (PageContext pContext) 748 749 { 750 751 final HttpServletRequest request = 752 753 (HttpServletRequest ) pContext.getRequest (); 754 755 return new EnumeratedMap () 756 757 { 758 759 public Enumeration enumerateKeys () 760 761 { 762 763 return request.getParameterNames (); 764 765 } 766 767 768 769 public Object getValue (Object pKey) 770 771 { 772 773 if (pKey instanceof String ) { 774 775 return request.getParameter ((String ) pKey); 776 777 } 778 779 else { 780 781 return null; 782 783 } 784 785 } 786 787 788 789 public boolean isMutable () 790 791 { 792 793 return false; 794 795 } 796 797 }; 798 799 } 800 801 802 803 805 814 815 public static Map createParamsMap (PageContext pContext) 816 817 { 818 819 final HttpServletRequest request = 820 821 (HttpServletRequest ) pContext.getRequest (); 822 823 return new EnumeratedMap () 824 825 { 826 827 public Enumeration enumerateKeys () 828 829 { 830 831 return request.getParameterNames (); 832 833 } 834 835 836 837 public Object getValue (Object pKey) 838 839 { 840 841 if (pKey instanceof String ) { 842 843 return request.getParameterValues ((String ) pKey); 844 845 } 846 847 else { 848 849 return null; 850 851 } 852 853 } 854 855 856 857 public boolean isMutable () 858 859 { 860 861 return false; 862 863 } 864 865 }; 866 867 } 868 869 870 871 873 882 883 public static Map createHeaderMap (PageContext pContext) 884 885 { 886 887 final HttpServletRequest request = 888 889 (HttpServletRequest ) pContext.getRequest (); 890 891 return new EnumeratedMap () 892 893 { 894 895 public Enumeration enumerateKeys () 896 897 { 898 899 return request.getHeaderNames (); 900 901 } 902 903 904 905 public Object getValue (Object pKey) 906 907 { 908 909 if (pKey instanceof String ) { 910 911 return request.getHeader ((String ) pKey); 912 913 } 914 915 else { 916 917 return null; 918 919 } 920 921 } 922 923 924 925 public boolean isMutable () 926 927 { 928 929 return false; 930 931 } 932 933 }; 934 935 } 936 937 938 939 941 950 951 public static Map createHeadersMap (PageContext pContext) 952 953 { 954 955 final HttpServletRequest request = 956 957 (HttpServletRequest ) pContext.getRequest (); 958 959 return new EnumeratedMap () 960 961 { 962 963 public Enumeration enumerateKeys () 964 965 { 966 967 return request.getHeaderNames (); 968 969 } 970 971 972 973 public Object getValue (Object pKey) 974 975 { 976 977 if (pKey instanceof String ) { 978 979 981 List l = new ArrayList (); 982 983 Enumeration enum_ = request.getHeaders ((String ) pKey); 984 985 if (enum_ != null) { 986 987 while (enum_.hasMoreElements ()) { 988 989 l.add (enum_.nextElement ()); 990 991 } 992 993 } 994 995 String [] ret = (String []) l.toArray (new String [l.size ()]); 996 997 return ret; 998 999 } 1000 1001 else { 1002 1003 return null; 1004 1005 } 1006 1007 } 1008 1009 1010 1011 public boolean isMutable () 1012 1013 { 1014 1015 return false; 1016 1017 } 1018 1019 }; 1020 1021 } 1022 1023 1024 1025 1027 1036 1037 public static Map createInitParamMap (PageContext pContext) 1038 1039 { 1040 1041 final ServletContext context = pContext.getServletContext (); 1042 1043 return new EnumeratedMap () 1044 1045 { 1046 1047 public Enumeration enumerateKeys () 1048 1049 { 1050 1051 return context.getInitParameterNames (); 1052 1053 } 1054 1055 1056 1057 public Object getValue (Object pKey) 1058 1059 { 1060 1061 if (pKey instanceof String ) { 1062 1063 return context.getInitParameter ((String ) pKey); 1064 1065 } 1066 1067 else { 1068 1069 return null; 1070 1071 } 1072 1073 } 1074 1075 1076 1077 public boolean isMutable () 1078 1079 { 1080 1081 return false; 1082 1083 } 1084 1085 }; 1086 1087 } 1088 1089 1090 1091 1093 1102 1103 public static Map createCookieMap (PageContext pContext) 1104 1105 { 1106 1107 1109 HttpServletRequest request = (HttpServletRequest ) pContext.getRequest (); 1110 1111 Cookie [] cookies = request.getCookies (); 1112 1113 Map ret = new HashMap (); 1114 1115 for (int i = 0; cookies != null && i < cookies.length; i++) { 1116 1117 Cookie cookie = cookies [i]; 1118 1119 if (cookie != null) { 1120 1121 String name = cookie.getName (); 1122 1123 if (!ret.containsKey (name)) { 1124 1125 ret.put (name, cookie); 1126 1127 } 1128 1129 } 1130 1131 } 1132 1133 return ret; 1134 1135 } 1136 1137 1138 1139 1141} 1142 1143 | Popular Tags |