001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.connection;
030:
031: import com.caucho.server.session.SessionManager;
032: import com.caucho.server.webapp.WebApp;
033: import com.caucho.util.FreeList;
034: import com.caucho.util.L10N;
035: import com.caucho.vfs.ReadStream;
036:
037: import javax.servlet.ServletException;
038: import javax.servlet.ServletRequest;
039: import javax.servlet.ServletRequestWrapper;
040: import javax.servlet.http.Cookie;
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpServletResponse;
043: import javax.servlet.http.HttpSession;
044: import java.io.IOException;
045: import java.util.HashMap;
046:
047: /**
048: * Any requests that depends on an underlying request, like
049: * include() requests or adapters for other servlet engines.
050: */
051: public class RequestAdapter extends RequestWrapper implements
052: CauchoRequest {
053: static final L10N L = new L10N(RequestAdapter.class);
054:
055: static final int MAX_DEPTH = 64;
056:
057: public static String REQUEST_URI = "javax.servlet.include.request_uri";
058: public static String CONTEXT_PATH = "javax.servlet.include.context_path";
059: public static String SERVLET_PATH = "javax.servlet.include.servlet_path";
060: public static String PATH_INFO = "javax.servlet.include.path_info";
061: public static String QUERY_STRING = "javax.servlet.include.query_string";
062:
063: public static String STATUS_CODE = "javax.servlet.error.status_code";
064: public static String EXCEPTION_TYPE = "javax.servlet.error.exception_type";
065: public static String MESSAGE = "javax.servlet.error.message";
066: public static String EXCEPTION = "javax.servlet.error.exception";
067: public static String ERROR_URI = "javax.servlet.error.request_uri";
068: public static String SERVLET_NAME = "javax.servlet.error.servlet_name";
069:
070: public static String JSP_EXCEPTION = "javax.servlet.jsp.jspException";
071:
072: public static String SHUTDOWN = "com.caucho.shutdown";
073:
074: private static final FreeList<RequestAdapter> _freeList = new FreeList<RequestAdapter>(
075: 16);
076:
077: // for real adapters
078: private WebApp _webApp;
079: private HttpServletResponse _response;
080:
081: private HashMap<String, String> _roleMap;
082:
083: protected RequestAdapter() {
084: super (null);
085: }
086:
087: protected RequestAdapter(HttpServletRequest request, WebApp app) {
088: super (request);
089:
090: _webApp = app;
091: }
092:
093: /**
094: * Creates a new RequestAdapter.
095: */
096: public static RequestAdapter create(HttpServletRequest request,
097: WebApp app) {
098: RequestAdapter reqAdapt = _freeList.allocate();
099:
100: if (reqAdapt == null)
101: return new RequestAdapter(request, app);
102: else {
103: reqAdapt.setRequest(request);
104: reqAdapt._webApp = app;
105:
106: return reqAdapt;
107: }
108: }
109:
110: /**
111: * Creates a new RequestAdapter.
112: */
113: public static RequestAdapter create() {
114: RequestAdapter reqAdapt = _freeList.allocate();
115:
116: if (reqAdapt != null)
117: return reqAdapt;
118: else
119: return new RequestAdapter();
120: }
121:
122: public void init(HttpServletRequest request,
123: HttpServletResponse response, WebApp app)
124: throws ServletException {
125: setRequest(request);
126:
127: _response = response;
128: _webApp = app;
129:
130: if (request == this
131: || request instanceof CauchoRequest
132: && ((CauchoRequest) request).getRequestDepth(0) > MAX_DEPTH) {
133: throw new ServletException(L.l(
134: "too many servlet includes `{0}'", request
135: .getRequestURI()));
136: }
137: }
138:
139: public boolean isTop() {
140: return false;
141: }
142:
143: public void setWebApp(WebApp app) {
144: _webApp = app;
145: }
146:
147: protected HttpServletResponse getResponse() {
148: return _response;
149: }
150:
151: public void setResponse(CauchoResponse response) {
152: _response = response;
153: }
154:
155: /**
156: * Returns the underlying read stream.
157: */
158: public ReadStream getStream() throws IOException {
159: if (getRequest() instanceof CauchoRequest)
160: return ((CauchoRequest) getRequest()).getStream();
161: else
162: return null;
163: }
164:
165: /**
166: * Returns the URI for the current page: included or top-level.
167: */
168: public String getPageURI() {
169: String uri = (String) getAttribute(REQUEST_URI);
170:
171: if (uri != null)
172: return uri;
173: else
174: return getRequestURI();
175: }
176:
177: public static String getPageURI(HttpServletRequest request) {
178: String uri = (String) request.getAttribute(REQUEST_URI);
179:
180: if (uri != null)
181: return uri;
182: else
183: return request.getRequestURI();
184: }
185:
186: public String getPageContextPath() {
187: String contextPath = (String) getAttribute(CONTEXT_PATH);
188:
189: if (contextPath != null)
190: return contextPath;
191: else
192: return getContextPath();
193: }
194:
195: public static String getPageContextPath(HttpServletRequest request) {
196: String contextPath = (String) request
197: .getAttribute(CONTEXT_PATH);
198:
199: if (contextPath != null)
200: return contextPath;
201: else
202: return request.getContextPath();
203: }
204:
205: /**
206: * Returns the servlet-path for the current page, i.e. this will return the
207: * url of the include page, not the original request.
208: */
209: public String getPageServletPath() {
210: String servletPath = (String) getAttribute(SERVLET_PATH);
211:
212: if (servletPath != null)
213: return servletPath;
214: else
215: return getServletPath();
216: }
217:
218: /**
219: * Returns the servlet-path for the current page, i.e. this will return the
220: * url of the include page, not the original request.
221: */
222: public static String getPageServletPath(HttpServletRequest request) {
223: String servletPath = (String) request
224: .getAttribute(SERVLET_PATH);
225:
226: if (servletPath != null)
227: return servletPath;
228: else
229: return request.getServletPath();
230: }
231:
232: /**
233: * Returns the path-info for the current page, i.e. this will return the
234: * url of the include page, not the original request.
235: */
236: public String getPagePathInfo() {
237: String uri = (String) getAttribute(REQUEST_URI);
238:
239: if (uri != null)
240: return (String) getAttribute(PATH_INFO);
241: else
242: return getPathInfo();
243: }
244:
245: /**
246: * Returns the path-info for the current page, i.e. this will return the
247: * url of the include page, not the original request.
248: */
249: public static String getPagePathInfo(HttpServletRequest request) {
250: String uri = (String) request.getAttribute(REQUEST_URI);
251:
252: if (uri != null)
253: return (String) request.getAttribute(PATH_INFO);
254: else
255: return request.getPathInfo();
256: }
257:
258: /**
259: * Returns the query-string for the current page, i.e. this will return the
260: * url of the include page, not the original request.
261: */
262: public String getPageQueryString() {
263: String uri = (String) getAttribute(REQUEST_URI);
264:
265: if (uri != null)
266: return (String) getAttribute(QUERY_STRING);
267: else
268: return getQueryString();
269: }
270:
271: /**
272: * Returns the query-string for the current page, i.e. this will return the
273: * url of the include page, not the original request.
274: */
275: public static String getPageQueryString(HttpServletRequest request) {
276: String uri = (String) request.getAttribute(REQUEST_URI);
277:
278: if (uri != null)
279: return (String) request.getAttribute(QUERY_STRING);
280: else
281: return request.getQueryString();
282: }
283:
284: public int getRequestDepth(int depth) {
285: if (depth > MAX_DEPTH)
286: throw new RuntimeException(L
287: .l("too many request dispatchers"));
288:
289: ServletRequest req = getRequest();
290: while (req != null) {
291: if (req instanceof CauchoRequest)
292: return ((CauchoRequest) req).getRequestDepth(depth + 1);
293: else if (req instanceof ServletRequestWrapper) {
294: ServletRequestWrapper reqWrap = (ServletRequestWrapper) req;
295:
296: req = reqWrap.getRequest();
297: } else
298: break;
299: }
300:
301: return depth + 2;
302: }
303:
304: public void setHeader(String key, String value) {
305: }
306:
307: public WebApp getWebApp() {
308: return _webApp;
309: }
310:
311: public void setVaryCookie(String cookie) {
312: // super.setVaryCookie(cookie);
313:
314: if (getRequest() instanceof CauchoRequest)
315: ((CauchoRequest) getRequest()).setVaryCookie(cookie);
316: }
317:
318: public String getVaryCookie() {
319: // super.setVaryCookie(cookie);
320:
321: if (getRequest() instanceof CauchoRequest)
322: return ((CauchoRequest) getRequest()).getVaryCookie();
323: else
324: return null;
325: }
326:
327: public boolean getVaryCookies() {
328: // super.setVaryCookie(cookie);
329:
330: if (getRequest() instanceof CauchoRequest)
331: return ((CauchoRequest) getRequest()).getVaryCookies();
332: else
333: return false;
334: }
335:
336: public void setHasCookie() {
337: // super.setHasCookie();
338:
339: if (getRequest() instanceof CauchoRequest)
340: ((CauchoRequest) getRequest()).setHasCookie();
341: }
342:
343: public boolean getHasCookie() {
344: // super.setHasCookie();
345:
346: if (getRequest() instanceof CauchoRequest)
347: return ((CauchoRequest) getRequest()).getHasCookie();
348: else
349: return false;
350: }
351:
352: public HttpSession getMemorySession() {
353: return getSession(false);
354: }
355:
356: public HttpSession getSession(boolean create) {
357: SessionManager manager = getSessionManager();
358:
359: setVaryCookie(getCookieName(manager));
360:
361: HttpSession session = super .getSession(create);
362:
363: if (session != null)
364: setHasCookie();
365:
366: return session;
367: }
368:
369: public String getRequestedSessionId() {
370: SessionManager manager = getSessionManager();
371:
372: setVaryCookie(getCookieName(manager));
373:
374: String id = super .getRequestedSessionId();
375:
376: if (id != null)
377: setHasCookie();
378:
379: return id;
380: }
381:
382: public boolean isRequestedSessionIdValid() {
383: SessionManager manager = getSessionManager();
384:
385: setVaryCookie(getCookieName(manager));
386:
387: boolean isValid = super .isRequestedSessionIdValid();
388:
389: if (isValid)
390: setHasCookie();
391:
392: return isValid;
393: }
394:
395: public boolean isRequestedSessionIdFromCookie() {
396: SessionManager manager = getSessionManager();
397:
398: setVaryCookie(getCookieName(manager));
399:
400: boolean isValid = super .isRequestedSessionIdFromCookie();
401: if (isValid)
402: setHasCookie();
403:
404: return isValid;
405: }
406:
407: public boolean isRequestedSessionIdFromURL() {
408: SessionManager manager = getSessionManager();
409:
410: setVaryCookie(getCookieName(manager));
411:
412: boolean isValid = super .isRequestedSessionIdFromURL();
413:
414: if (isValid)
415: setHasCookie();
416:
417: return isValid;
418: }
419:
420: protected final SessionManager getSessionManager() {
421: WebApp app = getWebApp();
422: if (app != null)
423: return app.getSessionManager();
424: else
425: return null;
426: }
427:
428: protected final String getCookieName(SessionManager manager) {
429: if (isSecure())
430: return manager.getCookieName();
431: else
432: return manager.getSSLCookieName();
433: }
434:
435: public Cookie[] getCookies() {
436: // page depends on any cookie
437: setVaryCookie(null);
438:
439: Cookie[] cookies = super .getCookies();
440: if (cookies != null && cookies.length > 0)
441: setHasCookie();
442:
443: return cookies;
444: }
445:
446: public Cookie getCookie(String name) {
447: // page depends on this cookie
448: setVaryCookie(name);
449:
450: if (getRequest() instanceof CauchoRequest)
451: return ((CauchoRequest) getRequest()).getCookie(name);
452:
453: Cookie[] cookies = super .getCookies();
454: for (int i = 0; i < cookies.length; i++) {
455: if (cookies[i].getName().equals(name)) {
456: setHasCookie();
457: return cookies[i];
458: }
459: }
460:
461: return null;
462: }
463:
464: public void killKeepalive() {
465: }
466:
467: public boolean allowKeepalive() {
468: return true;
469: }
470:
471: /**
472: * Sets the role map.
473: */
474: public HashMap<String, String> setRoleMap(
475: HashMap<String, String> map) {
476: HashMap<String, String> oldMap = _roleMap;
477: _roleMap = map;
478:
479: return oldMap;
480: }
481:
482: /**
483: * Checks the isUserInRole.
484: */
485: public boolean isUserInRole(String role) {
486: if (_roleMap != null) {
487: String newRole = _roleMap.get(role);
488:
489: if (newRole != null)
490: role = newRole;
491: }
492:
493: return super .isUserInRole(role);
494: }
495:
496: public boolean authenticate() throws ServletException, IOException {
497: return true;
498: }
499:
500: /**
501: * Frees the adapter for reuse.
502: */
503: public static void free(RequestAdapter reqAdapt) {
504: reqAdapt.free();
505:
506: _freeList.free(reqAdapt);
507: }
508:
509: /**
510: * Clears the adapter.
511: */
512: protected void free() {
513: super.free();
514:
515: _webApp = null;
516: _response = null;
517: }
518: }
|