1
17 package org.apache.catalina.connector;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.security.AccessController;
22 import java.security.PrivilegedAction;
23 import java.util.Collection;
24 import java.util.Enumeration;
25 import java.util.Locale;
26 import java.util.Map;
27
28 import javax.servlet.AsyncContext;
29 import javax.servlet.DispatcherType;
30 import javax.servlet.RequestDispatcher;
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletException;
33 import javax.servlet.ServletInputStream;
34 import javax.servlet.ServletRequest;
35 import javax.servlet.ServletResponse;
36 import javax.servlet.http.Cookie;
37 import javax.servlet.http.HttpServletMapping;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession;
41 import javax.servlet.http.HttpUpgradeHandler;
42 import javax.servlet.http.Part;
43 import javax.servlet.http.PushBuilder;
44
45 import org.apache.catalina.Globals;
46 import org.apache.catalina.security.SecurityUtil;
47 import org.apache.tomcat.util.res.StringManager;
48
49
56 @SuppressWarnings("deprecation")
57 public class RequestFacade implements HttpServletRequest {
58
59
60
61
62 private final class GetAttributePrivilegedAction
63 implements PrivilegedAction<Enumeration<String>> {
64
65 @Override
66 public Enumeration<String> run() {
67 return request.getAttributeNames();
68 }
69 }
70
71
72 private final class GetParameterMapPrivilegedAction
73 implements PrivilegedAction<Map<String,String[]>> {
74
75 @Override
76 public Map<String,String[]> run() {
77 return request.getParameterMap();
78 }
79 }
80
81
82 private final class GetRequestDispatcherPrivilegedAction
83 implements PrivilegedAction<RequestDispatcher> {
84
85 private final String path;
86
87 public GetRequestDispatcherPrivilegedAction(String path){
88 this.path = path;
89 }
90
91 @Override
92 public RequestDispatcher run() {
93 return request.getRequestDispatcher(path);
94 }
95 }
96
97
98 private final class GetParameterPrivilegedAction
99 implements PrivilegedAction<String> {
100
101 public String name;
102
103 public GetParameterPrivilegedAction(String name){
104 this.name = name;
105 }
106
107 @Override
108 public String run() {
109 return request.getParameter(name);
110 }
111 }
112
113
114 private final class GetParameterNamesPrivilegedAction
115 implements PrivilegedAction<Enumeration<String>> {
116
117 @Override
118 public Enumeration<String> run() {
119 return request.getParameterNames();
120 }
121 }
122
123
124 private final class GetParameterValuePrivilegedAction
125 implements PrivilegedAction<String[]> {
126
127 public String name;
128
129 public GetParameterValuePrivilegedAction(String name){
130 this.name = name;
131 }
132
133 @Override
134 public String[] run() {
135 return request.getParameterValues(name);
136 }
137 }
138
139
140 private final class GetCookiesPrivilegedAction
141 implements PrivilegedAction<Cookie[]> {
142
143 @Override
144 public Cookie[] run() {
145 return request.getCookies();
146 }
147 }
148
149
150 private final class GetCharacterEncodingPrivilegedAction
151 implements PrivilegedAction<String> {
152
153 @Override
154 public String run() {
155 return request.getCharacterEncoding();
156 }
157 }
158
159
160 private final class GetHeadersPrivilegedAction
161 implements PrivilegedAction<Enumeration<String>> {
162
163 private final String name;
164
165 public GetHeadersPrivilegedAction(String name){
166 this.name = name;
167 }
168
169 @Override
170 public Enumeration<String> run() {
171 return request.getHeaders(name);
172 }
173 }
174
175
176 private final class GetHeaderNamesPrivilegedAction
177 implements PrivilegedAction<Enumeration<String>> {
178
179 @Override
180 public Enumeration<String> run() {
181 return request.getHeaderNames();
182 }
183 }
184
185
186 private final class GetLocalePrivilegedAction
187 implements PrivilegedAction<Locale> {
188
189 @Override
190 public Locale run() {
191 return request.getLocale();
192 }
193 }
194
195
196 private final class GetLocalesPrivilegedAction
197 implements PrivilegedAction<Enumeration<Locale>> {
198
199 @Override
200 public Enumeration<Locale> run() {
201 return request.getLocales();
202 }
203 }
204
205 private final class GetSessionPrivilegedAction
206 implements PrivilegedAction<HttpSession> {
207
208 private final boolean create;
209
210 public GetSessionPrivilegedAction(boolean create){
211 this.create = create;
212 }
213
214 @Override
215 public HttpSession run() {
216 return request.getSession(create);
217 }
218 }
219
220
221
222
223
228 public RequestFacade(Request request) {
229
230 this.request = request;
231
232 }
233
234
235
236
237
238
241 protected Request request = null;
242
243
244
247 protected static final StringManager sm = StringManager.getManager(RequestFacade.class);
248
249
250
251
252
253
256 public void clear() {
257 request = null;
258 }
259
260
261
264 @Override
265 protected Object clone()
266 throws CloneNotSupportedException {
267 throw new CloneNotSupportedException();
268 }
269
270
271
272
273
274 @Override
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 @Override
287 public Enumeration<String> getAttributeNames() {
288
289 if (request == null) {
290 throw new IllegalStateException(
291 sm.getString("requestFacade.nullRequest"));
292 }
293
294 if (Globals.IS_SECURITY_ENABLED){
295 return AccessController.doPrivileged(
296 new GetAttributePrivilegedAction());
297 } else {
298 return request.getAttributeNames();
299 }
300 }
301
302
303 @Override
304 public String getCharacterEncoding() {
305
306 if (request == null) {
307 throw new IllegalStateException(
308 sm.getString("requestFacade.nullRequest"));
309 }
310
311 if (Globals.IS_SECURITY_ENABLED){
312 return AccessController.doPrivileged(
313 new GetCharacterEncodingPrivilegedAction());
314 } else {
315 return request.getCharacterEncoding();
316 }
317 }
318
319
320 @Override
321 public void setCharacterEncoding(String env)
322 throws java.io.UnsupportedEncodingException {
323
324 if (request == null) {
325 throw new IllegalStateException(
326 sm.getString("requestFacade.nullRequest"));
327 }
328
329 request.setCharacterEncoding(env);
330 }
331
332
333 @Override
334 public int getContentLength() {
335
336 if (request == null) {
337 throw new IllegalStateException(
338 sm.getString("requestFacade.nullRequest"));
339 }
340
341 return request.getContentLength();
342 }
343
344
345 @Override
346 public String getContentType() {
347
348 if (request == null) {
349 throw new IllegalStateException(
350 sm.getString("requestFacade.nullRequest"));
351 }
352
353 return request.getContentType();
354 }
355
356
357 @Override
358 public ServletInputStream getInputStream() throws IOException {
359
360 if (request == null) {
361 throw new IllegalStateException(
362 sm.getString("requestFacade.nullRequest"));
363 }
364
365 return request.getInputStream();
366 }
367
368
369 @Override
370 public String getParameter(String name) {
371
372 if (request == null) {
373 throw new IllegalStateException(
374 sm.getString("requestFacade.nullRequest"));
375 }
376
377 if (Globals.IS_SECURITY_ENABLED){
378 return AccessController.doPrivileged(
379 new GetParameterPrivilegedAction(name));
380 } else {
381 return request.getParameter(name);
382 }
383 }
384
385
386 @Override
387 public Enumeration<String> getParameterNames() {
388
389 if (request == null) {
390 throw new IllegalStateException(
391 sm.getString("requestFacade.nullRequest"));
392 }
393
394 if (Globals.IS_SECURITY_ENABLED){
395 return AccessController.doPrivileged(
396 new GetParameterNamesPrivilegedAction());
397 } else {
398 return request.getParameterNames();
399 }
400 }
401
402
403 @Override
404 public String[] getParameterValues(String name) {
405
406 if (request == null) {
407 throw new IllegalStateException(
408 sm.getString("requestFacade.nullRequest"));
409 }
410
411 String[] ret = null;
412
413
417 if (SecurityUtil.isPackageProtectionEnabled()){
418 ret = AccessController.doPrivileged(
419 new GetParameterValuePrivilegedAction(name));
420 if (ret != null) {
421 ret = ret.clone();
422 }
423 } else {
424 ret = request.getParameterValues(name);
425 }
426
427 return ret;
428 }
429
430
431 @Override
432 public Map<String,String[]> getParameterMap() {
433
434 if (request == null) {
435 throw new IllegalStateException(
436 sm.getString("requestFacade.nullRequest"));
437 }
438
439 if (Globals.IS_SECURITY_ENABLED){
440 return AccessController.doPrivileged(
441 new GetParameterMapPrivilegedAction());
442 } else {
443 return request.getParameterMap();
444 }
445 }
446
447
448 @Override
449 public String getProtocol() {
450
451 if (request == null) {
452 throw new IllegalStateException(
453 sm.getString("requestFacade.nullRequest"));
454 }
455
456 return request.getProtocol();
457 }
458
459
460 @Override
461 public String getScheme() {
462
463 if (request == null) {
464 throw new IllegalStateException(
465 sm.getString("requestFacade.nullRequest"));
466 }
467
468 return request.getScheme();
469 }
470
471
472 @Override
473 public String getServerName() {
474
475 if (request == null) {
476 throw new IllegalStateException(
477 sm.getString("requestFacade.nullRequest"));
478 }
479
480 return request.getServerName();
481 }
482
483
484 @Override
485 public int getServerPort() {
486
487 if (request == null) {
488 throw new IllegalStateException(
489 sm.getString("requestFacade.nullRequest"));
490 }
491
492 return request.getServerPort();
493 }
494
495
496 @Override
497 public BufferedReader getReader() throws IOException {
498
499 if (request == null) {
500 throw new IllegalStateException(
501 sm.getString("requestFacade.nullRequest"));
502 }
503
504 return request.getReader();
505 }
506
507
508 @Override
509 public String getRemoteAddr() {
510
511 if (request == null) {
512 throw new IllegalStateException(
513 sm.getString("requestFacade.nullRequest"));
514 }
515
516 return request.getRemoteAddr();
517 }
518
519
520 @Override
521 public String getRemoteHost() {
522
523 if (request == null) {
524 throw new IllegalStateException(
525 sm.getString("requestFacade.nullRequest"));
526 }
527
528 return request.getRemoteHost();
529 }
530
531
532 @Override
533 public void setAttribute(String name, Object o) {
534
535 if (request == null) {
536 throw new IllegalStateException(
537 sm.getString("requestFacade.nullRequest"));
538 }
539
540 request.setAttribute(name, o);
541 }
542
543
544 @Override
545 public void removeAttribute(String name) {
546
547 if (request == null) {
548 throw new IllegalStateException(
549 sm.getString("requestFacade.nullRequest"));
550 }
551
552 request.removeAttribute(name);
553 }
554
555
556 @Override
557 public Locale getLocale() {
558
559 if (request == null) {
560 throw new IllegalStateException(
561 sm.getString("requestFacade.nullRequest"));
562 }
563
564 if (Globals.IS_SECURITY_ENABLED){
565 return AccessController.doPrivileged(
566 new GetLocalePrivilegedAction());
567 } else {
568 return request.getLocale();
569 }
570 }
571
572
573 @Override
574 public Enumeration<Locale> getLocales() {
575
576 if (request == null) {
577 throw new IllegalStateException(
578 sm.getString("requestFacade.nullRequest"));
579 }
580
581 if (Globals.IS_SECURITY_ENABLED){
582 return AccessController.doPrivileged(
583 new GetLocalesPrivilegedAction());
584 } else {
585 return request.getLocales();
586 }
587 }
588
589
590 @Override
591 public boolean isSecure() {
592
593 if (request == null) {
594 throw new IllegalStateException(
595 sm.getString("requestFacade.nullRequest"));
596 }
597
598 return request.isSecure();
599 }
600
601
602 @Override
603 public RequestDispatcher getRequestDispatcher(String path) {
604
605 if (request == null) {
606 throw new IllegalStateException(
607 sm.getString("requestFacade.nullRequest"));
608 }
609
610 if (Globals.IS_SECURITY_ENABLED){
611 return AccessController.doPrivileged(
612 new GetRequestDispatcherPrivilegedAction(path));
613 } else {
614 return request.getRequestDispatcher(path);
615 }
616 }
617
618 @Override
619 public String getRealPath(String path) {
620
621 if (request == null) {
622 throw new IllegalStateException(
623 sm.getString("requestFacade.nullRequest"));
624 }
625
626 return request.getRealPath(path);
627 }
628
629
630 @Override
631 public String getAuthType() {
632
633 if (request == null) {
634 throw new IllegalStateException(
635 sm.getString("requestFacade.nullRequest"));
636 }
637
638 return request.getAuthType();
639 }
640
641
642 @Override
643 public Cookie[] getCookies() {
644
645 if (request == null) {
646 throw new IllegalStateException(
647 sm.getString("requestFacade.nullRequest"));
648 }
649
650 Cookie[] ret = null;
651
652
656 if (SecurityUtil.isPackageProtectionEnabled()){
657 ret = AccessController.doPrivileged(
658 new GetCookiesPrivilegedAction());
659 if (ret != null) {
660 ret = ret.clone();
661 }
662 } else {
663 ret = request.getCookies();
664 }
665
666 return ret;
667 }
668
669
670 @Override
671 public long getDateHeader(String name) {
672
673 if (request == null) {
674 throw new IllegalStateException(
675 sm.getString("requestFacade.nullRequest"));
676 }
677
678 return request.getDateHeader(name);
679 }
680
681
682 @Override
683 public String getHeader(String name) {
684
685 if (request == null) {
686 throw new IllegalStateException(
687 sm.getString("requestFacade.nullRequest"));
688 }
689
690 return request.getHeader(name);
691 }
692
693
694 @Override
695 public Enumeration<String> getHeaders(String name) {
696
697 if (request == null) {
698 throw new IllegalStateException(
699 sm.getString("requestFacade.nullRequest"));
700 }
701
702 if (Globals.IS_SECURITY_ENABLED){
703 return AccessController.doPrivileged(
704 new GetHeadersPrivilegedAction(name));
705 } else {
706 return request.getHeaders(name);
707 }
708 }
709
710
711 @Override
712 public Enumeration<String> getHeaderNames() {
713
714 if (request == null) {
715 throw new IllegalStateException(
716 sm.getString("requestFacade.nullRequest"));
717 }
718
719 if (Globals.IS_SECURITY_ENABLED){
720 return AccessController.doPrivileged(
721 new GetHeaderNamesPrivilegedAction());
722 } else {
723 return request.getHeaderNames();
724 }
725 }
726
727
728 @Override
729 public int getIntHeader(String name) {
730
731 if (request == null) {
732 throw new IllegalStateException(
733 sm.getString("requestFacade.nullRequest"));
734 }
735
736 return request.getIntHeader(name);
737 }
738
739
740 @Override
741 public HttpServletMapping getHttpServletMapping() {
742
743 if (request == null) {
744 throw new IllegalStateException(
745 sm.getString("requestFacade.nullRequest"));
746 }
747
748 return request.getHttpServletMapping();
749 }
750
751
752 @Override
753 public String getMethod() {
754
755 if (request == null) {
756 throw new IllegalStateException(
757 sm.getString("requestFacade.nullRequest"));
758 }
759
760 return request.getMethod();
761 }
762
763
764 @Override
765 public String getPathInfo() {
766
767 if (request == null) {
768 throw new IllegalStateException(
769 sm.getString("requestFacade.nullRequest"));
770 }
771
772 return request.getPathInfo();
773 }
774
775
776 @Override
777 public String getPathTranslated() {
778
779 if (request == null) {
780 throw new IllegalStateException(
781 sm.getString("requestFacade.nullRequest"));
782 }
783
784 return request.getPathTranslated();
785 }
786
787
788 @Override
789 public String getContextPath() {
790
791 if (request == null) {
792 throw new IllegalStateException(
793 sm.getString("requestFacade.nullRequest"));
794 }
795
796 return request.getContextPath();
797 }
798
799
800 @Override
801 public String getQueryString() {
802
803 if (request == null) {
804 throw new IllegalStateException(
805 sm.getString("requestFacade.nullRequest"));
806 }
807
808 return request.getQueryString();
809 }
810
811
812 @Override
813 public String getRemoteUser() {
814
815 if (request == null) {
816 throw new IllegalStateException(
817 sm.getString("requestFacade.nullRequest"));
818 }
819
820 return request.getRemoteUser();
821 }
822
823
824 @Override
825 public boolean isUserInRole(String role) {
826
827 if (request == null) {
828 throw new IllegalStateException(
829 sm.getString("requestFacade.nullRequest"));
830 }
831
832 return request.isUserInRole(role);
833 }
834
835
836 @Override
837 public java.security.Principal getUserPrincipal() {
838
839 if (request == null) {
840 throw new IllegalStateException(
841 sm.getString("requestFacade.nullRequest"));
842 }
843
844 return request.getUserPrincipal();
845 }
846
847
848 @Override
849 public String getRequestedSessionId() {
850
851 if (request == null) {
852 throw new IllegalStateException(
853 sm.getString("requestFacade.nullRequest"));
854 }
855
856 return request.getRequestedSessionId();
857 }
858
859
860 @Override
861 public String getRequestURI() {
862
863 if (request == null) {
864 throw new IllegalStateException(
865 sm.getString("requestFacade.nullRequest"));
866 }
867
868 return request.getRequestURI();
869 }
870
871
872 @Override
873 public StringBuffer getRequestURL() {
874
875 if (request == null) {
876 throw new IllegalStateException(
877 sm.getString("requestFacade.nullRequest"));
878 }
879
880 return request.getRequestURL();
881 }
882
883
884 @Override
885 public String getServletPath() {
886
887 if (request == null) {
888 throw new IllegalStateException(
889 sm.getString("requestFacade.nullRequest"));
890 }
891
892 return request.getServletPath();
893 }
894
895
896 @Override
897 public HttpSession getSession(boolean create) {
898
899 if (request == null) {
900 throw new IllegalStateException(
901 sm.getString("requestFacade.nullRequest"));
902 }
903
904 if (SecurityUtil.isPackageProtectionEnabled()){
905 return AccessController.
906 doPrivileged(new GetSessionPrivilegedAction(create));
907 } else {
908 return request.getSession(create);
909 }
910 }
911
912 @Override
913 public HttpSession getSession() {
914
915 if (request == null) {
916 throw new IllegalStateException(
917 sm.getString("requestFacade.nullRequest"));
918 }
919
920 return getSession(true);
921 }
922
923 @Override
924 public String changeSessionId() {
925
926 if (request == null) {
927 throw new IllegalStateException(
928 sm.getString("requestFacade.nullRequest"));
929 }
930
931 return request.changeSessionId();
932 }
933
934 @Override
935 public boolean isRequestedSessionIdValid() {
936
937 if (request == null) {
938 throw new IllegalStateException(
939 sm.getString("requestFacade.nullRequest"));
940 }
941
942 return request.isRequestedSessionIdValid();
943 }
944
945
946 @Override
947 public boolean isRequestedSessionIdFromCookie() {
948
949 if (request == null) {
950 throw new IllegalStateException(
951 sm.getString("requestFacade.nullRequest"));
952 }
953
954 return request.isRequestedSessionIdFromCookie();
955 }
956
957
958 @Override
959 public boolean isRequestedSessionIdFromURL() {
960
961 if (request == null) {
962 throw new IllegalStateException(
963 sm.getString("requestFacade.nullRequest"));
964 }
965
966 return request.isRequestedSessionIdFromURL();
967 }
968
969
970 @Override
971 public boolean isRequestedSessionIdFromUrl() {
972
973 if (request == null) {
974 throw new IllegalStateException(
975 sm.getString("requestFacade.nullRequest"));
976 }
977
978 return request.isRequestedSessionIdFromURL();
979 }
980
981
982 @Override
983 public String getLocalAddr() {
984
985 if (request == null) {
986 throw new IllegalStateException(
987 sm.getString("requestFacade.nullRequest"));
988 }
989
990 return request.getLocalAddr();
991 }
992
993
994 @Override
995 public String getLocalName() {
996
997 if (request == null) {
998 throw new IllegalStateException(
999 sm.getString("requestFacade.nullRequest"));
1000 }
1001
1002 return request.getLocalName();
1003 }
1004
1005
1006 @Override
1007 public int getLocalPort() {
1008
1009 if (request == null) {
1010 throw new IllegalStateException(
1011 sm.getString("requestFacade.nullRequest"));
1012 }
1013
1014 return request.getLocalPort();
1015 }
1016
1017
1018 @Override
1019 public int getRemotePort() {
1020
1021 if (request == null) {
1022 throw new IllegalStateException(
1023 sm.getString("requestFacade.nullRequest"));
1024 }
1025
1026 return request.getRemotePort();
1027 }
1028
1029
1030 @Override
1031 public ServletContext getServletContext() {
1032 if (request == null) {
1033 throw new IllegalStateException(
1034 sm.getString("requestFacade.nullRequest"));
1035 }
1036
1037 return request.getServletContext();
1038 }
1039
1040
1041 @Override
1042 public AsyncContext startAsync() throws IllegalStateException {
1043 return request.startAsync();
1044 }
1045
1046
1047 @Override
1048 public AsyncContext startAsync(ServletRequest request, ServletResponse response)
1049 throws IllegalStateException {
1050 return this.request.startAsync(request, response);
1051 }
1052
1053
1054 @Override
1055 public boolean isAsyncStarted() {
1056 return request.isAsyncStarted();
1057 }
1058
1059
1060 @Override
1061 public boolean isAsyncSupported() {
1062 return request.isAsyncSupported();
1063 }
1064
1065
1066 @Override
1067 public AsyncContext getAsyncContext() {
1068 return request.getAsyncContext();
1069 }
1070
1071
1072 @Override
1073 public DispatcherType getDispatcherType() {
1074 return request.getDispatcherType();
1075 }
1076
1077
1078 @Override
1079 public boolean authenticate(HttpServletResponse response)
1080 throws IOException, ServletException {
1081 return request.authenticate(response);
1082 }
1083
1084 @Override
1085 public void login(String username, String password)
1086 throws ServletException {
1087 request.login(username, password);
1088 }
1089
1090 @Override
1091 public void logout() throws ServletException {
1092 request.logout();
1093 }
1094
1095 @Override
1096 public Collection<Part> getParts() throws IllegalStateException,
1097 IOException, ServletException {
1098 return request.getParts();
1099 }
1100
1101
1102 @Override
1103 public Part getPart(String name) throws IllegalStateException, IOException,
1104 ServletException {
1105 return request.getPart(name);
1106 }
1107
1108
1109 public boolean getAllowTrace() {
1110 return request.getConnector().getAllowTrace();
1111 }
1112
1113
1114 @Override
1115 public long getContentLengthLong() {
1116 return request.getContentLengthLong();
1117 }
1118
1119
1120 @Override
1121 public <T extends HttpUpgradeHandler> T upgrade(
1122 Class<T> httpUpgradeHandlerClass) throws java.io.IOException, ServletException {
1123 return request.upgrade(httpUpgradeHandlerClass);
1124 }
1125
1126
1127 @Override
1128 public PushBuilder newPushBuilder() {
1129 return request.newPushBuilder();
1130 }
1131
1132
1133 public PushBuilder newPushBuilder(HttpServletRequest request) {
1134 return this.request.newPushBuilder(request);
1135 }
1136
1137
1138 @Override
1139 public boolean isTrailerFieldsReady() {
1140 return request.isTrailerFieldsReady();
1141 }
1142
1143
1144 @Override
1145 public Map<String, String> getTrailerFields() {
1146 return request.getTrailerFields();
1147 }
1148 }
1149