From 8f5bd3b063116bda59863597d3828df2eef90f4d Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Tue, 11 Jan 2011 19:55:31 +0000 Subject: [PATCH] Bug 333717 HttpClient can't get local listen address, http exchange can return the local address used for the connection. git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2650 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../eclipse/jetty/client/HttpExchange.java | 22 ++++++++++++++++++ .../jetty/client/HttpExchangeTest.java | 23 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpExchange.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpExchange.java index 9a1e0ae6532..dd5f26e8fb4 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpExchange.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpExchange.java @@ -97,6 +97,8 @@ public class HttpExchange private HttpEventListener _listener = new Listener(); private volatile HttpConnection _connection; + private Address _localAddress = null; + // a timeout for this exchange private long _timeout = -1; @@ -408,6 +410,19 @@ public class HttpExchange return _address; } + /** + * the local address used by the connection + * + * Note: this method will not be populated unless the exchange + * has been executed by the HttpClient + * + * @return the local address used for the running of the exchange if available, null otherwise. + */ + public Address getLocalAddress() + { + return _localAddress; + } + /** * @param scheme the scheme of the URL (for example 'http') */ @@ -660,6 +675,11 @@ public class HttpExchange void associate(HttpConnection connection) { + if ( connection.getEndPoint().getLocalHost() != null ) + { + _localAddress = new Address( connection.getEndPoint().getLocalHost(), connection.getEndPoint().getLocalPort() ); + } + _connection = connection; if (getStatus() == STATUS_CANCELLING) abort(); @@ -699,6 +719,8 @@ public class HttpExchange */ protected void onRequestCommitted() throws IOException { + _connection.getEndPoint(); + } /** diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java index 6c3c249d8ae..46e2523fa94 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java @@ -261,6 +261,29 @@ public class HttpExchangeTest extends TestCase } } + public void testLocalAddressAvailabilityWithContentExchange() throws Exception + { + for (int i=0;i<10;i++) + { + ContentExchange httpExchange=new ContentExchange(); + httpExchange.setURL(_scheme+"localhost:"+_port+"/?i="+i); + httpExchange.setMethod(HttpMethods.GET); + _httpClient.send(httpExchange); + int status = httpExchange.waitForDone(); + + assertNotNull(httpExchange.getLocalAddress()); + + //System.out.println("Local Address: " + httpExchange.getLocalAddress()); + + //httpExchange.waitForStatus(HttpExchange.STATUS_COMPLETED); + String result=httpExchange.getResponseContent(); + assertEquals("i="+i,0,result.indexOf("")); + assertEquals("i="+i,result.length()-10,result.indexOf("")); + assertEquals(HttpExchange.STATUS_COMPLETED, status); + Thread.sleep(5); + } + } + public void testShutdownWithExchange() throws Exception { final AtomicReference throwable=new AtomicReference();