001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.coyote.tomcat5;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.security.AccessController;
022: import java.security.PrivilegedAction;
023: import java.util.Enumeration;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import javax.servlet.RequestDispatcher;
028: import javax.servlet.ServletInputStream;
029: import javax.servlet.http.Cookie;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpSession;
032:
033: import org.apache.catalina.connector.RequestFacade;
034:
035: /**
036: * Facade class that wraps a Coyote request object.
037: * All methods are delegated to the wrapped request.
038: *
039: * @author Craig R. McClanahan
040: * @author Remy Maucherat
041: * @author Jean-Francois Arcand
042: * @version $Revision: 1.7 $ $Date: 2004/03/02 17:36:55 $
043: */
044:
045: public class CoyoteRequestFacade extends RequestFacade implements
046: HttpServletRequest {
047:
048: // ----------------------------------------------------------- DoPrivileged
049:
050: private final class GetAttributePrivilegedAction implements
051: PrivilegedAction {
052:
053: public Object run() {
054: return request.getAttributeNames();
055: }
056: }
057:
058: private final class GetParameterMapPrivilegedAction implements
059: PrivilegedAction {
060:
061: public Object run() {
062: return request.getParameterMap();
063: }
064: }
065:
066: private final class GetRequestDispatcherPrivilegedAction implements
067: PrivilegedAction {
068: private String path;
069:
070: public GetRequestDispatcherPrivilegedAction(String path) {
071: this .path = path;
072: }
073:
074: public Object run() {
075: return request.getRequestDispatcher(path);
076: }
077: }
078:
079: private final class GetParameterPrivilegedAction implements
080: PrivilegedAction {
081: public String name;
082:
083: public GetParameterPrivilegedAction(String name) {
084: this .name = name;
085: }
086:
087: public Object run() {
088: return request.getParameter(name);
089: }
090: }
091:
092: private final class GetParameterNamesPrivilegedAction implements
093: PrivilegedAction {
094:
095: public Object run() {
096: return request.getParameterNames();
097: }
098: }
099:
100: private final class GetParameterValuePrivilegedAction implements
101: PrivilegedAction {
102: public String name;
103:
104: public GetParameterValuePrivilegedAction(String name) {
105: this .name = name;
106: }
107:
108: public Object run() {
109: return request.getParameterValues(name);
110: }
111: }
112:
113: private final class GetCookiesPrivilegedAction implements
114: PrivilegedAction {
115:
116: public Object run() {
117: return request.getCookies();
118: }
119: }
120:
121: private final class GetCharacterEncodingPrivilegedAction implements
122: PrivilegedAction {
123:
124: public Object run() {
125: return request.getCharacterEncoding();
126: }
127: }
128:
129: private final class GetHeadersPrivilegedAction implements
130: PrivilegedAction {
131: private String name;
132:
133: public GetHeadersPrivilegedAction(String name) {
134: this .name = name;
135: }
136:
137: public Object run() {
138: return request.getHeaders(name);
139: }
140: }
141:
142: private final class GetHeaderNamesPrivilegedAction implements
143: PrivilegedAction {
144:
145: public Object run() {
146: return request.getHeaderNames();
147: }
148: }
149:
150: private final class GetLocalePrivilegedAction implements
151: PrivilegedAction {
152:
153: public Object run() {
154: return request.getLocale();
155: }
156: }
157:
158: private final class GetLocalesPrivilegedAction implements
159: PrivilegedAction {
160:
161: public Object run() {
162: return request.getLocales();
163: }
164: }
165:
166: private final class GetSessionPrivilegedAction implements
167: PrivilegedAction {
168: private boolean create;
169:
170: public GetSessionPrivilegedAction(boolean create) {
171: this .create = create;
172: }
173:
174: public Object run() {
175: return request.getSession(create);
176: }
177: }
178:
179: // ----------------------------------------------------------- Constructors
180:
181: /**
182: * Construct a wrapper for the specified request.
183: *
184: * @param request The request to be wrapped
185: */
186: public CoyoteRequestFacade(CoyoteRequest request) {
187:
188: super (request);
189: this .request = request;
190:
191: }
192:
193: // ----------------------------------------------------- Instance Variables
194:
195: /**
196: * The wrapped request.
197: */
198: protected CoyoteRequest request = null;
199:
200: // --------------------------------------------------------- Public Methods
201:
202: /**
203: * Clear facade.
204: */
205: public void clear() {
206: request = null;
207: }
208:
209: // ------------------------------------------------- ServletRequest Methods
210:
211: public Object getAttribute(String name) {
212: return request.getAttribute(name);
213: }
214:
215: public Enumeration getAttributeNames() {
216: if (System.getSecurityManager() != null) {
217: return (Enumeration) AccessController
218: .doPrivileged(new GetAttributePrivilegedAction());
219: } else {
220: return request.getAttributeNames();
221: }
222: }
223:
224: public String getCharacterEncoding() {
225: if (System.getSecurityManager() != null) {
226: return (String) AccessController
227: .doPrivileged(new GetCharacterEncodingPrivilegedAction());
228: } else {
229: return request.getCharacterEncoding();
230: }
231: }
232:
233: public void setCharacterEncoding(String env)
234: throws java.io.UnsupportedEncodingException {
235: request.setCharacterEncoding(env);
236: }
237:
238: public int getContentLength() {
239: return request.getContentLength();
240: }
241:
242: public String getContentType() {
243: return request.getContentType();
244: }
245:
246: public ServletInputStream getInputStream() throws IOException {
247: return request.getInputStream();
248: }
249:
250: public String getParameter(String name) {
251: if (System.getSecurityManager() != null) {
252: return (String) AccessController
253: .doPrivileged(new GetParameterPrivilegedAction(name));
254: } else {
255: return request.getParameter(name);
256: }
257: }
258:
259: public Enumeration getParameterNames() {
260: if (System.getSecurityManager() != null) {
261: return (Enumeration) AccessController
262: .doPrivileged(new GetParameterNamesPrivilegedAction());
263: } else {
264: return request.getParameterNames();
265: }
266: }
267:
268: public String[] getParameterValues(String name) {
269:
270: String[] ret = null;
271:
272: /*
273: * Clone the returned array only if there is a security manager
274: * in place, so that performance won't suffer in the nonsecure case
275: */
276: if (System.getSecurityManager() != null) {
277: ret = (String[]) AccessController
278: .doPrivileged(new GetParameterValuePrivilegedAction(
279: name));
280: if (ret != null) {
281: ret = (String[]) ret.clone();
282: }
283: } else {
284: ret = request.getParameterValues(name);
285: }
286:
287: return ret;
288: }
289:
290: public Map getParameterMap() {
291: if (System.getSecurityManager() != null) {
292: return (Map) AccessController
293: .doPrivileged(new GetParameterMapPrivilegedAction());
294: } else {
295: return request.getParameterMap();
296: }
297: }
298:
299: public String getProtocol() {
300: return request.getProtocol();
301: }
302:
303: public String getScheme() {
304: return request.getScheme();
305: }
306:
307: public String getServerName() {
308: return request.getServerName();
309: }
310:
311: public int getServerPort() {
312: return request.getServerPort();
313: }
314:
315: public BufferedReader getReader() throws IOException {
316: return request.getReader();
317: }
318:
319: public String getRemoteAddr() {
320: return request.getRemoteAddr();
321: }
322:
323: public String getRemoteHost() {
324: return request.getRemoteHost();
325: }
326:
327: public void setAttribute(String name, Object o) {
328: request.setAttribute(name, o);
329: }
330:
331: public void removeAttribute(String name) {
332: request.removeAttribute(name);
333: }
334:
335: public Locale getLocale() {
336: if (System.getSecurityManager() != null) {
337: return (Locale) AccessController
338: .doPrivileged(new GetLocalePrivilegedAction());
339: } else {
340: return request.getLocale();
341: }
342: }
343:
344: public Enumeration getLocales() {
345: if (System.getSecurityManager() != null) {
346: return (Enumeration) AccessController
347: .doPrivileged(new GetLocalesPrivilegedAction());
348: } else {
349: return request.getLocales();
350: }
351: }
352:
353: public boolean isSecure() {
354: return request.isSecure();
355: }
356:
357: public RequestDispatcher getRequestDispatcher(String path) {
358: if (System.getSecurityManager() != null) {
359: return (RequestDispatcher) AccessController
360: .doPrivileged(new GetRequestDispatcherPrivilegedAction(
361: path));
362: } else {
363: return request.getRequestDispatcher(path);
364: }
365: }
366:
367: public String getRealPath(String path) {
368: return request.getRealPath(path);
369: }
370:
371: public String getAuthType() {
372: return request.getAuthType();
373: }
374:
375: public Cookie[] getCookies() {
376:
377: Cookie[] ret = null;
378:
379: /*
380: * Clone the returned array only if there is a security manager
381: * in place, so that performance won't suffer in the nonsecure case
382: */
383: if (System.getSecurityManager() != null) {
384: ret = (Cookie[]) AccessController
385: .doPrivileged(new GetCookiesPrivilegedAction());
386: if (ret != null) {
387: ret = (Cookie[]) ret.clone();
388: }
389: } else {
390: ret = request.getCookies();
391: }
392:
393: return ret;
394: }
395:
396: public long getDateHeader(String name) {
397: return request.getDateHeader(name);
398: }
399:
400: public String getHeader(String name) {
401: return request.getHeader(name);
402: }
403:
404: public Enumeration getHeaders(String name) {
405: if (System.getSecurityManager() != null) {
406: return (Enumeration) AccessController
407: .doPrivileged(new GetHeadersPrivilegedAction(name));
408: } else {
409: return request.getHeaders(name);
410: }
411: }
412:
413: public Enumeration getHeaderNames() {
414: if (System.getSecurityManager() != null) {
415: return (Enumeration) AccessController
416: .doPrivileged(new GetHeaderNamesPrivilegedAction());
417: } else {
418: return request.getHeaderNames();
419: }
420: }
421:
422: public int getIntHeader(String name) {
423: return request.getIntHeader(name);
424: }
425:
426: public String getMethod() {
427: return request.getMethod();
428: }
429:
430: public String getPathInfo() {
431: return request.getPathInfo();
432: }
433:
434: public String getPathTranslated() {
435: return request.getPathTranslated();
436: }
437:
438: public String getContextPath() {
439: return request.getContextPath();
440: }
441:
442: public String getQueryString() {
443: return request.getQueryString();
444: }
445:
446: public String getRemoteUser() {
447: return request.getRemoteUser();
448: }
449:
450: public boolean isUserInRole(String role) {
451: return request.isUserInRole(role);
452: }
453:
454: public java.security.Principal getUserPrincipal() {
455: return request.getUserPrincipal();
456: }
457:
458: public String getRequestedSessionId() {
459: return request.getRequestedSessionId();
460: }
461:
462: public String getRequestURI() {
463: return request.getRequestURI();
464: }
465:
466: public StringBuffer getRequestURL() {
467: return request.getRequestURL();
468: }
469:
470: public String getServletPath() {
471: return request.getServletPath();
472: }
473:
474: public HttpSession getSession(boolean create) {
475:
476: if (System.getSecurityManager() != null) {
477: return (HttpSession) AccessController
478: .doPrivileged(new GetSessionPrivilegedAction(create));
479: } else {
480: return request.getSession(create);
481: }
482: }
483:
484: public HttpSession getSession() {
485: return getSession(true);
486: }
487:
488: public boolean isRequestedSessionIdValid() {
489: return request.isRequestedSessionIdValid();
490: }
491:
492: public boolean isRequestedSessionIdFromCookie() {
493: return request.isRequestedSessionIdFromCookie();
494: }
495:
496: public boolean isRequestedSessionIdFromURL() {
497: return request.isRequestedSessionIdFromURL();
498: }
499:
500: public boolean isRequestedSessionIdFromUrl() {
501: return request.isRequestedSessionIdFromURL();
502: }
503:
504: }
|