Index: java/org/apache/commons/httpclient/HttpMethodDirector.java =================================================================== retrieving revision 1.15 diff -u -r1.15 HttpMethodDirector.java --- java/org/apache/commons/httpclient/HttpMethodDirector.java 28 Jan 2004 21:39:16 -0000 1.15 +++ java/org/apache/commons/httpclient/HttpMethodDirector.java 29 Jan 2004 10:23:56 -0000 @@ -293,7 +293,7 @@ buffer.append(host); LOG.info(buffer.toString()); } - Credentials credentials = this.state.getCredentials(realm, host); + Credentials credentials = this.state.getCredentials(this.authScheme, host); if (credentials != null) { String authstring = this.authScheme.authenticate(credentials, method); if (authstring != null) { @@ -329,7 +329,7 @@ buffer.append(host); LOG.info(buffer.toString()); } - Credentials credentials = this.state.getProxyCredentials(realm, host); + Credentials credentials = this.state.getProxyCredentials(this.proxyAuthScheme, host); if (credentials != null) { String authstring = this.proxyAuthScheme.authenticate(credentials, method); if (authstring != null) { @@ -658,7 +658,7 @@ host = conn.getHost(); } String realm = this.authScheme.getRealm(); - Credentials credentials = this.state.getCredentials(realm, host); + Credentials credentials = this.state.getCredentials(this.authScheme, host); if (credentials == null) { if (LOG.isInfoEnabled()) { StringBuffer buffer = new StringBuffer(); @@ -718,7 +718,7 @@ String host = conn.getProxyHost(); String realm = this.proxyAuthScheme.getRealm(); - Credentials credentials = this.state.getProxyCredentials(realm, host); + Credentials credentials = this.state.getProxyCredentials(this.proxyAuthScheme, host); if (credentials == null) { if (LOG.isInfoEnabled()) { StringBuffer buffer = new StringBuffer(); Index: java/org/apache/commons/httpclient/HttpState.java =================================================================== retrieving revision 1.29 diff -u -r1.29 HttpState.java --- java/org/apache/commons/httpclient/HttpState.java 29 Oct 2003 03:12:27 -0000 1.29 +++ java/org/apache/commons/httpclient/HttpState.java 29 Jan 2004 10:23:56 -0000 @@ -66,12 +66,16 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; -import java.util.Map; import java.util.List; import java.util.Iterator; import org.apache.commons.httpclient.cookie.CookieSpec; import org.apache.commons.httpclient.cookie.CookiePolicy; +import org.apache.commons.httpclient.auth.AuthScheme; +import org.apache.commons.httpclient.auth.BasicScheme; +import org.apache.commons.httpclient.auth.CredentialsNotAvailableException; import org.apache.commons.httpclient.auth.HttpAuthRealm; +import org.apache.commons.httpclient.auth.CredentialsProvider; +import org.apache.commons.httpclient.auth.MalformedChallengeException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -100,22 +104,9 @@ * */ public class HttpState { - // ----------------------------------------------------- Instance Variables /** - * Map of {@link Credentials credentials} by realm that this - * HTTP state contains. - */ - private HashMap credMap = new HashMap(); - - /** - * Map of {@link Credentials proxy credentials} by realm that this - * HTTP state contains - */ - private HashMap proxyCred = new HashMap(); - - /** * The default authentication realm. */ public static final HttpAuthRealm DEFAULT_AUTH_REALM = new HttpAuthRealm(null, null); @@ -128,7 +119,15 @@ private boolean preemptive = false; private int cookiePolicy = 0; - // -------------------------------------------------------- Class Variables + + + private final BuiltinCredentialsProvider defaultCredentialsProvider = new BuiltinCredentialsProvider(); + private CredentialsProvider credentialsProvider = defaultCredentialsProvider; + + private final BuiltinCredentialsProvider defaultProxyCredentialsProvider = new BuiltinCredentialsProvider(); + private CredentialsProvider proxyCredentialsProvider = defaultProxyCredentialsProvider; + + // -------------------------------------------------------- Class Variables /** Log object for this class. */ private static final Log LOG = LogFactory.getLog(HttpState.class); @@ -366,44 +365,10 @@ public synchronized void setCredentials(String realm, String host, Credentials credentials) { LOG.trace( "enter HttpState.setCredentials(String realm, String host, Credentials credentials)"); - credMap.put(new HttpAuthRealm(host, realm), credentials); - } - - - /** - * Find matching {@link Credentials credentials} for the given authentication realm and host. - * - * If the realm exists on host, return the coresponding credentials. - * If the host exists with a null realm, return the corresponding - * credentials. - * If the realm exists with a null host, return the - * corresponding credentials. If the realm does not exist, return - * the default Credentials. If there are no default credentials, return - * null. - * - * @param map the credentials hash map - * @param realm the authentication realm - * @param host the host the realm is on - * @return the credentials - * - */ - private static Credentials matchCredentials(HashMap map, String realm, String host) { - HttpAuthRealm entry = new HttpAuthRealm(host, realm); - Credentials creds = (Credentials) map.get(entry); - if (creds == null && host != null && realm != null) { - entry = new HttpAuthRealm(host, null); - creds = (Credentials) map.get(entry); - if (creds == null) { - entry = new HttpAuthRealm(null, realm); - creds = (Credentials) map.get(entry); - } - } - if (creds == null) { - creds = (Credentials) map.get(DEFAULT_AUTH_REALM); - } - return creds; + defaultCredentialsProvider.add(new HttpAuthRealm(host, realm), credentials); } + /** * Get the {@link Credentials credentials} for the given authentication realm on the * given host. @@ -423,9 +388,26 @@ * @see #setCredentials(String, String, Credentials) */ + public synchronized Credentials getCredentials(AuthScheme authscheme, String host) { + LOG.trace("enter HttpState.getCredentials(AuthScheme, String"); + try { + return credentialsProvider.getCredentials(authscheme, host); + } catch (CredentialsNotAvailableException e) { + return null; + } + } + + /** + * @deprecated use getCredentials(AuthScheme, String) + */ public synchronized Credentials getCredentials(String realm, String host) { - LOG.trace("enter HttpState.getCredentials(String, String"); - return matchCredentials(this.credMap, realm, host); + AuthScheme scheme; + try { + scheme = new BasicScheme("basic realm="+realm); + } catch (MalformedChallengeException e) { + throw new RuntimeException(e.toString()); + } + return getCredentials(scheme, host); } /** @@ -453,7 +435,7 @@ Credentials credentials ) { LOG.trace("enter HttpState.setProxyCredentials(String, String, Credentials"); - proxyCred.put(new HttpAuthRealm(proxyHost, realm), credentials); + defaultProxyCredentialsProvider.add(new HttpAuthRealm(proxyHost, realm), credentials); } /** @@ -473,11 +455,28 @@ * @return the credentials * @see #setProxyCredentials(String, String, Credentials) */ - public synchronized Credentials getProxyCredentials(String realm, String proxyHost) { - LOG.trace("enter HttpState.getCredentials(String, String"); - return matchCredentials(this.proxyCred, realm, proxyHost); + public synchronized Credentials getProxyCredentials(AuthScheme authscheme, String proxyHost) { + LOG.trace("enter HttpState.getProxyCredentials(AuthScheme, String"); + try { + return proxyCredentialsProvider.getCredentials(authscheme, proxyHost); + } catch (CredentialsNotAvailableException e) { + return null; + } } + /** + * @deprecated use getProxyCredentials(AuthScheme, String) + */ + public synchronized Credentials getProxyCredentials(String realm, String host) { + AuthScheme scheme; + try { + scheme = new BasicScheme("basic realm="+realm); + } catch (MalformedChallengeException e) { + throw new RuntimeException(e.toString()); + } + return getProxyCredentials(scheme, host); + } + /** * Returns a string representation of this HTTP state. * @@ -489,9 +488,9 @@ StringBuffer sbResult = new StringBuffer(); sbResult.append("["); - sbResult.append(getProxyCredentialsStringRepresentation(proxyCred)); + sbResult.append(proxyCredentialsProvider.toString()); sbResult.append(" | "); - sbResult.append(getCredentialsStringRepresentation(proxyCred)); + sbResult.append(credentialsProvider.toString()); sbResult.append(" | "); sbResult.append(getCookiesStringRepresentation(cookies)); sbResult.append("]"); @@ -501,47 +500,6 @@ return strResult; } - /** - * Returns a string representation of the proxy credentials - * @param proxyCredMap The proxy credentials - * @return The string representation. - */ - private static String getProxyCredentialsStringRepresentation(final Map proxyCredMap) { - StringBuffer sbResult = new StringBuffer(); - Iterator iter = proxyCredMap.keySet().iterator(); - while (iter.hasNext()) { - Object key = iter.next(); - Credentials cred = (Credentials) proxyCredMap.get(key); - if (sbResult.length() > 0) { - sbResult.append(", "); - } - sbResult.append(key); - sbResult.append("#"); - sbResult.append(cred.toString()); - } - return sbResult.toString(); - } - - /** - * Returns a string representation of the credentials. - * @param credMap The credentials. - * @return The string representation. - */ - private static String getCredentialsStringRepresentation(final Map credMap) { - StringBuffer sbResult = new StringBuffer(); - Iterator iter = credMap.keySet().iterator(); - while (iter.hasNext()) { - Object key = iter.next(); - Credentials cred = (Credentials) credMap.get(key); - if (sbResult.length() > 0) { - sbResult.append(", "); - } - sbResult.append(key); - sbResult.append("#"); - sbResult.append(cred.toString()); - } - return sbResult.toString(); - } /** * Returns a string representation of the cookies. @@ -560,4 +518,83 @@ } return sbResult.toString(); } + + //-- inner class + + public final class BuiltinCredentialsProvider implements CredentialsProvider { + /** + * Map of {@link Credentials credentials} by realm that this + * HTTP state contains. + */ + private HashMap credMap = new HashMap(); + + + + public Credentials getCredentials(AuthScheme authscheme, String host) throws CredentialsNotAvailableException { + return matchCredentials(authscheme.getRealm(), host); + } + + /** + * @param realm + * @param credentials + */ + public void add(HttpAuthRealm realm, Credentials credentials) { + credMap.put(realm, credentials); + } + + /** + * Find matching {@link Credentials credentials} for the given authentication realm and host. + * + * If the realm exists on host, return the coresponding credentials. + * If the host exists with a null realm, return the corresponding + * credentials. + * If the realm exists with a null host, return the + * corresponding credentials. If the realm does not exist, return + * the default Credentials. If there are no default credentials, return + * null. + * + * @param realm the authentication realm + * @param host the host the realm is on + * @return the credentials + * + */ + private Credentials matchCredentials(String realm, String host) { + HttpAuthRealm entry = new HttpAuthRealm(host, realm); + Credentials creds = (Credentials) credMap.get(entry); + if (creds == null && host != null && realm != null) { + entry = new HttpAuthRealm(host, null); + creds = (Credentials) credMap.get(entry); + if (creds == null) { + entry = new HttpAuthRealm(null, realm); + creds = (Credentials) credMap.get(entry); + } + } + if (creds == null) { + creds = (Credentials) credMap.get(DEFAULT_AUTH_REALM); + } + return creds; + } + + /** + * Returns a string representation of the credentials. + * @return The string representation. + */ + public String toString() { + StringBuffer sbResult = new StringBuffer(); + Iterator iter = credMap.keySet().iterator(); + while (iter.hasNext()) { + Object key = iter.next(); + Credentials cred = (Credentials) credMap.get(key); + if (sbResult.length() > 0) { + sbResult.append(", "); + } + sbResult.append(key); + sbResult.append("#"); + sbResult.append(cred.toString()); + } + return sbResult.toString(); + } + + } + } Index: java/org/apache/commons/httpclient/auth/HttpAuthenticator.java =================================================================== retrieving revision 1.16 diff -u -r1.16 HttpAuthenticator.java --- java/org/apache/commons/httpclient/auth/HttpAuthenticator.java 12 Jan 2004 18:50:14 -0000 1.16 +++ java/org/apache/commons/httpclient/auth/HttpAuthenticator.java 29 Jan 2004 10:23:56 -0000 @@ -198,7 +198,7 @@ host = proxy ? conn.getProxyHost() : conn.getHost(); } Credentials credentials = proxy - ? state.getProxyCredentials(null, host) : state.getCredentials(null, host); + ? state.getProxyCredentials((AuthScheme) null, host) : state.getCredentials((AuthScheme) null, host); if (credentials == null) { return false; } @@ -329,8 +329,8 @@ LOG.debug(buffer.toString()); } Credentials credentials = proxy - ? state.getProxyCredentials(realm, host) - : state.getCredentials(realm, host); + ? state.getProxyCredentials(authscheme, host) + : state.getCredentials(authscheme, host); if (credentials == null) { StringBuffer buffer = new StringBuffer(); buffer.append("No credentials available for the "); Index: test/org/apache/commons/httpclient/TestAuthenticator.java =================================================================== retrieving revision 1.38 diff -u -r1.38 TestAuthenticator.java --- test/org/apache/commons/httpclient/TestAuthenticator.java 13 Jan 2004 18:47:27 -0000 1.38 +++ test/org/apache/commons/httpclient/TestAuthenticator.java 29 Jan 2004 10:23:57 -0000 @@ -135,10 +135,9 @@ } } } - String realm = authscheme.getRealm(); Credentials credentials = proxy - ? state.getProxyCredentials(realm, host) - : state.getCredentials(realm, host); + ? state.getProxyCredentials(authscheme, host) + : state.getCredentials(authscheme, host); if (credentials == null) { throw new CredentialsNotAvailableException("No credentials available"); } Index: src/java/org/apache/commons/httpclient/auth/CredentialsProvider.java =================================================================== RCS file: src/java/org/apache/commons/httpclient/auth/CredentialsProvider.java diff -N src/java/org/apache/commons/httpclient/auth/CredentialsProvider.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/java/org/apache/commons/httpclient/auth/CredentialsProvider.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,12 @@ +package org.apache.commons.httpclient.auth; + +import org.apache.commons.httpclient.Credentials; + +/** + * + * @author Ortwin Glück + */ +public interface CredentialsProvider { + public Credentials getCredentials(final AuthScheme authscheme, final String host) + throws CredentialsNotAvailableException; +}