001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.connector;
019:
020: import java.io.BufferedReader;
021: import java.io.IOException;
022: import java.security.AccessController;
023: import java.security.PrivilegedAction;
024: import java.util.Enumeration;
025: import java.util.Locale;
026: import java.util.Map;
027:
028: import javax.servlet.RequestDispatcher;
029: import javax.servlet.ServletInputStream;
030: import javax.servlet.http.Cookie;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpSession;
033:
034: import org.apache.catalina.Globals;
035: import org.apache.catalina.util.StringManager;
036:
037: import org.apache.catalina.security.SecurityUtil;
038:
039: /**
040: * Facade class that wraps a Coyote request object.
041: * All methods are delegated to the wrapped request.
042: *
043: * @author Craig R. McClanahan
044: * @author Remy Maucherat
045: * @author Jean-Francois Arcand
046: * @version $Revision: 505593 $ $Date: 2007-02-10 01:54:56 +0100 (sam., 10 févr. 2007) $
047: */
048:
049: @SuppressWarnings("deprecation")
050: public class RequestFacade implements HttpServletRequest {
051:
052: // ----------------------------------------------------------- DoPrivileged
053:
054: private final class GetAttributePrivilegedAction implements
055: PrivilegedAction {
056:
057: public Object run() {
058: return request.getAttributeNames();
059: }
060: }
061:
062: private final class GetParameterMapPrivilegedAction implements
063: PrivilegedAction {
064:
065: public Object run() {
066: return request.getParameterMap();
067: }
068: }
069:
070: private final class GetRequestDispatcherPrivilegedAction implements
071: PrivilegedAction {
072:
073: private String path;
074:
075: public GetRequestDispatcherPrivilegedAction(String path) {
076: this .path = path;
077: }
078:
079: public Object run() {
080: return request.getRequestDispatcher(path);
081: }
082: }
083:
084: private final class GetParameterPrivilegedAction implements
085: PrivilegedAction {
086:
087: public String name;
088:
089: public GetParameterPrivilegedAction(String name) {
090: this .name = name;
091: }
092:
093: public Object run() {
094: return request.getParameter(name);
095: }
096: }
097:
098: private final class GetParameterNamesPrivilegedAction implements
099: PrivilegedAction {
100:
101: public Object run() {
102: return request.getParameterNames();
103: }
104: }
105:
106: private final class GetParameterValuePrivilegedAction implements
107: PrivilegedAction {
108:
109: public String name;
110:
111: public GetParameterValuePrivilegedAction(String name) {
112: this .name = name;
113: }
114:
115: public Object run() {
116: return request.getParameterValues(name);
117: }
118: }
119:
120: private final class GetCookiesPrivilegedAction implements
121: PrivilegedAction {
122:
123: public Object run() {
124: return request.getCookies();
125: }
126: }
127:
128: private final class GetCharacterEncodingPrivilegedAction implements
129: PrivilegedAction {
130:
131: public Object run() {
132: return request.getCharacterEncoding();
133: }
134: }
135:
136: private final class GetHeadersPrivilegedAction implements
137: PrivilegedAction {
138:
139: private String name;
140:
141: public GetHeadersPrivilegedAction(String name) {
142: this .name = name;
143: }
144:
145: public Object run() {
146: return request.getHeaders(name);
147: }
148: }
149:
150: private final class GetHeaderNamesPrivilegedAction implements
151: PrivilegedAction {
152:
153: public Object run() {
154: return request.getHeaderNames();
155: }
156: }
157:
158: private final class GetLocalePrivilegedAction implements
159: PrivilegedAction {
160:
161: public Object run() {
162: return request.getLocale();
163: }
164: }
165:
166: private final class GetLocalesPrivilegedAction implements
167: PrivilegedAction {
168:
169: public Object run() {
170: return request.getLocales();
171: }
172: }
173:
174: private final class GetSessionPrivilegedAction implements
175: PrivilegedAction {
176:
177: private boolean create;
178:
179: public GetSessionPrivilegedAction(boolean create) {
180: this .create = create;
181: }
182:
183: public Object run() {
184: return request.getSession(create);
185: }
186: }
187:
188: // ----------------------------------------------------------- Constructors
189:
190: /**
191: * Construct a wrapper for the specified request.
192: *
193: * @param request The request to be wrapped
194: */
195: public RequestFacade(Request request) {
196:
197: this .request = request;
198:
199: }
200:
201: // ----------------------------------------------------- Instance Variables
202:
203: /**
204: * The wrapped request.
205: */
206: protected Request request = null;
207:
208: /**
209: * The string manager for this package.
210: */
211: protected static StringManager sm = StringManager
212: .getManager(Constants.Package);
213:
214: // --------------------------------------------------------- Public Methods
215:
216: /**
217: * Clear facade.
218: */
219: public void clear() {
220: request = null;
221: }
222:
223: /**
224: * Prevent cloning the facade.
225: */
226: protected Object clone() throws CloneNotSupportedException {
227: throw new CloneNotSupportedException();
228: }
229:
230: // ------------------------------------------------- ServletRequest Methods
231:
232: public Object getAttribute(String name) {
233:
234: if (request == null) {
235: throw new IllegalStateException(sm
236: .getString("requestFacade.nullRequest"));
237: }
238:
239: return request.getAttribute(name);
240: }
241:
242: public Enumeration getAttributeNames() {
243:
244: if (request == null) {
245: throw new IllegalStateException(sm
246: .getString("requestFacade.nullRequest"));
247: }
248:
249: if (Globals.IS_SECURITY_ENABLED) {
250: return (Enumeration) AccessController
251: .doPrivileged(new GetAttributePrivilegedAction());
252: } else {
253: return request.getAttributeNames();
254: }
255: }
256:
257: public String getCharacterEncoding() {
258:
259: if (request == null) {
260: throw new IllegalStateException(sm
261: .getString("requestFacade.nullRequest"));
262: }
263:
264: if (Globals.IS_SECURITY_ENABLED) {
265: return (String) AccessController
266: .doPrivileged(new GetCharacterEncodingPrivilegedAction());
267: } else {
268: return request.getCharacterEncoding();
269: }
270: }
271:
272: public void setCharacterEncoding(String env)
273: throws java.io.UnsupportedEncodingException {
274:
275: if (request == null) {
276: throw new IllegalStateException(sm
277: .getString("requestFacade.nullRequest"));
278: }
279:
280: request.setCharacterEncoding(env);
281: }
282:
283: public int getContentLength() {
284:
285: if (request == null) {
286: throw new IllegalStateException(sm
287: .getString("requestFacade.nullRequest"));
288: }
289:
290: return request.getContentLength();
291: }
292:
293: public String getContentType() {
294:
295: if (request == null) {
296: throw new IllegalStateException(sm
297: .getString("requestFacade.nullRequest"));
298: }
299:
300: return request.getContentType();
301: }
302:
303: public ServletInputStream getInputStream() throws IOException {
304:
305: if (request == null) {
306: throw new IllegalStateException(sm
307: .getString("requestFacade.nullRequest"));
308: }
309:
310: return request.getInputStream();
311: }
312:
313: public String getParameter(String name) {
314:
315: if (request == null) {
316: throw new IllegalStateException(sm
317: .getString("requestFacade.nullRequest"));
318: }
319:
320: if (Globals.IS_SECURITY_ENABLED) {
321: return (String) AccessController
322: .doPrivileged(new GetParameterPrivilegedAction(name));
323: } else {
324: return request.getParameter(name);
325: }
326: }
327:
328: public Enumeration getParameterNames() {
329:
330: if (request == null) {
331: throw new IllegalStateException(sm
332: .getString("requestFacade.nullRequest"));
333: }
334:
335: if (Globals.IS_SECURITY_ENABLED) {
336: return (Enumeration) AccessController
337: .doPrivileged(new GetParameterNamesPrivilegedAction());
338: } else {
339: return request.getParameterNames();
340: }
341: }
342:
343: public String[] getParameterValues(String name) {
344:
345: if (request == null) {
346: throw new IllegalStateException(sm
347: .getString("requestFacade.nullRequest"));
348: }
349:
350: String[] ret = null;
351:
352: /*
353: * Clone the returned array only if there is a security manager
354: * in place, so that performance won't suffer in the nonsecure case
355: */
356: if (SecurityUtil.isPackageProtectionEnabled()) {
357: ret = (String[]) AccessController
358: .doPrivileged(new GetParameterValuePrivilegedAction(
359: name));
360: if (ret != null) {
361: ret = (String[]) ret.clone();
362: }
363: } else {
364: ret = request.getParameterValues(name);
365: }
366:
367: return ret;
368: }
369:
370: public Map getParameterMap() {
371:
372: if (request == null) {
373: throw new IllegalStateException(sm
374: .getString("requestFacade.nullRequest"));
375: }
376:
377: if (Globals.IS_SECURITY_ENABLED) {
378: return (Map) AccessController
379: .doPrivileged(new GetParameterMapPrivilegedAction());
380: } else {
381: return request.getParameterMap();
382: }
383: }
384:
385: public String getProtocol() {
386:
387: if (request == null) {
388: throw new IllegalStateException(sm
389: .getString("requestFacade.nullRequest"));
390: }
391:
392: return request.getProtocol();
393: }
394:
395: public String getScheme() {
396:
397: if (request == null) {
398: throw new IllegalStateException(sm
399: .getString("requestFacade.nullRequest"));
400: }
401:
402: return request.getScheme();
403: }
404:
405: public String getServerName() {
406:
407: if (request == null) {
408: throw new IllegalStateException(sm
409: .getString("requestFacade.nullRequest"));
410: }
411:
412: return request.getServerName();
413: }
414:
415: public int getServerPort() {
416:
417: if (request == null) {
418: throw new IllegalStateException(sm
419: .getString("requestFacade.nullRequest"));
420: }
421:
422: return request.getServerPort();
423: }
424:
425: public BufferedReader getReader() throws IOException {
426:
427: if (request == null) {
428: throw new IllegalStateException(sm
429: .getString("requestFacade.nullRequest"));
430: }
431:
432: return request.getReader();
433: }
434:
435: public String getRemoteAddr() {
436:
437: if (request == null) {
438: throw new IllegalStateException(sm
439: .getString("requestFacade.nullRequest"));
440: }
441:
442: return request.getRemoteAddr();
443: }
444:
445: public String getRemoteHost() {
446:
447: if (request == null) {
448: throw new IllegalStateException(sm
449: .getString("requestFacade.nullRequest"));
450: }
451:
452: return request.getRemoteHost();
453: }
454:
455: public void setAttribute(String name, Object o) {
456:
457: if (request == null) {
458: throw new IllegalStateException(sm
459: .getString("requestFacade.nullRequest"));
460: }
461:
462: request.setAttribute(name, o);
463: }
464:
465: public void removeAttribute(String name) {
466:
467: if (request == null) {
468: throw new IllegalStateException(sm
469: .getString("requestFacade.nullRequest"));
470: }
471:
472: request.removeAttribute(name);
473: }
474:
475: public Locale getLocale() {
476:
477: if (request == null) {
478: throw new IllegalStateException(sm
479: .getString("requestFacade.nullRequest"));
480: }
481:
482: if (Globals.IS_SECURITY_ENABLED) {
483: return (Locale) AccessController
484: .doPrivileged(new GetLocalePrivilegedAction());
485: } else {
486: return request.getLocale();
487: }
488: }
489:
490: public Enumeration getLocales() {
491:
492: if (request == null) {
493: throw new IllegalStateException(sm
494: .getString("requestFacade.nullRequest"));
495: }
496:
497: if (Globals.IS_SECURITY_ENABLED) {
498: return (Enumeration) AccessController
499: .doPrivileged(new GetLocalesPrivilegedAction());
500: } else {
501: return request.getLocales();
502: }
503: }
504:
505: public boolean isSecure() {
506:
507: if (request == null) {
508: throw new IllegalStateException(sm
509: .getString("requestFacade.nullRequest"));
510: }
511:
512: return request.isSecure();
513: }
514:
515: public RequestDispatcher getRequestDispatcher(String path) {
516:
517: if (request == null) {
518: throw new IllegalStateException(sm
519: .getString("requestFacade.nullRequest"));
520: }
521:
522: if (Globals.IS_SECURITY_ENABLED) {
523: return (RequestDispatcher) AccessController
524: .doPrivileged(new GetRequestDispatcherPrivilegedAction(
525: path));
526: } else {
527: return request.getRequestDispatcher(path);
528: }
529: }
530:
531: public String getRealPath(String path) {
532:
533: if (request == null) {
534: throw new IllegalStateException(sm
535: .getString("requestFacade.nullRequest"));
536: }
537:
538: return request.getRealPath(path);
539: }
540:
541: public String getAuthType() {
542:
543: if (request == null) {
544: throw new IllegalStateException(sm
545: .getString("requestFacade.nullRequest"));
546: }
547:
548: return request.getAuthType();
549: }
550:
551: public Cookie[] getCookies() {
552:
553: if (request == null) {
554: throw new IllegalStateException(sm
555: .getString("requestFacade.nullRequest"));
556: }
557:
558: Cookie[] ret = null;
559:
560: /*
561: * Clone the returned array only if there is a security manager
562: * in place, so that performance won't suffer in the nonsecure case
563: */
564: if (SecurityUtil.isPackageProtectionEnabled()) {
565: ret = (Cookie[]) AccessController
566: .doPrivileged(new GetCookiesPrivilegedAction());
567: if (ret != null) {
568: ret = (Cookie[]) ret.clone();
569: }
570: } else {
571: ret = request.getCookies();
572: }
573:
574: return ret;
575: }
576:
577: public long getDateHeader(String name) {
578:
579: if (request == null) {
580: throw new IllegalStateException(sm
581: .getString("requestFacade.nullRequest"));
582: }
583:
584: return request.getDateHeader(name);
585: }
586:
587: public String getHeader(String name) {
588:
589: if (request == null) {
590: throw new IllegalStateException(sm
591: .getString("requestFacade.nullRequest"));
592: }
593:
594: return request.getHeader(name);
595: }
596:
597: public Enumeration getHeaders(String name) {
598:
599: if (request == null) {
600: throw new IllegalStateException(sm
601: .getString("requestFacade.nullRequest"));
602: }
603:
604: if (Globals.IS_SECURITY_ENABLED) {
605: return (Enumeration) AccessController
606: .doPrivileged(new GetHeadersPrivilegedAction(name));
607: } else {
608: return request.getHeaders(name);
609: }
610: }
611:
612: public Enumeration getHeaderNames() {
613:
614: if (request == null) {
615: throw new IllegalStateException(sm
616: .getString("requestFacade.nullRequest"));
617: }
618:
619: if (Globals.IS_SECURITY_ENABLED) {
620: return (Enumeration) AccessController
621: .doPrivileged(new GetHeaderNamesPrivilegedAction());
622: } else {
623: return request.getHeaderNames();
624: }
625: }
626:
627: public int getIntHeader(String name) {
628:
629: if (request == null) {
630: throw new IllegalStateException(sm
631: .getString("requestFacade.nullRequest"));
632: }
633:
634: return request.getIntHeader(name);
635: }
636:
637: public String getMethod() {
638:
639: if (request == null) {
640: throw new IllegalStateException(sm
641: .getString("requestFacade.nullRequest"));
642: }
643:
644: return request.getMethod();
645: }
646:
647: public String getPathInfo() {
648:
649: if (request == null) {
650: throw new IllegalStateException(sm
651: .getString("requestFacade.nullRequest"));
652: }
653:
654: return request.getPathInfo();
655: }
656:
657: public String getPathTranslated() {
658:
659: if (request == null) {
660: throw new IllegalStateException(sm
661: .getString("requestFacade.nullRequest"));
662: }
663:
664: return request.getPathTranslated();
665: }
666:
667: public String getContextPath() {
668:
669: if (request == null) {
670: throw new IllegalStateException(sm
671: .getString("requestFacade.nullRequest"));
672: }
673:
674: return request.getContextPath();
675: }
676:
677: public String getQueryString() {
678:
679: if (request == null) {
680: throw new IllegalStateException(sm
681: .getString("requestFacade.nullRequest"));
682: }
683:
684: return request.getQueryString();
685: }
686:
687: public String getRemoteUser() {
688:
689: if (request == null) {
690: throw new IllegalStateException(sm
691: .getString("requestFacade.nullRequest"));
692: }
693:
694: return request.getRemoteUser();
695: }
696:
697: public boolean isUserInRole(String role) {
698:
699: if (request == null) {
700: throw new IllegalStateException(sm
701: .getString("requestFacade.nullRequest"));
702: }
703:
704: return request.isUserInRole(role);
705: }
706:
707: public java.security.Principal getUserPrincipal() {
708:
709: if (request == null) {
710: throw new IllegalStateException(sm
711: .getString("requestFacade.nullRequest"));
712: }
713:
714: return request.getUserPrincipal();
715: }
716:
717: public String getRequestedSessionId() {
718:
719: if (request == null) {
720: throw new IllegalStateException(sm
721: .getString("requestFacade.nullRequest"));
722: }
723:
724: return request.getRequestedSessionId();
725: }
726:
727: public String getRequestURI() {
728:
729: if (request == null) {
730: throw new IllegalStateException(sm
731: .getString("requestFacade.nullRequest"));
732: }
733:
734: return request.getRequestURI();
735: }
736:
737: public StringBuffer getRequestURL() {
738:
739: if (request == null) {
740: throw new IllegalStateException(sm
741: .getString("requestFacade.nullRequest"));
742: }
743:
744: return request.getRequestURL();
745: }
746:
747: public String getServletPath() {
748:
749: if (request == null) {
750: throw new IllegalStateException(sm
751: .getString("requestFacade.nullRequest"));
752: }
753:
754: return request.getServletPath();
755: }
756:
757: public HttpSession getSession(boolean create) {
758:
759: if (request == null) {
760: throw new IllegalStateException(sm
761: .getString("requestFacade.nullRequest"));
762: }
763:
764: if (SecurityUtil.isPackageProtectionEnabled()) {
765: return (HttpSession) AccessController
766: .doPrivileged(new GetSessionPrivilegedAction(create));
767: } else {
768: return request.getSession(create);
769: }
770: }
771:
772: public HttpSession getSession() {
773:
774: if (request == null) {
775: throw new IllegalStateException(sm
776: .getString("requestFacade.nullRequest"));
777: }
778:
779: return getSession(true);
780: }
781:
782: public boolean isRequestedSessionIdValid() {
783:
784: if (request == null) {
785: throw new IllegalStateException(sm
786: .getString("requestFacade.nullRequest"));
787: }
788:
789: return request.isRequestedSessionIdValid();
790: }
791:
792: public boolean isRequestedSessionIdFromCookie() {
793:
794: if (request == null) {
795: throw new IllegalStateException(sm
796: .getString("requestFacade.nullRequest"));
797: }
798:
799: return request.isRequestedSessionIdFromCookie();
800: }
801:
802: public boolean isRequestedSessionIdFromURL() {
803:
804: if (request == null) {
805: throw new IllegalStateException(sm
806: .getString("requestFacade.nullRequest"));
807: }
808:
809: return request.isRequestedSessionIdFromURL();
810: }
811:
812: public boolean isRequestedSessionIdFromUrl() {
813:
814: if (request == null) {
815: throw new IllegalStateException(sm
816: .getString("requestFacade.nullRequest"));
817: }
818:
819: return request.isRequestedSessionIdFromURL();
820: }
821:
822: public String getLocalAddr() {
823:
824: if (request == null) {
825: throw new IllegalStateException(sm
826: .getString("requestFacade.nullRequest"));
827: }
828:
829: return request.getLocalAddr();
830: }
831:
832: public String getLocalName() {
833:
834: if (request == null) {
835: throw new IllegalStateException(sm
836: .getString("requestFacade.nullRequest"));
837: }
838:
839: return request.getLocalName();
840: }
841:
842: public int getLocalPort() {
843:
844: if (request == null) {
845: throw new IllegalStateException(sm
846: .getString("requestFacade.nullRequest"));
847: }
848:
849: return request.getLocalPort();
850: }
851:
852: public int getRemotePort() {
853:
854: if (request == null) {
855: throw new IllegalStateException(sm
856: .getString("requestFacade.nullRequest"));
857: }
858:
859: return request.getRemotePort();
860: }
861:
862: }
|