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 2007/04/12 17:21:54 UTC

svn commit: r527976 - in /jakarta/httpcomponents/httpclient/trunk/src: java/org/apache/http/client/HttpState.java test/org/apache/http/client/TestAll.java test/org/apache/http/client/TestHttpState.java

Author: olegk
Date: Thu Apr 12 08:21:53 2007
New Revision: 527976

URL: http://svn.apache.org/viewvc?view=rev&rev=527976
Log:
Ported HttpState from Commons HttpClient

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/HttpState.java
      - copied, changed from r527968, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpState.java
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestHttpState.java
      - copied, changed from r527975, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpState.java
Modified:
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestAll.java

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/HttpState.java (from r527968, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpState.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/HttpState.java?view=diff&rev=527976&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpState.java&r1=527968&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/HttpState.java&r2=527976
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpState.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/client/HttpState.java Thu Apr 12 08:21:53 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpState.java,v 1.38 2004/12/20 11:50:54 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -28,19 +28,15 @@
  *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.client;
 
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.HashMap;
-import java.util.Map;
-import java.util.List;
 import java.util.Iterator;
-import org.apache.commons.httpclient.cookie.CookieSpec;
-import org.apache.commons.httpclient.cookie.CookiePolicy;
-import org.apache.commons.httpclient.auth.AuthScope; 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.Credentials;
+import org.apache.http.cookie.Cookie;
 
 /**
  * <p>
@@ -54,7 +50,7 @@
  * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  * @author Sean C. Sullivan
  * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
  * 
@@ -82,25 +78,7 @@
      */
     private ArrayList cookies = new ArrayList();
 
-    private boolean preemptive = false;
-
-    private int cookiePolicy = -1;
-        // -------------------------------------------------------- Class Variables
-
-    /**
-     * The boolean system property name to turn on preemptive authentication.
-     * @deprecated This field and feature will be removed following HttpClient 3.0.
-     */
-    public static final String PREEMPTIVE_PROPERTY = "httpclient.authentication.preemptive";
-
-    /**
-     * The default value for {@link #PREEMPTIVE_PROPERTY}.
-     * @deprecated This field and feature will be removed following HttpClient 3.0.
-     */
-    public static final String PREEMPTIVE_DEFAULT = "false";
-    
-    /** Log object for this class. */
-    private static final Log LOG = LogFactory.getLog(HttpState.class);
+    // -------------------------------------------------------- Class Variables
 
     /**
      * Default constructor.
@@ -122,8 +100,6 @@
      * 
      */
     public synchronized void addCookie(Cookie cookie) {
-        LOG.trace("enter HttpState.addCookie(Cookie)");
-
         if (cookie != null) {
             // first remove any old cookie that is equivalent
             for (Iterator it = cookies.iterator(); it.hasNext();) {
@@ -148,11 +124,8 @@
      * 
      * @see #addCookie(Cookie)
      * 
-     * 
      */
     public synchronized void addCookies(Cookie[] cookies) {
-        LOG.trace("enter HttpState.addCookies(Cookie[])");
-
         if (cookies != null) {
             for (int i = 0; i < cookies.length; i++) {
                 this.addCookie(cookies[i]);
@@ -170,57 +143,10 @@
      * 
      */
     public synchronized Cookie[] getCookies() {
-        LOG.trace("enter HttpState.getCookies()");
         return (Cookie[]) (cookies.toArray(new Cookie[cookies.size()]));
     }
 
     /**
-     * Returns an array of {@link Cookie cookies} in this HTTP 
-     * state that match the given request parameters.
-     * 
-     * @param domain the request domain
-     * @param port the request port
-     * @param path the request path
-     * @param secure <code>true</code> when using HTTPS
-     * 
-     * @return an array of {@link Cookie cookies}.
-     * 
-     * @see #getCookies()
-     * 
-     * @deprecated use CookieSpec#match(String, int, String, boolean, Cookie)
-     */
-    public synchronized Cookie[] getCookies(
-        String domain, 
-        int port, 
-        String path, 
-        boolean secure
-    ) {
-        LOG.trace("enter HttpState.getCookies(String, int, String, boolean)");
-
-        CookieSpec matcher = CookiePolicy.getDefaultSpec();
-        ArrayList list = new ArrayList(cookies.size());
-        for (int i = 0, m = cookies.size(); i < m; i++) {
-            Cookie cookie = (Cookie) (cookies.get(i));
-            if (matcher.match(domain, port, path, secure, cookie)) {
-                list.add(cookie);
-            }
-        }
-        return (Cookie[]) (list.toArray(new Cookie[list.size()]));
-    }
-
-    /**
-     * Removes all of {@link Cookie cookies} in this HTTP state
-     * that have expired according to the current system time.
-     * 
-     * @see #purgeExpiredCookies(java.util.Date)
-     * 
-     */
-    public synchronized boolean purgeExpiredCookies() {
-        LOG.trace("enter HttpState.purgeExpiredCookies()");
-        return purgeExpiredCookies(new Date());
-    }
-
-    /**
      * Removes all of {@link Cookie cookies} in this HTTP state
      * that have expired by the specified {@link java.util.Date date}. 
      * 
@@ -232,12 +158,11 @@
      * 
      * @see #purgeExpiredCookies()
      */
-    public synchronized boolean purgeExpiredCookies(Date date) {
-        LOG.trace("enter HttpState.purgeExpiredCookies(Date)");
+    public synchronized boolean purgeExpiredCookies() {
         boolean removed = false;
         Iterator it = cookies.iterator();
         while (it.hasNext()) {
-            if (((Cookie) (it.next())).isExpired(date)) {
+            if (((Cookie) (it.next())).isExpired()) {
                 it.remove();
                 removed = true;
             }
@@ -245,100 +170,6 @@
         return removed;
     }
 
-
-    /**
-     * Returns the current {@link CookiePolicy cookie policy} for this
-     * HTTP state.
-     * 
-     * @return The {@link CookiePolicy cookie policy}.
-     * 
-     * @deprecated Use 
-     *  {@link org.apache.commons.httpclient.params.HttpMethodParams#getCookiePolicy()},
-     *  {@link HttpMethod#getParams()}.     
-     */
-    
-    public int getCookiePolicy() {
-        return this.cookiePolicy;
-    }
-    
-
-    /**
-     * Defines whether preemptive authentication should be 
-     * attempted.
-     * 
-     * @param value <tt>true</tt> if preemptive authentication should be 
-     * attempted, <tt>false</tt> otherwise. 
-     * 
-     * @deprecated Use 
-     * {@link org.apache.commons.httpclient.params.HttpClientParams#setAuthenticationPreemptive(boolean)}, 
-     * {@link HttpClient#getParams()}.
-     */
-    
-    public void setAuthenticationPreemptive(boolean value) {
-        this.preemptive = value;
-    }
-
-
-    /**
-     * Returns <tt>true</tt> if preemptive authentication should be 
-     * attempted, <tt>false</tt> otherwise.
-     * 
-     * @return boolean flag.
-     * 
-     * @deprecated Use 
-     * {@link org.apache.commons.httpclient.params.HttpClientParams#isAuthenticationPreemptive()}, 
-     * {@link HttpClient#getParams()}.
-     */
-    
-    public boolean isAuthenticationPreemptive() {
-        return this.preemptive;
-    }
-    
-
-    /**
-     * Sets the current {@link CookiePolicy cookie policy} for this HTTP
-     * state to one of the following supported policies: 
-     * {@link CookiePolicy#COMPATIBILITY}, 
-     * {@link CookiePolicy#NETSCAPE_DRAFT} or
-     * {@link CookiePolicy#RFC2109}.
-     * 
-     * @param policy new {@link CookiePolicy cookie policy}
-     * 
-     * @deprecated 
-     *  Use {@link org.apache.commons.httpclient.params.HttpMethodParams#setCookiePolicy(String)},
-     *  {@link HttpMethod#getParams()}.     
-     */
-    
-    public void setCookiePolicy(int policy) {
-        this.cookiePolicy = policy;
-    }
-
-    /** 
-     * Sets the {@link Credentials credentials} for the given authentication 
-     * realm on the given host. The <code>null</code> realm signifies default 
-     * credentials for the given host, which should be used when no 
-     * {@link Credentials credentials} have been explictly supplied for the 
-     * challenging realm. The <code>null</code> host signifies default 
-     * credentials, which should be used when no {@link Credentials credentials} 
-     * have been explictly supplied for the challenging host. Any previous 
-     * credentials for the given realm on the given host will be overwritten.
-     * 
-     * @param realm the authentication realm
-     * @param host the host the realm belongs to
-     * @param credentials the authentication {@link Credentials credentials} 
-     * for the given realm.
-     * 
-     * @see #getCredentials(String, String)
-     * @see #setProxyCredentials(String, String, Credentials) 
-     * 
-     * @deprecated use #setCredentials(AuthScope, Credentials)
-     */
-    
-    public synchronized void setCredentials(String realm, String host, Credentials credentials) {
-        LOG.trace("enter HttpState.setCredentials(String, String, Credentials)");
-        credMap.put(new AuthScope(host, AuthScope.ANY_PORT, realm, AuthScope.ANY_SCHEME), credentials);
-    }
-
     /** 
      * Sets the {@link Credentials credentials} for the given authentication 
      * scope. Any previous credentials for the given scope will be overwritten.
@@ -356,7 +187,6 @@
         if (authscope == null) {
             throw new IllegalArgumentException("Authentication scope may not be null");
         }
-        LOG.trace("enter HttpState.setCredentials(AuthScope, Credentials)");
         credMap.put(authscope, credentials);
     }
 
@@ -393,33 +223,6 @@
     }
     
     /**
-     * Get the {@link Credentials credentials} for the given authentication scope on the 
-     * given host.
-     *
-     * If the <i>realm</i> exists on <i>host</i>, return the coresponding credentials.
-     * If the <i>host</i> exists with a <tt>null</tt> <i>realm</i>, return the corresponding
-     * credentials.
-     * If the <i>realm</i> exists with a <tt>null</tt> <i>host</i>, return the
-     * corresponding credentials.  If the <i>realm</i> does not exist, return
-     * the default Credentials.  If there are no default credentials, return
-     * <code>null</code>.
-     *
-     * @param realm the authentication realm
-     * @param host the host the realm is on
-     * @return the credentials 
-     * 
-     * @see #setCredentials(String, String, Credentials)
-     * 
-     * @deprecated use #getCredentials(AuthScope)
-     */
-    
-    public synchronized Credentials getCredentials(String realm, String host) {
-        LOG.trace("enter HttpState.getCredentials(String, String");
-        return matchCredentials(this.credMap, 
-            new AuthScope(host, AuthScope.ANY_PORT, realm, AuthScope.ANY_SCHEME));
-    }
-
-    /**
      * Get the {@link Credentials credentials} for the given authentication scope.
      *
      * @param authscope the {@link AuthScope authentication scope}
@@ -433,39 +236,9 @@
         if (authscope == null) {
             throw new IllegalArgumentException("Authentication scope may not be null");
         }
-        LOG.trace("enter HttpState.getCredentials(AuthScope)");
         return matchCredentials(this.credMap, authscope);
     }
 
-    /**
-     * Sets the {@link Credentials credentials} for the given proxy authentication 
-     * realm on the given proxy host. The <code>null</code> proxy realm signifies 
-     * default credentials for the given proxy host, which should be used when no 
-     * {@link Credentials credentials} have been explictly supplied for the 
-     * challenging proxy realm. The <code>null</code> proxy host signifies default 
-     * credentials, which should be used when no {@link Credentials credentials} 
-     * have been explictly supplied for the challenging proxy host. Any previous 
-     * credentials for the given proxy realm on the given proxy host will be 
-     * overwritten.
-     *
-     * @param realm the authentication realm
-     * @param proxyHost the proxy host
-     * @param credentials the authentication credentials for the given realm
-     * 
-     * @see #getProxyCredentials(AuthScope)
-     * @see #setCredentials(AuthScope, Credentials)
-     * 
-     * @deprecated use #setProxyCredentials(AuthScope, Credentials)
-     */
-    public synchronized void setProxyCredentials(
-        String realm, 
-        String proxyHost, 
-        Credentials credentials
-    ) {
-        LOG.trace("enter HttpState.setProxyCredentials(String, String, Credentials");
-        proxyCred.put(new AuthScope(proxyHost, AuthScope.ANY_PORT, realm, AuthScope.ANY_SCHEME), credentials);
-    }
-
     /** 
      * Sets the {@link Credentials proxy credentials} for the given authentication 
      * realm. Any previous credentials for the given realm will be overwritten.
@@ -479,42 +252,16 @@
      * 
      * @since 3.0
      */
-    public synchronized void setProxyCredentials(final AuthScope authscope, 
-        final Credentials credentials)
-    {
+    public synchronized void setProxyCredentials(
+            final AuthScope authscope, 
+            final Credentials credentials) {
         if (authscope == null) {
             throw new IllegalArgumentException("Authentication scope may not be null");
         }
-        LOG.trace("enter HttpState.setProxyCredentials(AuthScope, Credentials)");
         proxyCred.put(authscope, credentials);
     }
 
     /**
-     * Get the {@link Credentials credentials} for the proxy host with the given 
-     * authentication scope.
-     *
-     * If the <i>realm</i> exists on <i>host</i>, return the coresponding credentials.
-     * If the <i>host</i> exists with a <tt>null</tt> <i>realm</i>, return the corresponding
-     * credentials.
-     * If the <i>realm</i> exists with a <tt>null</tt> <i>host</i>, return the
-     * corresponding credentials.  If the <i>realm</i> does not exist, return
-     * the default Credentials.  If there are no default credentials, return
-     * <code>null</code>.
-     * 
-     * @param realm the authentication realm
-     * @param proxyHost the proxy host the realm is on
-     * @return the credentials 
-     * @see #setProxyCredentials(String, String, Credentials)
-     * 
-     * @deprecated use #getProxyCredentials(AuthScope)
-     */
-    public synchronized Credentials getProxyCredentials(String realm, String proxyHost) {
-       LOG.trace("enter HttpState.getCredentials(String, String");
-        return matchCredentials(this.proxyCred, 
-            new AuthScope(proxyHost, AuthScope.ANY_PORT, realm, AuthScope.ANY_SCHEME));
-    }
-    
-    /**
      * Get the {@link Credentials proxy credentials} for the given authentication scope.
      *
      * @param authscope the {@link AuthScope authentication scope}
@@ -528,7 +275,6 @@
         if (authscope == null) {
             throw new IllegalArgumentException("Authentication scope may not be null");
         }
-        LOG.trace("enter HttpState.getProxyCredentials(AuthScope)");
         return matchCredentials(this.proxyCred, authscope);
     }
 
@@ -540,71 +286,24 @@
      * @see java.lang.Object#toString()
      */
     public synchronized String toString() {
-        StringBuffer sbResult = new StringBuffer();
-
-        sbResult.append("[");
-        sbResult.append(getCredentialsStringRepresentation(proxyCred));
-        sbResult.append(" | ");
-        sbResult.append(getCredentialsStringRepresentation(credMap));
-        sbResult.append(" | ");
-        sbResult.append(getCookiesStringRepresentation(cookies));
-        sbResult.append("]");
-
-        String strResult = sbResult.toString();
-
-        return strResult;
-    }
-    
-    /**
-     * Returns a string representation of the credentials.
-     * @param credMap The credentials.
-     * @return The string representation.
-     */
-    private static String getCredentialsStringRepresentation(final Map credMap) {
-        StringBuffer sbResult = new StringBuffer();
-        Iterator iter = credMap.keySet().iterator();
-        while (iter.hasNext()) {
-            Object key = iter.next();
-            Credentials cred = (Credentials) credMap.get(key);
-            if (sbResult.length() > 0) {
-                sbResult.append(", ");
-            }
-            sbResult.append(key);
-            sbResult.append("#");
-            sbResult.append(cred.toString());
-        }
-        return sbResult.toString();
-    }
-    
-    /**
-     * Returns a string representation of the cookies.
-     * @param cookies The cookies
-     * @return The string representation.
-     */
-    private static String getCookiesStringRepresentation(final List cookies) {
-        StringBuffer sbResult = new StringBuffer();
-        Iterator iter = cookies.iterator();
-        while (iter.hasNext()) {
-            Cookie ck = (Cookie) iter.next();
-            if (sbResult.length() > 0) {
-                sbResult.append("#");
-            }
-            sbResult.append(ck.toExternalForm());
-        }
-        return sbResult.toString();
+        StringBuffer buffer = new StringBuffer();
+        buffer.append(proxyCred);
+        buffer.append(credMap);
+        buffer.append(cookies);
+        return buffer.toString();
     }
     
     /**
      * Clears all credentials.
      */
-    public void clearCredentials() {
+    public synchronized void clearCredentials() {
         this.credMap.clear();
     }
     
     /**
      * Clears all proxy credentials.
      */
-    public void clearProxyCredentials() {
+    public synchronized void clearProxyCredentials() {
         this.proxyCred.clear();
     }
     
@@ -618,9 +317,10 @@
     /**
      * Clears the state information (all cookies, credentials and proxy credentials).
      */
-    public void clear() {
+    public synchronized void clear() {
         clearCookies();
         clearCredentials();
         clearProxyCredentials();
     }
+    
 }

Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestAll.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestAll.java?view=diff&rev=527976&r1=527975&r2=527976
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestAll.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestAll.java Thu Apr 12 08:21:53 2007
@@ -53,6 +53,7 @@
         suite.addTest(TestAllHttpClientImpl.suite());
         suite.addTest(TestAllConnImpl.suite());
         suite.addTest(TestAllSSL.suite());        
+        suite.addTest(TestHttpState.suite());        
         return suite;
     }
 

Copied: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestHttpState.java (from r527975, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpState.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestHttpState.java?view=diff&rev=527976&p1=jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpState.java&r1=527975&p2=jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestHttpState.java&r2=527976
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpState.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/client/TestHttpState.java Thu Apr 12 08:21:53 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpState.java,v 1.7 2004/06/23 06:50:25 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  * ====================================================================
@@ -24,14 +24,13 @@
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
- *
- * [Additional notices, if required by prior licensing conditions]
- *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.client;
 
-import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
 
 import junit.framework.*;
 
@@ -91,24 +90,6 @@
         assertEquals(CREDS1, state.getCredentials(SCOPE1));
         assertEquals(CREDS2, state.getCredentials(SCOPE2));
     }
-
-	public void testToString()
-	{
-        HttpState state = new HttpState();
-        assertNotNull(state.toString());
-        
-        state.addCookie(new Cookie("foo", "bar", "yeah"));
-        assertNotNull(state.toString());
-
-        state.addCookie(new Cookie("flub", "duck", "yuck"));
-        assertNotNull(state.toString());
-
-		state.setCredentials(SCOPE1, CREDS1);
-        assertNotNull(state.toString());
-        
-		state.setProxyCredentials(SCOPE2, CREDS2);
-        assertNotNull(state.toString());
-	}
 
     public void testHttpStateNoCredentials() {
         HttpState state = new HttpState();