1 2 3 27 28 29 package org.apache.coyote.tomcat5; 30 31 import java.io.InputStream ; 32 import java.io.BufferedReader ; 33 import java.io.IOException ; 34 import java.security.AccessController ; 35 import java.security.AccessControlException ; 36 import java.security.Permission ; 37 import java.security.PrivilegedAction ; 38 import java.security.PrivilegedActionException ; 39 import java.security.SecurityPermission ; 40 import java.util.Enumeration ; 41 import java.util.Map ; 42 import java.util.Locale ; 43 import java.net.Socket ; 44 import javax.servlet.ServletException ; 45 import javax.servlet.ServletInputStream ; 46 import javax.servlet.ServletRequest ; 47 import javax.servlet.RequestDispatcher ; 48 import javax.servlet.http.Cookie ; 49 import javax.servlet.http.HttpServletRequest ; 50 import javax.servlet.http.HttpSession ; 51 52 import org.apache.catalina.connector.RequestFacade; 53 import org.apache.catalina.session.StandardSessionFacade; 54 import org.apache.catalina.util.StringManager; 55 56 import org.apache.catalina.security.SecurityUtil; 57 58 67 public class CoyoteRequestFacade 68 extends RequestFacade 69 implements HttpServletRequest { 70 71 72 74 private final class GetAttributePrivilegedAction 75 implements PrivilegedAction { 76 77 public Object run() { 78 return request.getAttributeNames(); 79 } 80 } 81 82 83 private final class GetParameterMapPrivilegedAction 84 implements PrivilegedAction { 85 86 public Object run() { 87 return request.getParameterMap(); 88 } 89 } 90 91 92 private final class GetRequestDispatcherPrivilegedAction 93 implements PrivilegedAction { 94 95 private String path; 96 97 public GetRequestDispatcherPrivilegedAction(String path){ 98 this.path = path; 99 } 100 101 public Object run() { 102 return request.getRequestDispatcher(path); 103 } 104 } 105 106 107 private final class GetParameterPrivilegedAction 108 implements PrivilegedAction { 109 110 public String name; 111 112 public GetParameterPrivilegedAction(String name){ 113 this.name = name; 114 } 115 116 public Object run() { 117 return request.getParameter(name); 118 } 119 } 120 121 122 private final class GetParameterNamesPrivilegedAction 123 implements PrivilegedAction { 124 125 public Object run() { 126 return request.getParameterNames(); 127 } 128 } 129 130 131 private final class GetParameterValuePrivilegedAction 132 implements PrivilegedAction { 133 134 public String name; 135 136 public GetParameterValuePrivilegedAction(String name){ 137 this.name = name; 138 } 139 140 public Object run() { 141 return request.getParameterValues(name); 142 } 143 } 144 145 146 private final class GetCookiesPrivilegedAction 147 implements PrivilegedAction { 148 149 public Object run() { 150 return request.getCookies(); 151 } 152 } 153 154 155 private final class GetCharacterEncodingPrivilegedAction 156 implements PrivilegedAction { 157 158 public Object run() { 159 return request.getCharacterEncoding(); 160 } 161 } 162 163 164 private final class GetHeadersPrivilegedAction 165 implements PrivilegedAction { 166 167 private String name; 168 169 public GetHeadersPrivilegedAction(String name){ 170 this.name = name; 171 } 172 173 public Object run() { 174 return request.getHeaders(name); 175 } 176 } 177 178 179 private final class GetHeaderNamesPrivilegedAction 180 implements PrivilegedAction { 181 182 public Object run() { 183 return request.getHeaderNames(); 184 } 185 } 186 187 188 private final class GetLocalePrivilegedAction 189 implements PrivilegedAction { 190 191 public Object run() { 192 return request.getLocale(); 193 } 194 } 195 196 197 private final class GetLocalesPrivilegedAction 198 implements PrivilegedAction { 199 200 public Object run() { 201 return request.getLocales(); 202 } 203 } 204 205 private final class GetSessionPrivilegedAction 206 implements PrivilegedAction { 207 208 private boolean create; 209 210 public GetSessionPrivilegedAction(boolean create){ 211 this.create = create; 212 } 213 214 public Object run() { 215 return request.getSession(create); 216 } 217 } 218 219 220 222 223 228 public CoyoteRequestFacade(CoyoteRequest request) { 229 230 super(request); 231 this.request = request; 232 233 } 234 235 236 238 239 242 protected static StringManager sm = 243 StringManager.getManager(Constants.Package); 244 245 246 249 protected CoyoteRequest request = null; 250 251 252 254 255 258 protected Object clone() 259 throws CloneNotSupportedException { 260 throw new CloneNotSupportedException (); 261 } 262 263 264 267 public void clear() { 268 request = null; 269 } 270 271 272 274 275 public Object getAttribute(String name) { 276 277 if (request == null) { 278 throw new IllegalStateException ( 279 sm.getString("requestFacade.nullRequest")); 280 } 281 282 return request.getAttribute(name); 283 } 284 285 286 public Enumeration getAttributeNames() { 287 288 if (request == null) { 289 throw new IllegalStateException ( 290 sm.getString("requestFacade.nullRequest")); 291 } 292 293 if (SecurityUtil.isPackageProtectionEnabled()){ 294 return (Enumeration )AccessController.doPrivileged( 295 new GetAttributePrivilegedAction()); 296 } else { 297 return request.getAttributeNames(); 298 } 299 } 300 301 302 public String getCharacterEncoding() { 303 304 if (request == null) { 305 throw new IllegalStateException ( 306 sm.getString("requestFacade.nullRequest")); 307 } 308 309 if (SecurityUtil.isPackageProtectionEnabled()){ 310 return (String )AccessController.doPrivileged( 311 new GetCharacterEncodingPrivilegedAction()); 312 } else { 313 return request.getCharacterEncoding(); 314 } 315 } 316 317 318 public void setCharacterEncoding(String env) 319 throws java.io.UnsupportedEncodingException { 320 321 if (request == null) { 322 throw new IllegalStateException ( 323 sm.getString("requestFacade.nullRequest")); 324 } 325 326 request.setCharacterEncoding(env); 327 } 328 329 330 public int getContentLength() { 331 332 if (request == null) { 333 throw new IllegalStateException ( 334 sm.getString("requestFacade.nullRequest")); 335 } 336 337 return request.getContentLength(); 338 } 339 340 341 public String getContentType() { 342 343 if (request == null) { 344 throw new IllegalStateException ( 345 sm.getString("requestFacade.nullRequest")); 346 } 347 348 return request.getContentType(); 349 } 350 351 352 public ServletInputStream getInputStream() throws IOException { 353 354 if (request == null) { 355 throw new IllegalStateException ( 356 sm.getString("requestFacade.nullRequest")); 357 } 358 359 return request.getInputStream(); 360 } 361 362 363 public String getParameter(String name) { 364 365 if (request == null) { 366 throw new IllegalStateException ( 367 sm.getString("requestFacade.nullRequest")); 368 } 369 370 if (SecurityUtil.isPackageProtectionEnabled()){ 371 return (String )AccessController.doPrivileged( 372 new GetParameterPrivilegedAction(name)); 373 } else { 374 return request.getParameter(name); 375 } 376 } 377 378 379 public Enumeration getParameterNames() { 380 381 if (request == null) { 382 throw new IllegalStateException ( 383 sm.getString("requestFacade.nullRequest")); 384 } 385 386 if (SecurityUtil.isPackageProtectionEnabled()){ 387 return (Enumeration )AccessController.doPrivileged( 388 new GetParameterNamesPrivilegedAction()); 389 } else { 390 return request.getParameterNames(); 391 } 392 } 393 394 395 public String [] getParameterValues(String name) { 396 397 if (request == null) { 398 throw new IllegalStateException ( 399 sm.getString("requestFacade.nullRequest")); 400 } 401 402 String [] ret = null; 403 404 408 if (SecurityUtil.isPackageProtectionEnabled()){ 409 ret = (String []) AccessController.doPrivileged( 410 new GetParameterValuePrivilegedAction(name)); 411 if (ret != null) { 412 ret = (String []) ret.clone(); 413 } 414 } else { 415 ret = request.getParameterValues(name); 416 } 417 418 return ret; 419 } 420 421 422 public Map getParameterMap() { 423 424 if (request == null) { 425 throw new IllegalStateException ( 426 sm.getString("requestFacade.nullRequest")); 427 } 428 429 if (SecurityUtil.isPackageProtectionEnabled()){ 430 return (Map )AccessController.doPrivileged( 431 new GetParameterMapPrivilegedAction()); 432 } else { 433 return request.getParameterMap(); 434 } 435 } 436 437 438 public String getProtocol() { 439 440 if (request == null) { 441 throw new IllegalStateException ( 442 sm.getString("requestFacade.nullRequest")); 443 } 444 445 return request.getProtocol(); 446 } 447 448 449 public String getScheme() { 450 451 if (request == null) { 452 throw new IllegalStateException ( 453 sm.getString("requestFacade.nullRequest")); 454 } 455 456 return request.getScheme(); 457 } 458 459 460 public String getServerName() { 461 462 if (request == null) { 463 throw new IllegalStateException ( 464 sm.getString("requestFacade.nullRequest")); 465 } 466 467 return request.getServerName(); 468 } 469 470 471 public int getServerPort() { 472 473 if (request == null) { 474 throw new IllegalStateException ( 475 sm.getString("requestFacade.nullRequest")); 476 } 477 478 return request.getServerPort(); 479 } 480 481 482 public BufferedReader getReader() throws IOException { 483 484 if (request == null) { 485 throw new IllegalStateException ( 486 sm.getString("requestFacade.nullRequest")); 487 } 488 489 return request.getReader(); 490 } 491 492 493 public String getRemoteAddr() { 494 495 if (request == null) { 496 throw new IllegalStateException ( 497 sm.getString("requestFacade.nullRequest")); 498 } 499 500 return request.getRemoteAddr(); 501 } 502 503 504 public String getRemoteHost() { 505 506 if (request == null) { 507 throw new IllegalStateException ( 508 sm.getString("requestFacade.nullRequest")); 509 } 510 511 return request.getRemoteHost(); 512 } 513 514 515 public void setAttribute(String name, Object o) { 516 517 if (request == null) { 518 throw new IllegalStateException ( 519 sm.getString("requestFacade.nullRequest")); 520 } 521 522 request.setAttribute(name, o); 523 } 524 525 526 public void removeAttribute(String name) { 527 528 if (request == null) { 529 throw new IllegalStateException ( 530 sm.getString("requestFacade.nullRequest")); 531 } 532 533 request.removeAttribute(name); 534 } 535 536 537 public Locale getLocale() { 538 539 if (request == null) { 540 throw new IllegalStateException ( 541 sm.getString("requestFacade.nullRequest")); 542 } 543 544 if (SecurityUtil.isPackageProtectionEnabled()){ 545 return (Locale )AccessController.doPrivileged( 546 new GetLocalePrivilegedAction()); 547 } else { 548 return request.getLocale(); 549 } 550 } 551 552 553 public Enumeration getLocales() { 554 555 if (request == null) { 556 throw new IllegalStateException ( 557 sm.getString("requestFacade.nullRequest")); 558 } 559 560 if (SecurityUtil.isPackageProtectionEnabled()){ 561 return (Enumeration )AccessController.doPrivileged( 562 new GetLocalesPrivilegedAction()); 563 } else { 564 return request.getLocales(); 565 } 566 } 567 568 569 public boolean isSecure() { 570 571 if (request == null) { 572 throw new IllegalStateException ( 573 sm.getString("requestFacade.nullRequest")); 574 } 575 576 return request.isSecure(); 577 } 578 579 580 public RequestDispatcher getRequestDispatcher(String path) { 581 582 if (request == null) { 583 throw new IllegalStateException ( 584 sm.getString("requestFacade.nullRequest")); 585 } 586 587 if (SecurityUtil.isPackageProtectionEnabled()){ 588 return (RequestDispatcher )AccessController.doPrivileged( 589 new GetRequestDispatcherPrivilegedAction(path)); 590 } else { 591 return request.getRequestDispatcher(path); 592 } 593 } 594 595 596 public String getRealPath(String path) { 597 598 if (request == null) { 599 throw new IllegalStateException ( 600 sm.getString("requestFacade.nullRequest")); 601 } 602 603 return request.getRealPath(path); 604 } 605 606 607 public String getAuthType() { 608 609 if (request == null) { 610 throw new IllegalStateException ( 611 sm.getString("requestFacade.nullRequest")); 612 } 613 614 return request.getAuthType(); 615 } 616 617 618 public Cookie [] getCookies() { 619 620 if (request == null) { 621 throw new IllegalStateException ( 622 sm.getString("requestFacade.nullRequest")); 623 } 624 625 Cookie [] ret = null; 626 627 631 if (SecurityUtil.isPackageProtectionEnabled()){ 632 ret = (Cookie [])AccessController.doPrivileged( 633 new GetCookiesPrivilegedAction()); 634 if (ret != null) { 635 ret = (Cookie []) ret.clone(); 636 } 637 } else { 638 ret = request.getCookies(); 639 } 640 641 return ret; 642 } 643 644 645 public long getDateHeader(String name) { 646 647 if (request == null) { 648 throw new IllegalStateException ( 649 sm.getString("requestFacade.nullRequest")); 650 } 651 652 return request.getDateHeader(name); 653 } 654 655 656 public String getHeader(String name) { 657 658 if (request == null) { 659 throw new IllegalStateException ( 660 sm.getString("requestFacade.nullRequest")); 661 } 662 663 return request.getHeader(name); 664 } 665 666 667 public Enumeration getHeaders(String name) { 668 669 if (request == null) { 670 throw new IllegalStateException ( 671 sm.getString("requestFacade.nullRequest")); 672 } 673 674 if (SecurityUtil.isPackageProtectionEnabled()){ 675 return (Enumeration )AccessController.doPrivileged( 676 new GetHeadersPrivilegedAction(name)); 677 } else { 678 return request.getHeaders(name); 679 } 680 } 681 682 683 public Enumeration getHeaderNames() { 684 685 if (request == null) { 686 throw new IllegalStateException ( 687 sm.getString("requestFacade.nullRequest")); 688 } 689 690 if (SecurityUtil.isPackageProtectionEnabled()){ 691 return (Enumeration )AccessController.doPrivileged( 692 new GetHeaderNamesPrivilegedAction()); 693 } else { 694 return request.getHeaderNames(); 695 } 696 } 697 698 699 public int getIntHeader(String name) { 700 701 if (request == null) { 702 throw new IllegalStateException ( 703 sm.getString("requestFacade.nullRequest")); 704 } 705 706 return request.getIntHeader(name); 707 } 708 709 710 public String getMethod() { 711 712 if (request == null) { 713 throw new IllegalStateException ( 714 sm.getString("requestFacade.nullRequest")); 715 } 716 717 return request.getMethod(); 718 } 719 720 721 public String getPathInfo() { 722 723 if (request == null) { 724 throw new IllegalStateException ( 725 sm.getString("requestFacade.nullRequest")); 726 } 727 728 return request.getPathInfo(); 729 } 730 731 732 public String getPathTranslated() { 733 734 if (request == null) { 735 throw new IllegalStateException ( 736 sm.getString("requestFacade.nullRequest")); 737 } 738 739 return request.getPathTranslated(); 740 } 741 742 743 public String getContextPath() { 744 745 if (request == null) { 746 throw new IllegalStateException ( 747 sm.getString("requestFacade.nullRequest")); 748 } 749 750 return request.getContextPath(); 751 } 752 753 754 public String getQueryString() { 755 756 if (request == null) { 757 throw new IllegalStateException ( 758 sm.getString("requestFacade.nullRequest")); 759 } 760 761 return request.getQueryString(); 762 } 763 764 765 public String getRemoteUser() { 766 767 if (request == null) { 768 throw new IllegalStateException ( 769 sm.getString("requestFacade.nullRequest")); 770 } 771 772 return request.getRemoteUser(); 773 } 774 775 776 public boolean isUserInRole(String role) { 777 778 if (request == null) { 779 throw new IllegalStateException ( 780 sm.getString("requestFacade.nullRequest")); 781 } 782 783 return request.isUserInRole(role); 784 } 785 786 787 public java.security.Principal getUserPrincipal() { 788 789 if (request == null) { 790 throw new IllegalStateException ( 791 sm.getString("requestFacade.nullRequest")); 792 } 793 794 return request.getUserPrincipal(); 795 } 796 797 798 public String getRequestedSessionId() { 799 800 if (request == null) { 801 throw new IllegalStateException ( 802 sm.getString("requestFacade.nullRequest")); 803 } 804 805 return request.getRequestedSessionId(); 806 } 807 808 809 public String getRequestURI() { 810 811 if (request == null) { 812 throw new IllegalStateException ( 813 sm.getString("requestFacade.nullRequest")); 814 } 815 816 return request.getRequestURI(); 817 } 818 819 820 public StringBuffer getRequestURL() { 821 822 if (request == null) { 823 throw new IllegalStateException ( 824 sm.getString("requestFacade.nullRequest")); 825 } 826 827 return request.getRequestURL(); 828 } 829 830 831 public String getServletPath() { 832 833 if (request == null) { 834 throw new IllegalStateException ( 835 sm.getString("requestFacade.nullRequest")); 836 } 837 838 return request.getServletPath(); 839 } 840 841 842 public HttpSession getSession(boolean create) { 843 844 if (request == null) { 845 throw new IllegalStateException ( 846 sm.getString("requestFacade.nullRequest")); 847 } 848 849 if (SecurityUtil.isPackageProtectionEnabled()){ 850 return (HttpSession )AccessController. 851 doPrivileged(new GetSessionPrivilegedAction(create)); 852 } else { 853 return request.getSession(create); 854 } 855 } 856 857 public HttpSession getSession() { 858 859 if (request == null) { 860 throw new IllegalStateException ( 861 sm.getString("requestFacade.nullRequest")); 862 } 863 864 return getSession(true); 865 } 866 867 868 public boolean isRequestedSessionIdValid() { 869 870 if (request == null) { 871 throw new IllegalStateException ( 872 sm.getString("requestFacade.nullRequest")); 873 } 874 875 return request.isRequestedSessionIdValid(); 876 } 877 878 879 public boolean isRequestedSessionIdFromCookie() { 880 881 if (request == null) { 882 throw new IllegalStateException ( 883 sm.getString("requestFacade.nullRequest")); 884 } 885 886 return request.isRequestedSessionIdFromCookie(); 887 } 888 889 890 public boolean isRequestedSessionIdFromURL() { 891 892 if (request == null) { 893 throw new IllegalStateException ( 894 sm.getString("requestFacade.nullRequest")); 895 } 896 897 return request.isRequestedSessionIdFromURL(); 898 } 899 900 901 public boolean isRequestedSessionIdFromUrl() { 902 903 if (request == null) { 904 throw new IllegalStateException ( 905 sm.getString("requestFacade.nullRequest")); 906 } 907 908 return request.isRequestedSessionIdFromURL(); 909 } 910 911 912 916 public CoyoteRequest getUnwrappedCoyoteRequest() 917 throws AccessControlException { 918 919 SecurityManager sm = System.getSecurityManager(); 922 if (sm != null) { 923 Permission perm = 924 new SecurityPermission ("getUnwrappedCoyoteRequest"); 925 AccessController.checkPermission(perm); 926 } 927 928 return request; 929 } 930 932 } 933 | Popular Tags |