summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorn.christian@seime.no>2019-04-08 10:25:29 +0200
committerGitHub <noreply@github.com>2019-04-08 10:25:29 +0200
commitb56b193c02c4d5964b1b3668a76715eb44180b19 (patch)
tree06ad2aabfbfeb8ee3f3f4977dab53e67492d2222
parente3467597d1e5b27b94b2f3f2b392068cf827495c (diff)
parent09246b99a15117f682f542633d44f88832b29ee9 (diff)
Merge pull request #9035 from vespa-engine/bjorncs/http-utils
Bjorncs/http utils
-rw-r--r--http-utils/OWNERS1
-rw-r--r--http-utils/README.md1
-rw-r--r--http-utils/pom.xml52
-rw-r--r--http-utils/src/main/java/ai/vespa/util/http/VespaHttpClientBuilder.java (renamed from security-utils/src/main/java/com/yahoo/security/tls/https/VespaHttpClientBuilder.java)32
-rw-r--r--http-utils/src/test/java/ai/vespa/util/http/VespaHttpClientBuilderTest.java (renamed from security-utils/src/test/java/com/yahoo/security/tls/https/VespaHttpClientBuilderTest.java)4
-rw-r--r--jrt/pom.xml10
-rw-r--r--pom.xml1
-rw-r--r--security-utils/pom.xml10
8 files changed, 86 insertions, 25 deletions
diff --git a/http-utils/OWNERS b/http-utils/OWNERS
new file mode 100644
index 00000000000..569bf1cc3a1
--- /dev/null
+++ b/http-utils/OWNERS
@@ -0,0 +1 @@
+bjorncs
diff --git a/http-utils/README.md b/http-utils/README.md
new file mode 100644
index 00000000000..8bbe2cd7d05
--- /dev/null
+++ b/http-utils/README.md
@@ -0,0 +1 @@
+# Http utilities for Java \ No newline at end of file
diff --git a/http-utils/pom.xml b/http-utils/pom.xml
new file mode 100644
index 00000000000..aea402aef87
--- /dev/null
+++ b/http-utils/pom.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+<!-- Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>parent</artifactId>
+ <version>7-SNAPSHOT</version>
+ <relativePath>../parent/pom.xml</relativePath>
+ </parent>
+ <artifactId>http-utils</artifactId>
+ <packaging>jar</packaging>
+ <version>7-SNAPSHOT</version>
+ <dependencies>
+ <!-- provided -->
+ <dependency>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>security-utils</artifactId>
+ <version>${project.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- compile scope -->
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpcore</artifactId>
+ <scope>compile</scope>
+ </dependency>
+
+ <!-- test scope -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/security-utils/src/main/java/com/yahoo/security/tls/https/VespaHttpClientBuilder.java b/http-utils/src/main/java/ai/vespa/util/http/VespaHttpClientBuilder.java
index 9fa51fc36cb..5e7a9441fc8 100644
--- a/security-utils/src/main/java/com/yahoo/security/tls/https/VespaHttpClientBuilder.java
+++ b/http-utils/src/main/java/ai/vespa/util/http/VespaHttpClientBuilder.java
@@ -1,5 +1,5 @@
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.security.tls.https;
+package ai.vespa.util.http;
import com.yahoo.security.tls.MixedMode;
import com.yahoo.security.tls.TlsContext;
@@ -8,10 +8,15 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import javax.net.ssl.SSLParameters;
@@ -34,19 +39,33 @@ public class VespaHttpClientBuilder {
private static final Logger log = Logger.getLogger(VespaHttpClientBuilder.class.getName());
public interface ConnectionManagerFactory {
- HttpClientConnectionManager create(SSLConnectionSocketFactory sslSocketFactory);
+ HttpClientConnectionManager create(Registry<ConnectionSocketFactory> socketFactoryRegistry);
}
private VespaHttpClientBuilder() {}
+ /**
+ * Create a client builder with default connection manager.
+ */
public static HttpClientBuilder create() {
return createBuilder(null);
}
+ /**
+ * Create a client builder with a user specified connection manager.
+ */
public static HttpClientBuilder create(ConnectionManagerFactory connectionManagerFactory) {
return createBuilder(connectionManagerFactory);
}
+ /**
+ * Creates a client builder with a {@link BasicHttpClientConnectionManager} configured.
+ * This connection manager uses a single connection for all requests. See Javadoc for details.
+ */
+ public static HttpClientBuilder createWithBasicConnectionManager() {
+ return createBuilder(BasicHttpClientConnectionManager::new);
+ }
+
private static HttpClientBuilder createBuilder(ConnectionManagerFactory connectionManagerFactory) {
var builder = HttpClientBuilder.create();
addSslSocketFactory(builder, connectionManagerFactory);
@@ -60,7 +79,7 @@ public class VespaHttpClientBuilder {
log.log(Level.FINE, "Adding ssl socket factory to client");
SSLConnectionSocketFactory socketFactory = createSslSocketFactory(tlsContext);
if (connectionManagerFactory != null) {
- builder.setConnectionManager(connectionManagerFactory.create(socketFactory));
+ builder.setConnectionManager(connectionManagerFactory.create(createRegistry(socketFactory)));
} else {
builder.setSSLSocketFactory(socketFactory);
}
@@ -80,6 +99,13 @@ public class VespaHttpClientBuilder {
return new SSLConnectionSocketFactory(tlsContext.context(), parameters.getProtocols(), parameters.getCipherSuites(), new NoopHostnameVerifier());
}
+ private static Registry<ConnectionSocketFactory> createRegistry(SSLConnectionSocketFactory sslSocketFactory) {
+ return RegistryBuilder.<ConnectionSocketFactory>create()
+ .register("https", sslSocketFactory)
+ .register("http", PlainConnectionSocketFactory.getSocketFactory())
+ .build();
+ }
+
static class HttpToHttpsRewritingRequestInterceptor implements HttpRequestInterceptor {
@Override
public void process(HttpRequest request, HttpContext context) {
diff --git a/security-utils/src/test/java/com/yahoo/security/tls/https/VespaHttpClientBuilderTest.java b/http-utils/src/test/java/ai/vespa/util/http/VespaHttpClientBuilderTest.java
index 10b8458359c..7ffd0e459b0 100644
--- a/security-utils/src/test/java/com/yahoo/security/tls/https/VespaHttpClientBuilderTest.java
+++ b/http-utils/src/test/java/ai/vespa/util/http/VespaHttpClientBuilderTest.java
@@ -1,7 +1,7 @@
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.security.tls.https;
+package ai.vespa.util.http;
-import com.yahoo.security.tls.https.VespaHttpClientBuilder.HttpToHttpsRewritingRequestInterceptor;
+import ai.vespa.util.http.VespaHttpClientBuilder.HttpToHttpsRewritingRequestInterceptor;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.protocol.BasicHttpContext;
import org.junit.Test;
diff --git a/jrt/pom.xml b/jrt/pom.xml
index e9383654e30..5208c0417cc 100644
--- a/jrt/pom.xml
+++ b/jrt/pom.xml
@@ -34,16 +34,6 @@
<artifactId>security-utils</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
- <exclusions>
- <exclusion> <!-- not needed -->
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- </exclusion>
- <exclusion> <!-- not needed -->
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpcore</artifactId>
- </exclusion>
- </exclusions>
</dependency>
<dependency> <!-- required due to bug in maven dependency resolving - bouncycastle is compile scope in security-utils, yet it is not part of test scope here -->
<groupId>org.bouncycastle</groupId>
diff --git a/pom.xml b/pom.xml
index 7efcab645e1..1d98cc274f8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -79,6 +79,7 @@
<module>filedistribution</module>
<module>flags</module>
<module>fsa</module>
+ <module>http-utils</module>
<module>indexinglanguage</module>
<module>jaxrs_client_utils</module>
<module>jaxrs_utils</module>
diff --git a/security-utils/pom.xml b/security-utils/pom.xml
index f7704762250..10dec598915 100644
--- a/security-utils/pom.xml
+++ b/security-utils/pom.xml
@@ -31,16 +31,6 @@
<artifactId>jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpcore</artifactId>
- <scope>compile</scope>
- </dependency>
<!-- test scope -->
<dependency>