KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat5 > CoyoteRequestFacade


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.coyote.tomcat5;
30
31 import java.io.InputStream JavaDoc;
32 import java.io.BufferedReader JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.security.AccessController JavaDoc;
35 import java.security.AccessControlException JavaDoc;
36 import java.security.Permission JavaDoc;
37 import java.security.PrivilegedAction JavaDoc;
38 import java.security.PrivilegedActionException JavaDoc;
39 import java.security.SecurityPermission JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Locale JavaDoc;
43 import java.net.Socket JavaDoc;
44 import javax.servlet.ServletException JavaDoc;
45 import javax.servlet.ServletInputStream JavaDoc;
46 import javax.servlet.ServletRequest JavaDoc;
47 import javax.servlet.RequestDispatcher JavaDoc;
48 import javax.servlet.http.Cookie JavaDoc;
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpSession JavaDoc;
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 /**
59  * Facade class that wraps a Coyote request object.
60  * All methods are delegated to the wrapped request.
61  *
62  * @author Craig R. McClanahan
63  * @author Remy Maucherat
64  * @author Jean-Francois Arcand
65  * @version $Revision: 1.3 $ $Date: 2005/12/08 01:28:35 $
66  */

67 public class CoyoteRequestFacade
68     extends RequestFacade
69     implements HttpServletRequest JavaDoc {
70         
71         
72     // ----------------------------------------------------------- DoPrivileged
73

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

222
223     /**
224      * Construct a wrapper for the specified request.
225      *
226      * @param request The request to be wrapped
227      */

228     public CoyoteRequestFacade(CoyoteRequest request) {
229
230         super(request);
231         this.request = request;
232
233     }
234
235
236     // ----------------------------------------------- Class/Instance Variables
237

238
239     /**
240      * The string manager for this package.
241      */

242     protected static StringManager sm =
243         StringManager.getManager(Constants.Package);
244
245
246     /**
247      * The wrapped request.
248      */

249     protected CoyoteRequest request = null;
250
251
252     // --------------------------------------------------------- Public Methods
253

254     
255     /**
256     * Prevent cloning the facade.
257     */

258     protected Object JavaDoc clone()
259         throws CloneNotSupportedException JavaDoc {
260         throw new CloneNotSupportedException JavaDoc();
261     }
262     
263     
264     /**
265      * Clear facade.
266      */

267     public void clear() {
268         request = null;
269     }
270
271
272     // ------------------------------------------------- ServletRequest Methods
273

274
275     public Object JavaDoc getAttribute(String JavaDoc name) {
276
277         if (request == null) {
278             throw new IllegalStateException JavaDoc(
279                             sm.getString("requestFacade.nullRequest"));
280         }
281
282         return request.getAttribute(name);
283     }
284
285
286     public Enumeration JavaDoc getAttributeNames() {
287
288         if (request == null) {
289             throw new IllegalStateException JavaDoc(
290                             sm.getString("requestFacade.nullRequest"));
291         }
292
293         if (SecurityUtil.isPackageProtectionEnabled()){
294             return (Enumeration JavaDoc)AccessController.doPrivileged(
295                 new GetAttributePrivilegedAction());
296         } else {
297             return request.getAttributeNames();
298         }
299     }
300
301
302     public String JavaDoc getCharacterEncoding() {
303
304         if (request == null) {
305             throw new IllegalStateException JavaDoc(
306                             sm.getString("requestFacade.nullRequest"));
307         }
308
309         if (SecurityUtil.isPackageProtectionEnabled()){
310             return (String JavaDoc)AccessController.doPrivileged(
311                 new GetCharacterEncodingPrivilegedAction());
312         } else {
313             return request.getCharacterEncoding();
314         }
315     }
316
317
318     public void setCharacterEncoding(String JavaDoc env)
319             throws java.io.UnsupportedEncodingException JavaDoc {
320
321         if (request == null) {
322             throw new IllegalStateException JavaDoc(
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 JavaDoc(
334                             sm.getString("requestFacade.nullRequest"));
335         }
336
337         return request.getContentLength();
338     }
339
340
341     public String JavaDoc getContentType() {
342
343         if (request == null) {
344             throw new IllegalStateException JavaDoc(
345                             sm.getString("requestFacade.nullRequest"));
346         }
347
348         return request.getContentType();
349     }
350
351
352     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
353
354         if (request == null) {
355             throw new IllegalStateException JavaDoc(
356                             sm.getString("requestFacade.nullRequest"));
357         }
358
359         return request.getInputStream();
360     }
361
362
363     public String JavaDoc getParameter(String JavaDoc name) {
364
365         if (request == null) {
366             throw new IllegalStateException JavaDoc(
367                             sm.getString("requestFacade.nullRequest"));
368         }
369
370         if (SecurityUtil.isPackageProtectionEnabled()){
371             return (String JavaDoc)AccessController.doPrivileged(
372                 new GetParameterPrivilegedAction(name));
373         } else {
374             return request.getParameter(name);
375         }
376     }
377
378
379     public Enumeration JavaDoc getParameterNames() {
380
381         if (request == null) {
382             throw new IllegalStateException JavaDoc(
383                             sm.getString("requestFacade.nullRequest"));
384         }
385
386         if (SecurityUtil.isPackageProtectionEnabled()){
387             return (Enumeration JavaDoc)AccessController.doPrivileged(
388                 new GetParameterNamesPrivilegedAction());
389         } else {
390             return request.getParameterNames();
391         }
392     }
393
394
395     public String JavaDoc[] getParameterValues(String JavaDoc name) {
396
397         if (request == null) {
398             throw new IllegalStateException JavaDoc(
399                             sm.getString("requestFacade.nullRequest"));
400         }
401
402         String JavaDoc[] ret = null;
403
404         /*
405          * Clone the returned array only if there is a security manager
406          * in place, so that performance won't suffer in the nonsecure case
407          */

408         if (SecurityUtil.isPackageProtectionEnabled()){
409             ret = (String JavaDoc[]) AccessController.doPrivileged(
410                 new GetParameterValuePrivilegedAction(name));
411             if (ret != null) {
412                 ret = (String JavaDoc[]) ret.clone();
413             }
414         } else {
415             ret = request.getParameterValues(name);
416         }
417
418         return ret;
419     }
420
421
422     public Map JavaDoc getParameterMap() {
423
424         if (request == null) {
425             throw new IllegalStateException JavaDoc(
426                             sm.getString("requestFacade.nullRequest"));
427         }
428
429         if (SecurityUtil.isPackageProtectionEnabled()){
430             return (Map JavaDoc)AccessController.doPrivileged(
431                 new GetParameterMapPrivilegedAction());
432         } else {
433             return request.getParameterMap();
434         }
435     }
436
437
438     public String JavaDoc getProtocol() {
439
440         if (request == null) {
441             throw new IllegalStateException JavaDoc(
442                             sm.getString("requestFacade.nullRequest"));
443         }
444
445         return request.getProtocol();
446     }
447
448
449     public String JavaDoc getScheme() {
450
451         if (request == null) {
452             throw new IllegalStateException JavaDoc(
453                             sm.getString("requestFacade.nullRequest"));
454         }
455
456         return request.getScheme();
457     }
458
459
460     public String JavaDoc getServerName() {
461
462         if (request == null) {
463             throw new IllegalStateException JavaDoc(
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 JavaDoc(
475                             sm.getString("requestFacade.nullRequest"));
476         }
477
478         return request.getServerPort();
479     }
480
481
482     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
483
484         if (request == null) {
485             throw new IllegalStateException JavaDoc(
486                             sm.getString("requestFacade.nullRequest"));
487         }
488
489         return request.getReader();
490     }
491
492
493     public String JavaDoc getRemoteAddr() {
494
495         if (request == null) {
496             throw new IllegalStateException JavaDoc(
497                             sm.getString("requestFacade.nullRequest"));
498         }
499
500         return request.getRemoteAddr();
501     }
502
503
504     public String JavaDoc getRemoteHost() {
505
506         if (request == null) {
507             throw new IllegalStateException JavaDoc(
508                             sm.getString("requestFacade.nullRequest"));
509         }
510
511         return request.getRemoteHost();
512     }
513
514
515     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
516
517         if (request == null) {
518             throw new IllegalStateException JavaDoc(
519                             sm.getString("requestFacade.nullRequest"));
520         }
521
522         request.setAttribute(name, o);
523     }
524
525
526     public void removeAttribute(String JavaDoc name) {
527
528         if (request == null) {
529             throw new IllegalStateException JavaDoc(
530                             sm.getString("requestFacade.nullRequest"));
531         }
532
533         request.removeAttribute(name);
534     }
535
536
537     public Locale JavaDoc getLocale() {
538
539         if (request == null) {
540             throw new IllegalStateException JavaDoc(
541                             sm.getString("requestFacade.nullRequest"));
542         }
543
544         if (SecurityUtil.isPackageProtectionEnabled()){
545             return (Locale JavaDoc)AccessController.doPrivileged(
546                 new GetLocalePrivilegedAction());
547         } else {
548             return request.getLocale();
549         }
550     }
551
552
553     public Enumeration JavaDoc getLocales() {
554
555         if (request == null) {
556             throw new IllegalStateException JavaDoc(
557                             sm.getString("requestFacade.nullRequest"));
558         }
559
560         if (SecurityUtil.isPackageProtectionEnabled()){
561             return (Enumeration JavaDoc)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 JavaDoc(
573                             sm.getString("requestFacade.nullRequest"));
574         }
575
576         return request.isSecure();
577     }
578
579
580     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
581
582         if (request == null) {
583             throw new IllegalStateException JavaDoc(
584                             sm.getString("requestFacade.nullRequest"));
585         }
586
587         if (SecurityUtil.isPackageProtectionEnabled()){
588             return (RequestDispatcher JavaDoc)AccessController.doPrivileged(
589                 new GetRequestDispatcherPrivilegedAction(path));
590         } else {
591             return request.getRequestDispatcher(path);
592         }
593     }
594
595
596     public String JavaDoc getRealPath(String JavaDoc path) {
597
598         if (request == null) {
599             throw new IllegalStateException JavaDoc(
600                             sm.getString("requestFacade.nullRequest"));
601         }
602
603         return request.getRealPath(path);
604     }
605
606
607     public String JavaDoc getAuthType() {
608
609         if (request == null) {
610             throw new IllegalStateException JavaDoc(
611                             sm.getString("requestFacade.nullRequest"));
612         }
613
614         return request.getAuthType();
615     }
616
617
618     public Cookie JavaDoc[] getCookies() {
619
620         if (request == null) {
621             throw new IllegalStateException JavaDoc(
622                             sm.getString("requestFacade.nullRequest"));
623         }
624
625         Cookie JavaDoc[] ret = null;
626
627         /*
628          * Clone the returned array only if there is a security manager
629          * in place, so that performance won't suffer in the nonsecure case
630          */

631         if (SecurityUtil.isPackageProtectionEnabled()){
632             ret = (Cookie JavaDoc[])AccessController.doPrivileged(
633                 new GetCookiesPrivilegedAction());
634             if (ret != null) {
635                 ret = (Cookie JavaDoc[]) ret.clone();
636             }
637         } else {
638             ret = request.getCookies();
639         }
640
641         return ret;
642     }
643
644
645     public long getDateHeader(String JavaDoc name) {
646
647         if (request == null) {
648             throw new IllegalStateException JavaDoc(
649                             sm.getString("requestFacade.nullRequest"));
650         }
651
652         return request.getDateHeader(name);
653     }
654
655
656     public String JavaDoc getHeader(String JavaDoc name) {
657
658         if (request == null) {
659             throw new IllegalStateException JavaDoc(
660                             sm.getString("requestFacade.nullRequest"));
661         }
662
663         return request.getHeader(name);
664     }
665
666
667     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
668
669         if (request == null) {
670             throw new IllegalStateException JavaDoc(
671                             sm.getString("requestFacade.nullRequest"));
672         }
673
674         if (SecurityUtil.isPackageProtectionEnabled()){
675             return (Enumeration JavaDoc)AccessController.doPrivileged(
676                 new GetHeadersPrivilegedAction(name));
677         } else {
678             return request.getHeaders(name);
679         }
680     }
681
682
683     public Enumeration JavaDoc getHeaderNames() {
684
685         if (request == null) {
686             throw new IllegalStateException JavaDoc(
687                             sm.getString("requestFacade.nullRequest"));
688         }
689
690         if (SecurityUtil.isPackageProtectionEnabled()){
691             return (Enumeration JavaDoc)AccessController.doPrivileged(
692                 new GetHeaderNamesPrivilegedAction());
693         } else {
694             return request.getHeaderNames();
695         }
696     }
697
698
699     public int getIntHeader(String JavaDoc name) {
700
701         if (request == null) {
702             throw new IllegalStateException JavaDoc(
703                             sm.getString("requestFacade.nullRequest"));
704         }
705
706         return request.getIntHeader(name);
707     }
708
709
710     public String JavaDoc getMethod() {
711
712         if (request == null) {
713             throw new IllegalStateException JavaDoc(
714                             sm.getString("requestFacade.nullRequest"));
715         }
716
717         return request.getMethod();
718     }
719
720
721     public String JavaDoc getPathInfo() {
722
723         if (request == null) {
724             throw new IllegalStateException JavaDoc(
725                             sm.getString("requestFacade.nullRequest"));
726         }
727
728         return request.getPathInfo();
729     }
730
731
732     public String JavaDoc getPathTranslated() {
733
734         if (request == null) {
735             throw new IllegalStateException JavaDoc(
736                             sm.getString("requestFacade.nullRequest"));
737         }
738
739         return request.getPathTranslated();
740     }
741
742
743     public String JavaDoc getContextPath() {
744
745         if (request == null) {
746             throw new IllegalStateException JavaDoc(
747                             sm.getString("requestFacade.nullRequest"));
748         }
749
750         return request.getContextPath();
751     }
752
753
754     public String JavaDoc getQueryString() {
755
756         if (request == null) {
757             throw new IllegalStateException JavaDoc(
758                             sm.getString("requestFacade.nullRequest"));
759         }
760
761         return request.getQueryString();
762     }
763
764
765     public String JavaDoc getRemoteUser() {
766
767         if (request == null) {
768             throw new IllegalStateException JavaDoc(
769                             sm.getString("requestFacade.nullRequest"));
770         }
771
772         return request.getRemoteUser();
773     }
774
775
776     public boolean isUserInRole(String JavaDoc role) {
777
778         if (request == null) {
779             throw new IllegalStateException JavaDoc(
780                             sm.getString("requestFacade.nullRequest"));
781         }
782
783         return request.isUserInRole(role);
784     }
785
786
787     public java.security.Principal JavaDoc getUserPrincipal() {
788
789         if (request == null) {
790             throw new IllegalStateException JavaDoc(
791                             sm.getString("requestFacade.nullRequest"));
792         }
793
794         return request.getUserPrincipal();
795     }
796
797
798     public String JavaDoc getRequestedSessionId() {
799
800         if (request == null) {
801             throw new IllegalStateException JavaDoc(
802                             sm.getString("requestFacade.nullRequest"));
803         }
804
805         return request.getRequestedSessionId();
806     }
807
808
809     public String JavaDoc getRequestURI() {
810
811         if (request == null) {
812             throw new IllegalStateException JavaDoc(
813                             sm.getString("requestFacade.nullRequest"));
814         }
815
816         return request.getRequestURI();
817     }
818
819
820     public StringBuffer JavaDoc getRequestURL() {
821
822         if (request == null) {
823             throw new IllegalStateException JavaDoc(
824                             sm.getString("requestFacade.nullRequest"));
825         }
826
827         return request.getRequestURL();
828     }
829
830
831     public String JavaDoc getServletPath() {
832
833         if (request == null) {
834             throw new IllegalStateException JavaDoc(
835                             sm.getString("requestFacade.nullRequest"));
836         }
837
838         return request.getServletPath();
839     }
840
841
842     public HttpSession JavaDoc getSession(boolean create) {
843
844         if (request == null) {
845             throw new IllegalStateException JavaDoc(
846                             sm.getString("requestFacade.nullRequest"));
847         }
848
849         if (SecurityUtil.isPackageProtectionEnabled()){
850             return (HttpSession JavaDoc)AccessController.
851                 doPrivileged(new GetSessionPrivilegedAction(create));
852         } else {
853             return request.getSession(create);
854         }
855     }
856
857     public HttpSession JavaDoc getSession() {
858
859         if (request == null) {
860             throw new IllegalStateException JavaDoc(
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 JavaDoc(
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 JavaDoc(
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 JavaDoc(
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 JavaDoc(
905                             sm.getString("requestFacade.nullRequest"));
906         }
907
908         return request.isRequestedSessionIdFromURL();
909     }
910
911
912     //START S1AS 4703023
913
/**
914      * Return the original <code>CoyoteRequest</code> object.
915      */

916     public CoyoteRequest getUnwrappedCoyoteRequest()
917         throws AccessControlException JavaDoc {
918
919         // tomcat does not have any Permission types so instead of
920
// creating a TomcatPermission for this, use SecurityPermission.
921
SecurityManager JavaDoc sm = System.getSecurityManager();
922         if (sm != null) {
923             Permission JavaDoc perm =
924                 new SecurityPermission JavaDoc("getUnwrappedCoyoteRequest");
925             AccessController.checkPermission(perm);
926         }
927
928         return request;
929     }
930     //END S1AS 4703023
931

932 }
933
Popular Tags