001: package com.meterware.servletunit;
002:
003: /********************************************************************************************************************
004: * $Id: DispatchedRequestWrapper.java,v 1.1 2003/02/21 15:43:59 russgold Exp $
005: *
006: * Copyright (c) 2003, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.util.Enumeration;
024: import java.util.Map;
025: import java.util.Hashtable;
026:
027: import javax.servlet.http.HttpServletRequestWrapper;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.RequestDispatcher;
030:
031: /**
032: * This class represents a request dispatched via a RequestDispatcherImpl.
033: **/
034: class DispatchedRequestWrapper extends HttpServletRequestWrapper {
035:
036: /** Request-specific information, including parameters and paths. **/
037: private RequestContext _requestContext;
038:
039: /** The request being wrapped. **/
040: private HttpServletRequest _baseRequest;
041:
042: static HttpServletRequest createIncludeRequestWrapper(
043: HttpServletRequest request, RequestDispatcher dispatcher) {
044: return new IncludeRequestWrapper(request, dispatcher);
045: }
046:
047: static HttpServletRequest createForwardRequestWrapper(
048: HttpServletRequest request, RequestDispatcher dispatcher) {
049: return new ForwardRequestWrapper(request, dispatcher);
050: }
051:
052: DispatchedRequestWrapper(HttpServletRequest baseRequest,
053: RequestDispatcher dispatcher) {
054: super (baseRequest);
055: _baseRequest = baseRequest;
056: _requestContext = (RequestContext) dispatcher;
057: _requestContext.setParentRequest(baseRequest);
058: }
059:
060: HttpServletRequest getBaseRequest() {
061: return _baseRequest;
062: }
063:
064: public String getParameter(String s) {
065: return _requestContext.getParameter(s);
066: }
067:
068: public Enumeration getParameterNames() {
069: return _requestContext.getParameterNames();
070: }
071:
072: public String[] getParameterValues(String s) {
073: return _requestContext.getParameterValues(s);
074: }
075:
076: public Map getParameterMap() {
077: return _requestContext.getParameterMap();
078: }
079:
080: }
081:
082: class IncludeRequestWrapper extends DispatchedRequestWrapper {
083:
084: final static String REQUEST_URI = "javax.servlet.include.request_uri";
085: final static String CONTEXT_PATH = "javax.servlet.include.context_path";
086: final static String SERVLET_PATH = "javax.servlet.include.servlet_path";
087: final static String PATH_INFO = "javax.servlet.include.path_info";
088: final static String QUERY_STRING = "javax.servlet.include.query_string";
089:
090: private Hashtable _attributes = new Hashtable();
091:
092: IncludeRequestWrapper(HttpServletRequest request,
093: RequestDispatcher dispatcher) {
094: super (request, dispatcher);
095: _attributes.put(REQUEST_URI,
096: ((RequestDispatcherImpl) dispatcher).getRequestURI());
097: _attributes.put(CONTEXT_PATH, request.getContextPath());
098: _attributes.put(SERVLET_PATH,
099: ((RequestDispatcherImpl) dispatcher)
100: .getServletMetaData().getServletPath());
101: final String pathInfo = ((RequestDispatcherImpl) dispatcher)
102: .getServletMetaData().getPathInfo();
103: if (pathInfo != null)
104: _attributes.put(PATH_INFO, pathInfo);
105: }
106:
107: public Object getAttribute(String s) {
108: Object result = _attributes.get(s);
109: return (result != null) ? result : super .getAttribute(s);
110: }
111:
112: }
113:
114: class ForwardRequestWrapper extends DispatchedRequestWrapper {
115:
116: private RequestDispatcherImpl _requestContext;
117:
118: ForwardRequestWrapper(HttpServletRequest request,
119: RequestDispatcher dispatcher) {
120: super (request, dispatcher);
121: _requestContext = (RequestDispatcherImpl) dispatcher;
122: }
123:
124: public String getRequestURI() {
125: return _requestContext.getRequestURI();
126: }
127:
128: public String getQueryString() {
129: return super .getQueryString();
130: }
131:
132: public String getServletPath() {
133: return _requestContext.getServletMetaData().getServletPath();
134: }
135:
136: public String getPathInfo() {
137: return _requestContext.getServletMetaData().getPathInfo();
138: }
139: }
|