Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 83A9510474 for ; Tue, 4 Jun 2013 13:27:32 +0000 (UTC) Received: (qmail 52807 invoked by uid 500); 4 Jun 2013 13:27:32 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 52756 invoked by uid 500); 4 Jun 2013 13:27:30 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 52748 invoked by uid 99); 4 Jun 2013 13:27:29 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Jun 2013 13:27:29 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Jun 2013 13:27:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E543323889F1 for ; Tue, 4 Jun 2013 13:27:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1489438 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/conn/ main/java/org/apache/http/conn/socket/ main/java/org/apache/http/conn/ssl/ main/java/org/apache/http/impl/conn/ test/java/org/apache/http/conn/ t... Date: Tue, 04 Jun 2013 13:27:03 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130604132703.E543323889F1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Tue Jun 4 13:27:02 2013 New Revision: 1489438 URL: http://svn.apache.org/r1489438 Log: HTTPCLIENT-1362: better error messages for connect timed out and connection refused exceptions (second cut) Removed: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/AbstractConnectionSocketFactory.java Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ConnectTimeoutException.java (contents, props changed) httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/HttpHostConnectException.java (contents, props changed) httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/ConnectionSocketFactory.java (contents, props changed) httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/PlainSocketFactory.java (contents, props changed) httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java (contents, props changed) httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/TestExceptions.java httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ConnectTimeoutException.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ConnectTimeoutException.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ConnectTimeoutException.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ConnectTimeoutException.java Tue Jun 4 13:27:02 2013 @@ -29,7 +29,9 @@ package org.apache.http.conn; import java.io.IOException; import java.io.InterruptedIOException; +import java.net.InetAddress; import java.net.InetSocketAddress; +import java.util.Arrays; import org.apache.http.HttpHost; import org.apache.http.annotation.Immutable; @@ -47,7 +49,6 @@ public class ConnectTimeoutException ext private static final long serialVersionUID = -4816682903149535989L; private final HttpHost host; - private final InetSocketAddress remoteAddress; /** * Creates a ConnectTimeoutException with a null detail message. @@ -55,7 +56,6 @@ public class ConnectTimeoutException ext public ConnectTimeoutException() { super(); this.host = null; - this.remoteAddress = null; } /** @@ -64,7 +64,6 @@ public class ConnectTimeoutException ext public ConnectTimeoutException(final String message) { super(message); this.host = null; - this.remoteAddress = null; } /** @@ -73,15 +72,14 @@ public class ConnectTimeoutException ext * @since 4.3 */ public ConnectTimeoutException( + final IOException cause, final HttpHost host, - final InetSocketAddress remoteAddress, - final IOException cause) { + final InetAddress... remoteAddresses) { super("Connect to " + (host != null ? host.toHostString() : "remote host") + - (remoteAddress != null ? " (" + remoteAddress.getAddress() + ")" : "") + (remoteAddresses != null ? " " + Arrays.asList(remoteAddresses) : "") + ((cause != null && cause.getMessage() != null) ? " failed: " + cause.getMessage() : " timed out")); this.host = host; - this.remoteAddress = remoteAddress; initCause(cause); } @@ -92,11 +90,4 @@ public class ConnectTimeoutException ext return host; } - /** - * @since 4.3 - */ - public InetSocketAddress getRemoteAddress() { - return remoteAddress; - } - } Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ConnectTimeoutException.java ('svn:executable' removed) Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/HttpHostConnectException.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/HttpHostConnectException.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/HttpHostConnectException.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/HttpHostConnectException.java Tue Jun 4 13:27:02 2013 @@ -28,7 +28,9 @@ package org.apache.http.conn; import java.io.IOException; import java.net.ConnectException; +import java.net.InetAddress; import java.net.InetSocketAddress; +import java.util.Arrays; import org.apache.http.HttpHost; import org.apache.http.annotation.Immutable; @@ -45,10 +47,14 @@ public class HttpHostConnectException ex private static final long serialVersionUID = -3194482710275220224L; private final HttpHost host; - private final InetSocketAddress remoteAddress; + /** + * @deprecated (4.3) use {@link #HttpHostConnectException(java.io.IOException, org.apache.http.HttpHost, + * java.net.InetAddress...)} + */ + @Deprecated public HttpHostConnectException(final HttpHost host, final ConnectException cause) { - this(host, null, cause); + this(cause, host, null); } /** @@ -57,15 +63,14 @@ public class HttpHostConnectException ex * @since 4.3 */ public HttpHostConnectException( + final IOException cause, final HttpHost host, - final InetSocketAddress remoteAddress, - final IOException cause) { + final InetAddress... remoteAddresses) { super("Connect to " + (host != null ? host.toHostString() : "remote host") + - (remoteAddress != null ? " (" + remoteAddress.getAddress() + ")" : "") + (remoteAddresses != null ? " " + Arrays.asList(remoteAddresses) : "") + ((cause != null && cause.getMessage() != null) ? " failed: " + cause.getMessage() : " refused")); this.host = host; - this.remoteAddress = remoteAddress; initCause(cause); } @@ -73,11 +78,4 @@ public class HttpHostConnectException ex return this.host; } - /** - * @since 4.3 - */ - public InetSocketAddress getRemoteAddress() { - return remoteAddress; - } - } Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/HttpHostConnectException.java ('svn:executable' removed) Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/ConnectionSocketFactory.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/ConnectionSocketFactory.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== (empty) Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/ConnectionSocketFactory.java ('svn:executable' removed) Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/PlainSocketFactory.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/PlainSocketFactory.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/PlainSocketFactory.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/PlainSocketFactory.java Tue Jun 4 13:27:02 2013 @@ -28,8 +28,10 @@ package org.apache.http.conn.socket; import java.io.IOException; +import java.net.InetSocketAddress; import java.net.Socket; +import org.apache.http.HttpHost; import org.apache.http.annotation.Immutable; import org.apache.http.protocol.HttpContext; @@ -39,7 +41,7 @@ import org.apache.http.protocol.HttpCont * @since 4.3 */ @Immutable -public class PlainSocketFactory extends AbstractConnectionSocketFactory { +public class PlainSocketFactory implements ConnectionSocketFactory { public static final PlainSocketFactory INSTANCE = new PlainSocketFactory(); @@ -55,4 +57,27 @@ public class PlainSocketFactory extends return new Socket(); } + public Socket connectSocket( + final int connectTimeout, + final Socket socket, + final HttpHost host, + final InetSocketAddress remoteAddress, + final InetSocketAddress localAddress, + final HttpContext context) throws IOException { + final Socket sock = socket != null ? socket : createSocket(context); + if (localAddress != null) { + sock.bind(localAddress); + } + try { + sock.connect(remoteAddress, connectTimeout); + } catch (final IOException ex) { + try { + sock.close(); + } catch (final IOException ignore) { + } + throw ex; + } + return sock; + } + } Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/socket/PlainSocketFactory.java ('svn:executable' removed) Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java Tue Jun 4 13:27:02 2013 @@ -28,6 +28,7 @@ package org.apache.http.conn.ssl; import java.io.IOException; +import java.net.ConnectException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; @@ -51,7 +52,6 @@ import org.apache.http.conn.scheme.HostN import org.apache.http.conn.scheme.LayeredSchemeSocketFactory; import org.apache.http.conn.scheme.LayeredSocketFactory; import org.apache.http.conn.scheme.SchemeLayeredSocketFactory; -import org.apache.http.conn.socket.AbstractConnectionSocketFactory; import org.apache.http.conn.socket.LayeredConnectionSocketFactory; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; @@ -92,8 +92,7 @@ import org.apache.http.util.TextUtils; */ @SuppressWarnings("deprecation") @ThreadSafe -public class SSLSocketFactory extends AbstractConnectionSocketFactory - implements LayeredConnectionSocketFactory, SchemeLayeredSocketFactory, +public class SSLSocketFactory implements LayeredConnectionSocketFactory, SchemeLayeredSocketFactory, LayeredSchemeSocketFactory, LayeredSocketFactory { public static final String TLS = "TLS"; @@ -553,8 +552,19 @@ public class SSLSocketFactory extends Ab final HttpContext context) throws IOException { Args.notNull(host, "HTTP host"); Args.notNull(remoteAddress, "Remote address"); - final Socket sock = super.connectSocket( - connectTimeout, socket, host, remoteAddress, localAddress, context); + final Socket sock = socket != null ? socket : createSocket(context); + if (localAddress != null) { + sock.bind(localAddress); + } + try { + sock.connect(remoteAddress, connectTimeout); + } catch (final IOException ex) { + try { + sock.close(); + } catch (final IOException ignore) { + } + throw ex; + } // Setup SSL layering if necessary if (sock instanceof SSLSocket) { final SSLSocket sslsock = (SSLSocket) sock; Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java ('svn:executable' removed) Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/HttpClientConnectionOperator.java Tue Jun 4 13:27:02 2013 @@ -31,6 +31,7 @@ import java.net.ConnectException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; +import java.net.SocketTimeoutException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -42,6 +43,7 @@ import org.apache.http.config.SocketConf import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.DnsResolver; import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.ManagedHttpClientConnection; import org.apache.http.conn.SchemePortResolver; import org.apache.http.conn.UnsupportedSchemeException; @@ -123,13 +125,18 @@ class HttpClientConnectionOperator { } conn.bind(sock); return; - } catch (final ConnectException ex) { + } catch (final SocketTimeoutException ex) { if (last) { - throw ex; + throw new ConnectTimeoutException(ex, host, addresses); } - } catch (final ConnectTimeoutException ex) { + } catch (final ConnectException ex) { if (last) { - throw ex; + final String msg = ex.getMessage(); + if ("Connection timed out".equals(msg)) { + throw new ConnectTimeoutException(ex, host, addresses); + } else { + throw new HttpHostConnectException(ex, host, addresses); + } } } if (this.log.isDebugEnabled()) { Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/TestExceptions.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/TestExceptions.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/TestExceptions.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/TestExceptions.java Tue Jun 4 13:27:02 2013 @@ -33,7 +33,6 @@ import org.junit.Test; import java.io.IOException; import java.net.InetAddress; -import java.net.InetSocketAddress; /** * Unit tests for exceptions. @@ -62,7 +61,7 @@ public class TestExceptions { @Test public void testConnectTimeoutExceptionFromCause() { final IOException cause = new IOException("something awful"); - final ConnectTimeoutException ctx = new ConnectTimeoutException(null, null, cause); + final ConnectTimeoutException ctx = new ConnectTimeoutException(cause, null, null); Assert.assertEquals("Connect to remote host failed: something awful", ctx.getMessage()); } @@ -70,18 +69,17 @@ public class TestExceptions { public void testConnectTimeoutExceptionFromCauseAndHost() { final HttpHost target = new HttpHost("localhost"); final IOException cause = new IOException(); - final ConnectTimeoutException ctx = new ConnectTimeoutException(target, null, cause); + final ConnectTimeoutException ctx = new ConnectTimeoutException(cause, target, null); Assert.assertEquals("Connect to localhost timed out", ctx.getMessage()); } @Test public void testConnectTimeoutExceptionFromCauseHostAndRemoteAddress() throws Exception { final HttpHost target = new HttpHost("localhost"); - final InetSocketAddress remoteAddress = new InetSocketAddress( - InetAddress.getByAddress(new byte[] {1,2,3,4}), 1234); + final InetAddress remoteAddress = InetAddress.getByAddress(new byte[] {1,2,3,4}); final IOException cause = new IOException(); - final ConnectTimeoutException ctx = new ConnectTimeoutException(target, remoteAddress, cause); - Assert.assertEquals("Connect to localhost (/1.2.3.4) timed out", ctx.getMessage()); + final ConnectTimeoutException ctx = new ConnectTimeoutException(cause, target, remoteAddress); + Assert.assertEquals("Connect to localhost [/1.2.3.4] timed out", ctx.getMessage()); } @Test @@ -93,7 +91,7 @@ public class TestExceptions { @Test public void testHttpHostConnectExceptionFromCause() { final IOException cause = new IOException("something awful"); - final HttpHostConnectException ctx = new HttpHostConnectException(null, null, cause); + final HttpHostConnectException ctx = new HttpHostConnectException(cause, null, null); Assert.assertEquals("Connect to remote host failed: something awful", ctx.getMessage()); } @@ -101,18 +99,19 @@ public class TestExceptions { public void testHttpHostConnectExceptionFromCauseAndHost() { final HttpHost target = new HttpHost("localhost"); final IOException cause = new IOException(); - final HttpHostConnectException ctx = new HttpHostConnectException(target, null, cause); + final HttpHostConnectException ctx = new HttpHostConnectException(cause, target, null); Assert.assertEquals("Connect to localhost refused", ctx.getMessage()); } @Test public void testHttpHostConnectExceptionFromCauseHostAndRemoteAddress() throws Exception { final HttpHost target = new HttpHost("localhost"); - final InetSocketAddress remoteAddress = new InetSocketAddress( - InetAddress.getByAddress(new byte[] {1,2,3,4}), 1234); + final InetAddress remoteAddress1 = InetAddress.getByAddress(new byte[] {1,2,3,4}); + final InetAddress remoteAddress2 = InetAddress.getByAddress(new byte[] {5,6,7,8}); final IOException cause = new IOException(); - final HttpHostConnectException ctx = new HttpHostConnectException(target, remoteAddress, cause); - Assert.assertEquals("Connect to localhost (/1.2.3.4) refused", ctx.getMessage()); + final HttpHostConnectException ctx = new HttpHostConnectException(cause, target, + remoteAddress1, remoteAddress2); + Assert.assertEquals("Connect to localhost [/1.2.3.4, /5.6.7.8] refused", ctx.getMessage()); } @Test Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java?rev=1489438&r1=1489437&r2=1489438&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/conn/TestHttpClientConnectionOperator.java Tue Jun 4 13:27:02 2013 @@ -27,15 +27,18 @@ package org.apache.http.impl.conn; +import java.net.ConnectException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; +import java.net.SocketTimeoutException; import org.apache.http.HttpHost; import org.apache.http.config.Lookup; import org.apache.http.config.SocketConfig; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.DnsResolver; +import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.SchemePortResolver; import org.apache.http.conn.ManagedHttpClientConnection; import org.apache.http.conn.UnsupportedSchemeException; @@ -119,6 +122,28 @@ public class TestHttpClientConnectionOpe } @Test(expected=ConnectTimeoutException.class) + public void testConnectTimeout() throws Exception { + final HttpContext context = new BasicHttpContext(); + final HttpHost host = new HttpHost("somehost"); + final InetAddress ip1 = InetAddress.getByAddress(new byte[] {10, 0, 0, 1}); + final InetAddress ip2 = InetAddress.getByAddress(new byte[] {10, 0, 0, 2}); + + Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] { ip1, ip2 }); + Mockito.when(socketFactoryRegistry.lookup("http")).thenReturn(plainSocketFactory); + Mockito.when(schemePortResolver.resolve(host)).thenReturn(80); + Mockito.when(plainSocketFactory.createSocket(Mockito.any())).thenReturn(socket); + Mockito.when(plainSocketFactory.connectSocket( + Mockito.anyInt(), + Mockito.any(), + Mockito.any(), + Mockito.any(), + Mockito.any(), + Mockito.any())).thenThrow(new SocketTimeoutException()); + + connectionOperator.connect(conn, host, null, 1000, SocketConfig.DEFAULT, context); + } + + @Test(expected=HttpHostConnectException.class) public void testConnectFailure() throws Exception { final HttpContext context = new BasicHttpContext(); final HttpHost host = new HttpHost("somehost"); @@ -135,7 +160,7 @@ public class TestHttpClientConnectionOpe Mockito.any(), Mockito.any(), Mockito.any(), - Mockito.any())).thenThrow(new ConnectTimeoutException()); + Mockito.any())).thenThrow(new ConnectException()); connectionOperator.connect(conn, host, null, 1000, SocketConfig.DEFAULT, context); } @@ -158,7 +183,7 @@ public class TestHttpClientConnectionOpe Mockito.any(), Mockito.eq(new InetSocketAddress(ip1, 80)), Mockito.any(), - Mockito.any())).thenThrow(new ConnectTimeoutException()); + Mockito.any())).thenThrow(new ConnectException()); Mockito.when(plainSocketFactory.connectSocket( Mockito.anyInt(), Mockito.any(),