HTTPCLIENT-697: Throw a more intelligible exception when connection to a remote host cannot be established.

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
This commit is contained in:
Oleg Kalnichevski 2007-10-28 16:56:32 +00:00
parent a8e875970d
commit e445d72146
3 changed files with 75 additions and 11 deletions

View File

@ -1,5 +1,9 @@
Changes since release 4.0 Alpha 1 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 <olegk at apache.org>
* [HTTPCLIENT-689] Caching of SimpleDateFormat in DateUtils * [HTTPCLIENT-689] Caching of SimpleDateFormat in DateUtils
Contributed by Daniel Müller <strider at digitalstrider.com> Contributed by Daniel Müller <strider at digitalstrider.com>

View File

@ -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
* <http://www.apache.org/>.
*
*/
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;
}
}

View File

@ -32,6 +32,7 @@
package org.apache.http.impl.conn; package org.apache.http.impl.conn;
import java.io.IOException; import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket; import java.net.Socket;
import java.net.InetAddress; import java.net.InetAddress;
@ -40,6 +41,7 @@ import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.conn.Scheme; import org.apache.http.conn.Scheme;
import org.apache.http.conn.SchemeRegistry; import org.apache.http.conn.SchemeRegistry;
import org.apache.http.conn.SocketFactory; import org.apache.http.conn.SocketFactory;
@ -128,9 +130,13 @@ public class DefaultClientConnectionOperator
Socket sock = sf.createSocket(); Socket sock = sf.createSocket();
conn.announce(sock); conn.announce(sock);
try {
sock = sf.connectSocket(sock, target.getHostName(), sock = sf.connectSocket(sock, target.getHostName(),
schm.resolvePort(target.getPort()), schm.resolvePort(target.getPort()),
local, 0, params); local, 0, params);
} catch (ConnectException ex) {
throw new HttpHostConnectException(target, ex);
}
prepareSocket(sock, context, params); prepareSocket(sock, context, params);
final boolean secure = sf.isSecure(sock); final boolean secure = sf.isSecure(sock);
@ -180,15 +186,16 @@ public class DefaultClientConnectionOperator
") must have layered socket factory."); ") must have layered socket factory.");
} }
final LayeredSocketFactory ssf = final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory();
(LayeredSocketFactory)schm.getSocketFactory(); final Socket sock;
final Socket sock = ssf.createSocket try {
sock = lsf.createSocket
(conn.getSocket(), target.getHostName(), target.getPort(), true); (conn.getSocket(), target.getHostName(), target.getPort(), true);
} catch (ConnectException ex) {
throw new HttpHostConnectException(target, ex);
}
prepareSocket(sock, context, params); prepareSocket(sock, context, params);
conn.update(sock, target, lsf.isSecure(sock), params);
final boolean secure = ssf.isSecure(sock);
conn.update(sock, target, secure, params);
//@@@ error handling: close the layered socket in case of exception? //@@@ error handling: close the layered socket in case of exception?
} // updateSecureConnection } // updateSecureConnection