diff options
author | Morten Tokle <morten.tokle@gmail.com> | 2018-02-02 12:38:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-02 12:38:15 +0100 |
commit | 468e0e16a60a5feaf6d5eec971ff06078b6bb694 (patch) | |
tree | dd00a634732f9b98340adb7bf389d956eba74f45 | |
parent | b6e4d2af8c058edc1487c3ca49262cac5502e043 (diff) | |
parent | 200571ef6abc9ac5594624a12f6e7fb97da96f60 (diff) |
Merge pull request #4893 from vespa-engine/bjorncs/vespa-http-client
Bjorncs/vespa http client
3 files changed, 37 insertions, 3 deletions
diff --git a/vespa-http-client/pom.xml b/vespa-http-client/pom.xml index c336c641352..44f27fe1b6c 100644 --- a/vespa-http-client/pom.xml +++ b/vespa-http-client/pom.xml @@ -59,6 +59,11 @@ <version>${project.version}</version> </dependency> <dependency> + <groupId>com.yahoo.vespa</groupId> + <artifactId>component</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.8.4</version> diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java index 3fe42b21e93..d5f362ce1d1 100644 --- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java +++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java @@ -1,11 +1,14 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.http.client.config; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.google.common.annotations.Beta; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; import net.jcip.annotations.Immutable; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import java.util.Collection; import java.util.Collections; @@ -32,6 +35,7 @@ public final class ConnectionParams { */ public static final class Builder { private SSLContext sslContext = null; + private HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.getDefaultHostnameVerifier(); private long connectionTimeout = TimeUnit.SECONDS.toMillis(60); private final Multimap<String, String> headers = ArrayListMultimap.create(); private final Map<String, HeaderProvider> headerProviders = new HashMap<>(); @@ -60,6 +64,18 @@ public final class ConnectionParams { } /** + * Sets the {@link HostnameVerifier} for the connection to the gateway when SSL is enabled for Endpoint. + * Defaults to instance returned by {@link SSLConnectionSocketFactory#getDefaultHostnameVerifier()}. + * + * @param hostnameVerifier hostname verifier for connection to gateway. + * @return pointer to builder. + */ + public Builder setHostnameVerifier(HostnameVerifier hostnameVerifier) { + this.hostnameVerifier = hostnameVerifier; + return this; + } + + /** * Set custom headers to be used * * @param key header name @@ -218,6 +234,7 @@ public final class ConnectionParams { public ConnectionParams build() { return new ConnectionParams( sslContext, + hostnameVerifier, connectionTimeout, headers, headerProviders, @@ -268,8 +285,12 @@ public final class ConnectionParams { return sslContext; } + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } } private final SSLContext sslContext; + private final HostnameVerifier hostnameVerifier; private final long connectionTimeout; private final Multimap<String, String> headers = ArrayListMultimap.create(); private final Map<String, HeaderProvider> headerProviders = new HashMap<>(); @@ -287,6 +308,7 @@ public final class ConnectionParams { private ConnectionParams( SSLContext sslContext, + HostnameVerifier hostnameVerifier, long connectionTimeout, Multimap<String, String> headers, Map<String, HeaderProvider> headerProviders, @@ -302,6 +324,7 @@ public final class ConnectionParams { int traceEveryXOperation, boolean printTraceToStdErr) { this.sslContext = sslContext; + this.hostnameVerifier = hostnameVerifier; this.connectionTimeout = connectionTimeout; this.headers.putAll(headers); this.headerProviders.putAll(headerProviders); @@ -318,10 +341,16 @@ public final class ConnectionParams { this.printTraceToStdErr = printTraceToStdErr; } + @JsonIgnore public SSLContext getSslContext() { return sslContext; } + @JsonIgnore + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } + public Collection<Map.Entry<String, String>> getHeaders() { return Collections.unmodifiableCollection(headers.entries()); } diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java index 6c1d068236d..7cdd260285f 100644 --- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java +++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java @@ -2,6 +2,7 @@ package com.yahoo.vespa.http.client.core.communication; import com.google.common.annotations.Beta; +import com.yahoo.component.Vtag; import com.yahoo.vespa.http.client.config.ConnectionParams; import com.yahoo.vespa.http.client.config.Endpoint; import com.yahoo.vespa.http.client.config.FeedParams; @@ -391,15 +392,14 @@ class ApacheGatewayConnection implements GatewayConnection { if (useSsl && connectionParams.getSslContext() != null) { Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", new SSLConnectionSocketFactory( - // Alternative: SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER - connectionParams.getSslContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier())) + connectionParams.getSslContext(), connectionParams.getHostnameVerifier())) .register("http", PlainConnectionSocketFactory.INSTANCE) .build(); PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry); clientBuilder.setConnectionManager(connMgr); } - clientBuilder.setUserAgent("vespa-http-client"); + clientBuilder.setUserAgent(String.format("vespa-http-client (%s)", Vtag.currentVersion)); clientBuilder.setMaxConnPerRoute(1); clientBuilder.setMaxConnTotal(1); clientBuilder.disableContentCompression(); |