1 7 8 package java.net; 9 10 import java.util.Map ; 11 import java.util.List ; 12 import java.util.Collections ; 13 import java.util.Comparator ; 14 import java.io.IOException ; 15 16 98 public class CookieManager extends CookieHandler 99 { 100 101 102 private CookiePolicy policyCallback; 103 104 105 private CookieStore cookieJar = null; 106 107 108 109 110 117 public CookieManager() { 118 this(null, null); 119 } 120 121 122 133 public CookieManager(CookieStore store, 134 CookiePolicy cookiePolicy) 135 { 136 policyCallback = (cookiePolicy == null) ? CookiePolicy.ACCEPT_ORIGINAL_SERVER 138 : cookiePolicy; 139 140 if (store == null) { 142 cookieJar = new sun.net.www.protocol.http.InMemoryCookieStore(); 143 } else { 144 cookieJar = store; 145 } 146 } 147 148 149 150 151 161 public void setCookiePolicy(CookiePolicy cookiePolicy) { 162 if (cookiePolicy != null) policyCallback = cookiePolicy; 163 } 164 165 166 171 public CookieStore getCookieStore() { 172 return cookieJar; 173 } 174 175 176 public Map <String , List <String >> 177 get(URI uri, Map <String , List <String >> requestHeaders) 178 throws IOException 179 { 180 if (uri == null || requestHeaders == null) { 182 throw new IllegalArgumentException ("Argument is null"); 183 } 184 185 Map <String , List <String >> cookieMap = 186 new java.util.HashMap <String , List <String >>(); 187 if (cookieJar == null) 189 return Collections.unmodifiableMap(cookieMap); 190 191 List <HttpCookie > cookies = new java.util.ArrayList <HttpCookie >(); 192 for (HttpCookie cookie : cookieJar.get(uri)) { 193 if (pathMatches(uri.getPath(), cookie.getPath())) { 195 cookies.add(cookie); 196 } 197 } 198 199 List <String > cookieHeader = sortByPath(cookies); 201 202 cookieMap.put("Cookie", cookieHeader); 203 return Collections.unmodifiableMap(cookieMap); 204 } 205 206 207 public void 208 put(URI uri, Map <String , List <String >> responseHeaders) 209 throws IOException 210 { 211 if (uri == null || responseHeaders == null) { 213 throw new IllegalArgumentException ("Argument is null"); 214 } 215 216 217 if (cookieJar == null) 219 return; 220 221 for (String headerKey : responseHeaders.keySet()) { 222 if (headerKey == null 225 || !(headerKey.equalsIgnoreCase("Set-Cookie2") 226 || headerKey.equalsIgnoreCase("Set-Cookie") 227 ) 228 ) 229 { 230 continue; 231 } 232 233 for (String headerValue : responseHeaders.get(headerKey)) { 234 try { 235 List <HttpCookie > cookies = HttpCookie.parse(headerValue); 236 for (HttpCookie cookie : cookies) { 237 if (shouldAcceptInternal(uri, cookie)) { 238 cookieJar.add(uri, cookie); 239 } 240 } 241 } catch (IllegalArgumentException e) { 242 } 245 } 246 } 247 } 248 249 250 251 252 private boolean shouldAcceptInternal(URI uri, HttpCookie cookie) { 254 try { 255 return policyCallback.shouldAccept(uri, cookie); 256 } catch (Exception ignored) { return false; 258 } 259 } 260 261 262 265 private boolean pathMatches(String path, String pathToMatchWith) { 266 if (path == pathToMatchWith) 267 return true; 268 if (path == null || pathToMatchWith == null) 269 return false; 270 if (path.startsWith(pathToMatchWith)) 271 return true; 272 273 return false; 274 } 275 276 277 281 private List <String > sortByPath(List <HttpCookie > cookies) { 282 Collections.sort(cookies, new CookiePathComparator()); 283 284 List <String > cookieHeader = new java.util.ArrayList <String >(); 285 for (HttpCookie cookie : cookies) { 286 if (cookies.indexOf(cookie) == 0 && cookie.getVersion() > 0) { 291 cookieHeader.add("$Version=\"1\""); 292 } 293 294 cookieHeader.add(cookie.toString()); 295 } 296 return cookieHeader; 297 } 298 299 300 static class CookiePathComparator implements Comparator <HttpCookie > { 301 public int compare(HttpCookie c1, HttpCookie c2) { 302 if (c1 == c2) return 0; 303 if (c1 == null) return -1; 304 if (c2 == null) return 1; 305 306 if (!c1.getName().equals(c2.getName())) return 0; 308 309 if (c1.getPath().startsWith(c2.getPath())) 311 return -1; 312 else if (c2.getPath().startsWith(c1.getPath())) 313 return 1; 314 else 315 return 0; 316 } 317 } 318 } 319 | Popular Tags |