1 31 32 package org.apache.commons.httpclient; 33 34 import java.util.ArrayList ; 35 import java.util.Date ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 import java.util.List ; 39 import java.util.Iterator ; 40 import org.apache.commons.httpclient.cookie.CookieSpec; 41 import org.apache.commons.httpclient.cookie.CookiePolicy; 42 import org.apache.commons.httpclient.auth.HttpAuthRealm; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 46 70 public class HttpState { 71 72 74 77 private boolean preemptive; 78 79 82 public static final String PREEMPTIVE_PROPERTY = 83 "httpclient.authentication.preemptive"; 84 85 88 public static final String PREEMPTIVE_DEFAULT = "false"; 89 90 94 private HashMap credMap = new HashMap (); 95 96 100 private HashMap proxyCred = new HashMap (); 101 102 105 public static final HttpAuthRealm DEFAULT_AUTH_REALM = new HttpAuthRealm(null, null); 106 107 110 private ArrayList cookies = new ArrayList (); 111 115 private int cookiePolicy = CookiePolicy.RFC2109; 116 117 118 private HttpConnectionManager httpConnectionManager; 119 120 122 123 private static final Log LOG = LogFactory.getLog(HttpState.class); 124 125 128 public HttpState() { 129 130 super(); 131 132 this.cookiePolicy = CookiePolicy.getDefaultPolicy(); 133 134 String preemptiveDefault = null; 136 try { 137 preemptiveDefault = System.getProperty(PREEMPTIVE_PROPERTY); 138 } catch (SecurityException ignore) { 139 } 140 if (preemptiveDefault == null) { 141 preemptiveDefault = PREEMPTIVE_DEFAULT; 142 } 143 preemptiveDefault = preemptiveDefault.trim().toLowerCase(); 144 145 if (!(preemptiveDefault.equals("true") 146 || preemptiveDefault.equals("false"))) { LOG.warn("Configuration property " + PREEMPTIVE_PROPERTY 148 + " must be either true or false. Using default: " 149 + PREEMPTIVE_DEFAULT); 150 preemptiveDefault = PREEMPTIVE_DEFAULT; 151 } 152 this.preemptive = ("true".equals(preemptiveDefault)); 153 } 154 155 157 167 public synchronized void addCookie(Cookie cookie) { 168 LOG.trace("enter HttpState.addCookie(Cookie)"); 169 170 if (cookie != null) { 171 for (Iterator it = cookies.iterator(); it.hasNext();) { 173 Cookie tmp = (Cookie) it.next(); 174 if (cookie.equals(tmp)) { 175 it.remove(); 176 break; 177 } 178 } 179 if (!cookie.isExpired()) { 180 cookies.add(cookie); 181 } 182 } 183 } 184 185 195 public synchronized void addCookies(Cookie[] cookies) { 196 LOG.trace("enter HttpState.addCookies(Cookie[])"); 197 198 if (cookies != null) { 199 for (int i = 0; i < cookies.length; i++) { 200 this.addCookie(cookies[i]); 201 } 202 } 203 } 204 205 214 public synchronized Cookie[] getCookies() { 215 LOG.trace("enter HttpState.getCookies()"); 216 return (Cookie[]) (cookies.toArray(new Cookie[cookies.size()])); 217 } 218 219 235 public synchronized Cookie[] getCookies( 236 String domain, 237 int port, 238 String path, 239 boolean secure, 240 Date now 241 ) { 242 return getCookies(domain, port, path, secure); 243 } 244 245 246 261 public synchronized Cookie[] getCookies( 262 String domain, 263 int port, 264 String path, 265 boolean secure 266 ) { 267 LOG.trace("enter HttpState.getCookies(String, int, String, boolean)"); 268 269 CookieSpec matcher = CookiePolicy.getDefaultSpec(); 270 ArrayList list = new ArrayList (cookies.size()); 271 for (int i = 0, m = cookies.size(); i < m; i++) { 272 Cookie cookie = (Cookie) (cookies.get(i)); 273 if (matcher.match(domain, port, path, secure, cookie)) { 274 list.add(cookie); 275 } 276 } 277 return (Cookie[]) (list.toArray(new Cookie[list.size()])); 278 } 279 280 287 public synchronized boolean purgeExpiredCookies() { 288 LOG.trace("enter HttpState.purgeExpiredCookies()"); 289 return purgeExpiredCookies(new Date ()); 290 } 291 292 304 public synchronized boolean purgeExpiredCookies(Date date) { 305 LOG.trace("enter HttpState.purgeExpiredCookies(Date)"); 306 boolean removed = false; 307 Iterator it = cookies.iterator(); 308 while (it.hasNext()) { 309 if (((Cookie) (it.next())).isExpired(date)) { 310 it.remove(); 311 removed = true; 312 } 313 } 314 return removed; 315 } 316 317 318 324 325 public int getCookiePolicy() { 326 return this.cookiePolicy; 327 } 328 329 330 337 338 public void setAuthenticationPreemptive(boolean value) { 339 this.preemptive = value; 340 } 341 342 343 349 350 public boolean isAuthenticationPreemptive() { 351 return this.preemptive; 352 } 353 354 355 364 365 public void setCookiePolicy(int policy) { 366 this.cookiePolicy = policy; 367 } 368 369 386 387 public synchronized void setCredentials(String realm, Credentials credentials) { 388 LOG.trace("enter HttpState.setCredentials(String, Credentials)"); 389 setCredentials(realm, null, credentials); 390 } 391 392 410 411 public synchronized void setCredentials(String realm, String host, Credentials credentials) { 412 LOG.trace( 413 "enter HttpState.setCredentials(String realm, String host, Credentials credentials)"); 414 credMap.put(new HttpAuthRealm(host, realm), credentials); 415 } 416 417 418 435 private static Credentials matchCredentials(HashMap map, String realm, String host) { 436 HttpAuthRealm entry = new HttpAuthRealm(host, realm); 437 Credentials creds = (Credentials) map.get(entry); 438 if (creds == null && host != null && realm != null) { 439 entry = new HttpAuthRealm(host, null); 440 creds = (Credentials) map.get(entry); 441 if (creds == null) { 442 entry = new HttpAuthRealm(null, realm); 443 creds = (Credentials) map.get(entry); 444 } 445 } 446 if (creds == null) { 447 creds = (Credentials) map.get(DEFAULT_AUTH_REALM); 448 } 449 return creds; 450 } 451 452 470 471 public synchronized Credentials getCredentials(String realm, String host) { 472 LOG.trace("enter HttpState.getCredentials(String, String"); 473 return matchCredentials(this.credMap, realm, host); 474 } 475 476 489 490 public synchronized Credentials getCredentials(String realm) { 491 LOG.trace("enter HttpState.getCredentials(String)"); 492 493 return getCredentials(realm, null); 494 } 495 496 514 515 public synchronized void setProxyCredentials(String realm, Credentials credentials) { 516 LOG.trace("enter HttpState.setProxyCredentials(String, credentials)"); 517 setProxyCredentials(realm, null, credentials); 518 } 519 520 539 public synchronized void setProxyCredentials( 540 String realm, 541 String proxyHost, 542 Credentials credentials 543 ) { 544 LOG.trace("enter HttpState.setProxyCredentials(String, String, Credentials"); 545 proxyCred.put(new HttpAuthRealm(proxyHost, realm), credentials); 546 } 547 548 563 564 public synchronized Credentials getProxyCredentials(String realm) { 565 LOG.trace("enter HttpState.getProxyCredentials(String)"); 566 return getProxyCredentials(realm, null); 567 } 568 569 586 public synchronized Credentials getProxyCredentials(String realm, String proxyHost) { 587 LOG.trace("enter HttpState.getCredentials(String, String"); 588 return matchCredentials(this.proxyCred, realm, proxyHost); 589 } 590 591 598 public synchronized String toString() { 599 StringBuffer sbResult = new StringBuffer (); 600 601 sbResult.append("["); 602 sbResult.append(getProxyCredentialsStringRepresentation(proxyCred)); 603 sbResult.append(" | "); 604 sbResult.append(getCredentialsStringRepresentation(proxyCred)); 605 sbResult.append(" | "); 606 sbResult.append(getCookiesStringRepresentation(cookies)); 607 sbResult.append("]"); 608 609 String strResult = sbResult.toString(); 610 611 return strResult; 612 } 613 614 619 private static String getProxyCredentialsStringRepresentation(final Map proxyCredMap) { 620 StringBuffer sbResult = new StringBuffer (); 621 Iterator iter = proxyCredMap.keySet().iterator(); 622 while (iter.hasNext()) { 623 Object key = iter.next(); 624 Credentials cred = (Credentials) proxyCredMap.get(key); 625 if (sbResult.length() > 0) { 626 sbResult.append(", "); 627 } 628 sbResult.append(key); 629 sbResult.append("#"); 630 sbResult.append(cred.toString()); 631 } 632 return sbResult.toString(); 633 } 634 635 640 private static String getCredentialsStringRepresentation(final Map credMap) { 641 StringBuffer sbResult = new StringBuffer (); 642 Iterator iter = credMap.keySet().iterator(); 643 while (iter.hasNext()) { 644 Object key = iter.next(); 645 Credentials cred = (Credentials) credMap.get(key); 646 if (sbResult.length() > 0) { 647 sbResult.append(", "); 648 } 649 sbResult.append(key); 650 sbResult.append("#"); 651 sbResult.append(cred.toString()); 652 } 653 return sbResult.toString(); 654 } 655 656 661 private static String getCookiesStringRepresentation(final List cookies) { 662 StringBuffer sbResult = new StringBuffer (); 663 Iterator iter = cookies.iterator(); 664 while (iter.hasNext()) { 665 Cookie ck = (Cookie) iter.next(); 666 if (sbResult.length() > 0) { 667 sbResult.append("#"); 668 } 669 sbResult.append(ck.toExternalForm()); 670 } 671 return sbResult.toString(); 672 } 673 674 683 public synchronized HttpConnectionManager getHttpConnectionManager() { 684 return httpConnectionManager; 685 } 686 687 696 public synchronized void setHttpConnectionManager( 697 HttpConnectionManager httpConnectionManager 698 ) { 699 this.httpConnectionManager = httpConnectionManager; 700 } 701 } 702 | Popular Tags |