HTTPCLIENT-670: Pluggable hostname resolver

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@659194 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2008-05-22 18:33:47 +00:00
parent 5cd9d169f2
commit a89695fea4
4 changed files with 77 additions and 10 deletions

View File

@ -1,6 +1,9 @@
Changes since 4.0 Alpha 4
-------------------
* [HTTPCLIENT-670] Pluggable hostname resolver.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-719] Clone support for HTTP request and cookie objects.
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.http.conn.scheme;
import java.io.IOException;
import java.net.InetAddress;
public interface HostNameResolver {
InetAddress resolve (String hostname) throws IOException;
}

View File

@ -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;

View File

@ -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 {