hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r484777 - in /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn: ./ impl/
Date Fri, 08 Dec 2006 20:59:43 GMT
Author: olegk
Date: Fri Dec  8 12:59:42 2006
New Revision: 484777

URL: http://svn.apache.org/viewvc?view=rev&rev=484777
Log:
Ported HttpConfiguration, HttpConnection, HttpConnectionManager and SimpleHttpConnectionManager from HttpClient 3.x to the new API

Added:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ConnectionPoolTimeoutException.java
      - copied, changed from r418241, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ConnectionPoolTimeoutException.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HostConfiguration.java
      - copied, changed from r418211, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HostConfiguration.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpConnectionManager.java
      - copied, changed from r418211, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnectionManager.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpHostConnection.java   (with props)
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultHttpHostConnection.java
      - copied, changed from r484645, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnection.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/SimpleHttpConnectionManager.java
      - copied, changed from r418211, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ConnectionPoolTimeoutException.java (from r418241, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ConnectionPoolTimeoutException.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ConnectionPoolTimeoutException.java?view=diff&rev=484777&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ConnectionPoolTimeoutException.java&r1=418241&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ConnectionPoolTimeoutException.java&r2=484777
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ConnectionPoolTimeoutException.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/ConnectionPoolTimeoutException.java Fri Dec  8 12:59:42 2006
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/ConnectionPoolTimeoutException.java,v 1.1 2004/07/05 22:46:58 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -27,7 +27,7 @@
  *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.conn;
 
 /**
  * A timeout while connecting waiting for an available connection 
@@ -39,6 +39,8 @@
  */
 public class ConnectionPoolTimeoutException extends ConnectTimeoutException {
 
+    private static final long serialVersionUID = -7898874842020245128L;
+
     /**
      * Creates a ConnectTimeoutException with a <tt>null</tt> detail message.
      */
@@ -53,17 +55,6 @@
      */
     public ConnectionPoolTimeoutException(String message) {
         super(message);
-    }
-
-    /**
-     * Creates a new ConnectTimeoutException with the specified detail message and cause.
-     * 
-     * @param message the exception detail message
-     * @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
-     * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
-     */
-    public ConnectionPoolTimeoutException(String message, Throwable cause) {
-        super(message, cause);
     }
 
 }

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HostConfiguration.java (from r418211, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HostConfiguration.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HostConfiguration.java?view=diff&rev=484777&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HostConfiguration.java&r1=418211&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HostConfiguration.java&r2=484777
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HostConfiguration.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HostConfiguration.java Fri Dec  8 12:59:42 2006
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v 1.23 2005/01/14 21:16:40 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -27,21 +27,20 @@
  *
  */
 
-package org.apache.commons.httpclient;
-
-import org.apache.commons.httpclient.params.HostParams;
-import org.apache.commons.httpclient.protocol.Protocol;
-import org.apache.commons.httpclient.util.LangUtils;
+package org.apache.http.conn;
 
 import java.net.InetAddress;
 
+import org.apache.http.HttpHost;
+import org.apache.http.util.LangUtils;
+
 /**
  * Holds all of the variables needed to describe an HTTP connection to a host.  This includes 
  * remote host, port and protocol, proxy host and port, local address, and virtual host.
  * 
  * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * @author Laura Werner
  * 
  * @since 2.0 
@@ -59,14 +58,11 @@
     private HttpHost host = null;
 
     /** The host name of the proxy server */
-    private ProxyHost proxyHost = null;
+    private HttpHost proxyHost = null;
 
     /** The local address to use when creating the socket, or null to use the default */
     private InetAddress localAddress = null;
 
-    /** Parameters specific to this host */
-    private HostParams params = new HostParams();
-
     /**
      * Constructor for HostConfiguration.
      */
@@ -83,22 +79,17 @@
         // wrap all of the assignments in a synchronized block to avoid
         // having to negotiate the monitor for each method call
         synchronized (hostConfiguration) {
-            try {
-                if (hostConfiguration.host != null) {
-                    this.host = (HttpHost) hostConfiguration.host.clone();
-                } else {
-                    this.host = null;
-                }
-                if (hostConfiguration.proxyHost != null) {
-                    this.proxyHost = (ProxyHost) hostConfiguration.proxyHost.clone();
-                } else {
-                    this.proxyHost = null;
-                }
-                this.localAddress = hostConfiguration.getLocalAddress();
-                this.params = (HostParams)hostConfiguration.getParams().clone();
-            } catch (CloneNotSupportedException e) {
-                throw new IllegalArgumentException("Host configuration could not be cloned");
+            if (hostConfiguration.host != null) {
+                this.host = new HttpHost(hostConfiguration.host);
+            } else {
+                this.host = null;
+            }
+            if (hostConfiguration.proxyHost != null) {
+                this.proxyHost = new HttpHost(hostConfiguration.proxyHost);
+            } else {
+                this.proxyHost = null;
             }
+            this.localAddress = hostConfiguration.getLocalAddress();
         }        
     }
 
@@ -142,86 +133,12 @@
             } else {
                 appendComma = true;
             }
-            b.append("params=").append(this.params);
         }
         b.append("]");
         return b.toString();
     }    
     
     /**
-     * Tests if the host configuration equals the configuration set on the
-     * connection. True only if the host, port, protocol, local address and virtual address
-     * are equal.  If no host configuration has been set false will be returned.
-     * 
-     * @param connection the connection to test against
-     * @return <code>true</code> if the connection's host information equals that of this
-     * configuration
-     * 
-     * @see #proxyEquals(HttpConnection)
-     */
-    public synchronized boolean hostEquals(final HttpConnection connection) {
-        if (connection == null) {
-            throw new IllegalArgumentException("Connection may not be null");
-        }
-        if (this.host != null) {
-            if (!this.host.getHostName().equalsIgnoreCase(connection.getHost())) {
-                return false;
-            }
-            if (this.host.getPort() != connection.getPort()) {
-                return false;
-            }
-            if (!this.host.getProtocol().equals(connection.getProtocol())) {
-                return false;
-            }
-            if (this.localAddress != null) {
-                if (!this.localAddress.equals(connection.getLocalAddress())) {
-                    return false;
-                }
-            } else {
-                if (connection.getLocalAddress() != null) {
-                    return false; 
-                }
-            }
-            return true;
-        } else {
-            return false;   
-        }
-    }
-
-    /**
-     * Tests if the proxy configuration equals the configuration set on the
-     * connection. True only if the proxyHost and proxyPort are equal.
-     *
-     * @param connection the connection to test against
-     * @return <code>true</code> if the connection's proxy information equals that of this
-     * configuration
-     *
-     * @see #hostEquals(HttpConnection)
-     */
-    public synchronized boolean proxyEquals(final HttpConnection connection) {
-        if (connection == null) {
-            throw new IllegalArgumentException("Connection may not be null");
-        }
-        if (this.proxyHost != null) {
-            return
-                this.proxyHost.getHostName().equalsIgnoreCase(connection.getProxyHost())
-                && this.proxyHost.getPort() == connection.getProxyPort();
-        } else {
-            return connection.getProxyHost() == null;
-        }
-    }    
-    
-    /**
-     * Returns true if the host is set.
-     * @return <code>true</code> if the host is set.
-     * 
-     * @deprecated no longer used
-     */
-    public synchronized boolean isHostSet() {
-        return this.host != null;
-    }
-
-    /**
      * Sets the given host
      * 
      * @param host the host
@@ -238,42 +155,9 @@
      * @param protocol The protocol.
      */
     public synchronized void setHost(final String host, int port, final String protocol) {
-        this.host = new HttpHost(host, port, Protocol.getProtocol(protocol));
-    }
-    
-    /**
-     * Sets the given host, virtual host, port and protocol.
-     * 
-     * @param host the host(IP or DNS name)
-     * @param virtualHost the virtual host name or <code>null</code>
-     * @param port the host port or -1 to use protocol default
-     * @param protocol the protocol
-     * 
-     * @deprecated #setHost(String, int, Protocol)
-     */
-    public synchronized void setHost(final String host, final String virtualHost, int port, 
-        final Protocol protocol) {
-        setHost(host, port, protocol);
-        this.params.setVirtualHost(virtualHost);
-    }
-
-    /**
-     * Sets the given host, port and protocol.
-     *   
-     * @param host the host(IP or DNS name)
-     * @param port The port
-     * @param protocol the protocol
-     */
-    public synchronized void setHost(final String host, int port, final Protocol protocol) {
-        if (host == null) {
-            throw new IllegalArgumentException("host must not be null");   
-        }
-        if (protocol == null) {
-            throw new IllegalArgumentException("protocol must not be null");   
-        }
         this.host = new HttpHost(host, port, protocol);
     }
-
+    
     /**
      * Sets the given host and port.  Uses the default protocol "http".
      * 
@@ -281,7 +165,7 @@
      * @param port The port
      */
     public synchronized void setHost(final String host, int port) {
-        setHost(host, port, Protocol.getProtocol("http"));
+        setHost(host, port, "http");
     }
     
     /**
@@ -290,23 +174,10 @@
      * @param host The host(IP or DNS name).
      */
     public synchronized void setHost(final String host) {
-        Protocol defaultProtocol = Protocol.getProtocol("http"); 
-        setHost(host, defaultProtocol.getDefaultPort(), defaultProtocol);
+        setHost(host, -1, "http");
     }
     
     /**
-     * Sets the protocol, host and port from the given URI.
-     * @param uri the URI.
-     */
-    public synchronized void setHost(final URI uri) {
-        try {
-            setHost(uri.getHost(), uri.getPort(), uri.getScheme());
-        } catch (URIException e) {
-            throw new IllegalArgumentException(e.toString());
-        }
-    }
-
-    /**
      * Return the host url.
      * 
      * @return The host url.
@@ -320,29 +191,29 @@
     }
 
     /**
-     * Returns the host.
+     * Returns the target host.
      * 
-     * @return the host(IP or DNS name), or <code>null</code> if not set
+     * @return the target host, or <code>null</code> if not set
      * 
      * @see #isHostSet()
      */
-    public synchronized String getHost() {
-        if (this.host != null) {
-            return this.host.getHostName();
-        } else {
-            return null;
-        }
+    public synchronized HttpHost getHost() {
+        return this.host;
     }
 
     /**
-     * Returns the virtual host.
+     * Returns the host name.
      * 
-     * @return the virtual host name, or <code>null</code> if not set
+     * @return the host(IP or DNS name), or <code>null</code> if not set
      * 
-     * @deprecated use HostParams
+     * @see #isHostSet()
      */
-    public synchronized String getVirtualHost() {
-    	return this.params.getVirtualHost();
+    public synchronized String getHostName() {
+        if (this.host != null) {
+            return this.host.getHostName();
+        } else {
+            return null;
+        }
     }
 
     /**
@@ -364,33 +235,20 @@
      * Returns the protocol.
      * @return The protocol.
      */
-    public synchronized Protocol getProtocol() {
+    public synchronized Scheme getScheme() {
         if (this.host != null) {
-            return this.host.getProtocol();
+            return Scheme.getScheme(this.host.getSchemeName());
         } else {
             return null;
         }
     }
 
     /**
-     * Tests if the proxy host/port have been set.
-     * 
-     * @return <code>true</code> if a proxy server has been set.
-     * 
-     * @see #setProxy(String, int)
-     * 
-     * @deprecated no longer used
-     */    
-    public synchronized boolean isProxySet() {
-        return this.proxyHost != null;   
-    }
-
-    /**
      * Sets the given proxy host
      * 
      * @param proxyHost the proxy host
      */
-    public synchronized void setProxyHost(final ProxyHost proxyHost) {
+    public synchronized void setProxyHost(final HttpHost proxyHost) {
         this.proxyHost = proxyHost;
     }
     
@@ -400,7 +258,18 @@
      * @param proxyPort The proxy port
      */
     public synchronized void setProxy(final String proxyHost, int proxyPort) {
-        this.proxyHost = new ProxyHost(proxyHost, proxyPort); 
+        this.proxyHost = new HttpHost(proxyHost, proxyPort); 
+    }
+
+    /**
+     * Returns the proxyHost.
+     * 
+     * @return the proxy host, or <code>null</code> if not set
+     * 
+     * @see #isProxySet()
+     */
+    public synchronized HttpHost getProxyHost() {
+        return this.proxyHost;
     }
 
     /**
@@ -410,7 +279,7 @@
      * 
      * @see #isProxySet()
      */
-    public synchronized String getProxyHost() {
+    public synchronized String getProxyHostName() {
         if (this.proxyHost != null) {
             return this.proxyHost.getHostName();
         } else {
@@ -456,31 +325,6 @@
         return this.localAddress;
     }
     
-    /**
-     * Returns {@link HostParams HTTP protocol parameters} associated with this host.
-     *
-     * @return HTTP parameters.
-     *
-     * @since 3.0
-     */
-    public HostParams getParams() {
-        return this.params;
-    }
-
-    /**
-     * Assigns {@link HostParams HTTP protocol parameters} specific to this host.
-     * 
-     * @since 3.0
-     * 
-     * @see HostParams
-     */
-    public void setParams(final HostParams params) {
-        if (params == null) {
-            throw new IllegalArgumentException("Parameters may not be null");
-        }
-        this.params = params;
-    }
-
     /**
      * @see java.lang.Object#equals(java.lang.Object)
      */

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpConnectionManager.java (from r418211, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnectionManager.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpConnectionManager.java?view=diff&rev=484777&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnectionManager.java&r1=418211&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpConnectionManager.java&r2=484777
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnectionManager.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpConnectionManager.java Fri Dec  8 12:59:42 2006
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpConnectionManager.java,v 1.24 2004/07/05 22:46:58 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -27,19 +27,14 @@
  *
  */
 
-package org.apache.commons.httpclient;
-
-import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
+package org.apache.http.conn;
 
 /**
- * An interface for classes that manage HttpConnections.
+ * An interface for classes that manage {@link HttpHostConnection}s.
  * 
- * @see org.apache.commons.httpclient.HttpConnection
- * @see org.apache.commons.httpclient.HttpClient#HttpClient(HttpConnectionManager)
- *
  * @author Michael Becke
  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * 
  * @since 2.0
  */
@@ -56,35 +51,8 @@
      * connection
      * 
      * @return an HttpConnection for the given configuration
-     * 
-     * @see HttpConnection#setHttpConnectionManager(HttpConnectionManager)
-     */
-    HttpConnection getConnection(HostConfiguration hostConfiguration);
-
-    /**
-     * Gets an HttpConnection for a given host configuration. If a connection is
-     * not available, this method will block for at most the specified number of
-     * milliseconds or until a connection becomes available.
-     *
-     * The connection manager should be registered with any HttpConnection that
-     * is created.
-     *
-     * @param hostConfiguration the host configuration to use to configure the
-     * connection
-     * @param timeout - the time (in milliseconds) to wait for a connection to
-     * become available, 0 to specify an infinite timeout
-     * 
-     * @return an HttpConnection for the given configuraiton
-     * 
-     * @throws HttpException if no connection becomes available before the
-     * timeout expires
-     * 
-     * @see HttpConnection#setHttpConnectionManager(HttpConnectionManager)
-     * 
-     * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long) 
      */
-    HttpConnection getConnection(HostConfiguration hostConfiguration, long timeout)
-        throws HttpException;
+    HttpHostConnection getConnection(HostConfiguration hostConfiguration);
 
 	/**
 	 * Gets an HttpConnection for a given host configuration. If a connection is
@@ -104,19 +72,17 @@
 	 * @throws ConnectionPoolTimeoutException if no connection becomes available before the
 	 * timeout expires
 	 * 
-	 * @see HttpConnection#setHttpConnectionManager(HttpConnectionManager)
-     * 
      * @since 3.0
 	 */
-	HttpConnection getConnectionWithTimeout(HostConfiguration hostConfiguration, long timeout)
+    HttpHostConnection getConnection(HostConfiguration hostConfiguration, long timeout)
 		throws ConnectionPoolTimeoutException;
 
     /**
      * Releases the given HttpConnection for use by other requests.
      *
-     * @param conn - The HttpConnection to make available.
+     * @param conn - The HttpHostConnection to make available.
      */
-    void releaseConnection(HttpConnection conn);
+    void releaseConnection(HttpHostConnection conn);
 
     /**
      * Closes connections that have been idle for at least the given amount of time.  Only
@@ -128,23 +94,6 @@
      */
     void closeIdleConnections(long idleTimeout);
     
-    /**
-     * Returns {@link HttpConnectionManagerParams parameters} associated 
-     * with this connection manager.
-     * 
-     * @since 3.0
-     * 
-     * @see HttpConnectionManagerParams
-     */
-    HttpConnectionManagerParams getParams();
-
-    /**
-     * Assigns {@link HttpConnectionManagerParams parameters} for this 
-     * connection manager.
-     * 
-     * @since 3.0
-     * 
-     * @see HttpConnectionManagerParams
-     */
-    void setParams(final HttpConnectionManagerParams params);
+    void shutdown();
+    
 }

Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpHostConnection.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpHostConnection.java?view=auto&rev=484777
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpHostConnection.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpHostConnection.java Fri Dec  8 12:59:42 2006
@@ -0,0 +1,48 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.conn;
+
+import java.io.IOException;
+import java.net.SocketException;
+
+import org.apache.http.HttpClientConnection;
+import org.apache.http.params.HttpParams;
+
+public interface HttpHostConnection extends HttpClientConnection {
+    
+    HostConfiguration getHostConfiguration();
+
+    void open(HttpParams params) throws IOException;
+    
+    void tunnelCreated(HttpParams params) throws IOException;
+
+    void setSocketTimeout(int timeout) throws SocketException;
+    
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpHostConnection.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/HttpHostConnection.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultHttpHostConnection.java (from r484645, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnection.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultHttpHostConnection.java?view=diff&rev=484777&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnection.java&r1=484645&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultHttpHostConnection.java&r2=484777
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpConnection.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/DefaultHttpHostConnection.java Fri Dec  8 12:59:42 2006
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v 1.107 2005/01/14 21:30:59 olegk Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -28,729 +28,137 @@
  *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.conn.impl;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InterruptedIOException;
-import java.io.OutputStream;
-import java.lang.reflect.Method;
-import java.net.InetAddress;
 import java.net.Socket;
-import java.net.SocketException;
 
-import org.apache.commons.httpclient.params.HttpConnectionParams;
-import org.apache.commons.httpclient.protocol.Protocol;
-import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
-import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
-import org.apache.commons.httpclient.util.EncodingUtil;
-import org.apache.commons.httpclient.util.ExceptionUtil;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.conn.HostConfiguration;
+import org.apache.http.conn.HttpConnectionManager;
+import org.apache.http.conn.HttpHostConnection;
+import org.apache.http.conn.Scheme;
+import org.apache.http.conn.SecureSocketFactory;
+import org.apache.http.conn.SocketFactory;
+import org.apache.http.impl.SocketHttpClientConnection;
+import org.apache.http.params.HttpParams;
 
 /**
- * An abstraction of an HTTP {@link InputStream} and {@link OutputStream}
- * pair, together with the relevant attributes.
- * <p>
- * The following options are set on the socket before getting the input/output 
- * streams in the {@link #open()} method:
- * <table border=1><tr>
- *    <th>Socket Method
- *    <th>Sockets Option
- *    <th>Configuration
- * </tr><tr>
- *    <td>{@link java.net.Socket#setTcpNoDelay(boolean)}
- *    <td>SO_NODELAY
- *    <td>{@link HttpConnectionParams#setTcpNoDelay(boolean)}
- * </tr><tr>
- *    <td>{@link java.net.Socket#setSoTimeout(int)}
- *    <td>SO_TIMEOUT
- *    <td>{@link HttpConnectionParams#setSoTimeout(int)}
- * </tr><tr>
- *    <td>{@link java.net.Socket#setSendBufferSize(int)}
- *    <td>SO_SNDBUF
- *    <td>{@link HttpConnectionParams#setSendBufferSize(int)}
- * </tr><tr>
- *    <td>{@link java.net.Socket#setReceiveBufferSize(int)}
- *    <td>SO_RCVBUF
- *    <td>{@link HttpConnectionParams#setReceiveBufferSize(int)}
- * </tr></table>
+ * Default {@link HttpHostConnection} implementation.
  *
  * @author Rod Waldhoff
  * @author Sean C. Sullivan
  * @author Ortwin Glueck
  * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * @author Michael Becke
  * @author Eric E Johnson
  * @author Laura Werner
  * 
  * @version   $Revision$ $Date$
  */
-public class HttpConnection {
+public class DefaultHttpHostConnection 
+    extends SocketHttpClientConnection implements HttpHostConnection {
 
-    // ----------------------------------------------------------- Constructors
-
-    /**
-     * Creates a new HTTP connection for the given host and port.
-     *
-     * @param host the host to connect to
-     * @param port the port to connect to
-     */
-    public HttpConnection(String host, int port) {
-        this(null, -1, host, null, port, Protocol.getProtocol("http"));
-    }
-
-    /**
-     * Creates a new HTTP connection for the given host and port
-     * using the given protocol.
-     *
-     * @param host the host to connect to
-     * @param port the port to connect to
-     * @param protocol the protocol to use
-     */
-    public HttpConnection(String host, int port, Protocol protocol) {
-        this(null, -1, host, null, port, protocol);
-    }
-
-    /**
-     * Creates a new HTTP connection for the given host with the virtual 
-     * alias and port using given protocol.
-     *
-     * @param host the host to connect to
-     * @param virtualHost the virtual host requests will be sent to
-     * @param port the port to connect to
-     * @param protocol the protocol to use
-     */
-    public HttpConnection(String host, String virtualHost, int port, Protocol protocol) {
-        this(null, -1, host, virtualHost, port, protocol);
-    }
-
-    /**
-     * Creates a new HTTP connection for the given host and port via the 
-     * given proxy host and port using the default protocol.
-     *
-     * @param proxyHost the host to proxy via
-     * @param proxyPort the port to proxy via
-     * @param host the host to connect to
-     * @param port the port to connect to
-     */
-    public HttpConnection(
-        String proxyHost,
-        int proxyPort,
-        String host,
-        int port) {
-        this(proxyHost, proxyPort, host, null, port, Protocol.getProtocol("http"));
-    }
-
-    /**
-     * Creates a new HTTP connection for the given host configuration.
-     * 
-     * @param hostConfiguration the host/proxy/protocol to use
-     */
-    public HttpConnection(HostConfiguration hostConfiguration) {
-        this(hostConfiguration.getProxyHost(),
-             hostConfiguration.getProxyPort(),
-             hostConfiguration.getHost(),
-             hostConfiguration.getPort(),
-             hostConfiguration.getProtocol());
-        this.localAddress = hostConfiguration.getLocalAddress();
-    }
-
-    /**
-     * Creates a new HTTP connection for the given host with the virtual 
-     * alias and port via the given proxy host and port using the given 
-     * protocol.
-     * 
-     * @param proxyHost the host to proxy via
-     * @param proxyPort the port to proxy via
-     * @param host the host to connect to. Parameter value must be non-null.
-     * @param virtualHost No longer applicable. 
-     * @param port the port to connect to
-     * @param protocol The protocol to use. Parameter value must be non-null.
-     * 
-     * @deprecated use #HttpConnection(String, int, String, int, Protocol)
-     */
-    public HttpConnection(
-        String proxyHost,
-        int proxyPort,
-        String host,
-        String virtualHost,
-        int port,
-        Protocol protocol) {
-    	this(proxyHost, proxyPort, host, port, protocol);
-    }
-
-    /**
-     * Creates a new HTTP connection for the given host with the virtual 
-     * alias and port via the given proxy host and port using the given 
-     * protocol.
-     * 
-     * @param proxyHost the host to proxy via
-     * @param proxyPort the port to proxy via
-     * @param host the host to connect to. Parameter value must be non-null.
-     * @param port the port to connect to
-     * @param protocol The protocol to use. Parameter value must be non-null.
-     */
-    public HttpConnection(
-        String proxyHost,
-        int proxyPort,
-        String host,
-        int port,
-        Protocol protocol) {
-
-        if (host == null) {
-            throw new IllegalArgumentException("host parameter is null");
-        }
-        if (protocol == null) {
-            throw new IllegalArgumentException("protocol is null");
-        }
-
-        proxyHostName = proxyHost;
-        proxyPortNumber = proxyPort;
-        hostName = host;
-        portNumber = protocol.resolvePort(port);
-        protocolInUse = protocol;
-    }
-
-    // ------------------------------------------ Attribute Setters and Getters
-
-    /**
-     * Returns the connection socket.
-     *
-     * @return the socket.
-     * 
-     * @since 3.0
-     */
-    protected Socket getSocket() {
-        return this.socket;
-    }
-
-    /**
-     * Returns the host.
-     *
-     * @return the host.
-     */
-    public String getHost() {
-        return hostName;
-    }
-
-    /**
-     * Sets the host to connect to.
-     *
-     * @param host the host to connect to. Parameter value must be non-null.
-     * @throws IllegalStateException if the connection is already open
-     */
-    public void setHost(String host) throws IllegalStateException {
-        if (host == null) {
-            throw new IllegalArgumentException("host parameter is null");
-        }
-        assertNotOpen();
-        hostName = host;
-    }
-
-    /**
-     * Returns the target virtual host.
-     *
-     * @return the virtual host.
-     * 
-     * @deprecated no longer applicable
-     */
-
-    public String getVirtualHost() {
-        return this.hostName;
-    }
-
-    /**
-     * Sets the virtual host to target.
-     *
-     * @param host the virtual host name that should be used instead of 
-     *        physical host name when sending HTTP requests. Virtual host 
-     *        name can be set to <tt> null</tt> if virtual host name is not
-     *        to be used
-     * 
-     * @throws IllegalStateException if the connection is already open
-     * 
-     * @deprecated no longer applicable
-     */
-
-    public void setVirtualHost(String host) throws IllegalStateException {
-        assertNotOpen();
-    }
-
-    /**
-     * Returns the port of the host.
-     *
-     * If the port is -1 (or less than 0) the default port for
-     * the current protocol is returned.
-     *
-     * @return the port.
-     */
-    public int getPort() {
-        if (portNumber < 0) {
-            return isSecure() ? 443 : 80;
-        } else {
-            return portNumber;
-        }
-    }
-
-    /**
-     * Sets the port to connect to.
-     *
-     * @param port the port to connect to
-     * 
-     * @throws IllegalStateException if the connection is already open
-     */
-    public void setPort(int port) throws IllegalStateException {
-        assertNotOpen();
-        portNumber = port;
-    }
-
-    /**
-     * Returns the proxy host.
-     *
-     * @return the proxy host.
-     */
-    public String getProxyHost() {
-        return proxyHostName;
-    }
-
-    /**
-     * Sets the host to proxy through.
-     *
-     * @param host the host to proxy through.
-     * 
-     * @throws IllegalStateException if the connection is already open
-     */
-    public void setProxyHost(String host) throws IllegalStateException {
-        assertNotOpen();
-        proxyHostName = host;
-    }
-
-    /**
-     * Returns the port of the proxy host.
-     *
-     * @return the proxy port.
-     */
-    public int getProxyPort() {
-        return proxyPortNumber;
-    }
-
-    /**
-     * Sets the port of the host to proxy through.
-     *
-     * @param port the port of the host to proxy through.
-     * 
-     * @throws IllegalStateException if the connection is already open
-     */
-    public void setProxyPort(int port) throws IllegalStateException {
-        assertNotOpen();
-        proxyPortNumber = port;
-    }
-
-    /**
-     * Returns <tt>true</tt> if the connection is established over 
-     * a secure protocol.
-     *
-     * @return <tt>true</tt> if connected over a secure protocol.
-     */
-    public boolean isSecure() {
-        return protocolInUse.isSecure();
-    }
-
-    /**
-     * Returns the protocol used to establish the connection.
-     * @return The protocol
-     */
-    public Protocol getProtocol() {
-        return protocolInUse;
-    }
-
-    /**
-     * Sets the protocol used to establish the connection
-     * 
-     * @param protocol The protocol to use.
-     * 
-     * @throws IllegalStateException if the connection is already open
-     */
-    public void setProtocol(Protocol protocol) {
-        assertNotOpen();
-
-        if (protocol == null) {
-            throw new IllegalArgumentException("protocol is null");
-        }
-
-        protocolInUse = protocol;
-
-    }
+    private static final Log LOG = LogFactory.getLog(DefaultHttpHostConnection.class);
+    
+    /** the connection manager that created this connection or null */
+    private final HttpConnectionManager manager;
+    
+    private HostConfiguration hostconf;
 
-    /**
-     * Return the local address used when creating the connection.
-     * If <tt>null</tt>, the default address is used.
-     * 
-     * @return InetAddress the local address to be used when creating Sockets
-     */
-    public InetAddress getLocalAddress() {
-        return this.localAddress;
+    /** flag to indicate if this connection can be released, if locked the connection cannot be 
+     * released */
+    private boolean locked = false;
+    
+    /** Whether the connection is open via a secure tunnel or not */
+    private boolean tunnelEstablished = false;
+    
+    private HttpResponse lastResponse;
+    
+    public DefaultHttpHostConnection(final HttpConnectionManager manager) {
+        super();
+        this.manager = manager;
     }
     
-    /**
-     * Set the local address used when creating the connection.
-     * If unset or <tt>null</tt>, the default address is used.
-     * 
-     * @param localAddress the local address to use
-     */
-    public void setLocalAddress(InetAddress localAddress) {
+    public void setHostConfiguration(final HostConfiguration hostconf) {
         assertNotOpen();
-        this.localAddress = localAddress;
-    }
-
-    /**
-     * Tests if the connection is open. 
-     *
-     * @return <code>true</code> if the connection is open
-     */
-    public boolean isOpen() {
-        return isOpen;
-    }
-
-    /**
-     * Closes the connection if stale.
-     * 
-     * @return <code>true</code> if the connection was stale and therefore closed, 
-     * <code>false</code> otherwise.
-     * 
-     * @see #isStale()
-     * 
-     * @since 3.0
-     */
-    public boolean closeIfStale() throws IOException {
-        if (isOpen && isStale()) {
-            LOG.debug("Connection is stale, closing...");
-            close();
-            return true;
-        }
-        return false;
+        this.hostconf = hostconf;
     }
     
-    /**
-     * Tests if stale checking is enabled.
-     * 
-     * @return <code>true</code> if enabled
-     * 
-     * @see #isStale()
-     * 
-     * @deprecated Use {@link HttpConnectionParams#isStaleCheckingEnabled()},
-     * {@link HttpConnection#getParams()}.
-     */
-    public boolean isStaleCheckingEnabled() {
-        return this.params.isStaleCheckingEnabled();
-    }
-
-    /**
-     * Sets whether or not isStale() will be called when testing if this connection is open.
-     * 
-     * <p>Setting this flag to <code>false</code> will increase performance when reusing
-     * connections, but it will also make them less reliable.  Stale checking ensures that
-     * connections are viable before they are used.  When set to <code>false</code> some
-     * method executions will result in IOExceptions and they will have to be retried.</p>
-     * 
-     * @param staleCheckEnabled <code>true</code> to enable isStale()
-     * 
-     * @see #isStale()
-     * @see #isOpen()
-     * 
-     * @deprecated Use {@link HttpConnectionParams#setStaleCheckingEnabled(boolean)},
-     * {@link HttpConnection#getParams()}.
-     */
-    public void setStaleCheckingEnabled(boolean staleCheckEnabled) {
-        this.params.setStaleCheckingEnabled(staleCheckEnabled);
-    }
-
-    /**
-     * Determines whether this connection is "stale", which is to say that either
-     * it is no longer open, or an attempt to read the connection would fail.
-     *
-     * <p>Unfortunately, due to the limitations of the JREs prior to 1.4, it is
-     * not possible to test a connection to see if both the read and write channels
-     * are open - except by reading and writing.  This leads to a difficulty when
-     * some connections leave the "write" channel open, but close the read channel
-     * and ignore the request.  This function attempts to ameliorate that
-     * problem by doing a test read, assuming that the caller will be doing a
-     * write followed by a read, rather than the other way around.
-     * </p>
-     *
-     * <p>To avoid side-effects, the underlying connection is wrapped by a
-     * {@link BufferedInputStream}, so although data might be read, what is visible
-     * to clients of the connection will not change with this call.</p.
-     *
-     * @throws IOException if the stale connection test is interrupted.
-     * 
-     * @return <tt>true</tt> if the connection is already closed, or a read would
-     * fail.
-     */
-    protected boolean isStale() throws IOException {
-        boolean isStale = true;
-        if (isOpen) {
-            // the connection is open, but now we have to see if we can read it
-            // assume the connection is not stale.
-            isStale = false;
-            try {
-                if (inputStream.available() <= 0) {
-                    try {
-                        socket.setSoTimeout(1);
-                        inputStream.mark(1);
-                        int byteRead = inputStream.read();
-                        if (byteRead == -1) {
-                            // again - if the socket is reporting all data read,
-                            // probably stale
-                            isStale = true;
-                        } else {
-                            inputStream.reset();
-                        }
-                    } finally {
-                        socket.setSoTimeout(this.params.getSoTimeout());
-                    }
-                }
-            } catch (InterruptedIOException e) {
-                if (!ExceptionUtil.isSocketTimeoutException(e)) {
-                    throw e;
-                }
-                // aha - the connection is NOT stale - continue on!
-            } catch (IOException e) {
-                // oops - the connection is stale, the read or soTimeout failed.
-                LOG.debug(
-                    "An error occurred while reading from the socket, is appears to be stale",
-                    e
-                );
-                isStale = true;
-            }
-        }
-
-        return isStale;
-    }
-
-    /**
-     * Returns <tt>true</tt> if the connection is established via a proxy,
-     * <tt>false</tt> otherwise.
-     *
-     * @return <tt>true</tt> if a proxy is used to establish the connection, 
-     * <tt>false</tt> otherwise.
-     */
-    public boolean isProxied() {
-        return (!(null == proxyHostName || 0 >= proxyPortNumber));
-    }
-
-    /**
-     * Set the state to keep track of the last response for the last request.
-     *
-     * <p>The connection managers use this to ensure that previous requests are
-     * properly closed before a new request is attempted.  That way, a GET
-     * request need not be read in its entirety before a new request is issued.
-     * Instead, this stream can be closed as appropriate.</p>
-     *
-     * @param inStream  The stream associated with an HttpMethod.
-     */
-    public void setLastResponseInputStream(InputStream inStream) {
-        lastResponseInputStream = inStream;
-    }
-
-    /**
-     * Returns the stream used to read the last response's body.
-     *
-     * <p>Clients will generally not need to call this function unless
-     * using HttpConnection directly, instead of calling {@link HttpClient#executeMethod}.
-     * For those clients, call this function, and if it returns a non-null stream,
-     * close the stream before attempting to execute a method.  Note that
-     * calling "close" on the stream returned by this function <i>may</i> close
-     * the connection if the previous response contained a "Connection: close" header. </p>
-     *
-     * @return An {@link InputStream} corresponding to the body of the last
-     *  response.
-     */
-    public InputStream getLastResponseInputStream() {
-        return lastResponseInputStream;
-    }
-
-    // --------------------------------------------------- Other Public Methods
-
-    /**
-     * Returns {@link HttpConnectionParams HTTP protocol parameters} associated with this method.
-     *
-     * @return HTTP parameters.
-     *
-     * @since 3.0
-     */
-    public HttpConnectionParams getParams() {
-        return this.params;
-    }
-
-    /**
-     * Assigns {@link HttpConnectionParams HTTP protocol parameters} for this method.
-     * 
-     * @since 3.0
-     * 
-     * @see HttpConnectionParams
-     */
-    public void setParams(final HttpConnectionParams params) {
-        if (params == null) {
-            throw new IllegalArgumentException("Parameters may not be null");
-        }
-        this.params = params;
-    }
-
-    /**
-     * Set the {@link Socket}'s timeout, via {@link Socket#setSoTimeout}.  If the
-     * connection is already open, the SO_TIMEOUT is changed.  If no connection
-     * is open, then subsequent connections will use the timeout value.
-     * <p>
-     * Note: This is not a connection timeout but a timeout on network traffic!
-     *
-     * @param timeout the timeout value
-     * @throws SocketException - if there is an error in the underlying
-     * protocol, such as a TCP error.
-     * 
-     * @deprecated Use {@link HttpConnectionParams#setSoTimeout(int)},
-     * {@link HttpConnection#getParams()}.
-     */
-    public void setSoTimeout(int timeout)
-        throws SocketException, IllegalStateException {
-        this.params.setSoTimeout(timeout);
-        if (this.socket != null) {
-            this.socket.setSoTimeout(timeout);
-        }
-    }
-
-    /**
-     * Sets <code>SO_TIMEOUT</code> value directly on the underlying {@link Socket socket}. 
-     * This method does not change the default read timeout value set via 
-     * {@link HttpConnectionParams}.
-     *
-     * @param timeout the timeout value
-     * @throws SocketException - if there is an error in the underlying
-     * protocol, such as a TCP error.
-     * @throws IllegalStateException if not connected
-     * 
-     * @since 3.0
-     */
-    public void setSocketTimeout(int timeout)
-        throws SocketException, IllegalStateException {
-        assertOpen();
-        if (this.socket != null) {
-            this.socket.setSoTimeout(timeout);
-        }
-    }
-
-    /**
-     * Returns the {@link Socket}'s timeout, via {@link Socket#getSoTimeout}, if the
-     * connection is already open. If no connection is open, return the value subsequent 
-     * connection will use.
-     * <p>
-     * Note: This is not a connection timeout but a timeout on network traffic!
-     *
-     * @return the timeout value
-     * 
-     * @deprecated Use {@link HttpConnectionParams#getSoTimeout()},
-     * {@link HttpConnection#getParams()}.
-     */
-    public int getSoTimeout() throws SocketException {
-        return this.params.getSoTimeout();
-    }
-
-    /**
-     * Sets the connection timeout. This is the maximum time that may be spent
-     * until a connection is established. The connection will fail after this
-     * amount of time.
-     * @param timeout The timeout in milliseconds. 0 means timeout is not used.
-     * 
-     * @deprecated Use {@link HttpConnectionParams#setConnectionTimeout(int)},
-     * {@link HttpConnection#getParams()}.
-     */
-    public void setConnectionTimeout(int timeout) {
-        this.params.setConnectionTimeout(timeout);
+    public HostConfiguration getHostConfiguration() {
+        return this.hostconf;
     }
 
     /**
      * Establishes a connection to the specified host and port
      * (via a proxy if specified).
-     * The underlying socket is created from the {@link ProtocolSocketFactory}.
+     * The underlying socket is created from the {@link SocketFactory}.
      *
      * @throws IOException if an attempt to establish the connection results in an
      *   I/O error.
      */
-    public void open() throws IOException {
-        LOG.trace("enter HttpConnection.open()");
-
-        final String host = (proxyHostName == null) ? hostName : proxyHostName;
-        final int port = (proxyHostName == null) ? portNumber : proxyPortNumber;
+    public void open(final HttpParams params) 
+            throws IllegalStateException, IOException {
+        if (params == null) {
+            throw new IllegalArgumentException("HTTP parameters may not be null");
+        }
         assertNotOpen();
+        if (this.hostconf == null) {
+            throw new IllegalArgumentException("Host configuration is null");
+        }
+
+        HttpHost host = this.hostconf.getHost();
+        if (host == null) {
+            throw new IllegalStateException("Target http host is null");
+        }
+        HttpHost proxyHost = this.hostconf.getProxyHost();
         
         if (LOG.isDebugEnabled()) {
-            LOG.debug("Open connection to " + host + ":" + port);
+            if (proxyHost == null) {
+                LOG.debug("Open connection to " + host);
+            } else {
+                LOG.debug("Open connection to " + host + " via proxy " + proxyHost);
+            }
         }
+
+        // Determine the type of the connection
+        Scheme scheme = Scheme.getScheme(host.getSchemeName());
+        SocketFactory socketFactory = scheme.getSocketFactory();
+        boolean secure = (socketFactory instanceof SecureSocketFactory);
+        boolean proxied = (proxyHost != null);
         
-        try {
-            if (this.socket == null) {
-                usingSecureSocket = isSecure() && !isProxied();
-                // use the protocol's socket factory unless this is a secure
-                // proxied connection
-                ProtocolSocketFactory socketFactory = null;
-                if (isSecure() && isProxied()) {
-                    Protocol defaultprotocol = Protocol.getProtocol("http");
-                    socketFactory = defaultprotocol.getSocketFactory();
-                } else {
-                    socketFactory = this.protocolInUse.getSocketFactory();
-                }
-                this.socket = socketFactory.createSocket(
-                            host, port, 
-                            localAddress, 0,
-                            this.params);
-            }
+        // Determine the target host
+        HttpHost target = null;
+        if (proxyHost != null) {
+            target = proxyHost; 
+        } else {
+            target = host; 
+        }
 
-            /*
-            "Nagling has been broadly implemented across networks, 
-            including the Internet, and is generally performed by default 
-            - although it is sometimes considered to be undesirable in 
-            highly interactive environments, such as some client/server 
-            situations. In such cases, nagling may be turned off through 
-            use of the TCP_NODELAY sockets option." */
-
-            socket.setTcpNoDelay(this.params.getTcpNoDelay());
-            socket.setSoTimeout(this.params.getSoTimeout());
-            
-            int linger = this.params.getLinger();
-            if (linger >= 0) {
-                socket.setSoLinger(linger > 0, linger);
-            }
-            
-            int sndBufSize = this.params.getSendBufferSize();
-            if (sndBufSize >= 0) {
-                socket.setSendBufferSize(sndBufSize);
-            }        
-            int rcvBufSize = this.params.getReceiveBufferSize();
-            if (rcvBufSize >= 0) {
-                socket.setReceiveBufferSize(rcvBufSize);
-            }        
-            int outbuffersize = socket.getSendBufferSize();
-            if ((outbuffersize > 2048) || (outbuffersize <= 0)) {
-                outbuffersize = 2048;
-            }
-            int inbuffersize = socket.getReceiveBufferSize();
-            if ((inbuffersize > 2048) || (inbuffersize <= 0)) {
-                inbuffersize = 2048;
-            }
-            inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize);
-            outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize);
-            isOpen = true;
-        } catch (IOException e) {
-            // Connection wasn't opened properly
-            // so close everything out
-            closeSocketAndStreams();
-            throw e;
+        // Create the socket
+        String hostname = target.getHostName();
+        int port = target.getPort();
+        if (port < 0) {
+           port = scheme.getDefaultPort(); 
+        }
+        if (secure && proxied) {
+            scheme = Scheme.getScheme("http");
+            socketFactory = scheme.getSocketFactory();
+        } else {
+            scheme = Scheme.getScheme(target.getSchemeName());
         }
+        socketFactory = scheme.getSocketFactory();
+        Socket socket = socketFactory.createSocket(
+                hostname, port, 
+                this.hostconf.getLocalAddress(), 0, params);
+
+        // Bind connection to the socket
+        bind(socket, params);
     }
 
     /**
@@ -763,390 +171,47 @@
      * @throws IOException if an attempt to establish the secure tunnel results in an
      *   I/O error.
      */
-    public void tunnelCreated() throws IllegalStateException, IOException {
-        LOG.trace("enter HttpConnection.tunnelCreated()");
-
-        if (!isSecure() || !isProxied()) {
-            throw new IllegalStateException(
-                "Connection must be secure "
-                    + "and proxied to use this feature");
-        }
-
-        if (usingSecureSocket) {
-            throw new IllegalStateException("Already using a secure socket");
-        }
-        
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Secure tunnel to " + this.hostName + ":" + this.portNumber);
-        }
-
-        SecureProtocolSocketFactory socketFactory =
-            (SecureProtocolSocketFactory) protocolInUse.getSocketFactory();
-
-        socket = socketFactory.createSocket(socket, hostName, portNumber, true);
-        int sndBufSize = this.params.getSendBufferSize();
-        if (sndBufSize >= 0) {
-            socket.setSendBufferSize(sndBufSize);
-        }        
-        int rcvBufSize = this.params.getReceiveBufferSize();
-        if (rcvBufSize >= 0) {
-            socket.setReceiveBufferSize(rcvBufSize);
-        }        
-        int outbuffersize = socket.getSendBufferSize();
-        if (outbuffersize > 2048) {
-            outbuffersize = 2048;
-        }
-        int inbuffersize = socket.getReceiveBufferSize();
-        if (inbuffersize > 2048) {
-            inbuffersize = 2048;
-        }
-        inputStream = new BufferedInputStream(socket.getInputStream(), inbuffersize);
-        outputStream = new BufferedOutputStream(socket.getOutputStream(), outbuffersize);
-        usingSecureSocket = true;
-        tunnelEstablished = true;
-    }
-
-    /**
-     * Indicates if the connection is completely transparent from end to end.
-     *
-     * @return true if conncetion is not proxied or tunneled through a transparent
-     * proxy; false otherwise.
-     */
-    public boolean isTransparent() {
-        return !isProxied() || tunnelEstablished;
-    }
-
-    /**
-     * Flushes the output request stream.  This method should be called to 
-     * ensure that data written to the request OutputStream is sent to the server.
-     * 
-     * @throws IOException if an I/O problem occurs
-     */
-    public void flushRequestOutputStream() throws IOException {
-        LOG.trace("enter HttpConnection.flushRequestOutputStream()");
-        assertOpen();
-        outputStream.flush();
-    }
-
-    /**
-     * Returns an {@link OutputStream} suitable for writing the request.
-     *
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     * @return a stream to write the request to
-     */
-    public OutputStream getRequestOutputStream()
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.getRequestOutputStream()");
-        assertOpen();
-        OutputStream out = this.outputStream;
-        if (Wire.CONTENT_WIRE.enabled()) {
-            out = new WireLogOutputStream(out, Wire.CONTENT_WIRE);
-        }
-        return out;
-    }
-
-    /**
-     * Return a {@link InputStream} suitable for reading the response.
-     * @return InputStream The response input stream.
-     * @throws IOException If an IO problem occurs
-     * @throws IllegalStateException If the connection isn't open.
-     */
-    public InputStream getResponseInputStream()
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.getResponseInputStream()");
-        assertOpen();
-        return inputStream;
-    }
-
-    /**
-     * Tests if input data avaialble. This method returns immediately
-     * and does not perform any read operations on the input socket
-     * 
-     * @return boolean <tt>true</tt> if input data is available, 
-     *                 <tt>false</tt> otherwise.
-     * 
-     * @throws IOException If an IO problem occurs
-     * @throws IllegalStateException If the connection isn't open.
-     */
-    public boolean isResponseAvailable() 
-        throws IOException {
-        LOG.trace("enter HttpConnection.isResponseAvailable()");
-        if (this.isOpen) {
-            return this.inputStream.available() > 0;
-        } else {
-            return false;
+    public void tunnelCreated(final HttpParams params) 
+            throws IllegalStateException, IOException {
+        if (params == null) {
+            throw new IllegalArgumentException("HTTP parameters may not be null");
         }
-    }
-
-    /**
-     * Tests if input data becomes available within the given period time in milliseconds.
-     * 
-     * @param timeout The number milliseconds to wait for input data to become available 
-     * @return boolean <tt>true</tt> if input data is availble, 
-     *                 <tt>false</tt> otherwise.
-     * 
-     * @throws IOException If an IO problem occurs
-     * @throws IllegalStateException If the connection isn't open.
-     */
-    public boolean isResponseAvailable(int timeout) 
-        throws IOException {
-        LOG.trace("enter HttpConnection.isResponseAvailable(int)");
         assertOpen();
-        boolean result = false;
-        if (this.inputStream.available() > 0) {
-            result = true;
-        } else {
-            try {
-                this.socket.setSoTimeout(timeout);
-                inputStream.mark(1);
-                int byteRead = inputStream.read();
-                if (byteRead != -1) {
-                    inputStream.reset();
-                    LOG.debug("Input data available");
-                    result = true;
-                } else {
-                    LOG.debug("Input data not available");
-                }
-            } catch (InterruptedIOException e) {
-                if (!ExceptionUtil.isSocketTimeoutException(e)) {
-                    throw e;
-                }
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Input data not available after " + timeout + " ms");
-                }
-            } finally {
-                try {
-                    socket.setSoTimeout(this.params.getSoTimeout());
-                } catch (IOException ioe) {
-                    LOG.debug("An error ocurred while resetting soTimeout, we will assume that"
-                        + " no response is available.",
-                        ioe);
-                    result = false;
-                }
-            }
-        }
-        return result;
-    }
-
-    /**
-     * Writes the specified bytes to the output stream.
-     *
-     * @param data the data to be written
-     * @throws IllegalStateException if not connected
-     * @throws IOException if an I/O problem occurs
-     * @see #write(byte[],int,int)
-     */
-    public void write(byte[] data)
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.write(byte[])");
-        this.write(data, 0, data.length);
-    }
-
-    /**
-     * Writes <i>length</i> bytes in <i>data</i> starting at
-     * <i>offset</i> to the output stream.
-     *
-     * The general contract for
-     * write(b, off, len) is that some of the bytes in the array b are written
-     * to the output stream in order; element b[off] is the first byte written
-     * and b[off+len-1] is the last byte written by this operation.
-     *
-     * @param data array containing the data to be written.
-     * @param offset the start offset in the data.
-     * @param length the number of bytes to write.
-     * @throws IllegalStateException if not connected
-     * @throws IOException if an I/O problem occurs
-     */
-    public void write(byte[] data, int offset, int length)
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.write(byte[], int, int)");
-
-        if (offset < 0) {
-            throw new IllegalArgumentException("Array offset may not be negative");
+        if (this.tunnelEstablished) {
+            throw new IllegalStateException("Tunnel already established");
         }
-        if (length < 0) {
-            throw new IllegalArgumentException("Array length may not be negative");
+        HttpHost host = this.hostconf.getHost();
+        if (host == null) {
+            throw new IllegalStateException("Target http host is null");
         }
-        if (offset + length > data.length) {
-            throw new IllegalArgumentException("Given offset and length exceed the array length");
+        HttpHost proxyHost = this.hostconf.getProxyHost();
+        
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Secure tunnel to " + host);
         }
-        assertOpen();
-        this.outputStream.write(data, offset, length);
-    }
-
-    /**
-     * Writes the specified bytes, followed by <tt>"\r\n".getBytes()</tt> to the
-     * output stream.
-     *
-     * @param data the bytes to be written
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     */
-    public void writeLine(byte[] data)
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.writeLine(byte[])");
-        write(data);
-        writeLine();
-    }
-
-    /**
-     * Writes <tt>"\r\n".getBytes()</tt> to the output stream.
-     *
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     */
-    public void writeLine()
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.writeLine()");
-        write(CRLF);
-    }
-
-    /**
-     * @deprecated Use {@link #print(String, String)}
-     * 
-     * Writes the specified String (as bytes) to the output stream.
-     *
-     * @param data the string to be written
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     */
-    public void print(String data)
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.print(String)");
-        write(EncodingUtil.getBytes(data, "ISO-8859-1"));
-    }
-
-    /**
-     * Writes the specified String (as bytes) to the output stream.
-     *
-     * @param data the string to be written
-     * @param charset the charset to use for writing the data
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     * 
-     * @since 3.0
-     */
-    public void print(String data, String charset)
-    	throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.print(String)");
-        write(EncodingUtil.getBytes(data, charset));
-    }
-    
-    /**
-     * @deprecated Use {@link #printLine(String, String)}
-     * 
-     * Writes the specified String (as bytes), followed by
-     * <tt>"\r\n".getBytes()</tt> to the output stream.
-     *
-     * @param data the data to be written
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     */
-    public void printLine(String data)
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.printLine(String)");
-        writeLine(EncodingUtil.getBytes(data, "ISO-8859-1"));
-    }
-
-    /**
-     * Writes the specified String (as bytes), followed by
-     * <tt>"\r\n".getBytes()</tt> to the output stream.
-     *
-     * @param data the data to be written
-     * @param charset the charset to use for writing the data
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     * 
-     * @since 3.0
-     */
-    public void printLine(String data, String charset)
-    	throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.printLine(String)");
-        writeLine(EncodingUtil.getBytes(data, charset));
-    }    
-    
-    /**
-     * Writes <tt>"\r\n".getBytes()</tt> to the output stream.
-     *
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     */
-    public void printLine()
-        throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.printLine()");
-        writeLine();
-    }
-
-    /**
-     * Reads up to <tt>"\n"</tt> from the (unchunked) input stream.
-     * If the stream ends before the line terminator is found,
-     * the last part of the string will still be returned.
-     *
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     * @return a line from the response
-     * 
-     * @deprecated use #readLine(String)
-     */
-    public String readLine() throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.readLine()");
-
-        assertOpen();
-        return HttpParser.readLine(inputStream);
-    }
-
-    /**
-     * Reads up to <tt>"\n"</tt> from the (unchunked) input stream.
-     * If the stream ends before the line terminator is found,
-     * the last part of the string will still be returned.
-     * 
-     * @param charset the charset to use for reading the data
-     *
-     * @throws IllegalStateException if the connection is not open
-     * @throws IOException if an I/O problem occurs
-     * @return a line from the response
-     * 
-     * @since 3.0
-     */
-    public String readLine(final String charset) throws IOException, IllegalStateException {
-        LOG.trace("enter HttpConnection.readLine()");
-
-        assertOpen();
-        return HttpParser.readLine(inputStream, charset);
-    }
-
-    /**
-     * Attempts to shutdown the {@link Socket}'s output, via Socket.shutdownOutput()
-     * when running on JVM 1.3 or higher.
-     * 
-     * @deprecated unused
-     */
-    public void shutdownOutput() {
-        LOG.trace("enter HttpConnection.shutdownOutput()");
+        
+        Scheme scheme = Scheme.getScheme(host.getSchemeName());
+        SocketFactory socketFactory = scheme.getSocketFactory();
+        boolean secure = (socketFactory instanceof SecureSocketFactory);
+        boolean proxied = (proxyHost != null);
 
-        try {
-            // Socket.shutdownOutput is a JDK 1.3
-            // method. We'll use reflection in case
-            // we're running in an older VM
-            Class[] paramsClasses = new Class[0];
-            Method shutdownOutput =
-                socket.getClass().getMethod("shutdownOutput", paramsClasses);
-            Object[] params = new Object[0];
-            shutdownOutput.invoke(socket, params);
-        } catch (Exception ex) {
-            LOG.debug("Unexpected Exception caught", ex);
-            // Ignore, and hope everything goes right
+        if (!secure || !proxied) {
+            throw new IllegalStateException(
+                "Connection must be secure "
+                    + "and proxied to use this feature");
         }
-        // close output stream?
-    }
 
-    /**
-     * Closes the socket and streams.
-     */
-    public void close() {
-        LOG.trace("enter HttpConnection.close()");
-        closeSocketAndStreams();
+        String hostname = host.getHostName();
+        int port = host.getPort();
+        if (port < 0) {
+           port = scheme.getDefaultPort(); 
+        }
+        SecureSocketFactory securesocketFactory = (SecureSocketFactory) socketFactory;
+
+        Socket tunnel = securesocketFactory.createSocket(
+                this.socket, hostname, port, true);
+        bind(tunnel, params);
+        this.tunnelEstablished = true;
     }
 
     /**
@@ -1154,15 +219,7 @@
      * @return HttpConnectionManager
      */
     public HttpConnectionManager getHttpConnectionManager() {
-        return httpConnectionManager;
-    }
-
-    /**
-     * Sets the httpConnectionManager.
-     * @param httpConnectionManager The httpConnectionManager to set
-     */
-    public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) {
-        this.httpConnectionManager = httpConnectionManager;
+        return this.manager;
     }
 
     /**
@@ -1171,12 +228,11 @@
      * to call this method multiple times.
      */
     public void releaseConnection() {
-        LOG.trace("enter HttpConnection.releaseConnection()");
         if (locked) {
             LOG.debug("Connection is locked.  Call to releaseConnection() ignored.");
-        } else if (httpConnectionManager != null) {
+        } else if (this.manager != null) {
             LOG.debug("Releasing connection back to connection manager.");
-            httpConnectionManager.releaseConnection(this);
+            this.manager.releaseConnection(this);
         } else {
             LOG.warn("HttpConnectionManager is null.  Connection cannot be released.");
         }
@@ -1206,168 +262,13 @@
     protected void setLocked(boolean locked) {
         this.locked = locked;
     }
-    // ------------------------------------------------------ Protected Methods
-
-    /**
-     * Closes everything out.
-     */
-    protected void closeSocketAndStreams() {
-        LOG.trace("enter HttpConnection.closeSockedAndStreams()");
-
-        isOpen = false;
-        
-        // no longer care about previous responses...
-        lastResponseInputStream = null;
-
-        if (null != outputStream) {
-            OutputStream temp = outputStream;
-            outputStream = null;
-            try {
-                temp.close();
-            } catch (Exception ex) {
-                LOG.debug("Exception caught when closing output", ex);
-                // ignored
-            }
-        }
-
-        if (null != inputStream) {
-            InputStream temp = inputStream;
-            inputStream = null;
-            try {
-                temp.close();
-            } catch (Exception ex) {
-                LOG.debug("Exception caught when closing input", ex);
-                // ignored
-            }
-        }
-
-        if (null != socket) {
-            Socket temp = socket;
-            socket = null;
-            try {
-                temp.close();
-            } catch (Exception ex) {
-                LOG.debug("Exception caught when closing socket", ex);
-                // ignored
-            }
-        }
-        
-        tunnelEstablished = false;
-        usingSecureSocket = false;
-    }
-
-    /**
-     * Throws an {@link IllegalStateException} if the connection is already open.
-     *
-     * @throws IllegalStateException if connected
-     */
-    protected void assertNotOpen() throws IllegalStateException {
-        if (isOpen) {
-            throw new IllegalStateException("Connection is open");
-        }
-    }
 
-    /**
-     * Throws an {@link IllegalStateException} if the connection is not open.
-     *
-     * @throws IllegalStateException if not connected
-     */
-    protected void assertOpen() throws IllegalStateException {
-        if (!isOpen) {
-            throw new IllegalStateException("Connection is not open");
-        }
+    public HttpResponse getLastResponse() {
+        return this.lastResponse;
     }
 
-    /**
-     * Gets the socket's sendBufferSize.
-     * 
-     * @return the size of the buffer for the socket OutputStream, -1 if the value
-     * has not been set and the socket has not been opened
-     * 
-     * @throws SocketException if an error occurs while getting the socket value
-     * 
-     * @see Socket#getSendBufferSize()
-     */
-    public int getSendBufferSize() throws SocketException {
-        if (socket == null) {
-            return -1;
-        } else {
-            return socket.getSendBufferSize();
-        }
-    }
-
-    /**
-     * Sets the socket's sendBufferSize.
-     * 
-     * @param sendBufferSize the size to set for the socket OutputStream
-     * 
-     * @throws SocketException if an error occurs while setting the socket value
-     * 
-     * @see Socket#setSendBufferSize(int)
-     * 
-     * @deprecated Use {@link HttpConnectionParams#setSendBufferSize(int)},
-     * {@link HttpConnection#getParams()}.
-     */
-    public void setSendBufferSize(int sendBufferSize) throws SocketException {
-        this.params.setSendBufferSize(sendBufferSize);
+    public void setLastResponse(final HttpResponse lastResponse) {
+        this.lastResponse = lastResponse;
     }
 
-    // ------------------------------------------------------- Static Variable
-
-    /** <tt>"\r\n"</tt>, as bytes. */
-    private static final byte[] CRLF = new byte[] {(byte) 13, (byte) 10};
-
-    /** Log object for this class. */
-    private static final Log LOG = LogFactory.getLog(HttpConnection.class);
-    
-    // ----------------------------------------------------- Instance Variables
-    
-    /** My host. */
-    private String hostName = null;
-    
-    /** My port. */
-    private int portNumber = -1;
-    
-    /** My proxy host. */
-    private String proxyHostName = null;
-    
-    /** My proxy port. */
-    private int proxyPortNumber = -1;
-    
-    /** My client Socket. */
-    private Socket socket = null;
-    
-    /** My InputStream. */
-    private InputStream inputStream = null;
-
-    /** My OutputStream. */
-    private OutputStream outputStream = null;
-    
-    /** An {@link InputStream} for the response to an individual request. */
-    private InputStream lastResponseInputStream = null;
-    
-    /** Whether or not the connection is connected. */
-    protected boolean isOpen = false;
-    
-    /** the protocol being used */
-    private Protocol protocolInUse;
-    
-    /** Collection of HTTP parameters associated with this HTTP connection*/
-    private HttpConnectionParams params = new HttpConnectionParams();
-    
-    /** flag to indicate if this connection can be released, if locked the connection cannot be 
-     * released */
-    private boolean locked = false;
-    
-    /** Whether or not the socket is a secure one. */
-    private boolean usingSecureSocket = false;
-    
-    /** Whether the connection is open via a secure tunnel or not */
-    private boolean tunnelEstablished = false;
-    
-    /** the connection manager that created this connection or null */
-    private HttpConnectionManager httpConnectionManager;
-    
-    /** The local interface on which the connection is created, or null for the default */
-    private InetAddress localAddress;
 }

Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/SimpleHttpConnectionManager.java (from r418211, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/SimpleHttpConnectionManager.java?view=diff&rev=484777&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java&r1=418211&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/SimpleHttpConnectionManager.java&r2=484777
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/conn/impl/SimpleHttpConnectionManager.java Fri Dec  8 12:59:42 2006
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v 1.23 2004/10/16 22:40:08 mbecke Exp $
+ * $HeadURL$
  * $Revision$
  * $Date$
  *
@@ -27,14 +27,19 @@
  *
  */
 
-package org.apache.commons.httpclient;
+package org.apache.http.conn.impl;
 
 import java.io.IOException;
-import java.io.InputStream;
 
-import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.conn.HostConfiguration;
+import org.apache.http.conn.HttpConnectionManager;
+import org.apache.http.conn.HttpHostConnection;
+import org.apache.http.impl.DefaultHttpParams;
+import org.apache.http.params.HttpParams;
 
 /**
  * A connection manager that provides access to a single HttpConnection.  This
@@ -44,7 +49,7 @@
  * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
  * @author Eric Johnson
  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
  * @author Laura Werner
  * 
  * @since 2.0
@@ -64,25 +69,28 @@
      * consume it now.
      * @param conn The connection
      */
-    static void finishLastResponse(HttpConnection conn) {
-        InputStream lastResponse = conn.getLastResponseInputStream();
+    private void finishLastResponse(DefaultHttpHostConnection conn) {
+        HttpResponse lastResponse = conn.getLastResponse();
         if (lastResponse != null) {
-            conn.setLastResponseInputStream(null);
-            try {
-                lastResponse.close();
-            } catch (IOException ioe) {
-                conn.close();
+            conn.setLastResponse(null);
+            HttpEntity entity = lastResponse.getEntity();
+            if (entity != null) {
+                try {
+                    entity.consumeContent();
+                } catch (IOException ex) {
+                    LOG.debug("I/O error consuming response content", ex);
+                }
             }
         }
     }
     
     /** The http connection */
-    protected HttpConnection httpConnection;
+    protected DefaultHttpHostConnection httpConnection;
 
     /**
      * Collection of parameters associated with this connection manager.
      */
-    private HttpConnectionManagerParams params = new HttpConnectionManagerParams(); 
+    private HttpParams params = new DefaultHttpParams(); 
 
     /**
      * The time the connection was made idle.
@@ -124,37 +132,16 @@
     /**
      * @see HttpConnectionManager#getConnection(HostConfiguration)
      */
-    public HttpConnection getConnection(HostConfiguration hostConfiguration) {
+    public HttpHostConnection getConnection(HostConfiguration hostConfiguration) {
         return getConnection(hostConfiguration, 0);
     }
 
-    /**
-     * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
-     * 
-     * @return <code>true</code> if stale checking will be enabled on HttpConections
-     * 
-     * @see HttpConnection#isStaleCheckingEnabled()
-     * 
-     * @deprecated Use {@link HttpConnectionManagerParams#isStaleCheckingEnabled()},
-     * {@link HttpConnectionManager#getParams()}.
-     */
-    public boolean isConnectionStaleCheckingEnabled() {
-        return this.params.isStaleCheckingEnabled();
-    }
-
-    /**
-     * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
-     * 
-     * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled 
-     * on HttpConections
-     * 
-     * @see HttpConnection#setStaleCheckingEnabled(boolean)
-     * 
-     * @deprecated Use {@link HttpConnectionManagerParams#setStaleCheckingEnabled(boolean)},
-     * {@link HttpConnectionManager#getParams()}.
-     */
-    public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) {
-        this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
+    private void closeConnection() {
+        try {
+            this.httpConnection.close();
+        } catch (IOException ex) {
+            LOG.debug("I/O error closing connection", ex);
+        }
     }
     
     /**
@@ -167,31 +154,19 @@
      *        immediately.
      * @since 3.0
      */
-    public HttpConnection getConnectionWithTimeout(
+    public HttpHostConnection getConnection(
         HostConfiguration hostConfiguration, long timeout) {
 
         if (httpConnection == null) {
-            httpConnection = new HttpConnection(hostConfiguration);
-            httpConnection.setHttpConnectionManager(this);
-            httpConnection.getParams().setDefaults(this.params);
+            httpConnection = new DefaultHttpHostConnection(this);
         } else {
-
             // make sure the host and proxy are correct for this connection
             // close it and set the values if they are not
-            if (!hostConfiguration.hostEquals(httpConnection)
-                || !hostConfiguration.proxyEquals(httpConnection)) {
-                    
+            if (!hostConfiguration.equals(httpConnection)) {
                 if (httpConnection.isOpen()) {
-                    httpConnection.close();
+                    closeConnection();
                 }
-
-                httpConnection.setHost(hostConfiguration.getHost());
-                httpConnection.setPort(hostConfiguration.getPort());
-                httpConnection.setProtocol(hostConfiguration.getProtocol());
-                httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
-
-                httpConnection.setProxyHost(hostConfiguration.getProxyHost());
-                httpConnection.setProxyPort(hostConfiguration.getProxyPort());
+                httpConnection.setHostConfiguration(hostConfiguration);
             } else {
                 finishLastResponse(httpConnection);
             }
@@ -206,25 +181,15 @@
         return httpConnection;
     }
 
-	/**
-	 * @see HttpConnectionManager#getConnection(HostConfiguration, long)
-	 * 
-	 * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
-	 */
-	public HttpConnection getConnection(
-		HostConfiguration hostConfiguration, long timeout) {
-        return getConnectionWithTimeout(hostConfiguration, timeout);
-	}
-
     /**
      * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
      */
-    public void releaseConnection(HttpConnection conn) {
+    public void releaseConnection(HttpHostConnection conn) {
         if (conn != httpConnection) {
             throw new IllegalStateException("Unexpected release of an unknown connection.");
         }
         if (this.alwaysClose) {
-            httpConnection.close();
+            closeConnection();
         } else {
             // make sure the connection is reuseable
             finishLastResponse(httpConnection);
@@ -236,27 +201,11 @@
         idleStartTime = System.currentTimeMillis();
     }
 
-    /**
-     * Returns {@link HttpConnectionManagerParams parameters} associated 
-     * with this connection manager.
-     * 
-     * @since 2.1
-     * 
-     * @see HttpConnectionManagerParams
-     */
-    public HttpConnectionManagerParams getParams() {
+    public HttpParams getParams() {
         return this.params;
     }
 
-    /**
-     * Assigns {@link HttpConnectionManagerParams parameters} for this 
-     * connection manager.
-     * 
-     * @since 2.1
-     * 
-     * @see HttpConnectionManagerParams
-     */
-    public void setParams(final HttpConnectionManagerParams params) {
+    public void setParams(final HttpParams params) {
         if (params == null) {
             throw new IllegalArgumentException("Parameters may not be null");
         }
@@ -269,7 +218,7 @@
     public void closeIdleConnections(long idleTimeout) {
         long maxIdleTime = System.currentTimeMillis() - idleTimeout;
         if (idleStartTime <= maxIdleTime) {
-            httpConnection.close();
+            closeConnection();
         }
     }
     
@@ -277,7 +226,7 @@
      * since 3.1
      */
     public void shutdown() {
-        httpConnection.close();
+        closeConnection();
     }
     
-}
+}
\ No newline at end of file



Mime
View raw message