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