1 20 package org.apache.cactus.internal.util; 21 22 import java.net.URL ; 23 import java.util.Vector ; 24 25 import org.apache.cactus.Cookie; 26 import org.apache.cactus.ServletURL; 27 import org.apache.cactus.WebRequest; 28 import org.apache.cactus.internal.client.ClientException; 29 import org.apache.commons.httpclient.Header; 30 import org.apache.commons.httpclient.cookie.CookiePolicy; 31 import org.apache.commons.httpclient.cookie.CookieSpec; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 42 public class CookieUtil 43 { 44 47 private static final Log LOGGER = LogFactory.getLog(CookieUtil.class); 48 49 60 public static String getCookieDomain(WebRequest theRequest, 61 String theRealHost) 62 { 63 String domain; 64 ServletURL url = theRequest.getURL(); 65 66 if ((url != null) && (url.getHost() != null)) 67 { 68 domain = url.getHost(); 69 } 70 else 71 { 72 domain = theRealHost; 73 } 74 75 LOGGER.debug("Cookie validation domain = [" + domain + "]"); 76 77 return domain; 78 } 79 80 91 public static int getCookiePort(WebRequest theRequest, int theRealPort) 92 { 93 int port; 94 ServletURL url = theRequest.getURL(); 95 96 if ((url != null) && (url.getHost() != null)) 97 { 98 port = url.getPort(); 99 } 100 else 101 { 102 port = theRealPort; 103 } 104 105 LOGGER.debug("Cookie validation port = [" + port + "]"); 106 107 return port; 108 } 109 110 126 public static String getCookiePath(WebRequest theRequest, 127 String theRealPath) 128 { 129 String path; 130 ServletURL url = theRequest.getURL(); 131 132 if ((url != null) && (url.getPath() != null)) 133 { 134 path = url.getPath(); 135 } 136 else 137 { 138 String file = theRealPath; 139 140 if (file != null) 141 { 142 int q = file.lastIndexOf('?'); 143 144 if (q != -1) 145 { 146 path = file.substring(0, q); 147 } 148 else 149 { 150 path = file; 151 } 152 } 153 else 154 { 155 path = null; 156 } 157 } 158 159 LOGGER.debug("Cookie validation path = [" + path + "]"); 160 161 return path; 162 } 163 164 173 public static org.apache.commons.httpclient.Cookie createHttpClientCookie( 174 WebRequest theRequest, URL theUrl, Cookie theCactusCookie) 175 { 176 String domain; 178 if (theCactusCookie.getDomain() == null) 179 { 180 domain = CookieUtil.getCookieDomain(theRequest, theUrl.getHost()); 181 } 182 else 183 { 184 domain = theCactusCookie.getDomain(); 185 } 186 187 String path; 189 if (theCactusCookie.getPath() == null) 190 { 191 path = CookieUtil.getCookiePath(theRequest, theUrl.getFile()); 192 } 193 else 194 { 195 path = theCactusCookie.getPath(); 196 } 197 198 org.apache.commons.httpclient.Cookie httpclientCookie = 200 new org.apache.commons.httpclient.Cookie(domain, 201 theCactusCookie.getName(), theCactusCookie.getValue()); 202 httpclientCookie.setComment(theCactusCookie.getComment()); 203 httpclientCookie.setExpiryDate( 204 theCactusCookie.getExpiryDate()); 205 httpclientCookie.setPath(path); 206 httpclientCookie.setSecure(theCactusCookie.isSecure()); 207 208 return httpclientCookie; 209 } 210 211 219 public static org.apache.commons.httpclient.Cookie[] 220 createHttpClientCookies(WebRequest theRequest, URL theUrl) 221 { 222 Vector cactusCookies = theRequest.getCookies(); 223 224 org.apache.commons.httpclient.Cookie[] httpclientCookies = 226 new org.apache.commons.httpclient.Cookie[cactusCookies.size()]; 227 228 for (int i = 0; i < cactusCookies.size(); i++) 229 { 230 Cookie cactusCookie = (Cookie) cactusCookies.elementAt(i); 231 httpclientCookies[i] = CookieUtil.createHttpClientCookie( 232 theRequest, theUrl, cactusCookie); 233 } 234 235 return httpclientCookies; 236 } 237 238 250 public static Header createCookieHeader(String theDomain, String thePath, 251 org.apache.commons.httpclient.Cookie[] theCookies) 252 throws ClientException 253 { 254 Header cookieHeader = null; 255 256 int port = 80; 258 String host = theDomain; 259 int portIndex = theDomain.indexOf(":"); 260 if (portIndex != -1) 261 { 262 host = host.substring(0, portIndex); 263 port = Integer.parseInt(theDomain.substring(portIndex + 1)); 264 } 265 266 CookieSpec matcher = CookiePolicy.getDefaultSpec(); 267 org.apache.commons.httpclient.Cookie[] cookies = 268 matcher.match(host, port, thePath, false, theCookies); 269 if ((cookies != null) && (cookies.length > 0)) 270 { 271 cookieHeader = matcher.formatCookieHeader(cookies); 272 } 273 274 if (cookieHeader == null) 275 { 276 throw new ClientException("Failed to create Cookie header for [" 277 + "domain = [" + theDomain + ", path = [" + thePath 278 + ", cookies = [" + theCookies + "]]. Turn on HttpClient " 279 + "logging for more information about the error"); 280 } 281 282 return cookieHeader; 283 } 284 285 294 public static String getCookieString(WebRequest theRequest, URL theUrl) 295 throws ClientException 296 { 297 Vector cookies = theRequest.getCookies(); 299 300 if (!cookies.isEmpty()) 301 { 302 org.apache.commons.httpclient.Cookie[] httpclientCookies = 304 CookieUtil.createHttpClientCookies(theRequest, theUrl); 305 306 Header cookieHeader = createCookieHeader( 308 CookieUtil.getCookieDomain(theRequest, theUrl.getHost()), 309 CookieUtil.getCookiePath(theRequest, theUrl.getFile()), 310 httpclientCookies); 311 312 return cookieHeader.getValue(); 313 } 314 315 return null; 316 } 317 } 318 | Popular Tags |