Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5C3639ABC for ; Fri, 10 Feb 2012 14:28:58 +0000 (UTC) Received: (qmail 61312 invoked by uid 500); 10 Feb 2012 14:28:58 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 61275 invoked by uid 500); 10 Feb 2012 14:28:58 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 61268 invoked by uid 99); 10 Feb 2012 14:28:57 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Feb 2012 14:28:57 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Feb 2012 14:28:51 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7A41023888E7 for ; Fri, 10 Feb 2012 14:28:29 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1242789 - in /httpcomponents/httpasyncclient/trunk: ./ httpasyncclient/src/main/java/org/apache/http/impl/nio/client/ httpasyncclient/src/test/java/org/apache/http/impl/nio/client/ Date: Fri, 10 Feb 2012 14:28:29 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120210142829.7A41023888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Fri Feb 10 14:28:28 2012 New Revision: 1242789 URL: http://svn.apache.org/viewvc?rev=1242789&view=rev Log: Upgrade to HttpClient 4.2 API Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java (with props) httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java (with props) Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultHttpAsyncClient.java httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java httpcomponents/httpasyncclient/trunk/pom.xml Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java?rev=1242789&r1=1242788&r2=1242789&view=diff ============================================================================== --- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java (original) +++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/AbstractHttpAsyncClient.java Fri Feb 10 14:28:28 2012 @@ -41,7 +41,7 @@ import org.apache.http.HttpRequestInterc import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; import org.apache.http.auth.AuthSchemeRegistry; -import org.apache.http.client.AuthenticationHandler; +import org.apache.http.client.AuthenticationStrategy; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CookieStore; import org.apache.http.client.CredentialsProvider; @@ -60,15 +60,16 @@ import org.apache.http.cookie.CookieSpec import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.auth.BasicSchemeFactory; import org.apache.http.impl.auth.DigestSchemeFactory; +import org.apache.http.impl.auth.KerberosSchemeFactory; import org.apache.http.impl.auth.NTLMSchemeFactory; -import org.apache.http.impl.auth.NegotiateSchemeFactory; +import org.apache.http.impl.auth.SPNegoSchemeFactory; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; -import org.apache.http.impl.client.DefaultProxyAuthenticationHandler; import org.apache.http.impl.client.DefaultRedirectStrategy; -import org.apache.http.impl.client.DefaultTargetAuthenticationHandler; import org.apache.http.impl.client.DefaultUserTokenHandler; +import org.apache.http.impl.client.ProxyAuthenticationStrategy; +import org.apache.http.impl.client.TargetAuthenticationStrategy; import org.apache.http.impl.cookie.BestMatchSpecFactory; import org.apache.http.impl.cookie.BrowserCompatSpecFactory; import org.apache.http.impl.cookie.IgnoreSpecFactory; @@ -112,8 +113,8 @@ public abstract class AbstractHttpAsyncC private CookieSpecRegistry supportedCookieSpecs; private CookieStore cookieStore; private AuthSchemeRegistry supportedAuthSchemes; - private AuthenticationHandler targetAuthHandler; - private AuthenticationHandler proxyAuthHandler; + private AuthenticationStrategy targetAuthStrategy; + private AuthenticationStrategy proxyAuthStrategy; private CredentialsProvider credsProvider; private HttpRoutePlanner routePlanner; private UserTokenHandler userTokenHandler; @@ -180,7 +181,10 @@ public abstract class AbstractHttpAsyncC new NTLMSchemeFactory()); registry.register( AuthPolicy.SPNEGO, - new NegotiateSchemeFactory()); + new SPNegoSchemeFactory()); + registry.register( + AuthPolicy.KERBEROS, + new KerberosSchemeFactory()); return registry; } @@ -207,12 +211,12 @@ public abstract class AbstractHttpAsyncC return registry; } - protected AuthenticationHandler createTargetAuthenticationHandler() { - return new DefaultTargetAuthenticationHandler(); + protected AuthenticationStrategy createTargetAuthenticationStrategy() { + return new TargetAuthenticationStrategy(); } - protected AuthenticationHandler createProxyAuthenticationHandler() { - return new DefaultProxyAuthenticationHandler(); + protected AuthenticationStrategy createProxyAuthenticationStrategy() { + return new ProxyAuthenticationStrategy(); } protected CookieStore createCookieStore() { @@ -301,28 +305,28 @@ public abstract class AbstractHttpAsyncC this.supportedCookieSpecs = cookieSpecRegistry; } - public synchronized final AuthenticationHandler getTargetAuthenticationHandler() { - if (this.targetAuthHandler == null) { - this.targetAuthHandler = createTargetAuthenticationHandler(); + public synchronized final AuthenticationStrategy getTargetAuthenticationStrategy() { + if (this.targetAuthStrategy == null) { + this.targetAuthStrategy = createTargetAuthenticationStrategy(); } - return this.targetAuthHandler; + return this.targetAuthStrategy; } - public synchronized void setTargetAuthenticationHandler( - final AuthenticationHandler targetAuthHandler) { - this.targetAuthHandler = targetAuthHandler; + public synchronized void setTargetAuthenticationStrategy( + final AuthenticationStrategy targetAuthStrategy) { + this.targetAuthStrategy = targetAuthStrategy; } - public synchronized final AuthenticationHandler getProxyAuthenticationHandler() { - if (this.proxyAuthHandler == null) { - this.proxyAuthHandler = createProxyAuthenticationHandler(); + public synchronized final AuthenticationStrategy getProxyAuthenticationStrategy() { + if (this.proxyAuthStrategy == null) { + this.proxyAuthStrategy = createProxyAuthenticationStrategy(); } - return this.proxyAuthHandler; + return this.proxyAuthStrategy; } - public synchronized void setProxyAuthenticationHandler( - final AuthenticationHandler proxyAuthHandler) { - this.proxyAuthHandler = proxyAuthHandler; + public synchronized void setProxyAuthenticationStrategy( + final AuthenticationStrategy proxyAuthStrategy) { + this.proxyAuthStrategy = proxyAuthStrategy; } public synchronized final CookieStore getCookieStore() { @@ -527,8 +531,8 @@ public abstract class AbstractHttpAsyncC getConnectionReuseStrategy(), getConnectionKeepAliveStrategy(), getRedirectStrategy(), - getTargetAuthenticationHandler(), - getProxyAuthenticationHandler(), + getTargetAuthenticationStrategy(), + getProxyAuthenticationStrategy(), getUserTokenHandler(), getParams()); } Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java?rev=1242789&r1=1242788&r2=1242789&view=diff ============================================================================== --- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java (original) +++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java Fri Feb 10 14:28:28 2012 @@ -30,14 +30,11 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; -import java.util.Locale; -import java.util.Map; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log; import org.apache.http.ConnectionReuseStrategy; -import org.apache.http.Header; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpException; import org.apache.http.HttpHost; @@ -46,13 +43,11 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.ProtocolException; import org.apache.http.ProtocolVersion; +import org.apache.http.auth.AuthProtocolState; import org.apache.http.auth.AuthScheme; -import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthState; -import org.apache.http.auth.AuthenticationException; -import org.apache.http.auth.Credentials; -import org.apache.http.auth.MalformedChallengeException; -import org.apache.http.client.AuthenticationHandler; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.AuthenticationStrategy; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.NonRepeatableRequestException; import org.apache.http.client.RedirectException; @@ -69,8 +64,10 @@ import org.apache.http.conn.routing.Basi import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRouteDirector; import org.apache.http.conn.routing.HttpRoutePlanner; +import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.ClientParamsStack; import org.apache.http.impl.client.EntityEnclosingRequestWrapper; +import org.apache.http.impl.client.HttpAuthenticator; import org.apache.http.impl.client.RequestWrapper; import org.apache.http.impl.client.RoutedRequest; import org.apache.http.message.BasicHttpRequest; @@ -106,11 +103,12 @@ class DefaultAsyncRequestDirector imp private final ConnectionReuseStrategy reuseStrategy; private final ConnectionKeepAliveStrategy keepaliveStrategy; private final RedirectStrategy redirectStrategy; - private final AuthenticationHandler targetAuthHandler; - private final AuthenticationHandler proxyAuthHandler; + private final AuthenticationStrategy targetAuthStrategy; + private final AuthenticationStrategy proxyAuthStrategy; private final UserTokenHandler userTokenHandler; private final AuthState targetAuthState; private final AuthState proxyAuthState; + private final HttpAuthenticator authenticator; private final HttpParams clientParams; private RoutedRequest mainRequest; @@ -140,8 +138,8 @@ class DefaultAsyncRequestDirector imp final ConnectionReuseStrategy reuseStrategy, final ConnectionKeepAliveStrategy keepaliveStrategy, final RedirectStrategy redirectStrategy, - final AuthenticationHandler targetAuthHandler, - final AuthenticationHandler proxyAuthHandler, + final AuthenticationStrategy targetAuthStrategy, + final AuthenticationStrategy proxyAuthStrategy, final UserTokenHandler userTokenHandler, final HttpParams clientParams) { super(); @@ -157,11 +155,12 @@ class DefaultAsyncRequestDirector imp this.keepaliveStrategy = keepaliveStrategy; this.redirectStrategy = redirectStrategy; this.routeDirector = new BasicRouteDirector(); - this.targetAuthHandler = targetAuthHandler; - this.proxyAuthHandler = proxyAuthHandler; + this.targetAuthStrategy = targetAuthStrategy; + this.proxyAuthStrategy = proxyAuthStrategy; this.userTokenHandler = userTokenHandler; this.targetAuthState = new AuthState(); this.proxyAuthState = new AuthState(); + this.authenticator = new HttpAuthenticator(log); this.clientParams = clientParams; } @@ -239,6 +238,13 @@ class DefaultAsyncRequestDirector imp if (this.currentRequest == null) { this.currentRequest = this.mainRequest.getRequest(); + + String userinfo = this.currentRequest.getURI().getUserInfo(); + if (userinfo != null) { + this.targetAuthState.update( + new BasicScheme(), new UsernamePasswordCredentials(userinfo)); + } + // Re-write request URI if needed rewriteRequestURI(this.currentRequest, route); } @@ -404,8 +410,18 @@ class DefaultAsyncRequestDirector imp } else { this.log.debug("Connection cannot be kept alive"); this.managedConn.unmarkReusable(); - invalidateAuthIfSuccessful(this.proxyAuthState); - invalidateAuthIfSuccessful(this.targetAuthState); + if (this.proxyAuthState.getState() == AuthProtocolState.SUCCESS + && this.proxyAuthState.getAuthScheme() != null + && this.proxyAuthState.getAuthScheme().isConnectionBased()) { + this.log.debug("Resetting proxy auth state"); + this.proxyAuthState.reset(); + } + if (this.targetAuthState.getState() == AuthProtocolState.SUCCESS + && this.targetAuthState.getAuthScheme() != null + && this.targetAuthState.getAuthScheme().isConnectionBased()) { + this.log.debug("Resetting target auth state"); + this.targetAuthState.reset(); + } } if (this.finalResponse != null) { @@ -675,16 +691,14 @@ class DefaultAsyncRequestDirector imp } HttpHost newTarget = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); - // Unset auth scope - this.targetAuthState.setAuthScope(null); - this.proxyAuthState.setAuthScope(null); - - // Invalidate auth states if redirecting to another host + // Reset auth states if redirecting to another host if (!route.getTargetHost().equals(newTarget)) { - this.targetAuthState.invalidate(); + this.log.debug("Resetting target auth state"); + this.targetAuthState.reset(); AuthScheme authScheme = this.proxyAuthState.getAuthScheme(); if (authScheme != null && authScheme.isConnectionBased()) { - this.proxyAuthState.invalidate(); + this.log.debug("Resetting proxy auth state"); + this.proxyAuthState.reset(); } } @@ -703,159 +717,42 @@ class DefaultAsyncRequestDirector imp private RoutedRequest handleTargetChallenge( final CredentialsProvider credsProvider) throws HttpException { - if (this.targetAuthHandler.isAuthenticationRequested(this.currentResponse, this.localContext)) { - HttpRoute route = this.mainRequest.getRoute(); - - HttpHost target = (HttpHost) this.localContext.getAttribute( - ExecutionContext.HTTP_TARGET_HOST); - if (target == null) { - target = route.getTargetHost(); - } - - this.log.debug("Target requested authentication"); - Map challenges = this.targetAuthHandler.getChallenges( - this.currentResponse, this.localContext); - try { - processChallenges(challenges, - this.targetAuthState, this.targetAuthHandler, - this.currentResponse, this.localContext); - } catch (AuthenticationException ex) { - if (this.log.isWarnEnabled()) { - this.log.warn("Authentication error: " + ex.getMessage()); - return null; - } - } - updateAuthState(this.targetAuthState, target, credsProvider); - - if (this.targetAuthState.getCredentials() != null) { + HttpRoute route = this.mainRequest.getRoute(); + HttpHost target = (HttpHost) this.localContext.getAttribute( + ExecutionContext.HTTP_TARGET_HOST); + if (target == null) { + target = route.getTargetHost(); + } + if (this.authenticator.isAuthenticationRequested(target, this.currentResponse, + this.targetAuthStrategy, this.targetAuthState, this.localContext)) { + if (this.authenticator.authenticate(target, this.currentResponse, + this.targetAuthStrategy, this.targetAuthState, this.localContext)) { // Re-try the same request via the same route return this.mainRequest; } else { return null; } - } else { - // Reset target auth scope - this.targetAuthState.setAuthScope(null); } return null; } private RoutedRequest handleProxyChallenge( final CredentialsProvider credsProvider) throws HttpException { - if (this.proxyAuthHandler.isAuthenticationRequested(this.currentResponse, this.localContext)) { - HttpRoute route = this.mainRequest.getRoute(); - - HttpHost proxy = route.getProxyHost(); - - this.log.debug("Proxy requested authentication"); - Map challenges = this.proxyAuthHandler.getChallenges( - this.currentResponse, this.localContext); - try { - processChallenges(challenges, - this.proxyAuthState, this.proxyAuthHandler, - this.currentResponse, this.localContext); - } catch (AuthenticationException ex) { - if (this.log.isWarnEnabled()) { - this.log.warn("Authentication error: " + ex.getMessage()); - return null; - } - } - updateAuthState(this.proxyAuthState, proxy, credsProvider); - - if (this.proxyAuthState.getCredentials() != null) { + HttpRoute route = this.mainRequest.getRoute(); + HttpHost proxy = route.getProxyHost(); + if (this.authenticator.isAuthenticationRequested(proxy, this.currentResponse, + this.proxyAuthStrategy, this.proxyAuthState, this.localContext)) { + if (this.authenticator.authenticate(proxy, this.currentResponse, + this.proxyAuthStrategy, this.proxyAuthState, this.localContext)) { // Re-try the same request via the same route return this.mainRequest; } else { return null; } - } else { - // Reset proxy auth scope - this.proxyAuthState.setAuthScope(null); } return null; } - private void processChallenges( - final Map challenges, - final AuthState authState, - final AuthenticationHandler authHandler, - final HttpResponse response, - final HttpContext context) - throws MalformedChallengeException, AuthenticationException { - - AuthScheme authScheme = authState.getAuthScheme(); - if (authScheme == null) { - // Authentication not attempted before - authScheme = authHandler.selectScheme(challenges, response, context); - authState.setAuthScheme(authScheme); - } - String id = authScheme.getSchemeName(); - - Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH)); - if (challenge == null) { - throw new AuthenticationException(id + - " authorization challenge expected, but not found"); - } - authScheme.processChallenge(challenge); - this.log.debug("Authorization challenge processed"); - } - - private void updateAuthState( - final AuthState authState, - final HttpHost host, - final CredentialsProvider credsProvider) { - - if (!authState.isValid()) { - return; - } - - String hostname = host.getHostName(); - int port = host.getPort(); - if (port < 0) { - AsyncScheme scheme = this.connmgr.getSchemeRegistry().getScheme(host); - port = scheme.getDefaultPort(); - } - - AuthScheme authScheme = authState.getAuthScheme(); - AuthScope authScope = new AuthScope( - hostname, - port, - authScheme.getRealm(), - authScheme.getSchemeName()); - - if (this.log.isDebugEnabled()) { - this.log.debug("Authentication scope: " + authScope); - } - Credentials creds = authState.getCredentials(); - if (creds == null) { - creds = credsProvider.getCredentials(authScope); - if (this.log.isDebugEnabled()) { - if (creds != null) { - this.log.debug("Found credentials"); - } else { - this.log.debug("Credentials not found"); - } - } - } else { - if (authScheme.isComplete()) { - this.log.debug("Authentication failed"); - creds = null; - } - } - authState.setAuthScope(authScope); - authState.setCredentials(creds); - } - - private void invalidateAuthIfSuccessful(final AuthState authState) { - AuthScheme authscheme = authState.getAuthScheme(); - if (authscheme != null - && authscheme.isConnectionBased() - && authscheme.isComplete() - && authState.getCredentials() != null) { - authState.invalidate(); - } - } - public HttpContext getContext() { return this.localContext; } Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultHttpAsyncClient.java URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultHttpAsyncClient.java?rev=1242789&r1=1242788&r2=1242789&view=diff ============================================================================== --- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultHttpAsyncClient.java (original) +++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultHttpAsyncClient.java Fri Feb 10 14:28:28 2012 @@ -33,7 +33,6 @@ import org.apache.http.client.protocol.R import org.apache.http.client.protocol.RequestDefaultHeaders; import org.apache.http.client.protocol.RequestProxyAuthentication; import org.apache.http.client.protocol.RequestTargetAuthentication; -import org.apache.http.client.protocol.ResponseAuthCache; import org.apache.http.client.protocol.ResponseProcessCookies; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.nio.reactor.IOReactorConfig; @@ -103,7 +102,6 @@ public class DefaultHttpAsyncClient exte httpproc.addInterceptor(new ResponseProcessCookies()); // HTTP authentication interceptors httpproc.addInterceptor(new RequestAuthCache()); - httpproc.addInterceptor(new ResponseAuthCache()); httpproc.addInterceptor(new RequestTargetAuthentication()); httpproc.addInterceptor(new RequestProxyAuthentication()); return httpproc; Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java?rev=1242789&r1=1242788&r2=1242789&view=diff ============================================================================== --- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java (original) +++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthentication.java Fri Feb 10 14:28:28 2012 @@ -31,6 +31,7 @@ import java.util.concurrent.ExecutionExc import java.util.concurrent.Future; import org.apache.http.HttpAsyncTestBase; +import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -50,7 +51,7 @@ import org.apache.http.client.methods.Ht import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.DefaultHttpResponseFactory; import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.DefaultTargetAuthenticationHandler; +import org.apache.http.impl.client.TargetAuthenticationStrategy; import org.apache.http.impl.nio.DefaultNHttpServerConnection; import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory; import org.apache.http.localserver.BasicAuthTokenExtractor; @@ -80,6 +81,7 @@ import org.apache.http.protocol.Response import org.apache.http.protocol.ResponseContent; import org.apache.http.protocol.ResponseDate; import org.apache.http.protocol.ResponseServer; +import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -180,20 +182,21 @@ public class TestClientAuthentication ex } - static class TestTargetAuthenticationHandler extends DefaultTargetAuthenticationHandler { + static class TestTargetAuthenticationStrategy extends TargetAuthenticationStrategy { private int count; - public TestTargetAuthenticationHandler() { + public TestTargetAuthenticationStrategy() { super(); this.count = 0; } @Override public boolean isAuthenticationRequested( + final HttpHost authhost, final HttpResponse response, final HttpContext context) { - boolean res = super.isAuthenticationRequested(response, context); + boolean res = super.isAuthenticationRequested(authhost, response, context); if (res == true) { synchronized (this) { this.count++; @@ -445,10 +448,10 @@ public class TestClientAuthentication ex credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test", "test")); - TestTargetAuthenticationHandler authHandler = new TestTargetAuthenticationHandler(); + TestTargetAuthenticationStrategy authStrategy = new TestTargetAuthenticationStrategy(); this.httpclient.setCredentialsProvider(credsProvider); - this.httpclient.setTargetAuthenticationHandler(authHandler); + this.httpclient.setTargetAuthenticationStrategy(authStrategy); HttpContext context = new BasicHttpContext(); @@ -464,7 +467,38 @@ public class TestClientAuthentication ex Assert.assertNotNull(response2); Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode()); - Assert.assertEquals(1, authHandler.getCount()); + Assert.assertEquals(1, authStrategy.getCount()); + } + + @Test + public void testAuthenticationUserinfoInRequestSuccess() throws Exception { + HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry(); + registry.register("*", new BasicAsyncRequestHandler(new AuthHandler())); + HttpHost target = start(registry, null); + HttpGet httpget = new HttpGet("http://test:test@" + target.toHostString() + "/"); + Future future = this.httpclient.execute(target, httpget, null); + HttpResponse response = future.get(); + Assert.assertNotNull(response); + HttpEntity entity = response.getEntity(); + Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); + Assert.assertNotNull(entity); + EntityUtils.consume(entity); + } + + @Test + public void testAuthenticationUserinfoInRequestFailure() throws Exception { + HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry(); + registry.register("*", new BasicAsyncRequestHandler(new AuthHandler())); + HttpHost target = start(registry, null); + HttpGet httpget = new HttpGet("http://test:all-wrong@" + target.toHostString() + "/"); + + Future future = this.httpclient.execute(target, httpget, null); + HttpResponse response = future.get(); + Assert.assertNotNull(response); + HttpEntity entity = response.getEntity(); + Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); + Assert.assertNotNull(entity); + EntityUtils.consume(entity); } } Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java?rev=1242789&view=auto ============================================================================== --- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java (added) +++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java Fri Feb 10 14:28:28 2012 @@ -0,0 +1,224 @@ +/* + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.http.impl.nio.client; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.Future; + +import org.apache.http.HttpAsyncTestBase; +import org.apache.http.HttpEntity; +import org.apache.http.HttpException; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.HttpResponse; +import org.apache.http.HttpResponseInterceptor; +import org.apache.http.HttpStatus; +import org.apache.http.auth.AUTH; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.DefaultConnectionReuseStrategy; +import org.apache.http.impl.DefaultHttpResponseFactory; +import org.apache.http.impl.nio.DefaultNHttpServerConnection; +import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory; +import org.apache.http.localserver.RequestBasicAuth; +import org.apache.http.nio.NHttpConnectionFactory; +import org.apache.http.nio.protocol.BasicAsyncRequestHandler; +import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier; +import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry; +import org.apache.http.nio.protocol.HttpAsyncRequestHandlerResolver; +import org.apache.http.nio.protocol.HttpAsyncService; +import org.apache.http.nio.reactor.IOReactorStatus; +import org.apache.http.nio.reactor.ListenerEndpoint; +import org.apache.http.params.HttpParams; +import org.apache.http.protocol.HTTP; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.protocol.ImmutableHttpProcessor; +import org.apache.http.protocol.ResponseConnControl; +import org.apache.http.protocol.ResponseContent; +import org.apache.http.protocol.ResponseDate; +import org.apache.http.protocol.ResponseServer; +import org.apache.http.util.EntityUtils; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestClientAuthenticationFallBack extends HttpAsyncTestBase { + + @Before + public void setUp() throws Exception { + initServer(); + initClient(); + } + + @After + public void tearDown() throws Exception { + shutDownClient(); + shutDownServer(); + } + + @Override + public void initServer() throws Exception { + super.initServer(); + this.serverHttpProc = new ImmutableHttpProcessor( + new HttpRequestInterceptor[] { + new RequestBasicAuth() + }, + new HttpResponseInterceptor[] { + new ResponseDate(), + new ResponseServer(), + new ResponseContent(), + new ResponseConnControl(), + new ResponseBasicUnauthorized() + } + ); + } + + @Override + protected NHttpConnectionFactory createServerConnectionFactory( + final HttpParams params) throws Exception { + return new DefaultNHttpServerConnectionFactory(params); + } + + @Override + protected String getSchemeName() { + return "http"; + } + + private HttpHost start( + final HttpAsyncRequestHandlerResolver requestHandlerResolver, + final HttpAsyncExpectationVerifier expectationVerifier) throws Exception { + HttpAsyncService serviceHandler = new HttpAsyncService( + this.serverHttpProc, + new DefaultConnectionReuseStrategy(), + new DefaultHttpResponseFactory(), + requestHandlerResolver, + expectationVerifier, + this.serverParams); + this.server.start(serviceHandler); + this.httpclient.start(); + + ListenerEndpoint endpoint = this.server.getListenerEndpoint(); + endpoint.waitFor(); + + Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus()); + InetSocketAddress address = (InetSocketAddress) endpoint.getAddress(); + HttpHost target = new HttpHost("localhost", address.getPort(), getSchemeName()); + return target; + } + + public class ResponseBasicUnauthorized implements HttpResponseInterceptor { + + public void process( + final HttpResponse response, + final HttpContext context) throws HttpException, IOException { + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { + response.addHeader(AUTH.WWW_AUTH, "Digest realm=\"test realm\" invalid"); + response.addHeader(AUTH.WWW_AUTH, "Basic realm=\"test realm\""); + } + } + + } + + static class AuthHandler implements HttpRequestHandler { + + public void handle( + final HttpRequest request, + final HttpResponse response, + final HttpContext context) throws HttpException, IOException { + String creds = (String) context.getAttribute("creds"); + if (creds == null || !creds.equals("test:test")) { + response.setStatusCode(HttpStatus.SC_UNAUTHORIZED); + } else { + response.setStatusCode(HttpStatus.SC_OK); + StringEntity entity = new StringEntity("success", HTTP.ASCII); + response.setEntity(entity); + } + } + + } + + static class TestCredentialsProvider implements CredentialsProvider { + + private final Credentials creds; + private AuthScope authscope; + + TestCredentialsProvider(final Credentials creds) { + super(); + this.creds = creds; + } + + public void clear() { + } + + public Credentials getCredentials(AuthScope authscope) { + this.authscope = authscope; + return this.creds; + } + + public void setCredentials(AuthScope authscope, Credentials credentials) { + } + + public AuthScope getAuthScope() { + return this.authscope; + } + + } + + @Test + public void testBasicAuthenticationSuccess() throws Exception { + HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry(); + registry.register("*", new BasicAsyncRequestHandler(new AuthHandler())); + HttpHost target = start(registry, null); + + TestCredentialsProvider credsProvider = new TestCredentialsProvider( + new UsernamePasswordCredentials("test", "test")); + + + this.httpclient.setCredentialsProvider(credsProvider); + + HttpGet httpget = new HttpGet("/"); + + Future future = this.httpclient.execute(target, httpget, null); + HttpResponse response = future.get(); + Assert.assertNotNull(response); + HttpEntity entity = response.getEntity(); + Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); + Assert.assertNotNull(entity); + EntityUtils.consume(entity); + AuthScope authscope = credsProvider.getAuthScope(); + Assert.assertNotNull(authscope); + Assert.assertEquals("test realm", authscope.getRealm()); + } + +} Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientAuthenticationFallBack.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java?rev=1242789&view=auto ============================================================================== --- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java (added) +++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java Fri Feb 10 14:28:28 2012 @@ -0,0 +1,230 @@ +/* + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.http.impl.nio.client; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.http.HttpAsyncTestBase; +import org.apache.http.HttpEntity; +import org.apache.http.HttpException; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.HttpResponse; +import org.apache.http.HttpResponseInterceptor; +import org.apache.http.HttpStatus; +import org.apache.http.auth.AUTH; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.DefaultConnectionReuseStrategy; +import org.apache.http.impl.DefaultHttpResponseFactory; +import org.apache.http.impl.nio.DefaultNHttpServerConnection; +import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory; +import org.apache.http.localserver.RequestBasicAuth; +import org.apache.http.nio.NHttpConnectionFactory; +import org.apache.http.nio.protocol.BasicAsyncRequestHandler; +import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier; +import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry; +import org.apache.http.nio.protocol.HttpAsyncRequestHandlerResolver; +import org.apache.http.nio.protocol.HttpAsyncService; +import org.apache.http.nio.reactor.IOReactorStatus; +import org.apache.http.nio.reactor.ListenerEndpoint; +import org.apache.http.params.HttpParams; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HTTP; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpRequestHandler; +import org.apache.http.protocol.ImmutableHttpProcessor; +import org.apache.http.protocol.ResponseConnControl; +import org.apache.http.protocol.ResponseContent; +import org.apache.http.protocol.ResponseDate; +import org.apache.http.protocol.ResponseServer; +import org.apache.http.util.EntityUtils; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestClientReauthentication extends HttpAsyncTestBase { + + @Before + public void setUp() throws Exception { + initServer(); + initClient(); + } + + @After + public void tearDown() throws Exception { + shutDownClient(); + shutDownServer(); + } + + @Override + public void initServer() throws Exception { + super.initServer(); + this.serverHttpProc = new ImmutableHttpProcessor( + new HttpRequestInterceptor[] { + new RequestBasicAuth() + }, + new HttpResponseInterceptor[] { + new ResponseDate(), + new ResponseServer(), + new ResponseContent(), + new ResponseConnControl(), + new ResponseBasicUnauthorized() + } + ); + } + + @Override + protected NHttpConnectionFactory createServerConnectionFactory( + final HttpParams params) throws Exception { + return new DefaultNHttpServerConnectionFactory(params); + } + + @Override + protected String getSchemeName() { + return "http"; + } + + private HttpHost start( + final HttpAsyncRequestHandlerResolver requestHandlerResolver, + final HttpAsyncExpectationVerifier expectationVerifier) throws Exception { + HttpAsyncService serviceHandler = new HttpAsyncService( + this.serverHttpProc, + new DefaultConnectionReuseStrategy(), + new DefaultHttpResponseFactory(), + requestHandlerResolver, + expectationVerifier, + this.serverParams); + this.server.start(serviceHandler); + this.httpclient.start(); + + ListenerEndpoint endpoint = this.server.getListenerEndpoint(); + endpoint.waitFor(); + + Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus()); + InetSocketAddress address = (InetSocketAddress) endpoint.getAddress(); + HttpHost target = new HttpHost("localhost", address.getPort(), getSchemeName()); + return target; + } + + public class ResponseBasicUnauthorized implements HttpResponseInterceptor { + + public void process( + final HttpResponse response, + final HttpContext context) throws HttpException, IOException { + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { + response.addHeader(AUTH.WWW_AUTH, "Basic realm=\"test realm\""); + } + } + + } + + static class AuthHandler implements HttpRequestHandler { + + private AtomicLong count = new AtomicLong(0); + + public void handle( + final HttpRequest request, + final HttpResponse response, + final HttpContext context) throws HttpException, IOException { + String creds = (String) context.getAttribute("creds"); + if (creds == null || !creds.equals("test:test")) { + response.setStatusCode(HttpStatus.SC_UNAUTHORIZED); + } else { + // Make client re-authenticate on each fourth request + if (this.count.incrementAndGet() % 4 == 0) { + response.setStatusCode(HttpStatus.SC_UNAUTHORIZED); + } else { + response.setStatusCode(HttpStatus.SC_OK); + StringEntity entity = new StringEntity("success", HTTP.ASCII); + response.setEntity(entity); + } + } + } + + } + + static class TestCredentialsProvider implements CredentialsProvider { + + private final Credentials creds; + private AuthScope authscope; + + TestCredentialsProvider(final Credentials creds) { + super(); + this.creds = creds; + } + + public void clear() { + } + + public Credentials getCredentials(AuthScope authscope) { + this.authscope = authscope; + return this.creds; + } + + public void setCredentials(AuthScope authscope, Credentials credentials) { + } + + public AuthScope getAuthScope() { + return this.authscope; + } + + } + + @Test + public void testBasicAuthenticationSuccess() throws Exception { + HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry(); + registry.register("*", new BasicAsyncRequestHandler(new AuthHandler())); + HttpHost target = start(registry, null); + + TestCredentialsProvider credsProvider = new TestCredentialsProvider( + new UsernamePasswordCredentials("test", "test")); + + this.httpclient.setCredentialsProvider(credsProvider); + + HttpContext context = new BasicHttpContext(); + for (int i = 0; i < 10; i++) { + HttpGet httpget = new HttpGet("/"); + Future future = this.httpclient.execute(target, httpget, context, null); + HttpResponse response = future.get(); + Assert.assertNotNull(response); + HttpEntity entity = response.getEntity(); + Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); + Assert.assertNotNull(entity); + EntityUtils.consume(entity); + } + } + +} Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/test/java/org/apache/http/impl/nio/client/TestClientReauthentication.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: httpcomponents/httpasyncclient/trunk/pom.xml URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/pom.xml?rev=1242789&r1=1242788&r2=1242789&view=diff ============================================================================== --- httpcomponents/httpasyncclient/trunk/pom.xml (original) +++ httpcomponents/httpasyncclient/trunk/pom.xml Fri Feb 10 14:28:28 2012 @@ -72,7 +72,7 @@ true true 4.2-beta1 - 4.1.2 + 4.2-beta1 1.1.1 2.0.1 4.8.1