--- org/apache/http/client/HttpState.java-old 2007-07-14 17:45:32.000000000 +0200 +++ org/apache/http/client/HttpState.java 2007-10-26 14:47:29.000000000 +0200 @@ -31,7 +31,6 @@ package org.apache.http.client; import java.util.ArrayList; -import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -39,7 +38,6 @@ import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.cookie.Cookie; -import org.apache.http.cookie.CookieIdentityComparator; /** *

@@ -68,14 +66,13 @@ * Map of {@link Credentials credentials} by realm that this * HTTP state contains. */ - private final HashMap credMap; + private HashMap credMap = null; /** * Array of {@link Cookie cookies} that this HTTP state contains. */ - private final ArrayList cookies; + private ArrayList cookies = null; - private final Comparator cookieComparator; // -------------------------------------------------------- Class Variables @@ -83,10 +80,6 @@ * Default constructor. */ public HttpState() { - super(); - this.credMap = new HashMap(); - this.cookies = new ArrayList(); - this.cookieComparator = new CookieIdentityComparator(); } // ------------------------------------------------------------- Properties @@ -101,20 +94,43 @@ * @see #addCookies(Cookie[]) * */ - public synchronized void addCookie(Cookie cookie) { - if (cookie != null) { - // first remove any old cookie that is equivalent - for (Iterator it = cookies.iterator(); it.hasNext();) { - Cookie tmp = (Cookie) it.next(); - if (cookieComparator.compare(cookie, tmp) == 0) { - it.remove(); - break; - } - } - if (!cookie.isExpired(new Date())) { - cookies.add(cookie); + public synchronized void addCookie(Cookie entry) { + if (entry == null) + return; + boolean expired = entry.isExpired(new Date()); + + if (cookies == null) { // lazy initialization + if (! expired) { + cookies = new ArrayList(); + cookies.add(entry); + } + return; + } + + int i = cookies.size(); + while (--i >= 0) { // search existing + Cookie existing = (Cookie) cookies.get(i); + if (equal(existing, entry)) { + if (expired) + cookies.remove(i); + else + cookies.set(i, entry); + return; } } + + if (! expired) + cookies.add(entry); + } + + private static boolean equal(Cookie c1, Cookie c2) { + if (! c1.getName().equals(c2.getName())) + return false; + String d1 = c1.getDomain(); + String d2 = c2.getDomain(); + if (d1 == null) d1 = ""; + if (d2 == null) d2 = ""; + return d2.equalsIgnoreCase(d2); } /** @@ -142,7 +158,11 @@ * @return an array of {@link Cookie cookies}. */ public synchronized Cookie[] getCookies() { - return (Cookie[]) (cookies.toArray(new Cookie[cookies.size()])); + if (cookies == null) + return new Cookie[0]; + Cookie[] result = new Cookie[cookies.size()]; + cookies.toArray(result); + return result; } /** @@ -154,12 +174,15 @@ * @see Cookie#isExpired(Date) */ public synchronized boolean purgeExpiredCookies() { + if (cookies == null) + return false; boolean removed = false; Date now = new Date(); - Iterator it = cookies.iterator(); - while (it.hasNext()) { - if (((Cookie) (it.next())).isExpired(now)) { - it.remove(); + int i = cookies.size(); + while (--i >= 0) { + Cookie x = (Cookie) cookies.get(i); + if (x.isExpired(now)) { + cookies.remove(i); removed = true; } } @@ -176,45 +199,15 @@ * * @see #getCredentials(AuthScope) */ - public synchronized void setCredentials(final AuthScope authscope, final Credentials credentials) { - if (authscope == null) { + public synchronized void setCredentials(AuthScope authscope, Credentials credentials) { + if (authscope == null) throw new IllegalArgumentException("Authentication scope may not be null"); - } + if (credMap == null) // lazy initialization + credMap = new HashMap(); credMap.put(authscope, credentials); } - /** - * Find matching {@link Credentials credentials} for the given authentication scope. - * - * @param map the credentials hash map - * @param token the {@link AuthScope authentication scope} - * @return the credentials - * - */ - private static Credentials matchCredentials(final HashMap map, final AuthScope authscope) { - // see if we get a direct hit - Credentials creds = (Credentials)map.get(authscope); - if (creds == null) { - // Nope. - // Do a full scan - int bestMatchFactor = -1; - AuthScope bestMatch = null; - Iterator items = map.keySet().iterator(); - while (items.hasNext()) { - AuthScope current = (AuthScope)items.next(); - int factor = authscope.match(current); - if (factor > bestMatchFactor) { - bestMatchFactor = factor; - bestMatch = current; - } - } - if (bestMatch != null) { - creds = (Credentials)map.get(bestMatch); - } - } - return creds; - } - + /** * Get the {@link Credentials credentials} for the given authentication scope. * @@ -223,11 +216,30 @@ * * @see #setCredentials(AuthScope, Credentials) */ - public synchronized Credentials getCredentials(final AuthScope authscope) { - if (authscope == null) { + public synchronized Credentials getCredentials(AuthScope authscope) { + if (authscope == null) throw new IllegalArgumentException("Authentication scope may not be null"); - } - return matchCredentials(this.credMap, authscope); + if (credMap == null) + return null; + Object match = credMap.get(authscope); + if (match != null) + return (Credentials) match; + + // Go for the best result: + AuthScope best = null; + int bestFactor = -1; + Iterator scopes = credMap.keySet().iterator(); + while (scopes.hasNext()) { + AuthScope x = (AuthScope) scopes.next(); + int factor = authscope.match(x); + if (factor > bestFactor) { + best = x; + bestFactor = factor; + } + } + if (best == null) + return null; + return (Credentials) credMap.get(best); } /** @@ -248,22 +260,22 @@ * Clears all credentials. */ public synchronized void clearCredentials() { - this.credMap.clear(); + credMap = null; } /** * Clears all cookies. */ public synchronized void clearCookies() { - this.cookies.clear(); + cookies = null; } /** * Clears the state information (all cookies, credentials and proxy credentials). */ public synchronized void clear() { - clearCookies(); - clearCredentials(); + credMap = null; + cookies = null; } }