Source code: com/meterware/servletunit/DispatchedRequestWrapper.java
1 package com.meterware.servletunit;
2 /********************************************************************************************************************
3 * $Id: DispatchedRequestWrapper.java,v 1.1 2003/02/21 15:43:59 russgold Exp $
4 *
5 * Copyright (c) 2003, Russell Gold
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
16 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *******************************************************************************************************************/
22 import java.util.Enumeration;
23 import java.util.Map;
24 import java.util.Hashtable;
25
26 import javax.servlet.http.HttpServletRequestWrapper;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.RequestDispatcher;
29
30 /**
31 * This class represents a request dispatched via a RequestDispatcherImpl.
32 **/
33 class DispatchedRequestWrapper extends HttpServletRequestWrapper {
34
35 /** Request-specific information, including parameters and paths. **/
36 private RequestContext _requestContext;
37
38 /** The request being wrapped. **/
39 private HttpServletRequest _baseRequest;
40
41
42 static HttpServletRequest createIncludeRequestWrapper( HttpServletRequest request, RequestDispatcher dispatcher ) {
43 return new IncludeRequestWrapper( request, dispatcher );
44 }
45
46
47 static HttpServletRequest createForwardRequestWrapper( HttpServletRequest request, RequestDispatcher dispatcher ) {
48 return new ForwardRequestWrapper( request, dispatcher );
49 }
50
51
52 DispatchedRequestWrapper( HttpServletRequest baseRequest, RequestDispatcher dispatcher ) {
53 super( baseRequest );
54 _baseRequest = baseRequest;
55 _requestContext = (RequestContext) dispatcher;
56 _requestContext.setParentRequest( baseRequest );
57 }
58
59
60 HttpServletRequest getBaseRequest() {
61 return _baseRequest;
62 }
63
64
65 public String getParameter( String s ) {
66 return _requestContext.getParameter( s );
67 }
68
69
70 public Enumeration getParameterNames() {
71 return _requestContext.getParameterNames();
72 }
73
74
75 public String[] getParameterValues( String s ) {
76 return _requestContext.getParameterValues( s );
77 }
78
79
80 public Map getParameterMap() {
81 return _requestContext.getParameterMap();
82 }
83
84 }
85
86
87 class IncludeRequestWrapper extends DispatchedRequestWrapper {
88
89 final static String REQUEST_URI = "javax.servlet.include.request_uri";
90 final static String CONTEXT_PATH = "javax.servlet.include.context_path";
91 final static String SERVLET_PATH = "javax.servlet.include.servlet_path";
92 final static String PATH_INFO = "javax.servlet.include.path_info";
93 final static String QUERY_STRING = "javax.servlet.include.query_string";
94
95 private Hashtable _attributes = new Hashtable();
96
97
98 IncludeRequestWrapper( HttpServletRequest request, RequestDispatcher dispatcher ) {
99 super( request, dispatcher );
100 _attributes.put( REQUEST_URI, ((RequestDispatcherImpl) dispatcher ).getRequestURI() );
101 _attributes.put( CONTEXT_PATH, request.getContextPath() );
102 _attributes.put( SERVLET_PATH, ((RequestDispatcherImpl) dispatcher ).getServletMetaData().getServletPath() );
103 final String pathInfo = ((RequestDispatcherImpl) dispatcher ).getServletMetaData().getPathInfo();
104 if (pathInfo != null) _attributes.put( PATH_INFO, pathInfo );
105 }
106
107
108 public Object getAttribute( String s ) {
109 Object result = _attributes.get( s );
110 return (result != null) ? result : super.getAttribute( s );
111 }
112
113 }
114
115
116 class ForwardRequestWrapper extends DispatchedRequestWrapper {
117
118 private RequestDispatcherImpl _requestContext;
119
120 ForwardRequestWrapper( HttpServletRequest request, RequestDispatcher dispatcher ) {
121 super( request, dispatcher );
122 _requestContext = (RequestDispatcherImpl) dispatcher;
123 }
124
125
126 public String getRequestURI() {
127 return _requestContext.getRequestURI();
128 }
129
130
131 public String getQueryString() {
132 return super.getQueryString();
133 }
134
135
136 public String getServletPath() {
137 return _requestContext.getServletMetaData().getServletPath();
138 }
139
140
141 public String getPathInfo() {
142 return _requestContext.getServletMetaData().getPathInfo();
143 }
144 }
145
146
147
148