From a89695fea4427a70a5f01993d8899373c0e726f3 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Thu, 22 May 2008 18:33:47 +0000 Subject: [PATCH] HTTPCLIENT-670: Pluggable hostname resolver git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@659194 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 3 ++ .../http/conn/scheme/HostNameResolver.java | 41 +++++++++++++++++++ .../http/conn/scheme/PlainSocketFactory.java | 21 +++++++--- .../http/conn/ssl/SSLSocketFactory.java | 22 +++++++--- 4 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 3e718c393..d33ccaff4 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,9 @@ Changes since 4.0 Alpha 4 ------------------- +* [HTTPCLIENT-670] Pluggable hostname resolver. + Contributed by Oleg Kalnichevski + * [HTTPCLIENT-719] Clone support for HTTP request and cookie objects. Contributed by Oleg Kalnichevski diff --git a/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java b/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java new file mode 100644 index 000000000..ca6615cf6 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java @@ -0,0 +1,41 @@ +/* + * $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.scheme; + +import java.io.IOException; +import java.net.InetAddress; + +public interface HostNameResolver { + + InetAddress resolve (String hostname) throws IOException; + +} 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 48ca22d8a..ecc145df8 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 @@ -53,6 +53,8 @@ public final class PlainSocketFactory implements SocketFactory { private static final PlainSocketFactory DEFAULT_FACTORY = new PlainSocketFactory(); + private final HostNameResolver nameResolver; + /** * Gets the singleton instance of this class. * @return the one and only plain socket factory @@ -61,14 +63,16 @@ public final class PlainSocketFactory implements SocketFactory { return DEFAULT_FACTORY; } - /** - * Restricted default constructor. - */ - private PlainSocketFactory() { + public PlainSocketFactory(final HostNameResolver nameResolver) { super(); + this.nameResolver = nameResolver; } + public PlainSocketFactory() { + this(null); + } + // non-javadoc, see interface org.apache.http.conn.SocketFactory public Socket createSocket() { return new Socket(); @@ -103,7 +107,14 @@ public final class PlainSocketFactory implements SocketFactory { int timeout = HttpConnectionParams.getConnectionTimeout(params); - sock.connect(new InetSocketAddress(host, port), timeout); + InetSocketAddress remoteAddress; + if (this.nameResolver != null) { + remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port); + } else { + remoteAddress = new InetSocketAddress(host, port); + } + + sock.connect(remoteAddress, timeout); return sock; 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 ae952d357..188612db0 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.scheme.HostNameResolver; import org.apache.http.conn.scheme.LayeredSocketFactory; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; @@ -164,6 +165,7 @@ public class SSLSocketFactory implements LayeredSocketFactory { private final SSLContext sslcontext; private final javax.net.ssl.SSLSocketFactory socketfactory; + private final HostNameResolver nameResolver; private X509HostnameVerifier hostnameVerifier = BROWSER_COMPATIBLE_HOSTNAME_VERIFIER; public SSLSocketFactory( @@ -171,7 +173,8 @@ public class SSLSocketFactory implements LayeredSocketFactory { final KeyStore keystore, final String keystorePassword, final KeyStore truststore, - final SecureRandom random) + final SecureRandom random, + final HostNameResolver nameResolver) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { super(); @@ -189,6 +192,7 @@ public class SSLSocketFactory implements LayeredSocketFactory { this.sslcontext = SSLContext.getInstance(algorithm); this.sslcontext.init(keymanagers, trustmanagers, random); this.socketfactory = this.sslcontext.getSocketFactory(); + this.nameResolver = nameResolver; } public SSLSocketFactory( @@ -197,19 +201,19 @@ public class SSLSocketFactory implements LayeredSocketFactory { final KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { - this(TLS, keystore, keystorePassword, truststore, null); + this(TLS, keystore, keystorePassword, truststore, null, null); } public SSLSocketFactory(final KeyStore keystore, final String keystorePassword) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { - this(TLS, keystore, keystorePassword, null, null); + this(TLS, keystore, keystorePassword, null, null, null); } public SSLSocketFactory(final KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { - this(TLS, null, null, truststore, null); + this(TLS, null, null, truststore, null, null); } /** @@ -221,6 +225,7 @@ public class SSLSocketFactory implements LayeredSocketFactory { super(); this.sslcontext = null; this.socketfactory = HttpsURLConnection.getDefaultSSLSocketFactory(); + this.nameResolver = null; } private static KeyManager[] createKeyManagers(final KeyStore keystore, final String password) @@ -289,7 +294,14 @@ public class SSLSocketFactory implements LayeredSocketFactory { int connTimeout = HttpConnectionParams.getConnectionTimeout(params); int soTimeout = HttpConnectionParams.getSoTimeout(params); - sslsock.connect(new InetSocketAddress(host, port), connTimeout); + InetSocketAddress remoteAddress; + if (this.nameResolver != null) { + remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port); + } else { + remoteAddress = new InetSocketAddress(host, port); + } + + sslsock.connect(remoteAddress, connTimeout); sslsock.setSoTimeout(soTimeout); try {