From c4a9583527fe0ce651873eeda17cd6e58dfde6dd Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 20 Jul 2007 21:33:17 +0000 Subject: [PATCH] HTTPCLIENT-666: Replace HttpState with CredentialsProvier and CookieStore interfaces git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@558158 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 3 + .../examples/client/ClientAuthentication.java | 2 +- .../examples/client/ClientCustomContext.java | 4 +- .../http/examples/client/ClientFormLogin.java | 4 +- .../client/ClientProxyAuthentication.java | 2 +- .../org/apache/http/client/HttpState.java | 271 ------------------ .../http/impl/client/AbstractHttpClient.java | 40 ++- .../http/impl/client/DefaultHttpClient.java | 16 +- .../impl/conn/SingleClientConnManager.java | 2 - .../conn/ThreadSafeClientConnManager.java | 2 - 10 files changed, 50 insertions(+), 296 deletions(-) delete mode 100644 module-client/src/main/java/org/apache/http/client/HttpState.java diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 98d97957a..e164d943f 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,5 +1,8 @@ Changes since release 4.0 Alpha 1 +* [HTTPCLIENT-666] Replaced HttpState with CredentialsProvier and CookieStore interfaces + Contributed by Oleg Kalnichevski + * [HTTPCORE-100] revised HttpContext hierarchy Contributed by Roland Weber diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java b/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java index bbb06a968..19a81ee93 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientAuthentication.java @@ -47,7 +47,7 @@ public class ClientAuthentication { public static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); - httpclient.getState().setCredentials( + httpclient.getCredentialsProvider().setCredentials( new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password")); diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java b/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java index a8dfe9c4c..8482018cc 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientCustomContext.java @@ -35,10 +35,10 @@ 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; import org.apache.http.client.protocol.ClientContext; import org.apache.http.cookie.Cookie; +import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.BasicHttpContext; @@ -55,7 +55,7 @@ public class ClientCustomContext { HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of cookie store - CookieStore cookieStore = new HttpState(); + CookieStore cookieStore = new BasicCookieStore(); // Obtain default HTTP context HttpContext defaultContext = httpclient.getDefaultContext(); diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java b/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java index a958f6d8b..8cc361434 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientFormLogin.java @@ -64,7 +64,7 @@ public class ClientFormLogin { entity.consumeContent(); } System.out.println("Initial set of cookies:"); - Cookie[] cookies = httpclient.getState().getCookies(); + Cookie[] cookies = httpclient.getCookieStore().getCookies(); if (cookies.length == 0) { System.out.println("None"); } else { @@ -94,7 +94,7 @@ public class ClientFormLogin { } System.out.println("Post logon cookies:"); - cookies = httpclient.getState().getCookies(); + cookies = httpclient.getCookieStore().getCookies(); if (cookies.length == 0) { System.out.println("None"); } else { diff --git a/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java b/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java index f914ae311..ab651f103 100644 --- a/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java +++ b/module-client/src/examples/org/apache/http/examples/client/ClientProxyAuthentication.java @@ -48,7 +48,7 @@ public class ClientProxyAuthentication { DefaultHttpClient httpclient = new DefaultHttpClient(); - httpclient.getState().setCredentials( + httpclient.getCredentialsProvider().setCredentials( new AuthScope("localhost", 8080), new UsernamePasswordCredentials("username", "password")); diff --git a/module-client/src/main/java/org/apache/http/client/HttpState.java b/module-client/src/main/java/org/apache/http/client/HttpState.java deleted file mode 100644 index 38529256c..000000000 --- a/module-client/src/main/java/org/apache/http/client/HttpState.java +++ /dev/null @@ -1,271 +0,0 @@ -/* - * $HeadURL$ - * $Revision$ - * $Date$ - * - * ==================================================================== - * - * 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.client; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; - -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.Credentials; -import org.apache.http.cookie.Cookie; -import org.apache.http.cookie.CookieIdentityComparator; - -/** - *

- * A container for HTTP attributes that may persist from request - * to request, such as {@link Cookie cookies} and authentication - * {@link Credentials credentials}. - *

- * - * @author Remy Maucherat - * @author Rodney Waldhoff - * @author Jeff Dever - * @author Sean C. Sullivan - * @author Michael Becke - * @author Oleg Kalnichevski - * @author Mike Bowler - * @author Adrian Sutton - * - * @version $Revision$ $Date$ - * - * @deprecated - */ -public class HttpState implements CookieStore, CredentialsProvider { - - // ----------------------------------------------------- Instance Variables - - /** - * Map of {@link Credentials credentials} by realm that this - * HTTP state contains. - */ - private final HashMap credMap; - - /** - * Array of {@link Cookie cookies} that this HTTP state contains. - */ - private final ArrayList cookies; - - private final Comparator cookieComparator; - - // -------------------------------------------------------- Class Variables - - /** - * Default constructor. - */ - public HttpState() { - super(); - this.credMap = new HashMap(); - this.cookies = new ArrayList(); - this.cookieComparator = new CookieIdentityComparator(); - } - - // ------------------------------------------------------------- Properties - - /** - * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies. - * If the given cookie has already expired it will not be added, but existing - * values will still be removed. - * - * @param cookie the {@link Cookie cookie} to be added - * - * @see #addCookies(Cookie[]) - * - */ - public synchronized void addCookie(Cookie cookie) { - if (cookie != null) { - // first remove any old cookie that is equivalent - for (Iterator it = cookies.iterator(); it.hasNext();) { - Cookie tmp = (Cookie) it.next(); - if (cookieComparator.compare(cookie, tmp) == 0) { - it.remove(); - break; - } - } - if (!cookie.isExpired(new Date())) { - cookies.add(cookie); - } - } - } - - /** - * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and - * in the given array order. If any of the given cookies has already expired it will - * not be added, but existing values will still be removed. - * - * @param cookies the {@link Cookie cookies} to be added - * - * @see #addCookie(Cookie) - * - */ - public synchronized void addCookies(Cookie[] cookies) { - if (cookies != null) { - for (int i = 0; i < cookies.length; i++) { - this.addCookie(cookies[i]); - } - } - } - - /** - * Returns an array of {@link Cookie cookies} that this HTTP - * state currently contains. - * - * @return an array of {@link Cookie cookies}. - */ - public synchronized Cookie[] getCookies() { - return (Cookie[]) (cookies.toArray(new Cookie[cookies.size()])); - } - - /** - * Removes all of {@link Cookie cookies} in this HTTP state - * that have expired by the specified {@link java.util.Date date}. - * - * @return true if any cookies were purged. - * - * @see Cookie#isExpired(Date) - */ - public synchronized boolean clearExpired(Date date) { - boolean removed = false; - Iterator it = cookies.iterator(); - while (it.hasNext()) { - if (((Cookie) (it.next())).isExpired(date)) { - it.remove(); - removed = true; - } - } - return removed; - } - - - - /** - * Sets the {@link Credentials credentials} for the given authentication - * scope. Any previous credentials for the given scope will be overwritten. - * - * @param authscope the {@link AuthScope authentication scope} - * @param credentials the authentication {@link Credentials credentials} - * for the given scope. - * - * @see #getCredentials(AuthScope) - */ - public synchronized void setCredentials(final AuthScope authscope, final Credentials credentials) { - if (authscope == null) { - throw new IllegalArgumentException("Authentication scope may not be null"); - } - credMap.put(authscope, credentials); - } - - /** - * Find matching {@link Credentials credentials} for the given authentication scope. - * - * @param map the credentials hash map - * @param token the {@link AuthScope authentication scope} - * @return the credentials - * - */ - private static Credentials matchCredentials(final HashMap map, final AuthScope authscope) { - // see if we get a direct hit - Credentials creds = (Credentials)map.get(authscope); - if (creds == null) { - // Nope. - // Do a full scan - int bestMatchFactor = -1; - AuthScope bestMatch = null; - Iterator items = map.keySet().iterator(); - while (items.hasNext()) { - AuthScope current = (AuthScope)items.next(); - int factor = authscope.match(current); - if (factor > bestMatchFactor) { - bestMatchFactor = factor; - bestMatch = current; - } - } - if (bestMatch != null) { - creds = (Credentials)map.get(bestMatch); - } - } - return creds; - } - - /** - * Get the {@link Credentials credentials} for the given authentication scope. - * - * @param authscope the {@link AuthScope authentication scope} - * @return the credentials - * - * @see #setCredentials(AuthScope, Credentials) - */ - public synchronized Credentials getCredentials(final AuthScope authscope) { - if (authscope == null) { - throw new IllegalArgumentException("Authentication scope may not be null"); - } - return matchCredentials(this.credMap, authscope); - } - - /** - * Returns a string representation of this HTTP state. - * - * @return The string representation of the HTTP state. - * - * @see java.lang.Object#toString() - */ - public synchronized String toString() { - StringBuffer buffer = new StringBuffer(); - buffer.append(credMap); - buffer.append(cookies); - return buffer.toString(); - } - - /** - * Clears all credentials. - */ - public synchronized void clearCredentials() { - this.credMap.clear(); - } - - /** - * Clears all cookies. - */ - public synchronized void clearCookies() { - this.cookies.clear(); - } - - /** - * Clears the state information (all cookies, credentials and proxy credentials). - */ - public synchronized void clear() { - clearCookies(); - clearCredentials(); - } - -} diff --git a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java index dcdee1f46..c819334d1 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java @@ -45,9 +45,10 @@ import org.apache.http.HttpResponseInterceptor; import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; import org.apache.http.client.ClientRequestDirector; +import org.apache.http.client.CookieStore; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; import org.apache.http.client.HttpRequestRetryHandler; -import org.apache.http.client.HttpState; import org.apache.http.client.RedirectHandler; import org.apache.http.client.RoutedRequest; import org.apache.http.client.methods.HttpUriRequest; @@ -107,8 +108,11 @@ public abstract class AbstractHttpClient /** The authentication handler. */ private AuthenticationHandler authHandler; - /** The default HTTP state. */ - private HttpState defaultState; + /** The cookie store. */ + private CookieStore cookieStore; + + /** The credentials provider. */ + private CredentialsProvider credsProvider; /** * Creates a new HTTP client. @@ -154,7 +158,10 @@ public abstract class AbstractHttpClient protected abstract AuthenticationHandler createAuthenticationHandler(); - protected abstract HttpState createHttpState(); + protected abstract CookieStore createCookieStore(); + + + protected abstract CredentialsProvider createCredentialsProvider(); protected abstract void populateContext(HttpContext context); @@ -271,16 +278,29 @@ public abstract class AbstractHttpClient } - public synchronized final HttpState getState() { - if (defaultState == null) { - defaultState = createHttpState(); + public synchronized final CookieStore getCookieStore() { + if (cookieStore == null) { + cookieStore = createCookieStore(); } - return defaultState; + return cookieStore; } - public synchronized void setState(final HttpState state) { - defaultState = state; + public synchronized void setCookieStore(final CookieStore cookieStore) { + this.cookieStore = cookieStore; + } + + + public synchronized final CredentialsProvider getCredentialsProvider() { + if (credsProvider == null) { + credsProvider = createCredentialsProvider(); + } + return credsProvider; + } + + + public synchronized void setState(final CredentialsProvider credsProvider) { + this.credsProvider = credsProvider; } diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java index 68ce9dc0f..361b6b831 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpClient.java @@ -38,8 +38,9 @@ import org.apache.http.HttpRequest; import org.apache.http.HttpVersion; import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; +import org.apache.http.client.CookieStore; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpRequestRetryHandler; -import org.apache.http.client.HttpState; import org.apache.http.client.RedirectHandler; import org.apache.http.client.RoutedRequest; import org.apache.http.client.VersionInfo; @@ -238,8 +239,13 @@ public class DefaultHttpClient extends AbstractHttpClient { } - protected HttpState createHttpState() { - return new HttpState(); + protected CookieStore createCookieStore() { + return new BasicCookieStore(); + } + + + protected CredentialsProvider createCredentialsProvider() { + return new BasicCredentialsProvider(); } @@ -252,10 +258,10 @@ public class DefaultHttpClient extends AbstractHttpClient { getCookieSpecs()); context.setAttribute( ClientContext.COOKIE_STORE, - getState()); + getCookieStore()); context.setAttribute( ClientContext.CREDS_PROVIDER, - getState()); + getCredentialsProvider()); } diff --git a/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java b/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java index 9016c8c28..3f6a049cd 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java @@ -41,11 +41,9 @@ import org.apache.http.conn.HttpRoute; import org.apache.http.conn.ManagedClientConnection; import org.apache.http.conn.OperatedClientConnection; import org.apache.http.conn.SchemeRegistry; -import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; - /** * A connection "manager" for a single connection. * This manager is good only for single-threaded use. diff --git a/module-client/src/main/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java b/module-client/src/main/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java index dbf84ee58..74c671276 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/ThreadSafeClientConnManager.java @@ -47,12 +47,10 @@ import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionOperator; import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.HttpRoute; -import org.apache.http.conn.HttpRoute; import org.apache.http.conn.ManagedClientConnection; import org.apache.http.conn.OperatedClientConnection; import org.apache.http.conn.SchemeRegistry; import org.apache.http.conn.params.HttpConnectionManagerParams; -import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams;