KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > URL


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;
8
9 import java.net.InetAddress JavaDoc;
10 import java.net.UnknownHostException JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12
13
14 /**
15  * This interface is the URL that clients can setup so that
16  * when the tests are executing they see this URL rather
17  * then the URL of the remote servlet (or for that matter
18  * no URL at all if the test is being run locally)
19  */

20 public class URL {
21
22     /**
23      * The context path member
24      */

25     protected String JavaDoc contextPath;
26
27     /**
28      * The path info member
29      */

30     protected String JavaDoc pathInfo;
31
32     /**
33      * The protocol member
34      */

35     protected String JavaDoc protocol;
36
37     /**
38      * The query string member
39      */

40     protected String JavaDoc queryString;
41
42     /**
43      * The server name member
44      */

45     protected String JavaDoc serverName;
46
47     /**
48      * The port member
49      */

50     protected int serverPort;
51
52     /**
53      * The servlet path member
54      */

55     protected String JavaDoc servletPath;
56
57
58     /**
59      * Constructs a new empty URL
60      */

61     public URL()
62     {
63     }
64
65     /**
66      * This constructor uses the given URL object and the given context and servlet
67      * the retrieves the information needed out of that class.
68      *
69      * @param url The full URL specification
70      * @param context The context path from the server
71      * @param servlet The name of the servlet to use for the webflow URL
72      */

73     public URL(java.net.URL JavaDoc url, String JavaDoc context, String JavaDoc servlet) {
74
75         assert (context != null) : "context == null";
76         assert (context.charAt(0) != '/') : "context starts with /";
77
78         pathInfo = url.getPath();
79         protocol = url.getProtocol();
80         queryString = url.getQuery();
81         serverName = url.getHost();
82         serverPort = url.getPort();
83         contextPath = context;
84         servletPath = servlet;
85
86         // Strip of the context path and the servlet path from the URLs path
87
if (contextPath != null) {
88             pathInfo = pathInfo.substring(0, contextPath.length());
89         }
90
91         if (servletPath != null) {
92             pathInfo = pathInfo.substring(0, servletPath.length());
93         }
94     }
95
96     /**
97      * Constructs a new URL using the given values. All values are required unless
98      * otherwise specified
99      *
100      * @param context (Optional if using root context) The context root of the request
101      * @param pathInfo (Optional) The extra path info following the servlet path
102      * @param protocol (Optional) The protocol of the URL (i.e. http, https)
103      * @param queryString (Optional) The query string following the path
104      * @param serverName (Optional) The name of the server being spoofed
105      * (including the port number)
106      * @param servletPath The path to the servlet
107      */

108     public URL(String JavaDoc context, String JavaDoc pathInfo, String JavaDoc protocol,
109         String JavaDoc queryString, String JavaDoc serverName, String JavaDoc servletPath) {
110
111         this.contextPath = context;
112         this.pathInfo = pathInfo;
113         this.protocol = protocol;
114         this.queryString = queryString;
115         this.serverName = serverName;
116         this.servletPath = servletPath;
117
118         if (serverName == null) {
119             try {
120                 serverName = InetAddress.getLocalHost().getHostName();
121             } catch (UnknownHostException JavaDoc e) {
122                 throw new RuntimeException JavaDoc(e);
123             }
124         }
125         // Extract the port and host
126
int pos = serverName.indexOf(":");
127         if (pos == -1) {
128             this.serverPort = -1;
129         } else {
130             try {
131                 this.serverPort = Integer.parseInt(serverName.substring(pos + 1));
132                 this.serverName = serverName.substring(0, pos);
133             } catch (NumberFormatException JavaDoc e) {
134                 throw new IllegalArgumentException JavaDoc("Invalid port number: " + serverPort);
135             }
136         }
137     }
138
139     /**
140      * Constructs a new URL using the given values. All values are required unless
141      * otherwise specified
142      *
143      * @param request Used to fill in the missing parameters.
144      * @param context (Optional if using root context) The context root of the request
145      * @param pathInfo (Optional) The extra path info following the servlet path
146      * @param protocol (Optional) The protocol of the URL (i.e. http, https)
147      * @param queryString (Optional) The query string following the path
148      * @param serverName (Optional) The name of the server being spoofed
149      * (including the port number)
150      * @param servletPath The path to the servlet
151      */

152     public URL(HttpServletRequest JavaDoc request, String JavaDoc context, String JavaDoc pathInfo,
153             String JavaDoc protocol, String JavaDoc queryString, String JavaDoc serverName,
154             String JavaDoc servletPath) {
155
156         this.contextPath = context;
157         this.pathInfo = pathInfo;
158         this.protocol = protocol;
159         this.queryString = queryString;
160         this.servletPath = servletPath;
161
162         if (serverName == null) {
163             this.serverName = request.getServerName();
164             this.serverPort = request.getServerPort();
165         } else {
166             // Extract the port and host
167
int pos = serverName.indexOf(":");
168             if (pos == -1) {
169                 serverPort = -1;
170             } else {
171                 try {
172                     serverPort = Integer.parseInt(serverName.substring(pos + 1));
173                     this.serverName = serverName.substring(0, pos);
174                 } catch (NumberFormatException JavaDoc e) {
175                     throw new IllegalArgumentException JavaDoc("Invalid port number: " + serverPort);
176                 }
177             }
178         }
179
180         if (context == null) {
181             this.contextPath = request.getContextPath();
182         }
183
184         if (pathInfo == null) {
185             this.pathInfo = request.getPathInfo();
186         }
187
188         if (protocol == null) {
189             this.protocol = request.getProtocol();
190         }
191
192         if (queryString == null) {
193             this.queryString = request.getQueryString();
194         }
195     }
196
197
198     /**
199      * Gets the spoofed context path of the URL
200      *
201      * @return The context path spoofed on the server
202      */

203     public String JavaDoc getContextPath() {
204         if (contextPath == null) {
205             return "";
206         }
207
208         return "/" + contextPath;
209     }
210
211     /**
212      * Sets the spoofed context path of the URL
213      *
214      * @param path The context path spoofed on the server
215      */

216     public void setContextPath(String JavaDoc path)
217     {
218         contextPath = path;
219     }
220
221     /**
222      * Gets the spoofed path info of the URL
223      *
224      * @return The path info spoofed on the server
225      */

226     public String JavaDoc getPathInfo()
227     {
228         return pathInfo;
229     }
230
231     /**
232      * Sets the spoofed path info of the URL
233      *
234      * @param info The path info spoofed on the server
235      */

236     public void setPathInfo(String JavaDoc info)
237     {
238         pathInfo = info;
239     }
240
241     /**
242      * Gets the spoofed protocol used to connect to the URL
243      *
244      * @return The protocol used to connect to the URL (HTTP, HTTPS, etc).
245      */

246     public String JavaDoc getProtocol()
247     {
248         return protocol;
249     }
250
251     /**
252      * Sets the spoofed protocol used to connect to the URL
253      *
254      * @param protocol The protocol used to connect to the URL (HTTP, HTTPS, etc).
255      */

256     public void setProtocol(String JavaDoc protocol)
257     {
258         this.protocol = protocol;
259     }
260
261     /**
262      * Gets the spoofed query string of the URL
263      *
264      * @return The query string spoofed on the server
265      */

266     public String JavaDoc getQueryString()
267     {
268         return queryString;
269     }
270
271     /**
272      * Sets the spoofed query string of the URL
273      *
274      * @param query The query string spoofed on the server
275      */

276     public void setQueryString(String JavaDoc query)
277     {
278         queryString = query;
279     }
280
281     /**
282      * Gets the spoofed server name of the URL
283      *
284      * @return The server name spoofed on the server
285      */

286     public String JavaDoc getServerName()
287     {
288         return serverName;
289     }
290
291     /**
292      * Sets the spoofed server name of the URL
293      *
294      * @param name The server name spoofed on the server
295      */

296     public void setServerName(String JavaDoc name)
297     {
298         serverName = name;
299     }
300
301     /**
302      * Gets the spoofed port number of the URL
303      *
304      * @return The port number spoofed on the server
305      */

306     public int getServerPort()
307     {
308         return serverPort;
309     }
310
311     /**
312      * Sets the spoofed port number of the URL
313      *
314      * @param port The port number spoofed on the server
315      */

316     public void setServerPort(int port)
317     {
318         serverPort = port;
319     }
320
321     /**
322      * Gets the spoofed servlet path of the URL
323      *
324      * @return The servlet path spoofed on the server
325      */

326     public String JavaDoc getServletPath()
327     {
328         return servletPath;
329     }
330
331     /**
332      * Sets the spoofed servlet path of the URL
333      *
334      * @param path The servlet path spoofed on the server
335      */

336     public void setServletPath(String JavaDoc path)
337     {
338         servletPath = path;
339     }
340 }
341
Popular Tags