HTTPCLIENT-643: Automatic connect fail-over for multi-home remote servers

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@619163 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2008-02-06 21:41:06 +00:00
parent 634f7c49e6
commit aa020a5c0d
4 changed files with 127 additions and 8 deletions

View File

@ -1,6 +1,9 @@
Changes since 4.0 Alpha 2
-------------------
* [HTTPCLIENT-643] Automatic connect fail-over for multi-home remote servers.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-735] unsetting of DEFAULT_PROXY and FORCED_ROUTE in hierarchies
Contributed by Roland Weber <rolandw at apache.org>

View File

@ -36,6 +36,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.apache.http.conn.util.SocketUtils;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
@ -89,9 +90,6 @@ public final class PlainSocketFactory implements SocketFactory {
throw new IllegalArgumentException("Parameters may not be null.");
}
// resolve the target hostname first
final InetSocketAddress target = new InetSocketAddress(host, port);
if (sock == null)
sock = createSocket();
@ -107,7 +105,8 @@ public final class PlainSocketFactory implements SocketFactory {
}
int timeout = HttpConnectionParams.getConnectionTimeout(params);
sock.connect(target, timeout);
SocketUtils.connect(sock, host, port, timeout);
return sock;

View File

@ -32,6 +32,7 @@
package org.apache.http.conn.ssl;
import org.apache.http.conn.LayeredSocketFactory;
import org.apache.http.conn.util.SocketUtils;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
@ -272,9 +273,6 @@ public class SSLSocketFactory implements LayeredSocketFactory {
throw new IllegalArgumentException("Parameters may not be null.");
}
// resolve the target hostname first
final InetSocketAddress target = new InetSocketAddress(host, port);
SSLSocket sslock = (SSLSocket)
((sock != null) ? sock : createSocket());
@ -292,7 +290,8 @@ public class SSLSocketFactory implements LayeredSocketFactory {
int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
int soTimeout = HttpConnectionParams.getSoTimeout(params);
sslock.connect(target, connTimeout);
SocketUtils.connect(sock, host, port, connTimeout);
sslock.setSoTimeout(soTimeout);
try {
hostnameVerifier.verify(host, sslock);

View File

@ -0,0 +1,118 @@
/*
* $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.util;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SocketUtils {
/**
* Attempts to connects the socket to any of the {@link InetAddress}es the
* given host name resolves to. If connection to all addresses fail, the
* last I/O exception is propagated to the caller.
*
* @param sock socket to connect to any of the given addresses
* @param hostname Host name to connect to
* @param port the port to connect to
* @param timeout connection timeout
*
* @throws IOException if an error occurs during the connection
* @throws SocketTimeoutException if timeout expires before connecting
*/
public static void connect(
final Socket sock,
final String hostname,
int port,
int timeout) throws IOException {
InetAddress[] adrs = InetAddress.getAllByName(hostname);
List<InetAddress> list = new ArrayList<InetAddress>(adrs.length);
for (InetAddress adr: adrs) {
list.add(adr);
}
Collections.shuffle(list);
connect(sock, list, port, timeout);
}
/**
* Attempts to connects the socket to any of the {@link InetAddress}es given as a
* parameter, whichever succeeds first. If connection to all addresses fail, the
* last I/O exception is propagated to the caller.
*
* @param sock socket to connect to any of the given addresses
* @param addresses array of addresses
* @param port the port to connect to
* @param timeout connection timeout
*
* @throws IOException if an error occurs during the connection
* @throws SocketTimeoutException if timeout expires before connecting
*/
public static void connect(
final Socket sock,
final List<InetAddress> addresses,
int port,
int timeout) throws IOException {
if (sock == null) {
throw new IllegalArgumentException("Socket may not be null");
}
if (addresses == null) {
throw new IllegalArgumentException("List of addresses may not be null");
}
if (addresses.isEmpty()) {
throw new IllegalArgumentException("List of addresses may not be empty");
}
IOException lastEx = null;
for (InetAddress address: addresses) {
try {
sock.connect(new InetSocketAddress(address, port), timeout);
return;
} catch (SocketTimeoutException ex) {
throw ex;
} catch (IOException ex) {
// keep the last exception and retry
lastEx = ex;
}
}
if (lastEx != null) {
throw lastEx;
}
}
}