hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [3/3] httpcomponents-core git commit: Added default HttpHost to InetSocketAddress resolver
Date Wed, 25 Oct 2017 14:58:50 GMT
Added default HttpHost to InetSocketAddress resolver


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/aabcf754
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/aabcf754
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/aabcf754

Branch: refs/heads/master
Commit: aabcf75442080ce915a566011cb26e92068b3658
Parents: a3f112a
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Wed Oct 25 13:19:51 2017 +0200
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Wed Oct 25 13:19:51 2017 +0200

----------------------------------------------------------------------
 .../testing/classic/ClassicTestClient.java      |  4 +-
 .../core5/http/impl/DefaultAddressResolver.java | 58 ++++++++++++++++++++
 .../http/impl/bootstrap/AsyncRequester.java     | 24 +++-----
 .../http/impl/bootstrap/HttpAsyncRequester.java |  3 +-
 .../http/impl/bootstrap/HttpRequester.java      | 28 +++-------
 .../http/impl/bootstrap/RequesterBootstrap.java |  4 +-
 6 files changed, 82 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aabcf754/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
index 8d7c799..fc81197 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
@@ -41,6 +41,7 @@ import org.apache.hc.core5.http.config.H1Config;
 import org.apache.hc.core5.http.config.SocketConfig;
 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.hc.core5.http.impl.HttpProcessors;
+import org.apache.hc.core5.http.impl.DefaultAddressResolver;
 import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
 import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory;
 import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
@@ -97,7 +98,8 @@ public class ClassicTestClient {
                     connPool,
                     socketConfig,
                     new DefaultBHttpClientConnectionFactory(H1Config.DEFAULT, CharCodingConfig.DEFAULT),
-                    sslContext != null ? sslContext.getSocketFactory() : null);
+                    sslContext != null ? sslContext.getSocketFactory() : null,
+                    DefaultAddressResolver.INSTANCE);
             requesterRef.compareAndSet(null, requester);
         } else {
             throw new IllegalStateException("Requester has already been started");

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aabcf754/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultAddressResolver.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultAddressResolver.java
b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultAddressResolver.java
new file mode 100644
index 0000000..e1563d3
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/DefaultAddressResolver.java
@@ -0,0 +1,58 @@
+/*
+ * ====================================================================
+ * 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.hc.core5.http.impl;
+
+import java.net.InetSocketAddress;
+
+import org.apache.hc.core5.function.Resolver;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.URIScheme;
+
+public final class DefaultAddressResolver implements Resolver<HttpHost, InetSocketAddress>
{
+
+    public static final DefaultAddressResolver INSTANCE = new DefaultAddressResolver();
+
+    @Override
+    public InetSocketAddress resolve(final HttpHost host) {
+        if (host == null) {
+            return null;
+        }
+        int port = host.getPort();
+        if (port < 0) {
+            final String scheme = host.getSchemeName();
+            if (URIScheme.HTTP.same(scheme)) {
+                port = 80;
+            } else if (URIScheme.HTTPS.same(scheme)) {
+                port = 443;
+            }
+        }
+        final String hostName = host.getHostName();
+        return new InetSocketAddress(hostName, port);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aabcf754/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequester.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequester.java
b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequester.java
index c0270c1..5336ca5 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequester.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequester.java
@@ -37,8 +37,9 @@ import org.apache.hc.core5.concurrent.DefaultThreadFactory;
 import org.apache.hc.core5.concurrent.FutureCallback;
 import org.apache.hc.core5.function.Callback;
 import org.apache.hc.core5.function.Decorator;
+import org.apache.hc.core5.function.Resolver;
 import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.URIScheme;
+import org.apache.hc.core5.http.impl.DefaultAddressResolver;
 import org.apache.hc.core5.io.ShutdownType;
 import org.apache.hc.core5.net.NamedEndpoint;
 import org.apache.hc.core5.reactor.ConnectionInitiator;
@@ -56,13 +57,15 @@ import org.apache.hc.core5.util.TimeValue;
 public class AsyncRequester implements IOReactorService, ConnectionInitiator {
 
     private final DefaultConnectingIOReactor ioReactor;
+    private final Resolver<HttpHost, InetSocketAddress> addressResolver;
 
     public AsyncRequester(
             final IOEventHandlerFactory eventHandlerFactory,
             final IOReactorConfig ioReactorConfig,
             final Decorator<IOSession> ioSessionDecorator,
             final IOSessionListener sessionListener,
-            final Callback<IOSession> sessionShutdownCallback) {
+            final Callback<IOSession> sessionShutdownCallback,
+            final Resolver<HttpHost, InetSocketAddress> addressResolver) {
         this.ioReactor = new DefaultConnectingIOReactor(
                 eventHandlerFactory,
                 ioReactorConfig,
@@ -70,20 +73,7 @@ public class AsyncRequester implements IOReactorService, ConnectionInitiator
{
                 ioSessionDecorator,
                 sessionListener,
                 sessionShutdownCallback);
-    }
-
-    private InetSocketAddress toSocketAddress(final HttpHost host) {
-        int port = host.getPort();
-        if (port < 0) {
-            final String scheme = host.getSchemeName();
-            if (URIScheme.HTTP.same(scheme)) {
-                port = 80;
-            } else if (URIScheme.HTTPS.same(scheme)) {
-                port = 443;
-            }
-        }
-        final String hostName = host.getHostName();
-        return new InetSocketAddress(hostName, port);
+        this.addressResolver = addressResolver != null ? addressResolver : DefaultAddressResolver.INSTANCE;
     }
 
     @Override
@@ -104,7 +94,7 @@ public class AsyncRequester implements IOReactorService, ConnectionInitiator
{
             final FutureCallback<IOSession> callback) {
         Args.notNull(host, "Host");
         Args.notNull(timeout, "Timeout");
-        return connect(host, toSocketAddress(host), null, timeout, attachment, callback);
+        return connect(host, addressResolver.resolve(host), null, timeout, attachment, callback);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aabcf754/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
index 0b4e0d2..e6de70b 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpAsyncRequester.java
@@ -47,6 +47,7 @@ import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.URIScheme;
+import org.apache.hc.core5.http.impl.DefaultAddressResolver;
 import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncRequestProducer;
@@ -97,7 +98,7 @@ public class HttpAsyncRequester extends AsyncRequester implements ConnPoolContro
                 session.addFirst(new ShutdownCommand(ShutdownType.GRACEFUL));
             }
 
-        });
+        }, DefaultAddressResolver.INSTANCE);
         this.connPool = Args.notNull(connPool, "Connection pool");
         this.tlsStrategy = tlsStrategy;
     }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aabcf754/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
index 65dac19..c02d297 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
@@ -40,6 +40,7 @@ import java.util.concurrent.atomic.AtomicReference;
 
 import javax.net.ssl.SSLSocketFactory;
 
+import org.apache.hc.core5.function.Resolver;
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.ConnectionClosedException;
@@ -51,6 +52,7 @@ import org.apache.hc.core5.http.URIScheme;
 import org.apache.hc.core5.http.config.CharCodingConfig;
 import org.apache.hc.core5.http.config.H1Config;
 import org.apache.hc.core5.http.config.SocketConfig;
+import org.apache.hc.core5.http.impl.DefaultAddressResolver;
 import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory;
 import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
 import org.apache.hc.core5.http.io.EofSensorInputStream;
@@ -85,6 +87,7 @@ public class HttpRequester implements ConnPoolControl<HttpHost>, GracefullyClose
     private final SocketConfig socketConfig;
     private final HttpConnectionFactory<? extends HttpClientConnection> connectFactory;
     private final SSLSocketFactory sslSocketFactory;
+    private final Resolver<HttpHost, InetSocketAddress> addressResolver;
 
     public HttpRequester(
             final HttpRequestExecutor requestExecutor,
@@ -92,7 +95,8 @@ public class HttpRequester implements ConnPoolControl<HttpHost>, GracefullyClose
             final ManagedConnPool<HttpHost, HttpClientConnection> connPool,
             final SocketConfig socketConfig,
             final HttpConnectionFactory<? extends HttpClientConnection> connectFactory,
-            final SSLSocketFactory sslSocketFactory) {
+            final SSLSocketFactory sslSocketFactory,
+            final Resolver<HttpHost, InetSocketAddress> addressResolver) {
         this.requestExecutor = Args.notNull(requestExecutor, "Request executor");
         this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
         this.connPool = Args.notNull(connPool, "Connection pool");
@@ -100,6 +104,7 @@ public class HttpRequester implements ConnPoolControl<HttpHost>,
GracefullyClose
         this.connectFactory = connectFactory != null ? connectFactory : new DefaultBHttpClientConnectionFactory(
                 H1Config.DEFAULT, CharCodingConfig.DEFAULT);
         this.sslSocketFactory = sslSocketFactory != null ? sslSocketFactory : (SSLSocketFactory)
SSLSocketFactory.getDefault();
+        this.addressResolver = addressResolver != null ? addressResolver : DefaultAddressResolver.INSTANCE;
     }
 
     @Override
@@ -229,25 +234,10 @@ public class HttpRequester implements ConnPoolControl<HttpHost>,
GracefullyClose
             sock.setSoLinger(true, linger);
         }
 
-        final String scheme = targetHost.getSchemeName();
-        int port = targetHost.getPort();
-        if (port < 0) {
-            if (URIScheme.HTTP.same(scheme)) {
-                port = 80;
-            } else if (URIScheme.HTTPS.same(scheme)) {
-                port = 443;
-            }
-        }
-        final InetSocketAddress targetAddress;
-        if (targetHost.getAddress() != null) {
-            targetAddress = new InetSocketAddress(targetHost.getAddress(), port);
-        } else {
-            targetAddress = new InetSocketAddress(targetHost.getHostName(), port);
-        }
+        final InetSocketAddress targetAddress = addressResolver.resolve(targetHost);
         sock.connect(targetAddress, socketConfig.getSoTimeout().toMillisIntBound());
-
-        if (URIScheme.HTTPS.same(scheme)) {
-            return sslSocketFactory.createSocket(sock, targetHost.getHostName(), port, true);
+        if (URIScheme.HTTPS.same(targetHost.getSchemeName())) {
+            return sslSocketFactory.createSocket(sock, targetHost.getHostName(), targetAddress.getPort(),
true);
         } else {
             return sock;
         }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/aabcf754/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
index 7a5cebd..cb6700a 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java
@@ -34,6 +34,7 @@ import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.config.CharCodingConfig;
 import org.apache.hc.core5.http.config.H1Config;
 import org.apache.hc.core5.http.config.SocketConfig;
+import org.apache.hc.core5.http.impl.DefaultAddressResolver;
 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.hc.core5.http.impl.Http1StreamListener;
 import org.apache.hc.core5.http.impl.HttpProcessors;
@@ -176,7 +177,8 @@ public class RequesterBootstrap {
                 socketConfig != null ? socketConfig : SocketConfig.DEFAULT,
                 connectFactory != null ? connectFactory : new DefaultBHttpClientConnectionFactory(
                         H1Config.DEFAULT, CharCodingConfig.DEFAULT),
-                sslSocketFactory);
+                sslSocketFactory,
+                DefaultAddressResolver.INSTANCE);
     }
 
 }


Mime
View raw message