You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/10/18 16:44:28 UTC

svn commit: r585984 - in /jakarta/httpcomponents/httpclient/trunk: ./ module-client/src/main/java/org/apache/http/client/ module-client/src/main/java/org/apache/http/client/params/ module-client/src/main/java/org/apache/http/impl/client/

Author: olegk
Date: Thu Oct 18 07:44:26 2007
New Revision: 585984

URL: http://svn.apache.org/viewvc?rev=585984&view=rev
Log:
HTTPCLIENT-477: Use distinct instances of the authentication handler interface for authentication with target and proxy hosts

Contributed by Oleg Kalnichevski

Added:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractAuthenticationHandler.java
      - copied, changed from r580692, jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultProxyAuthenticationHandler.java
      - copied, changed from r580692, jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultTargetAuthenticationHandler.java
      - copied, changed from r580692, jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java
Removed:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java
Modified:
    jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/AuthenticationHandler.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/params/ClientPNames.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java

Modified: jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=585984&r1=585983&r2=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ jakarta/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Thu Oct 18 07:44:26 2007
@@ -1,5 +1,9 @@
 Changes since release 4.0 Alpha 1
 
+* [HTTPCLIENT-477] Use distinct instances of the authentication handler 
+  interface for authentication with target and proxy hosts
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * [HTTPCLIENT-690] ManagedClientConnection provides access to SSLSession
   Contributed by Roland Weber <rolandw at apache.org>
 

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/AuthenticationHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/AuthenticationHandler.java?rev=585984&r1=585983&r2=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/AuthenticationHandler.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/AuthenticationHandler.java Thu Oct 18 07:44:26 2007
@@ -44,25 +44,12 @@
  */
 public interface AuthenticationHandler {
 
-    boolean isTargetAuthenticationRequested(
-            HttpResponse response, 
-            HttpContext context);
+    boolean isAuthenticationRequested(HttpResponse response, HttpContext context);
     
-    boolean isProxyAuthenticationRequested(
-            HttpResponse response, 
-            HttpContext context);
+    Map getChallenges(HttpResponse response, HttpContext context) 
+        throws MalformedChallengeException;
     
-    Map getTargetChallenges(
-            HttpResponse response, 
-            HttpContext context) throws MalformedChallengeException;
-    
-    Map getProxyChallenges(
-            HttpResponse response, 
-            HttpContext context) throws MalformedChallengeException;
-
-    AuthScheme selectScheme(
-            Map challenges, 
-            HttpResponse response, 
-            HttpContext context) throws AuthenticationException;
+    AuthScheme selectScheme(Map challenges, HttpResponse response, HttpContext context) 
+        throws AuthenticationException;
     
 }

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/params/ClientPNames.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/params/ClientPNames.java?rev=585984&r1=585983&r2=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/params/ClientPNames.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/params/ClientPNames.java Thu Oct 18 07:44:26 2007
@@ -112,23 +112,6 @@
     public static final String PREEMPTIVE_AUTHENTICATION = "http.protocol.authentication-preemptive";
 
     /**
-     * The key used to look up the list of IDs of supported
-     * {@link AuthPolicy authentication schemes} in their order of preference.
-     * The scheme IDs are stored in a {@link java.util.Collection} as
-     * instances of {@link java.lang.String}.
-     * 
-     * <p>
-     * If several schemes are returned in the <tt>WWW-Authenticate</tt> 
-     * or <tt>Proxy-Authenticate</tt> header, this parameter defines which
-     * {@link AuthPolicy authentication schemes} takes precedence over others.
-     * The first item in the collection represents the most preferred 
-     * {@link AuthPolicy authentication scheme}, the last item represents
-     * the ID of the least preferred one.
-     * </p>
-     */
-    public static final String AUTH_SCHEME_PRIORITY = "http.protocol-auth-scheme-priority";
-
-    /**
      * Defines the name of the cookie specification to be used for HTTP state management.
      * <p>
      * This parameter expects a value of type {@link String}.
@@ -161,7 +144,6 @@
      * </p>
      */
     public static final String DEFAULT_HOST = "http.default-host";
-
 
 }
 

Copied: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractAuthenticationHandler.java (from r580692, jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractAuthenticationHandler.java?p2=jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractAuthenticationHandler.java&p1=jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java&r1=580692&r2=585984&rev=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractAuthenticationHandler.java Thu Oct 18 07:44:26 2007
@@ -32,7 +32,6 @@
 package org.apache.http.impl.client;
 
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -40,19 +39,15 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.http.Header;
 import org.apache.http.FormattedHeader;
+import org.apache.http.Header;
 import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.auth.AUTH;
 import org.apache.http.auth.AuthScheme;
 import org.apache.http.auth.AuthSchemeRegistry;
 import org.apache.http.auth.AuthenticationException;
 import org.apache.http.auth.MalformedChallengeException;
 import org.apache.http.client.AuthenticationHandler;
-import org.apache.http.client.params.ClientPNames;
 import org.apache.http.client.protocol.ClientContext;
-import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.util.CharArrayBuffer;
@@ -60,39 +55,19 @@
 /**
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  */
-public class DefaultAuthenticationHandler implements AuthenticationHandler {
+public abstract class AbstractAuthenticationHandler implements AuthenticationHandler {
 
-    private static final Log LOG = LogFactory.getLog(DefaultAuthenticationHandler.class);
+    private static final Log LOG = LogFactory.getLog(AbstractAuthenticationHandler.class);
     
     private static List DEFAULT_SCHEME_PRIORITY = Arrays.asList(new String[] {
             "digest",
             "basic"
     });
     
-    public DefaultAuthenticationHandler() {
+    public AbstractAuthenticationHandler() {
         super();
     }
     
-    public boolean isTargetAuthenticationRequested(
-            final HttpResponse response, 
-            final HttpContext context) {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        int status = response.getStatusLine().getStatusCode();
-        return status == HttpStatus.SC_UNAUTHORIZED;
-    }
-
-    public boolean isProxyAuthenticationRequested(
-            final HttpResponse response, 
-            final HttpContext context) {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        int status = response.getStatusLine().getStatusCode();
-        return status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED;
-    }
-
     protected Map parseChallenges(
             final Header[] headers) throws MalformedChallengeException {
         
@@ -127,26 +102,10 @@
         return map;
     }
     
-    public Map getTargetChallenges(
-            final HttpResponse response, 
-            final HttpContext context) throws MalformedChallengeException {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        Header[] headers = response.getHeaders(AUTH.WWW_AUTH);
-        return parseChallenges(headers);
-    }
-
-    public Map getProxyChallenges(
-            final HttpResponse response, 
-            final HttpContext context) throws MalformedChallengeException {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        Header[] headers = response.getHeaders(AUTH.PROXY_AUTH);
-        return parseChallenges(headers);
+    protected List getAuthPreferences() {
+        return DEFAULT_SCHEME_PRIORITY;
     }
-
+    
     public AuthScheme selectScheme(
             final Map challenges, 
             final HttpResponse response,
@@ -158,12 +117,7 @@
             throw new IllegalStateException("AuthScheme registry not set in HTTP context");
         }
         
-        HttpParams params = response.getParams();
-        Collection authPrefs = (Collection) params.getParameter(
-                ClientPNames.AUTH_SCHEME_PRIORITY);
-        if (authPrefs == null || authPrefs.isEmpty()) {
-            authPrefs = DEFAULT_SCHEME_PRIORITY;    
-        }
+        List authPrefs = getAuthPreferences();
         if (LOG.isDebugEnabled()) {
             LOG.debug("Supported authentication schemes in the order of preference: " 
                 + authPrefs);
@@ -178,7 +132,7 @@
                     LOG.debug(id + " authentication scheme selected");
                 }
                 try {
-                    authScheme = registry.getAuthScheme(id, params);
+                    authScheme = registry.getAuthScheme(id, response.getParams());
                 } catch (IllegalStateException e) {
                     throw new AuthenticationException(e.getMessage());
                 }

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java?rev=585984&r1=585983&r2=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java Thu Oct 18 07:44:26 2007
@@ -106,8 +106,11 @@
     /** The redirect handler. */
     private RedirectHandler redirectHandler;
 
-    /** The authentication handler. */
-    private AuthenticationHandler authHandler;
+    /** The target authentication handler. */
+    private AuthenticationHandler targetAuthHandler;
+
+    /** The proxy authentication handler. */
+    private AuthenticationHandler proxyAuthHandler;
 
     /** The cookie store. */
     private CookieStore cookieStore;
@@ -159,7 +162,10 @@
     protected abstract RedirectHandler createRedirectHandler();
 
     
-    protected abstract AuthenticationHandler createAuthenticationHandler();
+    protected abstract AuthenticationHandler createTargetAuthenticationHandler();
+
+    
+    protected abstract AuthenticationHandler createProxyAuthenticationHandler();
 
     
     protected abstract CookieStore createCookieStore();
@@ -285,16 +291,31 @@
     }
 
 
-    public synchronized final AuthenticationHandler getAuthenticationHandler() {
-        if (authHandler == null) {
-            authHandler = createAuthenticationHandler();
+    public synchronized final AuthenticationHandler getTargetAuthenticationHandler() {
+        if (targetAuthHandler == null) {
+            targetAuthHandler = createTargetAuthenticationHandler();
+        }
+        return targetAuthHandler;
+    }
+
+
+    public synchronized void setTargetAuthenticationHandler(
+            final AuthenticationHandler targetAuthHandler) {
+        this.targetAuthHandler = targetAuthHandler;
+    }
+
+
+    public synchronized final AuthenticationHandler getProxyAuthenticationHandler() {
+        if (proxyAuthHandler == null) {
+            proxyAuthHandler = createProxyAuthenticationHandler();
         }
-        return authHandler;
+        return proxyAuthHandler;
     }
 
 
-    public synchronized void setAuthenticationHandler(final AuthenticationHandler authHandler) {
-        this.authHandler = authHandler;
+    public synchronized void setProxyAuthenticationHandler(
+            final AuthenticationHandler proxyAuthHandler) {
+        this.proxyAuthHandler = proxyAuthHandler;
     }
 
 
@@ -495,7 +516,8 @@
                     getHttpProcessor().copy(),
                     getHttpRequestRetryHandler(),
                     getRedirectHandler(),
-                    getAuthenticationHandler(),
+                    getTargetAuthenticationHandler(),
+                    getProxyAuthenticationHandler(),
                     getParams());
         }
 

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java?rev=585984&r1=585983&r2=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java Thu Oct 18 07:44:26 2007
@@ -123,8 +123,11 @@
     /** The redirect handler. */
     protected final RedirectHandler redirectHandler;
     
-    /** The authentication handler. */
-    private final AuthenticationHandler authHandler;
+    /** The target authentication handler. */
+    private final AuthenticationHandler targetAuthHandler;
+    
+    /** The proxy authentication handler. */
+    private final AuthenticationHandler proxyAuthHandler;
     
     /** The HTTP parameters. */
     protected final HttpParams params;
@@ -146,7 +149,8 @@
             final HttpProcessor httpProcessor,
             final HttpRequestRetryHandler retryHandler,
             final RedirectHandler redirectHandler,
-            final AuthenticationHandler authHandler,
+            final AuthenticationHandler targetAuthHandler,
+            final AuthenticationHandler proxyAuthHandler,
             final HttpParams params) {
 
         if (conman == null) {
@@ -164,22 +168,26 @@
         if (redirectHandler == null) {
             throw new IllegalArgumentException("Redirect handler may not be null");
         }
-        if (authHandler == null) {
-            throw new IllegalArgumentException("Authentication handler may not be null");
+        if (targetAuthHandler == null) {
+            throw new IllegalArgumentException("Target authentication handler may not be null");
+        }
+        if (proxyAuthHandler == null) {
+            throw new IllegalArgumentException("Proxy authentication handler may not be null");
         }
         if (params == null) {
             throw new IllegalArgumentException("HTTP parameters may not be null");
         }
-        this.connManager   = conman;
-        this.reuseStrategy = reustrat;
-        this.httpProcessor = httpProcessor;
-        this.retryHandler  = retryHandler;
-        this.redirectHandler = redirectHandler;
-        this.authHandler   = authHandler;
-        this.params        = params;
-        this.requestExec   = new HttpRequestExecutor();
+        this.connManager       = conman;
+        this.reuseStrategy     = reustrat;
+        this.httpProcessor     = httpProcessor;
+        this.retryHandler      = retryHandler;
+        this.redirectHandler   = redirectHandler;
+        this.targetAuthHandler = targetAuthHandler;
+        this.proxyAuthHandler  = proxyAuthHandler;
+        this.params            = params;
+        this.requestExec       = new HttpRequestExecutor();
 
-        this.managedConn   = null;
+        this.managedConn       = null;
         
         this.redirectCount = 0;
         this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
@@ -596,12 +604,14 @@
                 context.getAttribute(ClientContext.CREDS_PROVIDER);
             
             if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
-                if (this.authHandler.isProxyAuthenticationRequested(response, context)) {
+                if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {
 
                     LOG.debug("Proxy requested authentication");
-                    Map challenges = this.authHandler.getProxyChallenges(response, context);
+                    Map challenges = this.proxyAuthHandler.getChallenges(response, context);
                     try {
-                        processChallenges(challenges, this.proxyAuthState, response, context);
+                        processChallenges(
+                                challenges, this.proxyAuthState, this.proxyAuthHandler, 
+                                response, context);
                     } catch (AuthenticationException ex) {
                         if (LOG.isWarnEnabled()) {
                             LOG.warn("Authentication error: " +  ex.getMessage());
@@ -805,7 +815,7 @@
     
         if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
 
-            if (this.authHandler.isTargetAuthenticationRequested(response, context)) {
+            if (this.targetAuthHandler.isAuthenticationRequested(response, context)) {
 
                 target = (HttpHost)
                     context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
@@ -814,9 +824,11 @@
                 }
                 
                 LOG.debug("Target requested authentication");
-                Map challenges = this.authHandler.getTargetChallenges(response, context); 
+                Map challenges = this.targetAuthHandler.getChallenges(response, context); 
                 try {
-                    processChallenges(challenges, this.targetAuthState, response, context);
+                    processChallenges(challenges, 
+                            this.targetAuthState, this.targetAuthHandler,
+                            response, context);
                 } catch (AuthenticationException ex) {
                     if (LOG.isWarnEnabled()) {
                         LOG.warn("Authentication error: " +  ex.getMessage());
@@ -836,12 +848,14 @@
                 this.targetAuthState.setAuthScope(null);
             }
             
-            if (this.authHandler.isProxyAuthenticationRequested(response, context)) {
+            if (this.proxyAuthHandler.isAuthenticationRequested(response, context)) {
 
                 LOG.debug("Proxy requested authentication");
-                Map challenges = this.authHandler.getProxyChallenges(response, context);
+                Map challenges = this.proxyAuthHandler.getChallenges(response, context);
                 try {
-                    processChallenges(challenges, this.proxyAuthState, response, context);
+                    processChallenges(challenges, 
+                            this.proxyAuthState, this.proxyAuthHandler, 
+                            response, context);
                 } catch (AuthenticationException ex) {
                     if (LOG.isWarnEnabled()) {
                         LOG.warn("Authentication error: " +  ex.getMessage());
@@ -895,6 +909,7 @@
     private void processChallenges(
             final Map challenges, 
             final AuthState authState,
+            final AuthenticationHandler authHandler,
             final HttpResponse response, 
             final HttpContext context) 
                 throws MalformedChallengeException, AuthenticationException {
@@ -902,18 +917,17 @@
         AuthScheme authScheme = authState.getAuthScheme();
         if (authScheme == null) {
             // Authentication not attempted before
-            authScheme = this.authHandler.selectScheme(challenges, response, context);
+            authScheme = authHandler.selectScheme(challenges, response, context);
             authState.setAuthScheme(authScheme);
         }
-        AuthScheme authscheme = authState.getAuthScheme();
-        String id = authscheme.getSchemeName();
+        String id = authScheme.getSchemeName();
 
         Header challenge = (Header) challenges.get(id.toLowerCase());
         if (challenge == null) {
             throw new AuthenticationException(id + 
                 " authorization challenge expected, but not found");
         }
-        authscheme.processChallenge(challenge);
+        authScheme.processChallenge(challenge);
         LOG.debug("Authorization challenge processed");
     }
     

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java?rev=585984&r1=585983&r2=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java Thu Oct 18 07:44:26 2007
@@ -249,8 +249,13 @@
     }
 
 
-    protected AuthenticationHandler createAuthenticationHandler() {
-        return new DefaultAuthenticationHandler();
+    protected AuthenticationHandler createTargetAuthenticationHandler() {
+        return new DefaultTargetAuthenticationHandler();
+    }
+
+
+    protected AuthenticationHandler createProxyAuthenticationHandler() {
+        return new DefaultProxyAuthenticationHandler();
     }
 
 

Copied: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultProxyAuthenticationHandler.java (from r580692, jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultProxyAuthenticationHandler.java?p2=jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultProxyAuthenticationHandler.java&p1=jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java&r1=580692&r2=585984&rev=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultProxyAuthenticationHandler.java Thu Oct 18 07:44:26 2007
@@ -31,59 +31,25 @@
 
 package org.apache.http.impl.client;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.http.Header;
-import org.apache.http.FormattedHeader;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.auth.AUTH;
-import org.apache.http.auth.AuthScheme;
-import org.apache.http.auth.AuthSchemeRegistry;
-import org.apache.http.auth.AuthenticationException;
 import org.apache.http.auth.MalformedChallengeException;
-import org.apache.http.client.AuthenticationHandler;
-import org.apache.http.client.params.ClientPNames;
-import org.apache.http.client.protocol.ClientContext;
-import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
-import org.apache.http.util.CharArrayBuffer;
 
 /**
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  */
-public class DefaultAuthenticationHandler implements AuthenticationHandler {
+public class DefaultProxyAuthenticationHandler extends AbstractAuthenticationHandler {
 
-    private static final Log LOG = LogFactory.getLog(DefaultAuthenticationHandler.class);
-    
-    private static List DEFAULT_SCHEME_PRIORITY = Arrays.asList(new String[] {
-            "digest",
-            "basic"
-    });
-    
-    public DefaultAuthenticationHandler() {
+    public DefaultProxyAuthenticationHandler() {
         super();
     }
     
-    public boolean isTargetAuthenticationRequested(
-            final HttpResponse response, 
-            final HttpContext context) {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        int status = response.getStatusLine().getStatusCode();
-        return status == HttpStatus.SC_UNAUTHORIZED;
-    }
-
-    public boolean isProxyAuthenticationRequested(
+    public boolean isAuthenticationRequested(
             final HttpResponse response, 
             final HttpContext context) {
         if (response == null) {
@@ -93,51 +59,7 @@
         return status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED;
     }
 
-    protected Map parseChallenges(
-            final Header[] headers) throws MalformedChallengeException {
-        
-        Map map = new HashMap(headers.length);
-        for (int i = 0; i < headers.length; i++) {
-            Header header = headers[i];
-            CharArrayBuffer buffer;
-            int pos;
-            if (header instanceof FormattedHeader) {
-                buffer = ((FormattedHeader) header).getBuffer();
-                pos = ((FormattedHeader) header).getValuePos();
-            } else {
-                String s = header.getValue();
-                if (s == null) {
-                    throw new MalformedChallengeException("Header value is null");
-                }
-                buffer = new CharArrayBuffer(s.length());
-                buffer.append(s);
-                pos = 0;
-            }
-            while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
-                pos++;
-            }
-            int beginIndex = pos;
-            while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
-                pos++;
-            }
-            int endIndex = pos;
-            String s = buffer.substring(beginIndex, endIndex);
-            map.put(s.toLowerCase(), header);
-        }
-        return map;
-    }
-    
-    public Map getTargetChallenges(
-            final HttpResponse response, 
-            final HttpContext context) throws MalformedChallengeException {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        Header[] headers = response.getHeaders(AUTH.WWW_AUTH);
-        return parseChallenges(headers);
-    }
-
-    public Map getProxyChallenges(
+    public Map getChallenges(
             final HttpResponse response, 
             final HttpContext context) throws MalformedChallengeException {
         if (response == null) {
@@ -145,58 +67,6 @@
         }
         Header[] headers = response.getHeaders(AUTH.PROXY_AUTH);
         return parseChallenges(headers);
-    }
-
-    public AuthScheme selectScheme(
-            final Map challenges, 
-            final HttpResponse response,
-            final HttpContext context) throws AuthenticationException {
-        
-        AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(
-                ClientContext.AUTHSCHEME_REGISTRY);
-        if (registry == null) {
-            throw new IllegalStateException("AuthScheme registry not set in HTTP context");
-        }
-        
-        HttpParams params = response.getParams();
-        Collection authPrefs = (Collection) params.getParameter(
-                ClientPNames.AUTH_SCHEME_PRIORITY);
-        if (authPrefs == null || authPrefs.isEmpty()) {
-            authPrefs = DEFAULT_SCHEME_PRIORITY;    
-        }
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Supported authentication schemes in the order of preference: " 
-                + authPrefs);
-        }
-
-        AuthScheme authScheme = null;
-        for (Iterator it = authPrefs.iterator(); it.hasNext(); ) {
-            String id = (String) it.next();
-            Header challenge = (Header) challenges.get(id.toLowerCase()); 
-            if (challenge != null) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug(id + " authentication scheme selected");
-                }
-                try {
-                    authScheme = registry.getAuthScheme(id, params);
-                } catch (IllegalStateException e) {
-                    throw new AuthenticationException(e.getMessage());
-                }
-                break;
-            } else {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Challenge for " + id + " authentication scheme not available");
-                    // Try again
-                }
-            }
-        }
-        if (authScheme == null) {
-            // If none selected, something is wrong
-            throw new AuthenticationException(
-                "Unable to respond to any of these challenges: "
-                    + challenges);
-        }
-        return authScheme;
     }
 
 }

Copied: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultTargetAuthenticationHandler.java (from r580692, jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultTargetAuthenticationHandler.java?p2=jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultTargetAuthenticationHandler.java&p1=jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java&r1=580692&r2=585984&rev=585984&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultAuthenticationHandler.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultTargetAuthenticationHandler.java Thu Oct 18 07:44:26 2007
@@ -31,49 +31,25 @@
 
 package org.apache.http.impl.client;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.http.Header;
-import org.apache.http.FormattedHeader;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.auth.AUTH;
-import org.apache.http.auth.AuthScheme;
-import org.apache.http.auth.AuthSchemeRegistry;
-import org.apache.http.auth.AuthenticationException;
 import org.apache.http.auth.MalformedChallengeException;
-import org.apache.http.client.AuthenticationHandler;
-import org.apache.http.client.params.ClientPNames;
-import org.apache.http.client.protocol.ClientContext;
-import org.apache.http.params.HttpParams;
-import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
-import org.apache.http.util.CharArrayBuffer;
 
 /**
  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  */
-public class DefaultAuthenticationHandler implements AuthenticationHandler {
+public class DefaultTargetAuthenticationHandler extends AbstractAuthenticationHandler {
 
-    private static final Log LOG = LogFactory.getLog(DefaultAuthenticationHandler.class);
-    
-    private static List DEFAULT_SCHEME_PRIORITY = Arrays.asList(new String[] {
-            "digest",
-            "basic"
-    });
-    
-    public DefaultAuthenticationHandler() {
+    public DefaultTargetAuthenticationHandler() {
         super();
     }
     
-    public boolean isTargetAuthenticationRequested(
+    public boolean isAuthenticationRequested(
             final HttpResponse response, 
             final HttpContext context) {
         if (response == null) {
@@ -83,51 +59,7 @@
         return status == HttpStatus.SC_UNAUTHORIZED;
     }
 
-    public boolean isProxyAuthenticationRequested(
-            final HttpResponse response, 
-            final HttpContext context) {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        int status = response.getStatusLine().getStatusCode();
-        return status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED;
-    }
-
-    protected Map parseChallenges(
-            final Header[] headers) throws MalformedChallengeException {
-        
-        Map map = new HashMap(headers.length);
-        for (int i = 0; i < headers.length; i++) {
-            Header header = headers[i];
-            CharArrayBuffer buffer;
-            int pos;
-            if (header instanceof FormattedHeader) {
-                buffer = ((FormattedHeader) header).getBuffer();
-                pos = ((FormattedHeader) header).getValuePos();
-            } else {
-                String s = header.getValue();
-                if (s == null) {
-                    throw new MalformedChallengeException("Header value is null");
-                }
-                buffer = new CharArrayBuffer(s.length());
-                buffer.append(s);
-                pos = 0;
-            }
-            while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
-                pos++;
-            }
-            int beginIndex = pos;
-            while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
-                pos++;
-            }
-            int endIndex = pos;
-            String s = buffer.substring(beginIndex, endIndex);
-            map.put(s.toLowerCase(), header);
-        }
-        return map;
-    }
-    
-    public Map getTargetChallenges(
+    public Map getChallenges(
             final HttpResponse response, 
             final HttpContext context) throws MalformedChallengeException {
         if (response == null) {
@@ -135,68 +67,6 @@
         }
         Header[] headers = response.getHeaders(AUTH.WWW_AUTH);
         return parseChallenges(headers);
-    }
-
-    public Map getProxyChallenges(
-            final HttpResponse response, 
-            final HttpContext context) throws MalformedChallengeException {
-        if (response == null) {
-            throw new IllegalArgumentException("HTTP response may not be null");
-        }
-        Header[] headers = response.getHeaders(AUTH.PROXY_AUTH);
-        return parseChallenges(headers);
-    }
-
-    public AuthScheme selectScheme(
-            final Map challenges, 
-            final HttpResponse response,
-            final HttpContext context) throws AuthenticationException {
-        
-        AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(
-                ClientContext.AUTHSCHEME_REGISTRY);
-        if (registry == null) {
-            throw new IllegalStateException("AuthScheme registry not set in HTTP context");
-        }
-        
-        HttpParams params = response.getParams();
-        Collection authPrefs = (Collection) params.getParameter(
-                ClientPNames.AUTH_SCHEME_PRIORITY);
-        if (authPrefs == null || authPrefs.isEmpty()) {
-            authPrefs = DEFAULT_SCHEME_PRIORITY;    
-        }
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Supported authentication schemes in the order of preference: " 
-                + authPrefs);
-        }
-
-        AuthScheme authScheme = null;
-        for (Iterator it = authPrefs.iterator(); it.hasNext(); ) {
-            String id = (String) it.next();
-            Header challenge = (Header) challenges.get(id.toLowerCase()); 
-            if (challenge != null) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug(id + " authentication scheme selected");
-                }
-                try {
-                    authScheme = registry.getAuthScheme(id, params);
-                } catch (IllegalStateException e) {
-                    throw new AuthenticationException(e.getMessage());
-                }
-                break;
-            } else {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Challenge for " + id + " authentication scheme not available");
-                    // Try again
-                }
-            }
-        }
-        if (authScheme == null) {
-            // If none selected, something is wrong
-            throw new AuthenticationException(
-                "Unable to respond to any of these challenges: "
-                    + challenges);
-        }
-        return authScheme;
     }
 
 }