You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2008/05/22 20:33:48 UTC

svn commit: r659194 - in /httpcomponents/httpclient/trunk: ./ module-client/src/main/java/org/apache/http/conn/scheme/ module-client/src/main/java/org/apache/http/conn/ssl/

Author: olegk
Date: Thu May 22 11:33:47 2008
New Revision: 659194

URL: http://svn.apache.org/viewvc?rev=659194&view=rev
Log:
HTTPCLIENT-670: Pluggable hostname resolver

Added:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java
Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=659194&r1=659193&r2=659194&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Thu May 22 11:33:47 2008
@@ -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>
 

Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java?rev=659194&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java (added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/HostNameResolver.java Thu May 22 11:33:47 2008
@@ -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;
+
+}

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java?rev=659194&r1=659193&r2=659194&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/scheme/PlainSocketFactory.java Thu May 22 11:33:47 2008
@@ -53,6 +53,8 @@
     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 @@
         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 @@
 
         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;
 

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java?rev=659194&r1=659193&r2=659194&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java (original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java Thu May 22 11:33:47 2008
@@ -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 @@
     
     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 @@
         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 @@
         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 @@
             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 @@
         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 @@
         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 {