* Changed protocol cookie interceptors to use CookieStore instead of HttpState

* Changed default client request redirector to use CredentialsProvider instead of HttpState
* Deprecated HttpState

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@558131 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-07-20 20:53:23 +00:00
parent 1ed3733d6c
commit 329a7c3f52
7 changed files with 42 additions and 36 deletions

View File

@ -33,6 +33,7 @@
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpState;
import org.apache.http.client.methods.HttpGet;
@ -53,15 +54,15 @@ public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
// Create a local instance of HttpState
HttpState localState = new HttpState();
// Create a local instance of cookie store
CookieStore cookieStore = new HttpState();
// Obtain default HTTP context
HttpContext defaultContext = httpclient.getDefaultContext();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext(defaultContext);
// Bind custom HTTP state to the local context
localContext.setAttribute(ClientContext.HTTP_STATE, localState);
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://www.google.com/");
@ -77,7 +78,7 @@ public final static void main(String[] args) throws Exception {
System.out.println("Response content length: " + entity.getContentLength());
System.out.println("Chunked?: " + entity.isChunked());
}
Cookie[] cookies = localState.getCookies();
Cookie[] cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.length; i++) {
System.out.println("Local cookie: " + cookies[i]);
}

View File

@ -59,6 +59,7 @@
*
* @version $Revision$ $Date$
*
* @deprecated
*/
public class HttpState implements CookieStore, CredentialsProvider {

View File

@ -40,9 +40,10 @@ public interface ClientContext {
public static final String COOKIESPEC_REGISTRY = "http.cookiespec-registry";
public static final String AUTHSCHEME_REGISTRY = "http.authscheme-registry";
public static final String HTTP_STATE = "http.state";
public static final String COOKIE_STORE = "http.cookie-store";
public static final String COOKIE_SPEC = "http.cookie-spec";
public static final String COOKIE_ORIGIN = "http.cookie-origin";
public static final String CREDS_PROVIDER = "http.auth.credentials-provider";
public static final String TARGET_AUTH_STATE = "http.auth.target-scope";
public static final String PROXY_AUTH_STATE = "http.auth.proxy-scope";
}

View File

@ -45,7 +45,7 @@
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.ProtocolException;
import org.apache.http.client.HttpState;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ManagedClientConnection;
@ -84,11 +84,11 @@ public void process(final HttpRequest request, final HttpContext context)
throw new IllegalArgumentException("HTTP context may not be null");
}
// Obtain HTTP state
HttpState state = (HttpState) context.getAttribute(
ClientContext.HTTP_STATE);
if (state == null) {
LOG.info("HTTP state not available in HTTP context");
// Obtain cookie store
CookieStore cookieStore = (CookieStore) context.getAttribute(
ClientContext.COOKIE_STORE);
if (cookieStore == null) {
LOG.info("Cookie store not available in HTTP context");
return;
}
@ -146,7 +146,7 @@ public void process(final HttpRequest request, final HttpContext context)
// Get an instance of the selected cookie policy
CookieSpec cookieSpec = registry.getCookieSpec(policy, request.getParams());
// Get all cookies available in the HTTP state
Cookie[] cookies = state.getCookies();
Cookie[] cookies = cookieStore.getCookies();
// Find cookies matching the given origin
List matchedCookies = new ArrayList(cookies.length);
for (int i = 0; i < cookies.length; i++) {

View File

@ -39,7 +39,7 @@
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.HttpState;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec;
@ -74,11 +74,11 @@ public void process(final HttpResponse response, final HttpContext context)
throw new IllegalArgumentException("HTTP context may not be null");
}
// Obtain HTTP state
HttpState state = (HttpState) context.getAttribute(
ClientContext.HTTP_STATE);
if (state == null) {
LOG.info("HTTP state not available in HTTP context");
// Obtain cookie store
CookieStore cookieStore = (CookieStore) context.getAttribute(
ClientContext.COOKIE_STORE);
if (cookieStore == null) {
LOG.info("Cookie store not available in HTTP context");
return;
}
// Obtain actual CookieSpec instance
@ -96,14 +96,14 @@ public void process(final HttpResponse response, final HttpContext context)
return;
}
Header[] headers = response.getHeaders(SM.SET_COOKIE);
processCookies(headers, cookieSpec, cookieOrigin, state);
processCookies(headers, cookieSpec, cookieOrigin, cookieStore);
}
private static void processCookies(
final Header[] headers,
final CookieSpec cookieSpec,
final CookieOrigin cookieOrigin,
final HttpState state) {
final CookieStore cookieStore) {
for (int i = 0; i < headers.length; i++) {
Header header = headers[i];
try {
@ -112,7 +112,7 @@ private static void processCookies(
Cookie cookie = cookies[c];
try {
cookieSpec.validate(cookie, cookieOrigin);
state.addCookie(cookie);
cookieStore.addCookie(cookie);
if (LOG.isDebugEnabled()) {
LOG.debug("Cookie accepted: \""

View File

@ -59,8 +59,8 @@
import org.apache.http.client.AuthState;
import org.apache.http.client.AuthenticationHandler;
import org.apache.http.client.ClientRequestDirector;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.HttpState;
import org.apache.http.client.RedirectException;
import org.apache.http.client.RedirectHandler;
import org.apache.http.client.RoutedRequest;
@ -580,10 +580,10 @@ protected boolean createTunnel(HttpRoute route, HttpContext context)
response.getStatusLine());
}
HttpState state = (HttpState)
context.getAttribute(ClientContext.HTTP_STATE);
CredentialsProvider credsProvider = (CredentialsProvider)
context.getAttribute(ClientContext.CREDS_PROVIDER);
if (state != null && HttpClientParams.isAuthenticating(params)) {
if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
if (this.authHandler.isProxyAuthenticationRequested(response, context)) {
LOG.debug("Proxy requested authentication");
@ -596,7 +596,7 @@ protected boolean createTunnel(HttpRoute route, HttpContext context)
break;
}
}
updateAuthState(this.proxyAuthState, proxy, state);
updateAuthState(this.proxyAuthState, proxy, credsProvider);
if (this.proxyAuthState.getCredentials() != null) {
done = false;
@ -758,10 +758,10 @@ protected RoutedRequest handleResponse(RoutedRequest roureq,
return new RoutedRequest.Impl(redirect, newRoute);
}
HttpState state = (HttpState)
context.getAttribute(ClientContext.HTTP_STATE);
if (state != null && HttpClientParams.isAuthenticating(params)) {
CredentialsProvider credsProvider = (CredentialsProvider)
context.getAttribute(ClientContext.CREDS_PROVIDER);
if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
if (this.authHandler.isTargetAuthenticationRequested(response, context)) {
@ -781,7 +781,7 @@ protected RoutedRequest handleResponse(RoutedRequest roureq,
return null;
}
}
updateAuthState(this.targetAuthState, target, state);
updateAuthState(this.targetAuthState, target, credsProvider);
if (this.targetAuthState.getCredentials() != null) {
// Re-try the same request via the same route
@ -806,7 +806,7 @@ protected RoutedRequest handleResponse(RoutedRequest roureq,
return null;
}
}
updateAuthState(this.proxyAuthState, proxy, state);
updateAuthState(this.proxyAuthState, proxy, credsProvider);
if (this.proxyAuthState.getCredentials() != null) {
// Re-try the same request via the same route
@ -879,7 +879,7 @@ private void processChallenges(
private void updateAuthState(
final AuthState authState,
final HttpHost host,
final HttpState state) {
final CredentialsProvider credsProvider) {
String hostname = host.getHostName();
int port = host.getPort();
@ -900,7 +900,7 @@ private void updateAuthState(
}
Credentials creds = authState.getCredentials();
if (creds == null) {
creds = state.getCredentials(authScope);
creds = credsProvider.getCredentials(authScope);
if (LOG.isDebugEnabled()) {
if (creds != null) {
LOG.debug("Found credentials");

View File

@ -251,7 +251,10 @@ protected void populateContext(final HttpContext context) {
ClientContext.COOKIESPEC_REGISTRY,
getCookieSpecs());
context.setAttribute(
ClientContext.HTTP_STATE,
ClientContext.COOKIE_STORE,
getState());
context.setAttribute(
ClientContext.CREDS_PROVIDER,
getState());
}