From 2c1d3f797adeb49c9a18bce0d10bb9a406e4d7fc Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 28 Nov 2012 13:36:41 +0000 Subject: [PATCH] Updated new connection managers to use SocketClientConnectionImpl instead of deprecated DefaultClientConnection git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1414675 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/client/utils/URLEncodedUtils.java | 2 +- .../http/conn/ManagedClientConnection.java | 2 +- ...ction.java => SocketClientConnection.java} | 12 +++- .../impl/client/DefaultUserTokenHandler.java | 6 +- .../impl/conn/AbstractClientConnAdapter.java | 12 ++++ .../BasicHttpClientConnectionManager.java | 7 ++- .../java/org/apache/http/impl/conn/CPool.java | 11 ++-- .../org/apache/http/impl/conn/CPoolEntry.java | 5 +- .../org/apache/http/impl/conn/CPoolProxy.java | 4 +- .../impl/conn/DefaultClientConnection.java | 12 ++-- .../conn/DefaultClientConnectionFactory.java | 7 ++- .../conn/HttpClientConnectionManagerBase.java | 7 ++- .../conn/HttpClientConnectionOperator.java | 15 ++--- .../conn/ManagedClientConnectionImpl.java | 9 +++ ...l.java => SocketClientConnectionImpl.java} | 56 ++++++++++++++++++- .../integration/TestConnectionManagement.java | 8 +-- .../TestBasicHttpClientConnectionManager.java | 7 ++- .../TestHttpClientConnectionManagerBase.java | 5 +- .../TestHttpClientConnectionOperator.java | 13 ++--- 19 files changed, 143 insertions(+), 57 deletions(-) rename httpclient/src/main/java/org/apache/http/conn/{HttpSSLConnection.java => SocketClientConnection.java} (83%) rename httpclient/src/main/java/org/apache/http/impl/conn/{ClientConnectionImpl.java => SocketClientConnectionImpl.java} (74%) diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java index 56b0905da..f6e40eee7 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java @@ -188,7 +188,7 @@ public class URLEncodedUtils { if (s == null) { return Collections.emptyList(); } - BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT; + BasicHeaderValueParser parser = BasicHeaderValueParser.INSTANCE; CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); ParserCursor cursor = new ParserCursor(0, buffer.length()); diff --git a/httpclient/src/main/java/org/apache/http/conn/ManagedClientConnection.java b/httpclient/src/main/java/org/apache/http/conn/ManagedClientConnection.java index b5c3e3a1c..3b124d8e3 100644 --- a/httpclient/src/main/java/org/apache/http/conn/ManagedClientConnection.java +++ b/httpclient/src/main/java/org/apache/http/conn/ManagedClientConnection.java @@ -49,7 +49,7 @@ import org.apache.http.conn.routing.HttpRoute; */ @Deprecated public interface ManagedClientConnection extends - HttpClientConnection, HttpRoutedConnection, HttpSSLConnection, ConnectionReleaseTrigger { + HttpClientConnection, HttpRoutedConnection, SocketClientConnection, ConnectionReleaseTrigger { /** * Indicates whether this connection is secure. diff --git a/httpclient/src/main/java/org/apache/http/conn/HttpSSLConnection.java b/httpclient/src/main/java/org/apache/http/conn/SocketClientConnection.java similarity index 83% rename from httpclient/src/main/java/org/apache/http/conn/HttpSSLConnection.java rename to httpclient/src/main/java/org/apache/http/conn/SocketClientConnection.java index 2b6ad3d73..7ac024f0a 100644 --- a/httpclient/src/main/java/org/apache/http/conn/HttpSSLConnection.java +++ b/httpclient/src/main/java/org/apache/http/conn/SocketClientConnection.java @@ -27,16 +27,24 @@ package org.apache.http.conn; +import java.io.IOException; +import java.net.Socket; + import javax.net.ssl.SSLSession; +import org.apache.http.HttpClientConnection; import org.apache.http.HttpInetConnection; /** - * Extended interface that exposes SSL session details. + * Extended interface that exposes {@link Socket} bind method and SSL session details. * * @since 4.3 */ -public interface HttpSSLConnection extends HttpInetConnection { +public interface SocketClientConnection extends HttpClientConnection, HttpInetConnection { + + void bind(Socket socket) throws IOException; + + Socket getSocket(); /** * Obtains the SSL session of the underlying connection, if any. diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultUserTokenHandler.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultUserTokenHandler.java index b2869d509..e06dd2896 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultUserTokenHandler.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultUserTokenHandler.java @@ -37,7 +37,7 @@ import org.apache.http.auth.AuthState; import org.apache.http.auth.Credentials; import org.apache.http.client.UserTokenHandler; import org.apache.http.client.protocol.ClientContext; -import org.apache.http.conn.HttpSSLConnection; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; @@ -76,8 +76,8 @@ public class DefaultUserTokenHandler implements UserTokenHandler { if (userPrincipal == null) { HttpConnection conn = (HttpConnection) context.getAttribute( ExecutionContext.HTTP_CONNECTION); - if (conn instanceof HttpSSLConnection) { - SSLSession sslsession = ((HttpSSLConnection) conn).getSSLSession(); + if (conn instanceof SocketClientConnection) { + SSLSession sslsession = ((SocketClientConnection) conn).getSSLSession(); if (sslsession != null) { userPrincipal = sslsession.getLocalPrincipal(); } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java index 7be7bc9d4..396264442 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java @@ -266,6 +266,18 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti return conn.isSecure(); } + public void bind(Socket socket) throws IOException { + throw new UnsupportedOperationException(); + } + + public Socket getSocket() { + OperatedClientConnection conn = getWrappedConnection(); + assertValid(conn); + if (!isOpen()) + return null; + return conn.getSocket(); + } + public SSLSession getSSLSession() { OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java b/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java index 7b03788c6..a342df0ff 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java @@ -42,6 +42,7 @@ import org.apache.http.conn.ConnectionRequest; import org.apache.http.conn.DnsResolver; import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.conn.HttpConnectionFactory; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.params.HttpParams; @@ -70,10 +71,10 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan private final Log log = LogFactory.getLog(getClass()); private final HttpClientConnectionOperator connectionOperator; - private final HttpConnectionFactory connFactory; + private final HttpConnectionFactory connFactory; @GuardedBy("this") - private DefaultClientConnection conn; + private SocketClientConnection conn; @GuardedBy("this") private HttpRoute route; @@ -96,7 +97,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan public BasicHttpClientConnectionManager( final SchemeRegistry schemeRegistry, final DnsResolver dnsResolver, - final HttpConnectionFactory connFactory) { + final HttpConnectionFactory connFactory) { if (schemeRegistry == null) { throw new IllegalArgumentException("Scheme registry may not be null"); } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/CPool.java b/httpclient/src/main/java/org/apache/http/impl/conn/CPool.java index 69edcf23a..2285c766d 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/CPool.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/CPool.java @@ -34,6 +34,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.annotation.ThreadSafe; import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.pool.AbstractConnPool; import org.apache.http.pool.ConnFactory; @@ -42,7 +43,7 @@ import org.apache.http.pool.ConnFactory; * @since 4.3 */ @ThreadSafe -class CPool extends AbstractConnPool { +class CPool extends AbstractConnPool { private static AtomicLong COUNTER = new AtomicLong(); @@ -59,15 +60,15 @@ class CPool extends AbstractConnPool { + static class InternalConnFactory implements ConnFactory { - public DefaultClientConnection create(final HttpRoute route) throws IOException { - return new DefaultClientConnection(); + public SocketClientConnection create(final HttpRoute route) throws IOException { + return new SocketClientConnectionImpl(8 * 1024); } } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/CPoolEntry.java b/httpclient/src/main/java/org/apache/http/impl/conn/CPoolEntry.java index 0b43232aa..640e3fe26 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/CPoolEntry.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/CPoolEntry.java @@ -33,6 +33,7 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log; import org.apache.http.HttpClientConnection; import org.apache.http.annotation.ThreadSafe; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.pool.PoolEntry; @@ -40,7 +41,7 @@ import org.apache.http.pool.PoolEntry; * @since 4.3 */ @ThreadSafe -class CPoolEntry extends PoolEntry { +class CPoolEntry extends PoolEntry { private final Log log; @@ -48,7 +49,7 @@ class CPoolEntry extends PoolEntry { final Log log, final String id, final HttpRoute route, - final DefaultClientConnection conn, + final SocketClientConnection conn, final long timeToLive, final TimeUnit tunit) { super(id, route, conn, timeToLive, tunit); this.log = log; diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/CPoolProxy.java b/httpclient/src/main/java/org/apache/http/impl/conn/CPoolProxy.java index ae72d106d..801b2f087 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/CPoolProxy.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/CPoolProxy.java @@ -34,7 +34,7 @@ import java.lang.reflect.Proxy; import org.apache.http.HttpClientConnection; import org.apache.http.annotation.NotThreadSafe; -import org.apache.http.conn.HttpSSLConnection; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.protocol.HttpContext; /** @@ -137,7 +137,7 @@ class CPoolProxy implements InvocationHandler { final CPoolEntry poolEntry) { return (HttpClientConnection) Proxy.newProxyInstance( CPoolProxy.class.getClassLoader(), - new Class[] { HttpClientConnection.class, HttpSSLConnection.class, HttpContext.class }, + new Class[] { HttpClientConnection.class, SocketClientConnection.class, HttpContext.class }, new CPoolProxy(poolEntry)); } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java index 8a27ead7e..28c872653 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java @@ -46,6 +46,7 @@ import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpResponseFactory; +import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.HttpContext; @@ -54,7 +55,7 @@ import org.apache.http.io.HttpMessageParser; import org.apache.http.io.SessionInputBuffer; import org.apache.http.io.SessionOutputBuffer; -import org.apache.http.conn.HttpSSLConnection; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.OperatedClientConnection; /** @@ -62,13 +63,12 @@ import org.apache.http.conn.OperatedClientConnection; * * @since 4.0 * - * @deprecated (4.3) deprecated in favor of {@link ClientConnectionImpl}. + * @deprecated (4.3) deprecated in favor of {@link SocketClientConnectionImpl}. */ -@SuppressWarnings("deprecation") @NotThreadSafe // connSecure, targetHost @Deprecated public class DefaultClientConnection extends SocketHttpClientConnection - implements OperatedClientConnection, HttpSSLConnection, HttpContext { + implements OperatedClientConnection, SocketClientConnection, HttpContext { private final Log log = LogFactory.getLog(getClass()); private final Log headerLog = LogFactory.getLog("org.apache.http.headers"); @@ -231,6 +231,10 @@ public class DefaultClientConnection extends SocketHttpClientConnection (buffer, null, responseFactory, params); } + public void bind(Socket socket) throws IOException { + bind(socket, new BasicHttpParams()); + } + public void update(Socket sock, HttpHost target, boolean secure, HttpParams params) throws IOException { diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionFactory.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionFactory.java index 80ae5967f..aec33ff3e 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionFactory.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionFactory.java @@ -28,18 +28,19 @@ package org.apache.http.impl.conn; import org.apache.http.annotation.Immutable; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.HttpConnectionFactory; /** * @since 4.3 */ @Immutable -public class DefaultClientConnectionFactory implements HttpConnectionFactory { +public class DefaultClientConnectionFactory implements HttpConnectionFactory { public static final DefaultClientConnectionFactory INSTANCE = new DefaultClientConnectionFactory(); - public DefaultClientConnection create() { - return new DefaultClientConnection(); + public SocketClientConnection create() { + return new SocketClientConnectionImpl(8 * 1024); } } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionManagerBase.java b/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionManagerBase.java index d66068be9..19d470ec1 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionManagerBase.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionManagerBase.java @@ -40,6 +40,7 @@ import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.ConnectionRequest; import org.apache.http.conn.DnsResolver; import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.params.HttpParams; @@ -165,7 +166,7 @@ abstract class HttpClientConnectionManagerBase implements HttpClientConnectionMa if (entry == null) { return; } - DefaultClientConnection conn = entry.getConnection(); + SocketClientConnection conn = entry.getConnection(); try { if (conn.isOpen()) { entry.setState(state); @@ -188,7 +189,7 @@ abstract class HttpClientConnectionManagerBase implements HttpClientConnectionMa if (managedConn == null) { throw new IllegalArgumentException("Connection may not be null"); } - DefaultClientConnection conn; + SocketClientConnection conn; synchronized (managedConn) { CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn); conn = entry.getConnection(); @@ -204,7 +205,7 @@ abstract class HttpClientConnectionManagerBase implements HttpClientConnectionMa if (managedConn == null) { throw new IllegalArgumentException("Connection may not be null"); } - DefaultClientConnection conn; + SocketClientConnection conn; synchronized (managedConn) { CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn); conn = entry.getConnection(); diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java b/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java index 314f360fb..b97e39992 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java @@ -42,6 +42,7 @@ import org.apache.http.conn.DnsResolver; import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.HttpInetSocketAddress; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeLayeredSocketFactory; import org.apache.http.conn.scheme.SchemeRegistry; @@ -86,7 +87,7 @@ class HttpClientConnectionOperator { } public void connect( - final DefaultClientConnection conn, + final SocketClientConnection conn, final HttpHost host, final InetAddress local, final HttpContext context, @@ -102,7 +103,7 @@ class HttpClientConnectionOperator { boolean last = i == addresses.length - 1; Socket sock = sf.createSocket(params); - conn.opening(sock, host); + conn.bind(sock); InetSocketAddress remoteAddress = new HttpInetSocketAddress(host, address, port); InetSocketAddress localAddress = null; @@ -114,11 +115,7 @@ class HttpClientConnectionOperator { } try { Socket connsock = sf.connectSocket(sock, remoteAddress, localAddress, params); - if (sock != connsock) { - sock = connsock; - conn.opening(sock, host); - } - conn.openCompleted(sf.isSecure(sock), params); + conn.bind(connsock); return; } catch (ConnectException ex) { if (last) { @@ -137,7 +134,7 @@ class HttpClientConnectionOperator { } public void upgrade( - final DefaultClientConnection conn, + final SocketClientConnection conn, final HttpHost host, final HttpContext context, final HttpParams params) throws IOException { @@ -157,7 +154,7 @@ class HttpClientConnectionOperator { } catch (ConnectException ex) { throw new HttpHostConnectException(host, ex); } - conn.update(sock, host, lsf.isSecure(sock), params); + conn.bind(sock); } } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java b/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java index 5b46da1d8..bec6a9489 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/ManagedClientConnectionImpl.java @@ -234,6 +234,15 @@ class ManagedClientConnectionImpl implements ManagedClientConnection { return conn.isSecure(); } + public void bind(Socket socket) throws IOException { + throw new UnsupportedOperationException(); + } + + public Socket getSocket() { + OperatedClientConnection conn = ensureConnection(); + return conn.getSocket(); + } + public SSLSession getSSLSession() { OperatedClientConnection conn = ensureConnection(); SSLSession result = null; diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/ClientConnectionImpl.java b/httpclient/src/main/java/org/apache/http/impl/conn/SocketClientConnectionImpl.java similarity index 74% rename from httpclient/src/main/java/org/apache/http/impl/conn/ClientConnectionImpl.java rename to httpclient/src/main/java/org/apache/http/impl/conn/SocketClientConnectionImpl.java index c4957bdb7..4c29ebd61 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/ClientConnectionImpl.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/SocketClientConnectionImpl.java @@ -29,24 +29,33 @@ package org.apache.http.impl.conn; import java.io.IOException; import java.io.InputStream; +import java.io.InterruptedIOException; import java.io.OutputStream; import java.net.Socket; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocket; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.Header; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.config.MessageConstraints; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.entity.ContentLengthStrategy; import org.apache.http.impl.DefaultBHttpClientConnection; import org.apache.http.io.HttpMessageParserFactory; import org.apache.http.io.HttpMessageWriterFactory; +import org.apache.http.protocol.HttpContext; -class ClientConnectionImpl extends DefaultBHttpClientConnection { +class SocketClientConnectionImpl extends DefaultBHttpClientConnection + implements SocketClientConnection, HttpContext { private static final AtomicLong COUNT = new AtomicLong(); @@ -54,8 +63,11 @@ class ClientConnectionImpl extends DefaultBHttpClientConnection { private final Log log; private final Log headerlog; private final Wire wire; + private final Map attributes; - public ClientConnectionImpl( + private volatile boolean shutdown; + + public SocketClientConnectionImpl( int buffersize, final CharsetDecoder chardecoder, final CharsetEncoder charencoder, @@ -71,9 +83,10 @@ class ClientConnectionImpl extends DefaultBHttpClientConnection { this.log = LogFactory.getLog(getClass()); this.headerlog = LogFactory.getLog("org.apache.http.headers"); this.wire = new Wire(LogFactory.getLog("org.apache.http.wire"), this.id); + this.attributes = new ConcurrentHashMap(); } - public ClientConnectionImpl(int buffersize) { + public SocketClientConnectionImpl(int buffersize) { this(buffersize, null, null, null, null, null, null, null); } @@ -90,6 +103,7 @@ class ClientConnectionImpl extends DefaultBHttpClientConnection { if (this.log.isDebugEnabled()) { this.log.debug(this.id + ": Shutdown connection"); } + this.shutdown = true; super.shutdown(); } @@ -133,4 +147,40 @@ class ClientConnectionImpl extends DefaultBHttpClientConnection { } } + public Object getAttribute(final String id) { + return this.attributes.get(id); + } + + public Object removeAttribute(final String id) { + return this.attributes.remove(id); + } + + public void setAttribute(final String id, final Object obj) { + this.attributes.put(id, obj); + } + + @Override + public void bind(final Socket socket) throws IOException { + if (this.shutdown) { + socket.close(); // allow this to throw... + // ...but if it doesn't, explicitly throw one ourselves. + throw new InterruptedIOException("Connection already shutdown"); + } + super.bind(socket); + } + + @Override + public Socket getSocket() { + return super.getSocket(); + } + + public SSLSession getSSLSession() { + Socket socket = super.getSocket(); + if (socket instanceof SSLSocket) { + return ((SSLSocket) socket).getSession(); + } else { + return null; + } + } + } diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionManagement.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionManagement.java index a3e47b3c4..0b0e316be 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionManagement.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestConnectionManagement.java @@ -522,9 +522,8 @@ public class TestConnectionManagement extends LocalServerTestBase { try { mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), context, params); - Assert.fail("expected exception"); + Assert.fail("IOException expected"); } catch(IOException expected) { - Assert.assertEquals("Connection already shutdown", expected.getMessage()); } abortingThread.join(5000); @@ -578,8 +577,9 @@ public class TestConnectionManagement extends LocalServerTestBase { try { mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), context, params); - Assert.fail("expected SocketException"); - } catch(SocketException expected) {} + Assert.fail("IOException expected"); + } catch(IOException expected) { + } abortingThread.join(5000); if(throwRef.get() != null) diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java index 3b23ca7a7..8d493b79f 100644 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java +++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java @@ -35,6 +35,7 @@ import org.apache.http.HttpHost; import org.apache.http.conn.ConnectionRequest; import org.apache.http.conn.DnsResolver; import org.apache.http.conn.HttpConnectionFactory; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; @@ -47,8 +48,8 @@ import org.mockito.Mockito; public class TestBasicHttpClientConnectionManager { - private DefaultClientConnection conn; - private HttpConnectionFactory connFactory; + private SocketClientConnection conn; + private HttpConnectionFactory connFactory; private Socket socket; private SchemeSocketFactory plainSocketFactory; private SchemeRegistry schemeRegistry; @@ -58,7 +59,7 @@ public class TestBasicHttpClientConnectionManager { @SuppressWarnings("unchecked") @Before public void setup() throws Exception { - conn = Mockito.mock(DefaultClientConnection.class); + conn = Mockito.mock(SocketClientConnection.class); connFactory = Mockito.mock(HttpConnectionFactory.class); Mockito.when(connFactory.create()).thenReturn(conn); socket = Mockito.mock(Socket.class); diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionManagerBase.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionManagerBase.java index 9edb3ad53..09a3a381c 100644 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionManagerBase.java +++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionManagerBase.java @@ -38,6 +38,7 @@ import org.apache.http.HttpHost; import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.ConnectionRequest; import org.apache.http.conn.DnsResolver; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; @@ -51,7 +52,7 @@ import org.mockito.Mockito; public class TestHttpClientConnectionManagerBase { - private DefaultClientConnection conn; + private SocketClientConnection conn; private Socket socket; private SchemeSocketFactory plainSocketFactory; private SchemeRegistry schemeRegistry; @@ -63,7 +64,7 @@ public class TestHttpClientConnectionManagerBase { @SuppressWarnings("unchecked") @Before public void setup() throws Exception { - conn = Mockito.mock(DefaultClientConnection.class); + conn = Mockito.mock(SocketClientConnection.class); socket = Mockito.mock(Socket.class); plainSocketFactory = Mockito.mock(SchemeSocketFactory.class); Mockito.when(plainSocketFactory.createSocket(Mockito.any())).thenReturn(socket); diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java index a5776aac0..be14f4941 100644 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java +++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java @@ -35,6 +35,7 @@ import org.apache.http.HttpHost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.DnsResolver; import org.apache.http.conn.HttpInetSocketAddress; +import org.apache.http.conn.SocketClientConnection; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeLayeredSocketFactory; import org.apache.http.conn.scheme.SchemeRegistry; @@ -49,7 +50,7 @@ import org.mockito.Mockito; public class TestHttpClientConnectionOperator { - private DefaultClientConnection conn; + private SocketClientConnection conn; private Socket socket; private SchemeSocketFactory plainSocketFactory; private SchemeLayeredSocketFactory sslSocketFactory; @@ -59,7 +60,7 @@ public class TestHttpClientConnectionOperator { @Before public void setup() throws Exception { - conn = Mockito.mock(DefaultClientConnection.class); + conn = Mockito.mock(SocketClientConnection.class); socket = Mockito.mock(Socket.class); plainSocketFactory = Mockito.mock(SchemeSocketFactory.class); sslSocketFactory = Mockito.mock(SchemeLayeredSocketFactory.class); @@ -97,8 +98,7 @@ public class TestHttpClientConnectionOperator { Mockito.verify(plainSocketFactory).connectSocket(socket, new InetSocketAddress(ip1, 80), new InetSocketAddress(local, 0), params); - Mockito.verify(conn).opening(socket, host); - Mockito.verify(conn).openCompleted(false, params); + Mockito.verify(conn, Mockito.times(2)).bind(socket); } @Test(expected=ConnectTimeoutException.class) @@ -146,8 +146,7 @@ public class TestHttpClientConnectionOperator { Mockito.verify(plainSocketFactory).connectSocket(socket, new HttpInetSocketAddress(host, ip2, 80), new InetSocketAddress(local, 0), params); - Mockito.verify(conn, Mockito.times(2)).opening(socket, host); - Mockito.verify(conn).openCompleted(false, params); + Mockito.verify(conn, Mockito.times(3)).bind(socket); } @Test @@ -164,7 +163,7 @@ public class TestHttpClientConnectionOperator { connectionOperator.upgrade(conn, host, context, params); - Mockito.verify(conn).update(socket, host, false, params); + Mockito.verify(conn).bind(socket); } @Test(expected=IllegalArgumentException.class)