Obtain HTTP state information from HTTP context
git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@538866 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
03cc12fa3c
commit
5ccb548a59
|
@ -439,7 +439,6 @@ public abstract class AbstractHttpClient
|
|||
getHttpRequestRetryHandler(),
|
||||
getRedirectHandler(),
|
||||
getAuthenticationHandler(),
|
||||
getState(),
|
||||
getParams());
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ import org.apache.http.auth.AuthenticationException;
|
|||
import org.apache.http.auth.MalformedChallengeException;
|
||||
import org.apache.http.client.AuthenticationHandler;
|
||||
import org.apache.http.client.params.HttpClientParams;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.message.BufferedHeader;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
@ -68,14 +69,8 @@ public class DefaultAuthenticationHandler implements AuthenticationHandler {
|
|||
"basic"
|
||||
});
|
||||
|
||||
private final AuthSchemeRegistry registry;
|
||||
|
||||
public DefaultAuthenticationHandler(final AuthSchemeRegistry registry) {
|
||||
public DefaultAuthenticationHandler() {
|
||||
super();
|
||||
if (registry == null) {
|
||||
throw new IllegalArgumentException("AuthScheme registry may not be null");
|
||||
}
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public boolean isTargetAuthenticationRequested(
|
||||
|
@ -157,6 +152,12 @@ public class DefaultAuthenticationHandler implements AuthenticationHandler {
|
|||
final HttpResponse response,
|
||||
final HttpContext context) throws AuthenticationException {
|
||||
|
||||
AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(
|
||||
HttpClientContext.AUTHSCHEME_REGISTRY);
|
||||
if (registry == null) {
|
||||
throw new IllegalStateException("AuthScheme registry not set in HTTP context");
|
||||
}
|
||||
|
||||
HttpParams params = response.getParams();
|
||||
Collection authPrefs = (Collection) params.getParameter(
|
||||
HttpClientParams.AUTH_SCHEME_PRIORITY);
|
||||
|
@ -177,7 +178,7 @@ public class DefaultAuthenticationHandler implements AuthenticationHandler {
|
|||
LOG.debug(id + " authentication scheme selected");
|
||||
}
|
||||
try {
|
||||
authScheme = this.registry.getAuthScheme(id, params);
|
||||
authScheme = registry.getAuthScheme(id, params);
|
||||
} catch (IllegalStateException e) {
|
||||
throw new AuthenticationException(e.getMessage());
|
||||
}
|
||||
|
|
|
@ -126,9 +126,6 @@ public class DefaultClientRequestDirector
|
|||
/** The authentication handler. */
|
||||
private final AuthenticationHandler authHandler;
|
||||
|
||||
/** The HTTP state */
|
||||
private final HttpState state;
|
||||
|
||||
/** The HTTP parameters. */
|
||||
protected final HttpParams params;
|
||||
|
||||
|
@ -150,7 +147,6 @@ public class DefaultClientRequestDirector
|
|||
final HttpRequestRetryHandler retryHandler,
|
||||
final RedirectHandler redirectHandler,
|
||||
final AuthenticationHandler authHandler,
|
||||
final HttpState state,
|
||||
final HttpParams params) {
|
||||
|
||||
if (conman == null) {
|
||||
|
@ -171,9 +167,6 @@ public class DefaultClientRequestDirector
|
|||
if (authHandler == null) {
|
||||
throw new IllegalArgumentException("Authentication handler may not be null");
|
||||
}
|
||||
if (state == null) {
|
||||
throw new IllegalArgumentException("HTTP state may not be null");
|
||||
}
|
||||
if (params == null) {
|
||||
throw new IllegalArgumentException("HTTP parameters may not be null");
|
||||
}
|
||||
|
@ -183,7 +176,6 @@ public class DefaultClientRequestDirector
|
|||
this.retryHandler = retryHandler;
|
||||
this.redirectHandler = redirectHandler;
|
||||
this.authHandler = authHandler;
|
||||
this.state = state;
|
||||
this.params = params;
|
||||
this.requestExec = new HttpRequestExecutor(params);
|
||||
|
||||
|
@ -658,7 +650,9 @@ public class DefaultClientRequestDirector
|
|||
return new RoutedRequest.Impl(redirect, newRoute);
|
||||
}
|
||||
|
||||
if (HttpClientParams.isAuthenticating(params)) {
|
||||
HttpState state = (HttpState) context.getAttribute(HttpClientContext.HTTP_STATE);
|
||||
|
||||
if (state != null && HttpClientParams.isAuthenticating(params)) {
|
||||
|
||||
if (this.authHandler.isTargetAuthenticationRequested(response, context)) {
|
||||
|
||||
|
@ -677,7 +671,7 @@ public class DefaultClientRequestDirector
|
|||
return null;
|
||||
}
|
||||
}
|
||||
updateAuthState(this.targetAuthState, target);
|
||||
updateAuthState(this.targetAuthState, target, state);
|
||||
|
||||
if (this.targetAuthState.getCredentials() != null) {
|
||||
// Re-try the same request via the same route
|
||||
|
@ -702,7 +696,7 @@ public class DefaultClientRequestDirector
|
|||
return null;
|
||||
}
|
||||
}
|
||||
updateAuthState(this.proxyAuthState, proxy);
|
||||
updateAuthState(this.proxyAuthState, proxy, state);
|
||||
|
||||
if (this.proxyAuthState.getCredentials() != null) {
|
||||
// Re-try the same request via the same route
|
||||
|
@ -801,7 +795,10 @@ public class DefaultClientRequestDirector
|
|||
}
|
||||
|
||||
|
||||
private void updateAuthState(final AuthState authState, final HttpHost host) {
|
||||
private void updateAuthState(
|
||||
final AuthState authState,
|
||||
final HttpHost host,
|
||||
final HttpState state) {
|
||||
AuthScheme authScheme = authState.getAuthScheme();
|
||||
AuthScope authScope = new AuthScope(
|
||||
host.getHostName(),
|
||||
|
@ -814,7 +811,7 @@ public class DefaultClientRequestDirector
|
|||
}
|
||||
Credentials creds = authState.getCredentials();
|
||||
if (creds == null) {
|
||||
creds = this.state.getCredentials(authScope);
|
||||
creds = state.getCredentials(authScope);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
if (creds != null) {
|
||||
LOG.debug("Found credentials");
|
||||
|
|
|
@ -224,7 +224,7 @@ public class DefaultHttpClient extends AbstractHttpClient {
|
|||
|
||||
|
||||
protected AuthenticationHandler createAuthenticationHandler() {
|
||||
return new DefaultAuthenticationHandler(getAuthSchemes());
|
||||
return new DefaultAuthenticationHandler();
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue