KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > http > HttpServletRequestWrapper


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal.http;
8
9
10 import java.io.File JavaDoc;
11 import javax.servlet.RequestDispatcher JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13
14 import com.inversoft.junit.URL;
15
16
17 /**
18  * <p>
19  * A dummy HTTP request object. This object allows much more
20  * flexibility with changing standards as well as changing
21  * requirements on specific projects. For example, if someone
22  * wishes to test a forward without actually doing the
23  * forward, a sub-class of this that allows them to test
24  * this.
25  * </p>
26  *
27  * <p>>
28  * Overall, this class is empty allowing all request calls to
29  * be propogated to the real request.
30  * </p>
31  *
32  * <p>
33  * This class is obsolete in J2EE 1.3 (servlet 2.3) because
34  * that spec already contains wrapper classes for the same
35  * purpose. However, these are backwards compatible and
36  * therefore will always be used
37  * </p>
38  *
39  * @author Brian Pontarelli
40  * @since 1.0
41  * @version 1.0
42  */

43 public class HttpServletRequestWrapper
44 extends javax.servlet.http.HttpServletRequestWrapper JavaDoc {
45
46     /**
47      * The spoofed URL (optional)
48      */

49     URL url;
50
51
52     /**
53      * Construct a dummy HTTP request that forwards all the calls to the original
54      * wrapped request.
55      *
56      * @param request The request to wrap inside this object
57      */

58     public HttpServletRequestWrapper(HttpServletRequest JavaDoc request) {
59         super(request);
60     }
61
62     /**
63      * Construct a dummy HTTP request that forwards all the calls to the original
64      * wrapped request.
65      *
66      * @param request The request to wrap inside this object
67      * @param url The URL to spoof
68      */

69     public HttpServletRequestWrapper(HttpServletRequest JavaDoc request, URL url) {
70         super(request);
71         this.url = url;
72     }
73
74
75     //-------------------------------------------------------------------------
76
// Spoofed URL methods
77
//-------------------------------------------------------------------------
78

79     /**
80      * If the URL is spoofed, returns the context path from it. Otherwise, returns
81      * the context path from the wrapped request.
82      *
83      * @return the context path from the spoofed URL or the wrapped request
84      */

85     public String JavaDoc getContextPath() {
86         if ((url != null) && (url.getContextPath() != null)) {
87             return url.getContextPath();
88         }
89
90         return super.getContextPath();
91     }
92
93     /**
94      * If the URL is spoofed, returns the path info from it. Otherwise, returns
95      * the path info from the wrapped request.
96      *
97      * @return the path info from the spoofed URL or the wrapped request
98      */

99     public String JavaDoc getPathInfo() {
100         if (url != null && url.getPathInfo() != null) {
101             return url.getPathInfo();
102         }
103
104         return super.getPathInfo();
105     }
106
107     /**
108      * If the URL is spoofed, returns the path translated from it. Otherwise, returns
109      * the path translated from the wrapped request.
110      *
111      * @return the path translated from the spoofed URL or the wrapped request
112      */

113     public String JavaDoc getPathTranslated() {
114         String JavaDoc pathTranslated;
115
116         if (url != null && url.getPathInfo() != null) {
117
118             String JavaDoc pathInfo = url.getPathInfo();
119
120             // If getRealPath returns null then getPathTranslated should also
121
// return null (see section SRV.4.5 of the Servlet 2.3 spec).
122
if (super.getRealPath("/") == null) {
123                 pathTranslated = null;
124             } else {
125
126                 // Compute the translated path using the root real path
127
String JavaDoc newPathInfo =
128                     (pathInfo.startsWith("/") ? pathInfo.substring(1) : pathInfo);
129
130                 if (super.getRealPath("/").endsWith("/")) {
131                     pathTranslated =
132                         super.getRealPath("/")
133                             + newPathInfo.replace('/', File.separatorChar);
134                 } else {
135                     pathTranslated =
136                         super.getRealPath("/")
137                             + File.separatorChar
138                             + newPathInfo.replace('/', File.separatorChar);
139                 }
140             }
141         } else {
142             pathTranslated = super.getPathTranslated();
143         }
144
145         return pathTranslated;
146     }
147
148     /**
149      * If the URL is spoofed, returns the protocol from it. Otherwise, returns
150      * the protocol from the wrapped request.
151      *
152      * @return The protocol from the spoofed URL or the wrapped request
153      */

154     public String JavaDoc getProtocol() {
155         if (url != null && url.getProtocol() != null) {
156             return url.getProtocol();
157         }
158
159         return super.getProtocol();
160     }
161
162     /**
163      * If the URL is spoofed, returns the query string from it. Otherwise, returns
164      * the query string from the wrapped request.
165      *
166      * @return the query string from the spoofed URL or the wrapped request
167      */

168     public String JavaDoc getQueryString() {
169         if (url != null && url.getQueryString() != null) {
170             return url.getQueryString();
171         }
172
173         return super.getQueryString();
174     }
175
176     /**
177      * Since we are overriding the HTTP request, we also need to override the
178      * request dispatcher. That means we have to do all the logic to retrieve
179      * the dispatcher here. That means qualifying relative names, etc.<br>
180      * <br>
181      * This is also necessary because we might spoof the URL
182      *
183      * @param thePath The path to the resource
184      * @return A wrapped request dispatcher
185      */

186     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc thePath) {
187
188         if (thePath == null) {
189             return null;
190         }
191
192         String JavaDoc fullPath;
193
194         // The spec says that the path can be relative, in which case it will
195
// be relative to the request. So for relative paths, we need to take
196
// into account the simulated URL (ServletURL).
197
if (thePath.startsWith("/")) {
198             fullPath = thePath;
199         } else {
200             String JavaDoc pI = getPathInfo();
201             if (pI == null) {
202                 fullPath = catPath(getServletPath(), thePath);
203             } else {
204                 fullPath = catPath(getServletPath() + pI, thePath);
205             }
206
207             if (fullPath == null) {
208                 return null;
209             }
210         }
211
212         return new RequestDispatcherWrapper(
213             super.getRequest().getRequestDispatcher(fullPath));
214     }
215
216     /**
217      * If the URL was spoofed, returns the request URI from the spoofed URL.
218      * Otherwise, returns the request URI from the wrapped request.
219      *
220      * @return the URI from the spoofed URL or the wrapped request
221      */

222     public String JavaDoc getRequestURI() {
223         if (url != null) {
224
225             return super.getContextPath()
226                 + ((super.getServletPath() == null) ? "" : super.getServletPath())
227                 + ((super.getPathInfo() == null) ? "" : super.getPathInfo());
228         }
229
230         return super.getRequestURI();
231     }
232
233     /**
234      * If the URL was spoofed, returns the server name from the spoofed URL.
235      * Otherwise, returns the server name from the wrapped request.
236      *
237      * @return the server name from the spoofed URL or the wrapped request
238      */

239     public String JavaDoc getServerName() {
240         if ((url != null) && (url.getServerName() != null)) {
241             return url.getServerName();
242         }
243
244         return super.getServerName();
245     }
246
247     /**
248      * If the URL was spoofed, returns the server port from the spoofed URL.
249      * Otherwise, returns the server port from the wrapped request.
250      *
251      * @return the server port from the spoofed URL or the wrapped request
252      */

253     public int getServerPort() {
254         if (url != null && url.getServerPort() != -1) {
255             return url.getServerPort();
256         }
257
258         return super.getServerPort();
259     }
260
261     /**
262      * If the URL was spoofed, returns the servlet path from the spoofed URL.
263      * Otherwise, returns the servlet path from the wrapped request.
264      *
265      * @return The servlet path from the spoofed URL or the wrapped request
266      */

267     public String JavaDoc getServletPath() {
268         if (url != null && url.getServletPath() != null) {
269             return url.getServletPath();
270         }
271
272         return super.getServletPath();
273     }
274
275     //-------------------------------------------------------------------------
276
// Helper methods
277
//-------------------------------------------------------------------------
278

279     /**
280      * Will concatenate 2 paths, normalising it. For example :
281      * ( /a/b/c + d = /a/b/d, /a/b/c + ../d = /a/d ). Code borrowed from
282      * Tomcat 3.2.2 !
283      *
284      * @param theLookupPath the first part of the path
285      * @param thePath the part to add to the lookup path
286      * @return the concatenated thePath or null if an error occurs
287      */

288     String JavaDoc catPath(String JavaDoc theLookupPath, String JavaDoc thePath) {
289         // Cut off the last slash and everything beyond
290
int index = theLookupPath.lastIndexOf("/");
291         theLookupPath = theLookupPath.substring(0, index);
292
293         // Deal with .. by chopping dirs off the lookup thePath
294
while (thePath.startsWith("../")) {
295             if (theLookupPath.length() > 0) {
296                 index = theLookupPath.lastIndexOf("/");
297                 theLookupPath = theLookupPath.substring(0, index);
298             } else {
299                 // More ..'s than dirs, return null
300
return null;
301             }
302
303             index = thePath.indexOf("../") + 3;
304             thePath = thePath.substring(index);
305         }
306
307         return theLookupPath + "/" + thePath;
308     }
309 }
310
Popular Tags