From 90fc40bbf240b0da6635ce09884ff4bbfd75e3d1 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 30 Jan 2013 16:59:07 +0000 Subject: [PATCH] Use InetSocketAddress instead of InetAddress in the HttpClientConnectionManager#connect method git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1440519 13f79535-47bb-0310-9956-ffa450edef68 --- .../conn/HttpClientConnectionManager.java | 4 ++-- .../apache/http/conn/routing/HttpRoute.java | 5 +++++ .../BasicHttpClientConnectionManager.java | 4 +--- .../PoolingHttpClientConnectionManager.java | 4 +--- .../http/impl/execchain/MainClientExec.java | 6 ++--- .../impl/execchain/MinimalClientExec.java | 2 +- .../client/integration/TestAbortHandling.java | 6 ++--- .../integration/TestConnectionManagement.java | 22 +++++++++---------- 8 files changed, 27 insertions(+), 26 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/conn/HttpClientConnectionManager.java b/httpclient/src/main/java/org/apache/http/conn/HttpClientConnectionManager.java index 36e3a8fec..b705d6367 100644 --- a/httpclient/src/main/java/org/apache/http/conn/HttpClientConnectionManager.java +++ b/httpclient/src/main/java/org/apache/http/conn/HttpClientConnectionManager.java @@ -27,7 +27,7 @@ package org.apache.http.conn; import java.io.IOException; -import java.net.InetAddress; +import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; import org.apache.http.HttpClientConnection; @@ -80,7 +80,7 @@ public interface HttpClientConnectionManager { void connect( HttpClientConnection conn, - HttpHost host, InetAddress localAddress, + HttpHost host, InetSocketAddress localAddress, int connectTimeout, HttpContext context) throws IOException; diff --git a/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java b/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java index 892000daa..2cb1b4444 100644 --- a/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java +++ b/httpclient/src/main/java/org/apache/http/conn/routing/HttpRoute.java @@ -28,6 +28,7 @@ package org.apache.http.conn.routing; import java.net.InetAddress; +import java.net.InetSocketAddress; import org.apache.http.HttpHost; import org.apache.http.annotation.Immutable; @@ -255,6 +256,10 @@ public final class HttpRoute implements RouteInfo, Cloneable { } + public final InetSocketAddress getLocalSocketAddress() { + return this.localAddress != null ? new InetSocketAddress(this.localAddress, 0) : null; + } + public final int getHopCount() { return proxyChain.length+1; } 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 60eb20ac3..a49fa2d92 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 @@ -29,7 +29,6 @@ package org.apache.http.impl.conn; import java.io.Closeable; import java.io.IOException; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.Date; import java.util.concurrent.TimeUnit; @@ -304,13 +303,12 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan public void connect( final HttpClientConnection conn, final HttpHost host, - final InetAddress local, + final InetSocketAddress localAddress, final int connectTimeout, final HttpContext context) throws IOException { Args.notNull(conn, "Connection"); Args.notNull(host, "HTTP host"); Asserts.check(conn == this.conn, "Connection not obtained from this manager"); - final InetSocketAddress localAddress = local != null ? new InetSocketAddress(local, 0) : null; this.connectionOperator.connect(this.conn, host, localAddress, connectTimeout, this.socketConfig, context); } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java b/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java index 78d4cb281..bdd1b62d8 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java @@ -28,7 +28,6 @@ package org.apache.http.impl.conn; import java.io.Closeable; import java.io.IOException; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -297,7 +296,7 @@ public class PoolingHttpClientConnectionManager public void connect( final HttpClientConnection managedConn, final HttpHost host, - final InetAddress local, + final InetSocketAddress localAddress, final int connectTimeout, final HttpContext context) throws IOException { Args.notNull(managedConn, "Connection"); @@ -313,7 +312,6 @@ public class PoolingHttpClientConnectionManager if (socketConfig == null) { socketConfig = SocketConfig.DEFAULT; } - final InetSocketAddress localAddress = local != null ? new InetSocketAddress(local, 0) : null; this.connectionOperator.connect( conn, host, localAddress, connectTimeout, socketConfig, context); } diff --git a/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java b/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java index 74af34f6f..91db53a9a 100644 --- a/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java +++ b/httpclient/src/main/java/org/apache/http/impl/execchain/MainClientExec.java @@ -353,7 +353,7 @@ public class MainClientExec implements ClientExecChain { case HttpRouteDirector.CONNECT_TARGET: this.connManager.connect( managedConn, - route.getTargetHost(), route.getLocalAddress(), + route.getTargetHost(), route.getLocalSocketAddress(), timeout > 0 ? timeout : 0, context); tracker.connectTarget(route.isSecure()); @@ -361,7 +361,7 @@ public class MainClientExec implements ClientExecChain { case HttpRouteDirector.CONNECT_PROXY: this.connManager.connect( managedConn, - route.getProxyHost(), route.getLocalAddress(), + route.getProxyHost(), route.getLocalSocketAddress(), timeout > 0 ? timeout : 0, context); final HttpHost proxy = route.getProxyHost(); @@ -434,7 +434,7 @@ public class MainClientExec implements ClientExecChain { if (!managedConn.isOpen()) { this.connManager.connect( managedConn, - route.getProxyHost(), route.getLocalAddress(), + route.getProxyHost(), route.getLocalSocketAddress(), timeout > 0 ? timeout : 0, context); } diff --git a/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java b/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java index c313e467a..9b5d50c78 100644 --- a/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java +++ b/httpclient/src/main/java/org/apache/http/impl/execchain/MinimalClientExec.java @@ -146,7 +146,7 @@ public class MinimalClientExec implements ClientExecChain { final int timeout = config.getConnectTimeout(); this.connManager.connect( managedConn, - route.getTargetHost(), route.getLocalAddress(), + route.getTargetHost(), route.getLocalSocketAddress(), timeout > 0 ? timeout : 0, context); } else { diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java index a16cb8728..d17bc8af6 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java @@ -28,7 +28,7 @@ package org.apache.http.impl.client.integration; import java.io.IOException; import java.net.ConnectException; -import java.net.InetAddress; +import java.net.InetSocketAddress; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -297,7 +297,7 @@ public class TestAbortHandling extends IntegrationTestBase { Mockito.doThrow(new ConnectException()).when(connmgr).connect( Mockito.any(HttpClientConnection.class), Mockito.any(HttpHost.class), - Mockito.any(InetAddress.class), + Mockito.any(InetSocketAddress.class), Mockito.anyInt(), Mockito.any(HttpContext.class)); @@ -465,7 +465,7 @@ public class TestAbortHandling extends IntegrationTestBase { public void connect( final HttpClientConnection conn, final HttpHost host, - final InetAddress localAddress, + final InetSocketAddress localAddress, final int connectTimeout, final HttpContext context) throws IOException { throw new UnsupportedOperationException("just a mockup"); 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 8777b461c..8596fdff2 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 @@ -112,7 +112,7 @@ public class TestConnectionManagement extends LocalServerTestBase { final HttpContext context = new BasicHttpContext(); HttpClientConnection conn = getConnection(mgr, route); - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); @@ -146,7 +146,7 @@ public class TestConnectionManagement extends LocalServerTestBase { conn = getConnection(mgr, route); Assert.assertFalse("connection should have been closed", conn.isOpen()); - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); // repeat the communication, no need to prepare the request again context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); @@ -200,7 +200,7 @@ public class TestConnectionManagement extends LocalServerTestBase { final HttpContext context = new BasicHttpContext(); HttpClientConnection conn = getConnection(mgr, route); - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); @@ -235,7 +235,7 @@ public class TestConnectionManagement extends LocalServerTestBase { Assert.assertFalse("connection should have been closed", conn.isOpen()); // repeat the communication, no need to prepare the request again - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); response = exec.execute(request, conn, context); @@ -270,7 +270,7 @@ public class TestConnectionManagement extends LocalServerTestBase { Assert.assertTrue("connection should have been closed", !conn.isOpen()); // repeat the communication, no need to prepare the request again - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); response = exec.execute(request, conn, context); @@ -297,7 +297,7 @@ public class TestConnectionManagement extends LocalServerTestBase { final HttpContext context = new BasicHttpContext(); final HttpClientConnection conn = getConnection(mgr, route); - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); Assert.assertEquals(1, mgr.getTotalStats().getLeased()); Assert.assertEquals(1, mgr.getStats(route).getLeased()); @@ -337,7 +337,7 @@ public class TestConnectionManagement extends LocalServerTestBase { final HttpContext context = new BasicHttpContext(); final HttpClientConnection conn = getConnection(mgr, route); - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); Assert.assertEquals(1, mgr.getTotalStats().getLeased()); Assert.assertEquals(1, mgr.getStats(route).getLeased()); @@ -385,7 +385,7 @@ public class TestConnectionManagement extends LocalServerTestBase { new BasicHttpRequest("GET", uri, HttpVersion.HTTP_1_1); HttpClientConnection conn = getConnection(mgr, route); - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target); @@ -457,7 +457,7 @@ public class TestConnectionManagement extends LocalServerTestBase { abortingThread.start(); try { - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); Assert.fail("expected SocketException"); } catch(final SocketException expected) {} @@ -511,7 +511,7 @@ public class TestConnectionManagement extends LocalServerTestBase { abortingThread.start(); try { - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); Assert.fail("IOException expected"); } catch(final IOException expected) { } @@ -566,7 +566,7 @@ public class TestConnectionManagement extends LocalServerTestBase { abortingThread.start(); try { - mgr.connect(conn, route.getTargetHost(), route.getLocalAddress(), 0, context); + mgr.connect(conn, route.getTargetHost(), route.getLocalSocketAddress(), 0, context); Assert.fail("IOException expected"); } catch(final IOException expected) { }