|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ServletURL.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus;
|
|
58 |
|
|
59 |
import javax.servlet.http.HttpServletRequest;
|
|
60 |
|
|
61 |
import org.apache.cactus.server.ServletUtil;
|
|
62 |
import org.apache.commons.logging.Log;
|
|
63 |
import org.apache.commons.logging.LogFactory;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Simulate an HTTP URL by breaking it into its different parts.
|
|
67 |
* <br><code><pre><b>
|
|
68 |
* URL = "http://" + serverName (including port) + requestURI ? queryString<br>
|
|
69 |
* requestURI = contextPath + servletPath + pathInfo
|
|
70 |
* </b></pre></code>
|
|
71 |
* From the Servlet 2.2 specification :<br>
|
|
72 |
* <code><pre><ul><li><b>Context Path</b>: The path prefix associated with the
|
|
73 |
* ServletContext that this servlet is a part of. If this context is the
|
|
74 |
* default context rooted at the base of the web server's URL namespace, this
|
|
75 |
* path will be an empty string. Otherwise, this path starts with a "/"
|
|
76 |
* character but does not end with a "/" character.</li>
|
|
77 |
* <li><b>Servlet Path</b>: The path section that directly corresponds to the
|
|
78 |
* mapping which activated this request. This path starts with a "/"
|
|
79 |
* character.</li>
|
|
80 |
* <li><b>PathInfo</b>: The part of the request path that is not part of the
|
|
81 |
* Context Path or the Servlet Path.</li></ul></pre></code>
|
|
82 |
* From the Servlet 2.3 specification :<br>
|
|
83 |
* <code><pre><ul><li><b>Context Path</b>: The path prefix associated with the
|
|
84 |
* ServletContext that this servlet is a part of. If this context is the
|
|
85 |
* default context rooted at the base of the web server's URL namespace, this
|
|
86 |
* path will be an empty string. Otherwise, this path starts with a "/"
|
|
87 |
* character but does not end with a "/" character.</li>
|
|
88 |
* <li><b>Servlet Path</b>: The path section that directly corresponds to the
|
|
89 |
* mapping which activated this request. This path starts with a "/"
|
|
90 |
* character <b>except in the case where the request is matched with the
|
|
91 |
* "/*" pattern, in which case it is the empty string</b>.</li>
|
|
92 |
* <li><b>PathInfo</b>: The part of the request path that is not part of the
|
|
93 |
* Context Path or the Servlet Path. <b>It is either null if there is no
|
|
94 |
* extra path, or is a string with a leading "/"</b>.</li></ul></pre></code>
|
|
95 |
*
|
|
96 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
97 |
*
|
|
98 |
* @version $Id: ServletURL.java,v 1.15 2003/06/07 13:51:32 vmassol Exp $
|
|
99 |
*/
|
|
100 |
public class ServletURL |
|
101 |
{ |
|
102 |
/**
|
|
103 |
* Name of the parameter in the HTTP request that represents the protocol
|
|
104 |
* (HTTP, HTTPS, etc) in the URL to simulate. The name is voluntarily long
|
|
105 |
* so that it will not clash with a user-defined parameter.
|
|
106 |
*/
|
|
107 |
public static final String URL_PROTOCOL_PARAM = "Cactus_URL_Protocol"; |
|
108 |
|
|
109 |
/**
|
|
110 |
* Name of the parameter in the HTTP request that represents the Server
|
|
111 |
* name (+ port) in the URL to simulate. The name is voluntarily long so
|
|
112 |
* that it will not clash with a user-defined parameter.
|
|
113 |
*/
|
|
114 |
public static final String URL_SERVER_NAME_PARAM = "Cactus_URL_Server"; |
|
115 |
|
|
116 |
/**
|
|
117 |
* Name of the parameter in the HTTP request that represents the context
|
|
118 |
* path in the URL to simulate. The name is voluntarily long so that it
|
|
119 |
* will not clash with a user-defined parameter.
|
|
120 |
*/
|
|
121 |
public static final String URL_CONTEXT_PATH_PARAM = |
|
122 |
"Cactus_URL_ContextPath";
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Name of the parameter in the HTTP request that represents the Servlet
|
|
126 |
* Path in the URL to simulate. The name is voluntarily long so that it
|
|
127 |
* will not clash with a user-defined parameter.
|
|
128 |
*/
|
|
129 |
public static final String URL_SERVLET_PATH_PARAM = |
|
130 |
"Cactus_URL_ServletPath";
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Name of the parameter in the HTTP request that represents the Path Info
|
|
134 |
* in the URL to simulate. The name is voluntarily long so that it will not
|
|
135 |
* clash with a user-defined parameter.
|
|
136 |
*/
|
|
137 |
public static final String URL_PATH_INFO_PARAM = "Cactus_URL_PathInfo"; |
|
138 |
|
|
139 |
/**
|
|
140 |
* Name of the parameter in the HTTP request that represents the Query
|
|
141 |
* String in the URL to simulate. The name is voluntarily long so that it
|
|
142 |
* will not clash with a user-defined parameter.
|
|
143 |
*/
|
|
144 |
public static final String URL_QUERY_STRING_PARAM = |
|
145 |
"Cactus_URL_QueryString";
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Http protocol.
|
|
149 |
*/
|
|
150 |
public static final String PROTOCOL_HTTP = "http"; |
|
151 |
|
|
152 |
/**
|
|
153 |
* Https protocol.
|
|
154 |
*/
|
|
155 |
public static final String PROTOCOL_HTTPS = "https"; |
|
156 |
|
|
157 |
/**
|
|
158 |
* Default port of the HTTP protocol.
|
|
159 |
*/
|
|
160 |
private static final int DEFAULT_PORT_HTTP = 80; |
|
161 |
|
|
162 |
/**
|
|
163 |
* Default port of HTTP over SSL.
|
|
164 |
*/
|
|
165 |
private static final int DEFAULT_PORT_HTTPS = 443; |
|
166 |
|
|
167 |
/**
|
|
168 |
* The logger
|
|
169 |
*/
|
|
170 |
private static final Log LOGGER = LogFactory.getLog(ServletURL.class); |
|
171 |
|
|
172 |
/**
|
|
173 |
* The server name to simulate (including port number)
|
|
174 |
*/
|
|
175 |
private String serverName;
|
|
176 |
|
|
177 |
/**
|
|
178 |
* The context path to simulate
|
|
179 |
*/
|
|
180 |
private String contextPath;
|
|
181 |
|
|
182 |
/**
|
|
183 |
* The servlet path to simulate
|
|
184 |
*/
|
|
185 |
private String servletPath;
|
|
186 |
|
|
187 |
/**
|
|
188 |
* The Path Info to simulate
|
|
189 |
*/
|
|
190 |
private String pathInfo;
|
|
191 |
|
|
192 |
/**
|
|
193 |
* The Query string
|
|
194 |
*/
|
|
195 |
private String queryString;
|
|
196 |
|
|
197 |
/**
|
|
198 |
* The protocol to use. Default to HTTP.
|
|
199 |
*/
|
|
200 |
private String protocol = PROTOCOL_HTTP;
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Default constructor. Need to call the different setters to make this
|
|
204 |
* a valid object.
|
|
205 |
*/
|
|
206 | 0 |
public ServletURL()
|
207 |
{ |
|
208 |
} |
|
209 |
|
|
210 |
/**
|
|
211 |
* Creates the URL to simulate.
|
|
212 |
*
|
|
213 |
* @param theProtocol the protocol to simulate (either
|
|
214 |
* <code>ServletURL.PROTOCOL_HTTP</code> or
|
|
215 |
* <code>ServletURL.PROTOCOL_HTTPS</code>.
|
|
216 |
* @param theServerName the server name (and port) in the URL to simulate,
|
|
217 |
* i.e. this is the name that will be returned by the
|
|
218 |
* <code>HttpServletRequest.getServerName()</code> and
|
|
219 |
* <code>HttpServletRequest.getServerPort()</code>. Can
|
|
220 |
* be null. If null, then the server name and port from
|
|
221 |
* the Servlet Redirector will be returned.
|
|
222 |
* @param theContextPath the webapp context path in the URL to simulate,
|
|
223 |
* i.e. this is the name that will be returned by the
|
|
224 |
* <code>HttpServletRequest.getContextPath()</code>.
|
|
225 |
* Can be null. If null, then the context from the
|
|
226 |
* Servlet Redirector will be used.
|
|
227 |
* Format: "/" + name or an empty string for the
|
|
228 |
* default context. Must not end with a "/" character.
|
|
229 |
* @param theServletPath the servlet path in the URL to simulate,
|
|
230 |
* i.e. this is the name that will be returned by the
|
|
231 |
* <code>HttpServletRequest.getServletPath()</code>.
|
|
232 |
* Can be null. If null, then the servlet path from
|
|
233 |
* the Servlet Redirector will be used.
|
|
234 |
* Format : "/" + name or an empty string.
|
|
235 |
* @param thePathInfo the path info in the URL to simulate, i.e. this is
|
|
236 |
* the name that will be returned by the
|
|
237 |
* <code>HttpServletRequest.getPathInfo()</code>. Can
|
|
238 |
* be null. Format : "/" + name.
|
|
239 |
* @param theQueryString the Query string in the URL to simulate, i.e. this
|
|
240 |
* is the string that will be returned by the
|
|
241 |
* <code>HttpServletResquest.getQueryString()</code>.
|
|
242 |
* Can be null.
|
|
243 |
*/
|
|
244 | 0 |
public ServletURL(String theProtocol, String theServerName,
|
245 |
String theContextPath, String theServletPath, String thePathInfo, |
|
246 |
String theQueryString) |
|
247 |
{ |
|
248 | 0 |
setProtocol(theProtocol); |
249 | 0 |
setServerName(theServerName); |
250 | 0 |
setContextPath(theContextPath); |
251 | 0 |
setServletPath(theServletPath); |
252 | 0 |
setPathInfo(thePathInfo); |
253 | 0 |
setQueryString(theQueryString); |
254 |
} |
|
255 |
|
|
256 |
/**
|
|
257 |
* Creates the URL to simulate, using the default HTTP protocol.
|
|
258 |
*
|
|
259 |
* @param theServerName the server name (and port) in the URL to simulate,
|
|
260 |
* i.e. this is the name that will be returned by the
|
|
261 |
* <code>HttpServletRequest.getServerName()</code> and
|
|
262 |
* <code>HttpServletRequest.getServerPort()</code>. Can
|
|
263 |
* be null. If null, then the server name and port from
|
|
264 |
* the Servlet Redirector will be returned.
|
|
265 |
* @param theContextPath the webapp context path in the URL to simulate,
|
|
266 |
* i.e. this is the name that will be returned by the
|
|
267 |
* <code>HttpServletRequest.getContextPath()</code>.
|
|
268 |
* Can be null. If null, then the context from the
|
|
269 |
* Servlet Redirector will be used.
|
|
270 |
* Format: "/" + name or an empty string for the
|
|
271 |
* default context. Must not end with a "/" character.
|
|
272 |
* @param theServletPath the servlet path in the URL to simulate,
|
|
273 |
* i.e. this is the name that will be returned by the
|
|
274 |
* <code>HttpServletRequest.getServletPath()</code>.
|
|
275 |
* Can be null. If null, then the servlet path from
|
|
276 |
* the Servlet Redirector will be used.
|
|
277 |
* Format : "/" + name or an empty string.
|
|
278 |
* @param thePathInfo the path info in the URL to simulate, i.e. this is
|
|
279 |
* the name that will be returned by the
|
|
280 |
* <code>HttpServletRequest.getPathInfo()</code>. Can
|
|
281 |
* be null. Format : "/" + name.
|
|
282 |
* @param theQueryString the Query string in the URL to simulate, i.e. this
|
|
283 |
* is the string that will be returned by the
|
|
284 |
* <code>HttpServletResquest.getQueryString()</code>.
|
|
285 |
* Can be null.
|
|
286 |
*/
|
|
287 | 0 |
public ServletURL(String theServerName, String theContextPath,
|
288 |
String theServletPath, String thePathInfo, String theQueryString) |
|
289 |
{ |
|
290 | 0 |
this(PROTOCOL_HTTP, theServerName, theContextPath, theServletPath,
|
291 |
thePathInfo, theQueryString); |
|
292 |
} |
|
293 |
|
|
294 |
/**
|
|
295 |
* @return the protocol used to connect to the URL (HTTP, HTTPS, etc).
|
|
296 |
*/
|
|
297 | 0 |
public String getProtocol()
|
298 |
{ |
|
299 | 0 |
return this.protocol; |
300 |
} |
|
301 |
|
|
302 |
/**
|
|
303 |
* Sets the protocol to simulate (either
|
|
304 |
* <code>ServletURL.PROTOCOL_HTTP</code> or
|
|
305 |
* <code>ServletURL.PROTOCOL_HTTPS</code>. If parameter is null then
|
|
306 |
* PROTOCOL_HTTP is assumed.
|
|
307 |
*
|
|
308 |
* @param theProtocol the protocol to simulate
|
|
309 |
*/
|
|
310 | 0 |
public void setProtocol(String theProtocol) |
311 |
{ |
|
312 |
// Only HTTP and HTTPS are currently supported.
|
|
313 | 0 |
if ((!theProtocol.equals(PROTOCOL_HTTP))
|
314 |
&& (!theProtocol.equals(PROTOCOL_HTTPS))) |
|
315 |
{ |
|
316 | 0 |
throw new RuntimeException("Invalid protocol [" + theProtocol |
317 |
+ "]. Currently supported protocols are ["
|
|
318 |
+ PROTOCOL_HTTP + "] and ["
|
|
319 |
+ PROTOCOL_HTTPS + "].");
|
|
320 |
} |
|
321 |
|
|
322 | 0 |
this.protocol = theProtocol;
|
323 |
} |
|
324 |
|
|
325 |
/**
|
|
326 |
* @return the simulated URL server name (including the port number)
|
|
327 |
*/
|
|
328 | 0 |
public String getServerName()
|
329 |
{ |
|
330 | 0 |
return this.serverName; |
331 |
} |
|
332 |
|
|
333 |
/**
|
|
334 |
* Sets the server name (and port) in the URL to simulate, ie this is the
|
|
335 |
* name that will be returned by the
|
|
336 |
* <code>HttpServletRequest.getServerName()</code> and
|
|
337 |
* <code>HttpServletRequest.getServerPort()</code>. Does not need to be
|
|
338 |
* set. If not set or null, then the server name and port from the Servlet
|
|
339 |
* Redirector will be returned.
|
|
340 |
*
|
|
341 |
* @param theServerName the server name and port (ex:
|
|
342 |
* "jakarta.apache.org:80")
|
|
343 |
*/
|
|
344 | 0 |
public void setServerName(String theServerName) |
345 |
{ |
|
346 | 0 |
this.serverName = theServerName;
|
347 |
} |
|
348 |
|
|
349 |
/**
|
|
350 |
* Returns the host name.
|
|
351 |
*
|
|
352 |
* <p>
|
|
353 |
* The host name is extracted from the specified server name (as in
|
|
354 |
* <code><strong>jakarta.apache.org</strong>:80</code>). If the server
|
|
355 |
* name has not been set, this method will return <code>null</code>.
|
|
356 |
* </p>
|
|
357 |
*
|
|
358 |
* @return the simulated URL server name (excluding the port number)
|
|
359 |
*/
|
|
360 | 0 |
public String getHost()
|
361 |
{ |
|
362 | 0 |
String host = getServerName(); |
363 |
|
|
364 | 0 |
if (host != null) |
365 |
{ |
|
366 | 0 |
int pos = host.indexOf(":"); |
367 |
|
|
368 | 0 |
if (pos > 0)
|
369 |
{ |
|
370 | 0 |
host = host.substring(0, pos); |
371 |
} |
|
372 |
} |
|
373 |
|
|
374 | 0 |
return host;
|
375 |
} |
|
376 |
|
|
377 |
/**
|
|
378 |
* Returns the port.
|
|
379 |
*
|
|
380 |
* <p>
|
|
381 |
* The port is extracted from the specified server name (as in
|
|
382 |
* <code>jakarta.apache.org:<strong>80</strong></code>). If the server
|
|
383 |
* name doesn't contain a port number, the default port number is returned
|
|
384 |
* (80 for HTTP, 443 for HTTP over SSL). If a port number is specified but
|
|
385 |
* illegal, or the server name has not been set, this method will return
|
|
386 |
* -1.
|
|
387 |
* </p>
|
|
388 |
*
|
|
389 |
* @return the simulated port number or -1 if an illegal port has been
|
|
390 |
* specified
|
|
391 |
*/
|
|
392 | 0 |
public int getPort() |
393 |
{ |
|
394 | 0 |
int port = -1;
|
395 |
|
|
396 | 0 |
if (getServerName() != null) |
397 |
{ |
|
398 | 0 |
int pos = getServerName().indexOf(":"); |
399 |
|
|
400 | 0 |
if (pos < 0)
|
401 |
{ |
|
402 |
// the server name doesn't contain a port specification, so use
|
|
403 |
// the default port for the protocol
|
|
404 | 0 |
port = getDefaultPort(); |
405 |
} |
|
406 |
else
|
|
407 |
{ |
|
408 |
// parse the port encoded in the server name
|
|
409 | 0 |
try
|
410 |
{ |
|
411 | 0 |
port = Integer.parseInt(getServerName().substring(pos + 1)); |
412 | 0 |
if (port < 0)
|
413 |
{ |
|
414 | 0 |
port = -1; |
415 |
} |
|
416 |
} |
|
417 |
catch (NumberFormatException e)
|
|
418 |
{ |
|
419 | 0 |
port = -1; |
420 |
} |
|
421 |
} |
|
422 |
} |
|
423 |
|
|
424 | 0 |
return port;
|
425 |
} |
|
426 |
|
|
427 |
/**
|
|
428 |
* @return the simulated URL context path
|
|
429 |
*/
|
|
430 | 0 |
public String getContextPath()
|
431 |
{ |
|
432 | 0 |
return this.contextPath; |
433 |
} |
|
434 |
|
|
435 |
/**
|
|
436 |
* Sets the webapp context path in the URL to simulate, ie this is the
|
|
437 |
* name that will be returned by the
|
|
438 |
* <code>HttpServletRequest.getContextPath()</code>. If not set, the
|
|
439 |
* context from the Servlet Redirector will be returned. Format: "/" +
|
|
440 |
* name or an empty string for the default context. If not an empty
|
|
441 |
* string the last character must not be "/".
|
|
442 |
*
|
|
443 |
* @param theContextPath the context path to simulate
|
|
444 |
*/
|
|
445 | 0 |
public void setContextPath(String theContextPath) |
446 |
{ |
|
447 | 0 |
if ((theContextPath != null) && (theContextPath.length() > 0)) |
448 |
{ |
|
449 | 0 |
if (!theContextPath.startsWith("/")) |
450 |
{ |
|
451 | 0 |
throw new IllegalArgumentException("The Context Path must" |
452 |
+ " start with a \"/\" character.");
|
|
453 |
} |
|
454 | 0 |
if (theContextPath.endsWith("/")) |
455 |
{ |
|
456 | 0 |
throw new IllegalArgumentException("The Context Path must not" |
457 |
+ " end with a \"/\" character.");
|
|
458 |
} |
|
459 |
} |
|
460 |
|
|
461 | 0 |
this.contextPath = theContextPath;
|
462 |
} |
|
463 |
|
|
464 |
/**
|
|
465 |
* @return the simulated URL servlet path
|
|
466 |
*/
|
|
467 | 0 |
public String getServletPath()
|
468 |
{ |
|
469 | 0 |
return this.servletPath; |
470 |
} |
|
471 |
|
|
472 |
/**
|
|
473 |
* Sets the servlet path in the URL to simulate, ie this is the name that
|
|
474 |
* will be returned by the <code>HttpServletRequest.getServletPath()</code>.
|
|
475 |
* If null then the servlet path from the Servlet Redirector will be
|
|
476 |
* returned. Format : "/" + name or an empty string.
|
|
477 |
*
|
|
478 |
* @param theServletPath the servlet path to simulate
|
|
479 |
*/
|
|
480 | 0 |
public void setServletPath(String theServletPath) |
481 |
{ |
|
482 | 0 |
if ((theServletPath != null) && (theServletPath.length() > 0)) |
483 |
{ |
|
484 | 0 |
if (!theServletPath.startsWith("/")) |
485 |
{ |
|
486 | 0 |
throw new IllegalArgumentException("The Servlet Path must" |
487 |
+ " start with a \"/\" character.");
|
|
488 |
} |
|
489 |
} |
|
490 |
|
|
491 | 0 |
this.servletPath = theServletPath;
|
492 |
} |
|
493 |
|
|
494 |
/**
|
|
495 |
* @return the simulated URL path info
|
|
496 |
*/
|
|
497 | 0 |
public String getPathInfo()
|
498 |
{ |
|
499 | 0 |
return this.pathInfo; |
500 |
} |
|
501 |
|
|
502 |
/**
|
|
503 |
* Sets the path info in the URL to simulate, ie this is the name that will
|
|
504 |
* be returned by the <code>HttpServletRequest.getPathInfo()</code>.
|
|
505 |
* If null then no path info will be set (and the Path Info from the
|
|
506 |
* Servlet Redirector will <b>not</b> be used).
|
|
507 |
* Format : "/" + name.
|
|
508 |
*
|
|
509 |
* @param thePathInfo the path info to simulate
|
|
510 |
*/
|
|
511 | 0 |
public void setPathInfo(String thePathInfo) |
512 |
{ |
|
513 | 0 |
if ((thePathInfo != null) && (thePathInfo.length() == 0)) |
514 |
{ |
|
515 | 0 |
throw new IllegalArgumentException("The Path Info must" |
516 |
+ " not be an empty string. Use null if you don't"
|
|
517 |
+ " want to have a path info.");
|
|
518 |
} |
|
519 | 0 |
else if (thePathInfo != null) |
520 |
{ |
|
521 | 0 |
if (!thePathInfo.startsWith("/")) |
522 |
{ |
|
523 | 0 |
throw new IllegalArgumentException("The Path Info must" |
524 |
+ " start with a \"/\" character.");
|
|
525 |
} |
|
526 |
} |
|
527 |
|
|
528 | 0 |
this.pathInfo = thePathInfo;
|
529 |
} |
|
530 |
|
|
531 |
/**
|
|
532 |
* @return the simulated Query String
|
|
533 |
*/
|
|
534 | 0 |
public String getQueryString()
|
535 |
{ |
|
536 | 0 |
return this.queryString; |
537 |
} |
|
538 |
|
|
539 |
/**
|
|
540 |
* Sets the Query string in the URL to simulate, ie this is the string that
|
|
541 |
* will be returned by the
|
|
542 |
* <code>HttpServletResquest.getQueryString()</code>. If not set, the
|
|
543 |
* query string from the Servlet Redirector will be returned.
|
|
544 |
*
|
|
545 |
* @param theQueryString the query string to simulate
|
|
546 |
*/
|
|
547 | 0 |
public void setQueryString(String theQueryString) |
548 |
{ |
|
549 | 0 |
this.queryString = theQueryString;
|
550 |
} |
|
551 |
|
|
552 |
/**
|
|
553 |
* @return the path (contextPath + servletPath + pathInfo) or null if
|
|
554 |
* not set
|
|
555 |
*/
|
|
556 | 0 |
public String getPath()
|
557 |
{ |
|
558 | 0 |
String path; |
559 |
|
|
560 | 0 |
path = (getContextPath() == null) ? "" : getContextPath(); |
561 | 0 |
path += ((getServletPath() == null) ? "" : getServletPath()); |
562 | 0 |
path += ((getPathInfo() == null) ? "" : getPathInfo()); |
563 |
|
|
564 | 0 |
if (path.length() == 0)
|
565 |
{ |
|
566 | 0 |
path = null;
|
567 |
} |
|
568 |
|
|
569 | 0 |
return path;
|
570 |
} |
|
571 |
|
|
572 |
/**
|
|
573 |
* Saves the current URL to a <code>WebRequest</code> object.
|
|
574 |
*
|
|
575 |
* @param theRequest the object to which the current URL should be saved to
|
|
576 |
*/
|
|
577 | 0 |
public void saveToRequest(WebRequest theRequest) |
578 |
{ |
|
579 |
// Note: All these pareameters are passed in the URL. This is to allow
|
|
580 |
// the user to send whatever he wants in the request body. For example
|
|
581 |
// a file, ...
|
|
582 | 0 |
theRequest.addParameter(URL_PROTOCOL_PARAM, getProtocol(), |
583 |
WebRequest.GET_METHOD); |
|
584 |
|
|
585 | 0 |
if (getServerName() != null) |
586 |
{ |
|
587 | 0 |
theRequest.addParameter(URL_SERVER_NAME_PARAM, getServerName(), |
588 |
WebRequest.GET_METHOD); |
|
589 |
} |
|
590 |
|
|
591 | 0 |
if (getContextPath() != null) |
592 |
{ |
|
593 | 0 |
theRequest.addParameter(URL_CONTEXT_PATH_PARAM, getContextPath(), |
594 |
WebRequest.GET_METHOD); |
|
595 |
} |
|
596 |
|
|
597 | 0 |
if (getServletPath() != null) |
598 |
{ |
|
599 | 0 |
theRequest.addParameter(URL_SERVLET_PATH_PARAM, getServletPath(), |
600 |
WebRequest.GET_METHOD); |
|
601 |
} |
|
602 |
|
|
603 | 0 |
if (getPathInfo() != null) |
604 |
{ |
|
605 | 0 |
theRequest.addParameter(URL_PATH_INFO_PARAM, getPathInfo(), |
606 |
WebRequest.GET_METHOD); |
|
607 |
} |
|
608 |
|
|
609 | 0 |
if (getQueryString() != null) |
610 |
{ |
|
611 | 0 |
theRequest.addParameter(URL_QUERY_STRING_PARAM, getQueryString(), |
612 |
WebRequest.GET_METHOD); |
|
613 |
} |
|
614 |
} |
|
615 |
|
|
616 |
/**
|
|
617 |
* Creates a <code>ServletURL</code> object by loading it's values from the
|
|
618 |
* HTTP request.
|
|
619 |
*
|
|
620 |
* @param theRequest the incoming HTTP request.
|
|
621 |
* @return the <code>ServletURL</code> object unserialized from the HTTP
|
|
622 |
* request
|
|
623 |
*/
|
|
624 | 0 |
public static ServletURL loadFromRequest(HttpServletRequest theRequest) |
625 |
{ |
|
626 | 0 |
String qString = theRequest.getQueryString(); |
627 | 0 |
boolean isDefined = false; |
628 |
|
|
629 | 0 |
ServletURL url = new ServletURL();
|
630 |
|
|
631 | 0 |
String protocol = ServletUtil.getQueryStringParameter(qString, |
632 |
URL_PROTOCOL_PARAM); |
|
633 |
|
|
634 | 0 |
if (protocol != null) |
635 |
{ |
|
636 | 0 |
isDefined = true;
|
637 | 0 |
url.setProtocol(protocol); |
638 |
} |
|
639 |
|
|
640 | 0 |
String serverName = ServletUtil.getQueryStringParameter(qString, |
641 |
URL_SERVER_NAME_PARAM); |
|
642 |
|
|
643 | 0 |
if (serverName != null) |
644 |
{ |
|
645 | 0 |
isDefined = true;
|
646 | 0 |
url.setServerName(serverName); |
647 |
} |
|
648 |
|
|
649 | 0 |
String contextPath = ServletUtil.getQueryStringParameter(qString, |
650 |
URL_CONTEXT_PATH_PARAM); |
|
651 |
|
|
652 | 0 |
if (contextPath != null) |
653 |
{ |
|
654 | 0 |
isDefined = true;
|
655 | 0 |
url.setContextPath(contextPath); |
656 |
} |
|
657 |
|
|
658 | 0 |
String servletPath = ServletUtil.getQueryStringParameter(qString, |
659 |
URL_SERVLET_PATH_PARAM); |
|
660 |
|
|
661 | 0 |
if (servletPath != null) |
662 |
{ |
|
663 | 0 |
isDefined = true;
|
664 | 0 |
url.setServletPath(servletPath); |
665 |
} |
|
666 |
|
|
667 | 0 |
String pathInfo = ServletUtil.getQueryStringParameter(qString, |
668 |
URL_PATH_INFO_PARAM); |
|
669 |
|
|
670 | 0 |
if (pathInfo != null) |
671 |
{ |
|
672 | 0 |
isDefined = true;
|
673 | 0 |
url.setPathInfo(pathInfo); |
674 |
} |
|
675 |
|
|
676 | 0 |
String queryString = ServletUtil.getQueryStringParameter(qString, |
677 |
URL_QUERY_STRING_PARAM); |
|
678 |
|
|
679 | 0 |
if (queryString != null) |
680 |
{ |
|
681 | 0 |
isDefined = true;
|
682 | 0 |
url.setQueryString(queryString); |
683 |
} |
|
684 |
|
|
685 | 0 |
if (!isDefined)
|
686 |
{ |
|
687 | 0 |
LOGGER.debug("Undefined simulation URL");
|
688 | 0 |
url = null;
|
689 |
} |
|
690 |
else
|
|
691 |
{ |
|
692 | 0 |
LOGGER.debug("Simulation URL = [" + url + "]"); |
693 |
} |
|
694 |
|
|
695 | 0 |
return url;
|
696 |
} |
|
697 |
|
|
698 |
/**
|
|
699 |
* @return a string representation
|
|
700 |
*/
|
|
701 | 0 |
public String toString()
|
702 |
{ |
|
703 | 0 |
StringBuffer buffer = new StringBuffer();
|
704 |
|
|
705 | 0 |
buffer.append("protocol = [" + getProtocol() + "], "); |
706 | 0 |
buffer.append("host name = [" + getHost() + "], "); |
707 | 0 |
buffer.append("port = [" + getPort() + "], "); |
708 | 0 |
buffer.append("context path = [" + getContextPath() + "], "); |
709 | 0 |
buffer.append("servlet path = [" + getServletPath() + "], "); |
710 | 0 |
buffer.append("path info = [" + getPathInfo() + "], "); |
711 | 0 |
buffer.append("query string = [" + getQueryString() + "]"); |
712 |
|
|
713 | 0 |
return buffer.toString();
|
714 |
} |
|
715 |
|
|
716 |
/**
|
|
717 |
* Returns the default port for the protocol.
|
|
718 |
*
|
|
719 |
* @return the default port (80 for HTTP, 443 for HTTP over SSL)
|
|
720 |
*/
|
|
721 | 0 |
private int getDefaultPort() |
722 |
{ |
|
723 | 0 |
if (PROTOCOL_HTTPS.equals(getProtocol()))
|
724 |
{ |
|
725 | 0 |
return DEFAULT_PORT_HTTPS;
|
726 |
} |
|
727 |
else
|
|
728 |
{ |
|
729 | 0 |
return DEFAULT_PORT_HTTP;
|
730 |
} |
|
731 |
} |
|
732 |
|
|
733 |
} |
|
734 |
|
|