KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > connector > RequestFacade


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 JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.security.AccessController JavaDoc;
24 import java.security.PrivilegedAction JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.RequestDispatcher JavaDoc;
30 import javax.servlet.ServletInputStream JavaDoc;
31 import javax.servlet.http.Cookie JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34
35 import org.apache.catalina.util.StringManager;
36
37
38 import org.apache.catalina.security.SecurityUtil;
39
40 /**
41  * Facade class that wraps a Coyote request object.
42  * All methods are delegated to the wrapped request.
43  *
44  * @author Craig R. McClanahan
45  * @author Remy Maucherat
46  * @author Jean-Francois Arcand
47  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
48  */

49
50 @SuppressWarnings JavaDoc("deprecation")
51 public class RequestFacade implements HttpServletRequest JavaDoc {
52         
53         
54     // ----------------------------------------------------------- DoPrivileged
55

56     private final class GetAttributePrivilegedAction
57             implements PrivilegedAction JavaDoc {
58         
59         public Object JavaDoc run() {
60             return request.getAttributeNames();
61         }
62     }
63      
64     
65     private final class GetParameterMapPrivilegedAction
66             implements PrivilegedAction JavaDoc {
67         
68         public Object JavaDoc run() {
69             return request.getParameterMap();
70         }
71     }
72     
73     
74     private final class GetRequestDispatcherPrivilegedAction
75             implements PrivilegedAction JavaDoc {
76
77         private String JavaDoc path;
78
79         public GetRequestDispatcherPrivilegedAction(String JavaDoc path){
80             this.path = path;
81         }
82         
83         public Object JavaDoc run() {
84             return request.getRequestDispatcher(path);
85         }
86     }
87     
88     
89     private final class GetParameterPrivilegedAction
90             implements PrivilegedAction JavaDoc {
91
92         public String JavaDoc name;
93
94         public GetParameterPrivilegedAction(String JavaDoc name){
95             this.name = name;
96         }
97
98         public Object JavaDoc run() {
99             return request.getParameter(name);
100         }
101     }
102     
103      
104     private final class GetParameterNamesPrivilegedAction
105             implements PrivilegedAction JavaDoc {
106         
107         public Object JavaDoc run() {
108             return request.getParameterNames();
109         }
110     }
111     
112     
113     private final class GetParameterValuePrivilegedAction
114             implements PrivilegedAction JavaDoc {
115
116         public String JavaDoc name;
117
118         public GetParameterValuePrivilegedAction(String JavaDoc name){
119             this.name = name;
120         }
121
122         public Object JavaDoc run() {
123             return request.getParameterValues(name);
124         }
125     }
126   
127     
128     private final class GetCookiesPrivilegedAction
129             implements PrivilegedAction JavaDoc {
130         
131         public Object JavaDoc run() {
132             return request.getCookies();
133         }
134     }
135     
136     
137     private final class GetCharacterEncodingPrivilegedAction
138             implements PrivilegedAction JavaDoc {
139         
140         public Object JavaDoc run() {
141             return request.getCharacterEncoding();
142         }
143     }
144         
145     
146     private final class GetHeadersPrivilegedAction
147             implements PrivilegedAction JavaDoc {
148
149         private String JavaDoc name;
150
151         public GetHeadersPrivilegedAction(String JavaDoc name){
152             this.name = name;
153         }
154         
155         public Object JavaDoc run() {
156             return request.getHeaders(name);
157         }
158     }
159         
160     
161     private final class GetHeaderNamesPrivilegedAction
162             implements PrivilegedAction JavaDoc {
163
164         public Object JavaDoc run() {
165             return request.getHeaderNames();
166         }
167     }
168             
169     
170     private final class GetLocalePrivilegedAction
171             implements PrivilegedAction JavaDoc {
172
173         public Object JavaDoc run() {
174             return request.getLocale();
175         }
176     }
177             
178     
179     private final class GetLocalesPrivilegedAction
180             implements PrivilegedAction JavaDoc {
181
182         public Object JavaDoc run() {
183             return request.getLocales();
184         }
185     }
186     
187     private final class GetSessionPrivilegedAction
188             implements PrivilegedAction JavaDoc {
189
190         private boolean create;
191         
192         public GetSessionPrivilegedAction(boolean create){
193             this.create = create;
194         }
195                 
196         public Object JavaDoc run() {
197             return request.getSession(create);
198         }
199     }
200
201     // ----------------------------------------------------------- Constructors
202

203
204     /**
205      * Construct a wrapper for the specified request.
206      *
207      * @param request The request to be wrapped
208      */

209     public RequestFacade(Request request) {
210
211         this.request = request;
212
213     }
214
215
216     // ----------------------------------------------------- Instance Variables
217

218
219     /**
220      * The wrapped request.
221      */

222     protected Request request = null;
223
224
225     /**
226      * The string manager for this package.
227      */

228     protected static StringManager sm =
229         StringManager.getManager(Constants.Package);
230
231
232     // --------------------------------------------------------- Public Methods
233

234
235     /**
236      * Clear facade.
237      */

238     public void clear() {
239         request = null;
240     }
241
242     
243     /**
244      * Prevent cloning the facade.
245      */

246     protected Object JavaDoc clone()
247         throws CloneNotSupportedException JavaDoc {
248         throw new CloneNotSupportedException JavaDoc();
249     }
250
251
252     // ------------------------------------------------- ServletRequest Methods
253

254
255     public Object JavaDoc getAttribute(String JavaDoc name) {
256
257         if (request == null) {
258             throw new IllegalStateException JavaDoc(
259                             sm.getString("requestFacade.nullRequest"));
260         }
261
262         return request.getAttribute(name);
263     }
264
265
266     public Enumeration JavaDoc getAttributeNames() {
267
268         if (request == null) {
269             throw new IllegalStateException JavaDoc(
270                             sm.getString("requestFacade.nullRequest"));
271         }
272
273         if (System.getSecurityManager() != null){
274             return (Enumeration JavaDoc)AccessController.doPrivileged(
275                 new GetAttributePrivilegedAction());
276         } else {
277             return request.getAttributeNames();
278         }
279     }
280
281
282     public String JavaDoc getCharacterEncoding() {
283
284         if (request == null) {
285             throw new IllegalStateException JavaDoc(
286                             sm.getString("requestFacade.nullRequest"));
287         }
288
289         if (System.getSecurityManager() != null){
290             return (String JavaDoc)AccessController.doPrivileged(
291                 new GetCharacterEncodingPrivilegedAction());
292         } else {
293             return request.getCharacterEncoding();
294         }
295     }
296
297
298     public void setCharacterEncoding(String JavaDoc env)
299             throws java.io.UnsupportedEncodingException JavaDoc {
300
301         if (request == null) {
302             throw new IllegalStateException JavaDoc(
303                             sm.getString("requestFacade.nullRequest"));
304         }
305
306         request.setCharacterEncoding(env);
307     }
308
309
310     public int getContentLength() {
311
312         if (request == null) {
313             throw new IllegalStateException JavaDoc(
314                             sm.getString("requestFacade.nullRequest"));
315         }
316
317         return request.getContentLength();
318     }
319
320
321     public String JavaDoc getContentType() {
322
323         if (request == null) {
324             throw new IllegalStateException JavaDoc(
325                             sm.getString("requestFacade.nullRequest"));
326         }
327
328         return request.getContentType();
329     }
330
331
332     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
333
334         if (request == null) {
335             throw new IllegalStateException JavaDoc(
336                             sm.getString("requestFacade.nullRequest"));
337         }
338
339         return request.getInputStream();
340     }
341
342
343     public String JavaDoc getParameter(String JavaDoc name) {
344
345         if (request == null) {
346             throw new IllegalStateException JavaDoc(
347                             sm.getString("requestFacade.nullRequest"));
348         }
349
350         if (System.getSecurityManager() != null){
351             return (String JavaDoc)AccessController.doPrivileged(
352                 new GetParameterPrivilegedAction(name));
353         } else {
354             return request.getParameter(name);
355         }
356     }
357
358
359     public Enumeration JavaDoc getParameterNames() {
360
361         if (request == null) {
362             throw new IllegalStateException JavaDoc(
363                             sm.getString("requestFacade.nullRequest"));
364         }
365
366         if (System.getSecurityManager() != null){
367             return (Enumeration JavaDoc)AccessController.doPrivileged(
368                 new GetParameterNamesPrivilegedAction());
369         } else {
370             return request.getParameterNames();
371         }
372     }
373
374
375     public String JavaDoc[] getParameterValues(String JavaDoc name) {
376
377         if (request == null) {
378             throw new IllegalStateException JavaDoc(
379                             sm.getString("requestFacade.nullRequest"));
380         }
381
382         String JavaDoc[] ret = null;
383
384         /*
385          * Clone the returned array only if there is a security manager
386          * in place, so that performance won't suffer in the nonsecure case
387          */

388         if (SecurityUtil.isPackageProtectionEnabled()){
389             ret = (String JavaDoc[]) AccessController.doPrivileged(
390                 new GetParameterValuePrivilegedAction(name));
391             if (ret != null) {
392                 ret = (String JavaDoc[]) ret.clone();
393             }
394         } else {
395             ret = request.getParameterValues(name);
396         }
397
398         return ret;
399     }
400
401
402     public Map JavaDoc getParameterMap() {
403
404         if (request == null) {
405             throw new IllegalStateException JavaDoc(
406                             sm.getString("requestFacade.nullRequest"));
407         }
408
409         if (System.getSecurityManager() != null){
410             return (Map JavaDoc)AccessController.doPrivileged(
411                 new GetParameterMapPrivilegedAction());
412         } else {
413             return request.getParameterMap();
414         }
415     }
416
417
418     public String JavaDoc getProtocol() {
419
420         if (request == null) {
421             throw new IllegalStateException JavaDoc(
422                             sm.getString("requestFacade.nullRequest"));
423         }
424
425         return request.getProtocol();
426     }
427
428
429     public String JavaDoc getScheme() {
430
431         if (request == null) {
432             throw new IllegalStateException JavaDoc(
433                             sm.getString("requestFacade.nullRequest"));
434         }
435
436         return request.getScheme();
437     }
438
439
440     public String JavaDoc getServerName() {
441
442         if (request == null) {
443             throw new IllegalStateException JavaDoc(
444                             sm.getString("requestFacade.nullRequest"));
445         }
446
447         return request.getServerName();
448     }
449
450
451     public int getServerPort() {
452
453         if (request == null) {
454             throw new IllegalStateException JavaDoc(
455                             sm.getString("requestFacade.nullRequest"));
456         }
457
458         return request.getServerPort();
459     }
460
461
462     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
463
464         if (request == null) {
465             throw new IllegalStateException JavaDoc(
466                             sm.getString("requestFacade.nullRequest"));
467         }
468
469         return request.getReader();
470     }
471
472
473     public String JavaDoc getRemoteAddr() {
474
475         if (request == null) {
476             throw new IllegalStateException JavaDoc(
477                             sm.getString("requestFacade.nullRequest"));
478         }
479
480         return request.getRemoteAddr();
481     }
482
483
484     public String JavaDoc getRemoteHost() {
485
486         if (request == null) {
487             throw new IllegalStateException JavaDoc(
488                             sm.getString("requestFacade.nullRequest"));
489         }
490
491         return request.getRemoteHost();
492     }
493
494
495     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
496
497         if (request == null) {
498             throw new IllegalStateException JavaDoc(
499                             sm.getString("requestFacade.nullRequest"));
500         }
501
502         request.setAttribute(name, o);
503     }
504
505
506     public void removeAttribute(String JavaDoc name) {
507
508         if (request == null) {
509             throw new IllegalStateException JavaDoc(
510                             sm.getString("requestFacade.nullRequest"));
511         }
512
513         request.removeAttribute(name);
514     }
515
516
517     public Locale JavaDoc getLocale() {
518
519         if (request == null) {
520             throw new IllegalStateException JavaDoc(
521                             sm.getString("requestFacade.nullRequest"));
522         }
523
524         if (System.getSecurityManager() != null){
525             return (Locale JavaDoc)AccessController.doPrivileged(
526                 new GetLocalePrivilegedAction());
527         } else {
528             return request.getLocale();
529         }
530     }
531
532
533     public Enumeration JavaDoc getLocales() {
534
535         if (request == null) {
536             throw new IllegalStateException JavaDoc(
537                             sm.getString("requestFacade.nullRequest"));
538         }
539
540         if (System.getSecurityManager() != null){
541             return (Enumeration JavaDoc)AccessController.doPrivileged(
542                 new GetLocalesPrivilegedAction());
543         } else {
544             return request.getLocales();
545         }
546     }
547
548
549     public boolean isSecure() {
550
551         if (request == null) {
552             throw new IllegalStateException JavaDoc(
553                             sm.getString("requestFacade.nullRequest"));
554         }
555
556         return request.isSecure();
557     }
558
559
560     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
561
562         if (request == null) {
563             throw new IllegalStateException JavaDoc(
564                             sm.getString("requestFacade.nullRequest"));
565         }
566
567         if (System.getSecurityManager() != null){
568             return (RequestDispatcher JavaDoc)AccessController.doPrivileged(
569                 new GetRequestDispatcherPrivilegedAction(path));
570         } else {
571             return request.getRequestDispatcher(path);
572         }
573     }
574
575     public String JavaDoc getRealPath(String JavaDoc path) {
576
577         if (request == null) {
578             throw new IllegalStateException JavaDoc(
579                             sm.getString("requestFacade.nullRequest"));
580         }
581
582         return request.getRealPath(path);
583     }
584
585
586     public String JavaDoc getAuthType() {
587
588         if (request == null) {
589             throw new IllegalStateException JavaDoc(
590                             sm.getString("requestFacade.nullRequest"));
591         }
592
593         return request.getAuthType();
594     }
595
596
597     public Cookie JavaDoc[] getCookies() {
598
599         if (request == null) {
600             throw new IllegalStateException JavaDoc(
601                             sm.getString("requestFacade.nullRequest"));
602         }
603
604         Cookie JavaDoc[] ret = null;
605
606         /*
607          * Clone the returned array only if there is a security manager
608          * in place, so that performance won't suffer in the nonsecure case
609          */

610         if (SecurityUtil.isPackageProtectionEnabled()){
611             ret = (Cookie JavaDoc[])AccessController.doPrivileged(
612                 new GetCookiesPrivilegedAction());
613             if (ret != null) {
614                 ret = (Cookie JavaDoc[]) ret.clone();
615             }
616         } else {
617             ret = request.getCookies();
618         }
619
620         return ret;
621     }
622
623
624     public long getDateHeader(String JavaDoc name) {
625
626         if (request == null) {
627             throw new IllegalStateException JavaDoc(
628                             sm.getString("requestFacade.nullRequest"));
629         }
630
631         return request.getDateHeader(name);
632     }
633
634
635     public String JavaDoc getHeader(String JavaDoc name) {
636
637         if (request == null) {
638             throw new IllegalStateException JavaDoc(
639                             sm.getString("requestFacade.nullRequest"));
640         }
641
642         return request.getHeader(name);
643     }
644
645
646     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
647
648         if (request == null) {
649             throw new IllegalStateException JavaDoc(
650                             sm.getString("requestFacade.nullRequest"));
651         }
652
653         if (System.getSecurityManager() != null){
654             return (Enumeration JavaDoc)AccessController.doPrivileged(
655                 new GetHeadersPrivilegedAction(name));
656         } else {
657             return request.getHeaders(name);
658         }
659     }
660
661
662     public Enumeration JavaDoc getHeaderNames() {
663
664         if (request == null) {
665             throw new IllegalStateException JavaDoc(
666                             sm.getString("requestFacade.nullRequest"));
667         }
668
669         if (System.getSecurityManager() != null){
670             return (Enumeration JavaDoc)AccessController.doPrivileged(
671                 new GetHeaderNamesPrivilegedAction());
672         } else {
673             return request.getHeaderNames();
674         }
675     }
676
677
678     public int getIntHeader(String JavaDoc name) {
679
680         if (request == null) {
681             throw new IllegalStateException JavaDoc(
682                             sm.getString("requestFacade.nullRequest"));
683         }
684
685         return request.getIntHeader(name);
686     }
687
688
689     public String JavaDoc getMethod() {
690
691         if (request == null) {
692             throw new IllegalStateException JavaDoc(
693                             sm.getString("requestFacade.nullRequest"));
694         }
695
696         return request.getMethod();
697     }
698
699
700     public String JavaDoc getPathInfo() {
701
702         if (request == null) {
703             throw new IllegalStateException JavaDoc(
704                             sm.getString("requestFacade.nullRequest"));
705         }
706
707         return request.getPathInfo();
708     }
709
710
711     public String JavaDoc getPathTranslated() {
712
713         if (request == null) {
714             throw new IllegalStateException JavaDoc(
715                             sm.getString("requestFacade.nullRequest"));
716         }
717
718         return request.getPathTranslated();
719     }
720
721
722     public String JavaDoc getContextPath() {
723
724         if (request == null) {
725             throw new IllegalStateException JavaDoc(
726                             sm.getString("requestFacade.nullRequest"));
727         }
728
729         return request.getContextPath();
730     }
731
732
733     public String JavaDoc getQueryString() {
734
735         if (request == null) {
736             throw new IllegalStateException JavaDoc(
737                             sm.getString("requestFacade.nullRequest"));
738         }
739
740         return request.getQueryString();
741     }
742
743
744     public String JavaDoc getRemoteUser() {
745
746         if (request == null) {
747             throw new IllegalStateException JavaDoc(
748                             sm.getString("requestFacade.nullRequest"));
749         }
750
751         return request.getRemoteUser();
752     }
753
754
755     public boolean isUserInRole(String JavaDoc role) {
756
757         if (request == null) {
758             throw new IllegalStateException JavaDoc(
759                             sm.getString("requestFacade.nullRequest"));
760         }
761
762         return request.isUserInRole(role);
763     }
764
765
766     public java.security.Principal JavaDoc getUserPrincipal() {
767
768         if (request == null) {
769             throw new IllegalStateException JavaDoc(
770                             sm.getString("requestFacade.nullRequest"));
771         }
772
773         return request.getUserPrincipal();
774     }
775
776
777     public String JavaDoc getRequestedSessionId() {
778
779         if (request == null) {
780             throw new IllegalStateException JavaDoc(
781                             sm.getString("requestFacade.nullRequest"));
782         }
783
784         return request.getRequestedSessionId();
785     }
786
787
788     public String JavaDoc getRequestURI() {
789
790         if (request == null) {
791             throw new IllegalStateException JavaDoc(
792                             sm.getString("requestFacade.nullRequest"));
793         }
794
795         return request.getRequestURI();
796     }
797
798
799     public StringBuffer JavaDoc getRequestURL() {
800
801         if (request == null) {
802             throw new IllegalStateException JavaDoc(
803                             sm.getString("requestFacade.nullRequest"));
804         }
805
806         return request.getRequestURL();
807     }
808
809
810     public String JavaDoc getServletPath() {
811
812         if (request == null) {
813             throw new IllegalStateException JavaDoc(
814                             sm.getString("requestFacade.nullRequest"));
815         }
816
817         return request.getServletPath();
818     }
819
820
821     public HttpSession JavaDoc getSession(boolean create) {
822
823         if (request == null) {
824             throw new IllegalStateException JavaDoc(
825                             sm.getString("requestFacade.nullRequest"));
826         }
827
828         if (SecurityUtil.isPackageProtectionEnabled()){
829             return (HttpSession JavaDoc)AccessController.
830                 doPrivileged(new GetSessionPrivilegedAction(create));
831         } else {
832             return request.getSession(create);
833         }
834     }
835
836     public HttpSession JavaDoc getSession() {
837
838         if (request == null) {
839             throw new IllegalStateException JavaDoc(
840                             sm.getString("requestFacade.nullRequest"));
841         }
842
843         return getSession(true);
844     }
845
846
847     public boolean isRequestedSessionIdValid() {
848
849         if (request == null) {
850             throw new IllegalStateException JavaDoc(
851                             sm.getString("requestFacade.nullRequest"));
852         }
853
854         return request.isRequestedSessionIdValid();
855     }
856
857
858     public boolean isRequestedSessionIdFromCookie() {
859
860         if (request == null) {
861             throw new IllegalStateException JavaDoc(
862                             sm.getString("requestFacade.nullRequest"));
863         }
864
865         return request.isRequestedSessionIdFromCookie();
866     }
867
868
869     public boolean isRequestedSessionIdFromURL() {
870
871         if (request == null) {
872             throw new IllegalStateException JavaDoc(
873                             sm.getString("requestFacade.nullRequest"));
874         }
875
876         return request.isRequestedSessionIdFromURL();
877     }
878
879
880     public boolean isRequestedSessionIdFromUrl() {
881
882         if (request == null) {
883             throw new IllegalStateException JavaDoc(
884                             sm.getString("requestFacade.nullRequest"));
885         }
886
887         return request.isRequestedSessionIdFromURL();
888     }
889
890
891     public String JavaDoc getLocalAddr() {
892
893         if (request == null) {
894             throw new IllegalStateException JavaDoc(
895                             sm.getString("requestFacade.nullRequest"));
896         }
897
898         return request.getLocalAddr();
899     }
900
901
902     public String JavaDoc getLocalName() {
903
904         if (request == null) {
905             throw new IllegalStateException JavaDoc(
906                             sm.getString("requestFacade.nullRequest"));
907         }
908
909         return request.getLocalName();
910     }
911
912
913     public int getLocalPort() {
914
915         if (request == null) {
916             throw new IllegalStateException JavaDoc(
917                             sm.getString("requestFacade.nullRequest"));
918         }
919
920         return request.getLocalPort();
921     }
922
923
924     public int getRemotePort() {
925
926         if (request == null) {
927             throw new IllegalStateException JavaDoc(
928                             sm.getString("requestFacade.nullRequest"));
929         }
930
931         return request.getRemotePort();
932     }
933
934 }
935
Popular Tags