|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CookieUtil.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.util;
|
|
58 |
|
|
59 |
import java.net.URL;
|
|
60 |
import java.util.Vector;
|
|
61 |
|
|
62 |
import org.apache.cactus.Cookie;
|
|
63 |
import org.apache.cactus.ServletURL;
|
|
64 |
import org.apache.cactus.WebRequest;
|
|
65 |
import org.apache.cactus.client.ClientException;
|
|
66 |
import org.apache.commons.httpclient.Header;
|
|
67 |
import org.apache.commons.httpclient.HttpState;
|
|
68 |
import org.apache.commons.httpclient.cookie.CookiePolicy;
|
|
69 |
import org.apache.commons.httpclient.cookie.CookieSpec;
|
|
70 |
import org.apache.commons.logging.Log;
|
|
71 |
import org.apache.commons.logging.LogFactory;
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Utility methods to manipulate cookies and transform Cactus cookie objects
|
|
75 |
* to HttpClient cookie objects.
|
|
76 |
*
|
|
77 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
78 |
* @version $Id: CookieUtil.java,v 1.3 2003/06/22 19:20:10 vmassol Exp $
|
|
79 |
* @since 1.5
|
|
80 |
*/
|
|
81 |
public class CookieUtil |
|
82 |
{ |
|
83 |
/**
|
|
84 |
* The logger
|
|
85 |
*/
|
|
86 |
private static final Log LOGGER = LogFactory.getLog(CookieUtil.class); |
|
87 |
|
|
88 |
/**
|
|
89 |
* Returns the domain that will be used to send the cookies. If a host
|
|
90 |
* was specified using <code>setURL()</code> then the domain will be
|
|
91 |
* this host. Otherwise it will be the real redirector host.
|
|
92 |
*
|
|
93 |
* @param theRequest the request containing all data to pass to the server
|
|
94 |
* redirector.
|
|
95 |
* @param theRealHost the real host to which we are connecting to. We will
|
|
96 |
* use it if no simulation host has been specified.
|
|
97 |
* @return the cookie domain to use
|
|
98 |
*/
|
|
99 | 0 |
public static String getCookieDomain(WebRequest theRequest, |
100 |
String theRealHost) |
|
101 |
{ |
|
102 | 0 |
String domain; |
103 | 0 |
ServletURL url = theRequest.getURL(); |
104 |
|
|
105 | 0 |
if ((url != null) && (url.getHost() != null)) |
106 |
{ |
|
107 | 0 |
domain = url.getHost(); |
108 |
} |
|
109 |
else
|
|
110 |
{ |
|
111 | 0 |
domain = theRealHost; |
112 |
} |
|
113 |
|
|
114 | 0 |
LOGGER.debug("Cookie validation domain = [" + domain + "]"); |
115 |
|
|
116 | 0 |
return domain;
|
117 |
} |
|
118 |
|
|
119 |
/**
|
|
120 |
* Returns the port that will be used to send the cookies. If a port
|
|
121 |
* was specified using <code>setURL()</code> then the port sent will be
|
|
122 |
* this port. Otherwise it will be the real redirector port.
|
|
123 |
*
|
|
124 |
* @param theRequest the request containing all data to pass to the server
|
|
125 |
* redirector.
|
|
126 |
* @param theRealPort the real port to which we are connecting to. We will
|
|
127 |
* use it if no simulation port has been specified.
|
|
128 |
* @return the cookie domain to use
|
|
129 |
*/
|
|
130 | 0 |
public static int getCookiePort(WebRequest theRequest, int theRealPort) |
131 |
{ |
|
132 | 0 |
int port;
|
133 | 0 |
ServletURL url = theRequest.getURL(); |
134 |
|
|
135 | 0 |
if ((url != null) && (url.getHost() != null)) |
136 |
{ |
|
137 | 0 |
port = url.getPort(); |
138 |
} |
|
139 |
else
|
|
140 |
{ |
|
141 | 0 |
port = theRealPort; |
142 |
} |
|
143 |
|
|
144 | 0 |
LOGGER.debug("Cookie validation port = [" + port + "]"); |
145 |
|
|
146 | 0 |
return port;
|
147 |
} |
|
148 |
|
|
149 |
/**
|
|
150 |
* Returns the path that will be used to validate if a cookie will be
|
|
151 |
* sent or not. The algorithm is as follows : if the cookie path is not
|
|
152 |
* set (i.e. null) then the cookie is always sent (provided the domain
|
|
153 |
* is right). If the cookie path is set, the cookie is sent only if
|
|
154 |
* the request path starts with the same string as the cookie path. If
|
|
155 |
* <code>setURL()</code> has been called, return the path it has been
|
|
156 |
* set to (context + servletPath + pathInfo). Otherwise return the
|
|
157 |
* real redirector path.
|
|
158 |
*
|
|
159 |
* @param theRequest the request containing all data to pass to the server
|
|
160 |
* redirector.
|
|
161 |
* @param theRealPath the real path to which we are connecting to. We will
|
|
162 |
* use it if no simulation path has been specified.
|
|
163 |
* @return the path to use to decide if a cookie will get sent
|
|
164 |
*/
|
|
165 | 0 |
public static String getCookiePath(WebRequest theRequest, |
166 |
String theRealPath) |
|
167 |
{ |
|
168 | 0 |
String path; |
169 | 0 |
ServletURL url = theRequest.getURL(); |
170 |
|
|
171 | 0 |
if ((url != null) && (url.getPath() != null)) |
172 |
{ |
|
173 | 0 |
path = url.getPath(); |
174 |
} |
|
175 |
else
|
|
176 |
{ |
|
177 | 0 |
String file = theRealPath; |
178 |
|
|
179 | 0 |
if (file != null) |
180 |
{ |
|
181 | 0 |
int q = file.lastIndexOf('?');
|
182 |
|
|
183 | 0 |
if (q != -1)
|
184 |
{ |
|
185 | 0 |
path = file.substring(0, q); |
186 |
} |
|
187 |
else
|
|
188 |
{ |
|
189 | 0 |
path = file; |
190 |
} |
|
191 |
} |
|
192 |
else
|
|
193 |
{ |
|
194 | 0 |
path = null;
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 | 0 |
LOGGER.debug("Cookie validation path = [" + path + "]"); |
199 |
|
|
200 | 0 |
return path;
|
201 |
} |
|
202 |
|
|
203 |
/**
|
|
204 |
* Create a Commons-HttpClient cookie from a Cactus cookie, with information
|
|
205 |
* from the web request and the URL.
|
|
206 |
*
|
|
207 |
* @param theRequest The request
|
|
208 |
* @param theUrl The URL
|
|
209 |
* @param theCactusCookie The Cactus Cookie object
|
|
210 |
* @return The HttpClient cookie
|
|
211 |
*/
|
|
212 | 0 |
public static org.apache.commons.httpclient.Cookie createHttpClientCookie( |
213 |
WebRequest theRequest, URL theUrl, Cookie theCactusCookie) |
|
214 |
{ |
|
215 |
// If no domain has been specified, use a default one
|
|
216 | 0 |
String domain; |
217 | 0 |
if (theCactusCookie.getDomain() == null) |
218 |
{ |
|
219 | 0 |
domain = CookieUtil.getCookieDomain(theRequest, theUrl.getHost()); |
220 |
} |
|
221 |
else
|
|
222 |
{ |
|
223 | 0 |
domain = theCactusCookie.getDomain(); |
224 |
} |
|
225 |
|
|
226 |
// If not path has been specified , use a default one
|
|
227 | 0 |
String path; |
228 | 0 |
if (theCactusCookie.getPath() == null) |
229 |
{ |
|
230 | 0 |
path = CookieUtil.getCookiePath(theRequest, theUrl.getFile()); |
231 |
} |
|
232 |
else
|
|
233 |
{ |
|
234 | 0 |
path = theCactusCookie.getPath(); |
235 |
} |
|
236 |
|
|
237 |
// Assemble the HttpClient cookie
|
|
238 | 0 |
org.apache.commons.httpclient.Cookie httpclientCookie = |
239 |
new org.apache.commons.httpclient.Cookie(domain,
|
|
240 |
theCactusCookie.getName(), theCactusCookie.getValue()); |
|
241 | 0 |
httpclientCookie.setComment(theCactusCookie.getComment()); |
242 | 0 |
httpclientCookie.setExpiryDate( |
243 |
theCactusCookie.getExpiryDate()); |
|
244 | 0 |
httpclientCookie.setPath(path); |
245 | 0 |
httpclientCookie.setSecure(theCactusCookie.isSecure()); |
246 |
|
|
247 | 0 |
return httpclientCookie;
|
248 |
} |
|
249 |
|
|
250 |
/**
|
|
251 |
* Transforms an array of Cactus cookies into an array of Commons-HttpClient
|
|
252 |
* cookies, using information from the request and URL.
|
|
253 |
*
|
|
254 |
* @param theRequest The request
|
|
255 |
* @param theUrl The URL
|
|
256 |
* @return The array of HttpClient cookies
|
|
257 |
*/
|
|
258 |
public static org.apache.commons.httpclient.Cookie[] |
|
259 | 0 |
createHttpClientCookies(WebRequest theRequest, URL theUrl) |
260 |
{ |
|
261 | 0 |
Vector cactusCookies = theRequest.getCookies(); |
262 |
|
|
263 |
// transform the Cactus cookies into HttpClient cookies
|
|
264 | 0 |
org.apache.commons.httpclient.Cookie[] httpclientCookies = |
265 |
new org.apache.commons.httpclient.Cookie[cactusCookies.size()];
|
|
266 |
|
|
267 | 0 |
for (int i = 0; i < cactusCookies.size(); i++) |
268 |
{ |
|
269 | 0 |
Cookie cactusCookie = (Cookie) cactusCookies.elementAt(i); |
270 | 0 |
httpclientCookies[i] = CookieUtil.createHttpClientCookie( |
271 |
theRequest, theUrl, cactusCookie); |
|
272 |
} |
|
273 |
|
|
274 | 0 |
return httpclientCookies;
|
275 |
} |
|
276 |
|
|
277 |
/**
|
|
278 |
* Create a HttpClient {@link Header} for cookies that matches
|
|
279 |
* the domain and path.
|
|
280 |
*
|
|
281 |
* @param theDomain the cookie domain to match
|
|
282 |
* @param thePath the cookie path to match
|
|
283 |
* @param theCookies the list of potential cookies
|
|
284 |
* @return the HttpClient {@link Header} containing the matching
|
|
285 |
* cookies
|
|
286 |
* @throws ClientException if no cookie was matching the domain
|
|
287 |
* and path
|
|
288 |
*/
|
|
289 | 0 |
public static Header createCookieHeader(String theDomain, String thePath, |
290 |
org.apache.commons.httpclient.Cookie[] theCookies) |
|
291 |
throws ClientException
|
|
292 |
{ |
|
293 | 0 |
Header cookieHeader = null;
|
294 |
|
|
295 |
// separate domain into host and port
|
|
296 | 0 |
int port = 80;
|
297 | 0 |
String host = theDomain; |
298 | 0 |
int portIndex = theDomain.indexOf(":"); |
299 | 0 |
if (portIndex != -1)
|
300 |
{ |
|
301 | 0 |
host = host.substring(0, portIndex); |
302 | 0 |
port = Integer.parseInt(theDomain.substring(portIndex + 1)); |
303 |
} |
|
304 |
|
|
305 | 0 |
CookieSpec matcher = CookiePolicy.getDefaultSpec(); |
306 | 0 |
org.apache.commons.httpclient.Cookie[] cookies = |
307 |
matcher.match(host, port, thePath, false, theCookies);
|
|
308 | 0 |
if ((cookies != null) && (cookies.length > 0)) |
309 |
{ |
|
310 | 0 |
cookieHeader = matcher.formatCookieHeader(cookies); |
311 |
} |
|
312 |
|
|
313 | 0 |
if (cookieHeader == null) |
314 |
{ |
|
315 | 0 |
throw new ClientException("Failed to create Cookie header for [" |
316 |
+ "domain = [" + theDomain + ", path = [" + thePath |
|
317 |
+ ", cookies = [" + theCookies + "]]. Turn on HttpClient " |
|
318 |
+ "logging for more information about the error");
|
|
319 |
} |
|
320 |
|
|
321 | 0 |
return cookieHeader;
|
322 |
} |
|
323 |
|
|
324 |
/**
|
|
325 |
* @return the cookie string which will be added as a HTTP "Cookie" header
|
|
326 |
* or null if no cookie has been set
|
|
327 |
* @param theRequest the request containing all data to pass to the server
|
|
328 |
* redirector.
|
|
329 |
* @param theUrl the URL to connect to
|
|
330 |
* @throws ClientException if an error occurred when creating the cookie
|
|
331 |
* string
|
|
332 |
*/
|
|
333 | 0 |
public static String getCookieString(WebRequest theRequest, URL theUrl) |
334 |
throws ClientException
|
|
335 |
{ |
|
336 |
// If no Cookies, then exit
|
|
337 | 0 |
Vector cookies = theRequest.getCookies(); |
338 |
|
|
339 | 0 |
if (!cookies.isEmpty())
|
340 |
{ |
|
341 |
// transform the Cactus cookies into HttpClient cookies
|
|
342 | 0 |
org.apache.commons.httpclient.Cookie[] httpclientCookies = |
343 |
CookieUtil.createHttpClientCookies(theRequest, theUrl); |
|
344 |
|
|
345 |
// and create the cookie header to send
|
|
346 | 0 |
Header cookieHeader = createCookieHeader( |
347 |
CookieUtil.getCookieDomain(theRequest, theUrl.getHost()), |
|
348 |
CookieUtil.getCookiePath(theRequest, theUrl.getFile()), |
|
349 |
httpclientCookies); |
|
350 |
|
|
351 | 0 |
return cookieHeader.getValue();
|
352 |
} |
|
353 |
|
|
354 | 0 |
return null; |
355 |
} |
|
356 |
|
|
357 |
/**
|
|
358 |
* Create an HttpClient {@link HttpState} object containing all cookies
|
|
359 |
* stored in the passed {@link WebRequest} object.
|
|
360 |
*
|
|
361 |
* @param theRequest the request containing the cookies to use when calling
|
|
362 |
* the server side
|
|
363 |
* @param theUrl the URL to connect to
|
|
364 |
* @return an HttpClient {@link HttpState} object which has been set with
|
|
365 |
* the cookies
|
|
366 |
*/
|
|
367 | 0 |
public static HttpState createHttpState(WebRequest theRequest, |
368 |
URL theUrl) |
|
369 |
{ |
|
370 | 0 |
HttpState state = new HttpState();
|
371 | 0 |
state.addCookies(CookieUtil.createHttpClientCookies(theRequest, |
372 |
theUrl)); |
|
373 | 0 |
return state;
|
374 |
} |
|
375 |
} |
|
376 |
|
|