From e445d72146beb7c482f3d12cd6ced92c461c9ac8 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sun, 28 Oct 2007 16:56:32 +0000 Subject: [PATCH] HTTPCLIENT-697: Throw a more intelligible exception when connection to a remote host cannot be established. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Contributed by Oleg Kalnichevski Reviewed by Ortwin Glück git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@589382 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 4 ++ .../http/conn/HttpHostConnectException.java | 53 +++++++++++++++++++ .../conn/DefaultClientConnectionOperator.java | 29 ++++++---- 3 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/conn/HttpHostConnectException.java diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index d4304efb2..44b9022d0 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,5 +1,9 @@ Changes since release 4.0 Alpha 1 +* [HTTPCLIENT-697] Throw a more intelligible exception when connection + to a remote host cannot be established. + Contributed by Oleg Kalnichevski + * [HTTPCLIENT-689] Caching of SimpleDateFormat in DateUtils Contributed by Daniel Müller diff --git a/module-client/src/main/java/org/apache/http/conn/HttpHostConnectException.java b/module-client/src/main/java/org/apache/http/conn/HttpHostConnectException.java new file mode 100644 index 000000000..ebc5233f8 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/conn/HttpHostConnectException.java @@ -0,0 +1,53 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.conn; + +import java.net.ConnectException; + +import org.apache.http.HttpHost; + +public class HttpHostConnectException extends ConnectException { + + private static final long serialVersionUID = -3194482710275220224L; + + private final HttpHost host; + + public HttpHostConnectException(final HttpHost host, final ConnectException cause) { + super("Connection to " + host + " refused"); + this.host = host; + initCause(cause); + } + + public HttpHost getHost() { + return this.host; + } + +} diff --git a/module-client/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java b/module-client/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java index a39742d96..5a7e49915 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/DefaultClientConnectionOperator.java @@ -32,6 +32,7 @@ package org.apache.http.impl.conn; import java.io.IOException; +import java.net.ConnectException; import java.net.Socket; import java.net.InetAddress; @@ -40,6 +41,7 @@ import org.apache.http.params.HttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.protocol.HttpContext; +import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.Scheme; import org.apache.http.conn.SchemeRegistry; import org.apache.http.conn.SocketFactory; @@ -128,9 +130,13 @@ public class DefaultClientConnectionOperator Socket sock = sf.createSocket(); conn.announce(sock); - sock = sf.connectSocket(sock, target.getHostName(), - schm.resolvePort(target.getPort()), - local, 0, params); + try { + sock = sf.connectSocket(sock, target.getHostName(), + schm.resolvePort(target.getPort()), + local, 0, params); + } catch (ConnectException ex) { + throw new HttpHostConnectException(target, ex); + } prepareSocket(sock, context, params); final boolean secure = sf.isSecure(sock); @@ -180,15 +186,16 @@ public class DefaultClientConnectionOperator ") must have layered socket factory."); } - final LayeredSocketFactory ssf = - (LayeredSocketFactory)schm.getSocketFactory(); - final Socket sock = ssf.createSocket - (conn.getSocket(), target.getHostName(), target.getPort(), true); + final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory(); + final Socket sock; + try { + sock = lsf.createSocket + (conn.getSocket(), target.getHostName(), target.getPort(), true); + } catch (ConnectException ex) { + throw new HttpHostConnectException(target, ex); + } prepareSocket(sock, context, params); - - final boolean secure = ssf.isSecure(sock); - - conn.update(sock, target, secure, params); + conn.update(sock, target, lsf.isSecure(sock), params); //@@@ error handling: close the layered socket in case of exception? } // updateSecureConnection