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 2015/07/23 11:59:37 UTC
svn commit: r1692369 - in /httpcomponents/httpclient/trunk/httpclient/src:
main/java/org/apache/http/impl/auth/ main/java/org/apache/http/impl/client/
main/java/org/apache/http/impl/execchain/
test/java/org/apache/http/impl/auth/
Author: olegk
Date: Thu Jul 23 09:59:37 2015
New Revision: 1692369
URL: http://svn.apache.org/r1692369
Log:
HttpAuthenticator API changes
Modified:
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/auth/HttpAuthenticator.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java
httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/auth/TestHttpAuthenticator.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/auth/HttpAuthenticator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/auth/HttpAuthenticator.java?rev=1692369&r1=1692368&r2=1692369&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/auth/HttpAuthenticator.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/auth/HttpAuthenticator.java Thu Jul 23 09:59:37 2015
@@ -81,7 +81,7 @@ public class HttpAuthenticator {
this(null);
}
- public boolean updateAuthState(
+ public boolean isChallenged(
final HttpHost host,
final ChallengeType challengeType,
final HttpResponse response,
@@ -121,7 +121,7 @@ public class HttpAuthenticator {
}
}
- public boolean handleAuthChallenge(
+ public boolean prepareAuthResponse(
final HttpHost host,
final ChallengeType challengeType,
final HttpResponse response,
@@ -184,13 +184,7 @@ public class HttpAuthenticator {
break;
case CHALLENGED:
case HANDSHAKE:
- if (authScheme == null) {
- this.log.debug("Auth scheme is null");
- clearCache(host, context);
- authState.reset();
- authState.setState(AuthProtocolState.FAILURE);
- return false;
- }
+ Asserts.notNull(authScheme, "AuthScheme");
case UNCHALLENGED:
if (authScheme != null) {
final String id = authScheme.getSchemeName();
@@ -234,13 +228,13 @@ public class HttpAuthenticator {
}
}
- public void generateAuthResponse(
+ public void addAuthResponse(
final HttpRequest request,
final AuthState authState,
final HttpContext context) throws HttpException, IOException {
AuthScheme authScheme = authState.getAuthScheme();
Credentials creds = authState.getCredentials();
- switch (authState.getState()) { // TODO add UNCHALLENGED and HANDSHAKE cases
+ switch (authState.getState()) {
case FAILURE:
return;
case SUCCESS:
@@ -249,6 +243,9 @@ public class HttpAuthenticator {
return;
}
break;
+ case HANDSHAKE:
+ Asserts.notNull(authScheme, "AuthScheme");
+ break;
case CHALLENGED:
final Queue<AuthOption> authOptions = authState.getAuthOptions();
if (authOptions != null) {
@@ -275,6 +272,7 @@ public class HttpAuthenticator {
} else {
Asserts.notNull(authScheme, "AuthScheme");
}
+ default:
}
if (authScheme != null) {
try {
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java?rev=1692369&r1=1692368&r2=1692369&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java Thu Jul 23 09:59:37 2015
@@ -177,7 +177,7 @@ public class ProxyClient {
conn.bind(socket);
}
- this.authenticator.generateAuthResponse(connect, this.proxyAuthState, context);
+ this.authenticator.addAuthResponse(connect, this.proxyAuthState, context);
response = this.requestExec.execute(connect, conn, context);
@@ -186,8 +186,8 @@ public class ProxyClient {
throw new HttpException("Unexpected response to CONNECT request: " +
response.getStatusLine());
}
- if (this.authenticator.updateAuthState(proxy, ChallengeType.PROXY, response, this.proxyAuthState, context)) {
- if (this.authenticator.handleAuthChallenge(proxy, ChallengeType.PROXY, response,
+ if (this.authenticator.isChallenged(proxy, ChallengeType.PROXY, response, this.proxyAuthState, context)) {
+ if (this.authenticator.prepareAuthResponse(proxy, ChallengeType.PROXY, response,
this.proxyAuthStrategy, this.proxyAuthState, context)) {
// Retry request
if (this.reuseStrategy.keepAlive(response, context)) {
Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java?rev=1692369&r1=1692368&r2=1692369&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java Thu Jul 23 09:59:37 2015
@@ -248,13 +248,13 @@ public class MainClientExec implements C
if (this.log.isDebugEnabled()) {
this.log.debug("Target auth state: " + targetAuthState.getState());
}
- this.authenticator.generateAuthResponse(request, targetAuthState, context);
+ this.authenticator.addAuthResponse(request, targetAuthState, context);
}
if (!request.containsHeader(HttpHeaders.PROXY_AUTHORIZATION) && !route.isTunnelled()) {
if (this.log.isDebugEnabled()) {
this.log.debug("Proxy auth state: " + proxyAuthState.getState());
}
- this.authenticator.generateAuthResponse(request, proxyAuthState, context);
+ this.authenticator.addAuthResponse(request, proxyAuthState, context);
}
response = requestExecutor.execute(request, managedConn, context);
@@ -455,7 +455,7 @@ public class MainClientExec implements C
}
connect.removeHeaders(HttpHeaders.PROXY_AUTHORIZATION);
- this.authenticator.generateAuthResponse(connect, proxyAuthState, context);
+ this.authenticator.addAuthResponse(connect, proxyAuthState, context);
response = this.requestExecutor.execute(connect, managedConn, context);
@@ -466,9 +466,9 @@ public class MainClientExec implements C
}
if (config.isAuthenticationEnabled()) {
- if (this.authenticator.updateAuthState(proxy, ChallengeType.PROXY, response,
+ if (this.authenticator.isChallenged(proxy, ChallengeType.PROXY, response,
proxyAuthState, context)) {
- if (this.authenticator.handleAuthChallenge(proxy, ChallengeType.PROXY, response,
+ if (this.authenticator.prepareAuthResponse(proxy, ChallengeType.PROXY, response,
this.proxyAuthStrategy, proxyAuthState, context)) {
// Retry request
if (this.reuseStrategy.keepAlive(response, context)) {
@@ -547,7 +547,7 @@ public class MainClientExec implements C
route.getTargetHost().getPort(),
target.getSchemeName());
}
- final boolean targetAuthRequested = this.authenticator.updateAuthState(
+ final boolean targetAuthRequested = this.authenticator.isChallenged(
target, ChallengeType.TARGET, response, targetAuthState, context);
HttpHost proxy = route.getProxyHost();
@@ -555,15 +555,15 @@ public class MainClientExec implements C
if (proxy == null) {
proxy = route.getTargetHost();
}
- final boolean proxyAuthRequested = this.authenticator.updateAuthState(
+ final boolean proxyAuthRequested = this.authenticator.isChallenged(
proxy, ChallengeType.PROXY, response, proxyAuthState, context);
if (targetAuthRequested) {
- return this.authenticator.handleAuthChallenge(target, ChallengeType.TARGET, response,
+ return this.authenticator.prepareAuthResponse(target, ChallengeType.TARGET, response,
this.targetAuthStrategy, targetAuthState, context);
}
if (proxyAuthRequested) {
- return this.authenticator.handleAuthChallenge(proxy, ChallengeType.PROXY, response,
+ return this.authenticator.prepareAuthResponse(proxy, ChallengeType.PROXY, response,
this.proxyAuthStrategy, proxyAuthState, context);
}
}
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/auth/TestHttpAuthenticator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/auth/TestHttpAuthenticator.java?rev=1692369&r1=1692368&r2=1692369&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/auth/TestHttpAuthenticator.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/auth/TestHttpAuthenticator.java Thu Jul 23 09:59:37 2015
@@ -101,7 +101,7 @@ public class TestHttpAuthenticator {
public void testUpdateAuthState() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=test");
- Assert.assertTrue(this.httpAuthenticator.updateAuthState(
+ Assert.assertTrue(this.httpAuthenticator.isChallenged(
this.defaultHost, ChallengeType.TARGET, response, this.authState, this.context));
Mockito.verifyZeroInteractions(this.authCache);
}
@@ -114,7 +114,7 @@ public class TestHttpAuthenticator {
this.authState.update(this.authScheme, this.credentials);
this.authState.setState(AuthProtocolState.SUCCESS);
- Assert.assertTrue(this.httpAuthenticator.updateAuthState(
+ Assert.assertTrue(this.httpAuthenticator.isChallenged(
this.defaultHost, ChallengeType.TARGET, response, this.authState, this.context));
Mockito.verify(this.authCache).remove(this.defaultHost);
@@ -124,7 +124,7 @@ public class TestHttpAuthenticator {
public void testAuthenticationNotRequestedUnchallenged() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
- Assert.assertFalse(this.httpAuthenticator.updateAuthState(
+ Assert.assertFalse(this.httpAuthenticator.isChallenged(
this.defaultHost, ChallengeType.TARGET, response, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.UNCHALLENGED, this.authState.getState());
}
@@ -135,7 +135,7 @@ public class TestHttpAuthenticator {
this.authState.update(this.authScheme, this.credentials);
this.authState.setState(AuthProtocolState.CHALLENGED);
- Assert.assertFalse(this.httpAuthenticator.updateAuthState(
+ Assert.assertFalse(this.httpAuthenticator.isChallenged(
this.defaultHost, ChallengeType.TARGET, response, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.SUCCESS, this.authState.getState());
@@ -148,7 +148,7 @@ public class TestHttpAuthenticator {
this.authState.update(this.authScheme, this.credentials);
this.authState.setState(AuthProtocolState.HANDSHAKE);
- Assert.assertFalse(this.httpAuthenticator.updateAuthState(
+ Assert.assertFalse(this.httpAuthenticator.isChallenged(
this.defaultHost, ChallengeType.TARGET, response, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.SUCCESS, this.authState.getState());
@@ -165,8 +165,8 @@ public class TestHttpAuthenticator {
final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
- Assert.assertTrue(this.httpAuthenticator.handleAuthChallenge(
- host,ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
+ Assert.assertTrue(this.httpAuthenticator.prepareAuthResponse(
+ host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.CHALLENGED, this.authState.getState());
final Queue<AuthOption> options = this.authState.getAuthOptions();
@@ -187,7 +187,7 @@ public class TestHttpAuthenticator {
final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
- Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertFalse(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
}
@@ -200,7 +200,7 @@ public class TestHttpAuthenticator {
final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
- Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertFalse(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
}
@@ -215,7 +215,7 @@ public class TestHttpAuthenticator {
final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
- Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertFalse(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
}
@@ -231,7 +231,7 @@ public class TestHttpAuthenticator {
final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
- Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertFalse(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.FAILURE, this.authState.getState());
@@ -250,7 +250,7 @@ public class TestHttpAuthenticator {
final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
- Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertFalse(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.FAILURE, this.authState.getState());
@@ -269,7 +269,7 @@ public class TestHttpAuthenticator {
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(new BasicScheme(), this.credentials);
- Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertFalse(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.FAILURE, this.authState.getState());
Assert.assertNull(this.authState.getCredentials());
@@ -288,7 +288,7 @@ public class TestHttpAuthenticator {
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(new DigestScheme(), this.credentials);
- Assert.assertTrue(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertTrue(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.HANDSHAKE, this.authState.getState());
@@ -306,7 +306,7 @@ public class TestHttpAuthenticator {
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.update(new BasicScheme(), this.credentials);
- Assert.assertTrue(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertTrue(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.CHALLENGED, this.authState.getState());
@@ -328,7 +328,7 @@ public class TestHttpAuthenticator {
final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy();
- Assert.assertFalse(this.httpAuthenticator.handleAuthChallenge(
+ Assert.assertFalse(this.httpAuthenticator.prepareAuthResponse(
host, ChallengeType.TARGET, response, authStrategy, this.authState, this.context));
Assert.assertEquals(AuthProtocolState.UNCHALLENGED, this.authState.getState());
@@ -342,7 +342,7 @@ public class TestHttpAuthenticator {
this.authState.setState(AuthProtocolState.FAILURE);
this.authState.update(this.authScheme, this.credentials);
- this.httpAuthenticator.generateAuthResponse(request, authState, context);
+ this.httpAuthenticator.addAuthResponse(request, authState, context);
Assert.assertFalse(request.containsHeader(HttpHeaders.AUTHORIZATION));
@@ -363,7 +363,7 @@ public class TestHttpAuthenticator {
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(HttpHeaders.AUTHORIZATION, "stuff"));
- this.httpAuthenticator.generateAuthResponse(request, authState, context);
+ this.httpAuthenticator.addAuthResponse(request, authState, context);
Assert.assertTrue(request.containsHeader(HttpHeaders.AUTHORIZATION));
@@ -383,7 +383,7 @@ public class TestHttpAuthenticator {
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(HttpHeaders.AUTHORIZATION, "stuff"));
- this.httpAuthenticator.generateAuthResponse(request, authState, context);
+ this.httpAuthenticator.addAuthResponse(request, authState, context);
Assert.assertSame(this.authScheme, this.authState.getAuthScheme());
Assert.assertSame(this.credentials, this.authState.getCredentials());
@@ -414,7 +414,7 @@ public class TestHttpAuthenticator {
authOptions.add(new AuthOption(authScheme2, this.credentials));
this.authState.update(authOptions);
- this.httpAuthenticator.generateAuthResponse(request, authState, context);
+ this.httpAuthenticator.addAuthResponse(request, authState, context);
Assert.assertSame(authScheme2, this.authState.getAuthScheme());
Assert.assertSame(this.credentials, this.authState.getCredentials());
@@ -438,7 +438,7 @@ public class TestHttpAuthenticator {
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(HttpHeaders.AUTHORIZATION, "stuff"));
- this.httpAuthenticator.generateAuthResponse(request, authState, context);
+ this.httpAuthenticator.addAuthResponse(request, authState, context);
Assert.assertSame(this.authScheme, this.authState.getAuthScheme());
Assert.assertSame(this.credentials, this.authState.getCredentials());
@@ -461,7 +461,7 @@ public class TestHttpAuthenticator {
Mockito.any(HttpRequest.class),
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(HttpHeaders.AUTHORIZATION, "stuff"));
- this.httpAuthenticator.generateAuthResponse(request, authState, context);
+ this.httpAuthenticator.addAuthResponse(request, authState, context);
Assert.assertFalse(request.containsHeader(HttpHeaders.AUTHORIZATION));