From d855722988cd19867f500f0909565ddc156b6159 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 14 Feb 2009 17:01:38 +0000 Subject: [PATCH] HTTPCLIENT-822: Default socket factories to re-throw SocketTimeoutException as ConnectTimeoutException in case of connect failure due to time out git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@744525 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 15 ++++++--- .../conn/MultihomePlainSocketFactory.java | 31 ++----------------- .../http/conn/scheme/PlainSocketFactory.java | 10 ++++-- .../http/conn/ssl/SSLSocketFactory.java | 10 ++++-- 4 files changed, 28 insertions(+), 38 deletions(-) diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index c3688a7b0..8da91e1c0 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,3 +1,14 @@ +Changes since 4.0 beta 2 +------------------- + +* [HTTPCLIENT-822] Default socket factories to rethrow SocketTimeoutException as + ConnectTimeoutException in case of connect failure due to a time out. + Contributed by Oleg Kalnichevski + +* [HTTPCLIENT-813] Fixed default port resolution. Invalid ports no longer + get replaced with the default port value. + Contributed by Oleg Kalnichevski + Release 4.0 beta 2 ------------------- @@ -9,10 +20,6 @@ bundle combining HttpClient and HttpMime jars. All upstream projects are strongly encouraged to upgrade. -* [HTTPCLIENT-813] Fixed default port resolution. Invalid ports no longer - get replaced with the default port value. - Contributed by Oleg Kalnichevski - * Fixed NPE in DefaultRequestDirector thrown when retrying a failed request over a proxied connection. Contributed by Oleg Kalnichevski diff --git a/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java b/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java index ab19dbc2f..24a2bae6b 100644 --- a/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java +++ b/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java @@ -41,7 +41,6 @@ import java.util.Collections; import java.util.List; import java.util.Arrays; -import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.SocketFactory; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; @@ -131,12 +130,12 @@ public final class MultihomePlainSocketFactory implements SocketFactory { Collections.shuffle(addresses); IOException lastEx = null; - for (InetAddress address: addresses) { + for (InetAddress remoteAddress: addresses) { try { - sock.connect(new InetSocketAddress(address, port), timeout); + sock.connect(new InetSocketAddress(remoteAddress, port), timeout); break; } catch (SocketTimeoutException ex) { - throw ex; + throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); } catch (IOException ex) { // create new socket sock = new Socket(); @@ -185,28 +184,4 @@ public final class MultihomePlainSocketFactory implements SocketFactory { } // isSecure - - /** - * Compares this factory with an object. - * There is only one instance of this class. - * - * @param obj the object to compare with - * - * @return iff the argument is this object - */ - @Override - public boolean equals(Object obj) { - return (obj == this); - } - - /** - * Obtains a hash code for this object. - * All instances of this class have the same hash code. - * There is only one instance of this class. - */ - @Override - public int hashCode() { - return PlainSocketFactory.class.hashCode(); - } - } diff --git a/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java b/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java index 7680341a4..5558cea75 100644 --- a/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java +++ b/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java @@ -35,7 +35,9 @@ import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; +import java.net.SocketTimeoutException; +import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; @@ -115,9 +117,11 @@ public final class PlainSocketFactory implements SocketFactory { } else { remoteAddress = new InetSocketAddress(host, port); } - - sock.connect(remoteAddress, timeout); - + try { + sock.connect(remoteAddress, timeout); + } catch (SocketTimeoutException ex) { + throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); + } return sock; } // connectSocket diff --git a/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java b/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java index 5f90185e9..04b3e42df 100644 --- a/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java +++ b/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java @@ -31,6 +31,7 @@ package org.apache.http.conn.ssl; +import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.scheme.HostNameResolver; import org.apache.http.conn.scheme.LayeredSocketFactory; import org.apache.http.params.HttpConnectionParams; @@ -47,6 +48,7 @@ import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; +import java.net.SocketTimeoutException; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.KeyStore; @@ -318,9 +320,11 @@ public class SSLSocketFactory implements LayeredSocketFactory { } else { remoteAddress = new InetSocketAddress(host, port); } - - sslsock.connect(remoteAddress, connTimeout); - + try { + sock.connect(remoteAddress, connTimeout); + } catch (SocketTimeoutException ex) { + throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out"); + } sslsock.setSoTimeout(soTimeout); try { hostnameVerifier.verify(host, sslsock);