Skip to content

Commit

Permalink
Merge pull request #32 from Bisnode/Prefer-HTTP/1.1-for-non-https
Browse files Browse the repository at this point in the history
Use HTTP/1.1 unless HTTPS scheme is in URL
  • Loading branch information
lukasz-kaminski committed Oct 1, 2020
2 parents d9e74bf + b03698e commit e3c0d73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/com/bisnode/opa/client/OpaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.bisnode.opa.client.rest.OpaRestClient;

import java.net.http.HttpClient;
import java.util.Objects;

/**
* Opa client featuring {@link OpaDataApi}, {@link OpaQueryApi} and {@link OpaPolicyApi}
Expand Down Expand Up @@ -73,7 +74,10 @@ public Builder opaConfiguration(String url) {
}

public OpaClient build() {
HttpClient httpClient = HttpClient.newHttpClient();
Objects.requireNonNull(opaConfiguration, "build() called without opaConfiguration provided");
HttpClient httpClient = HttpClient.newBuilder()
.version(opaConfiguration.getHttpVersion())
.build();
OpaRestClient opaRestClient = new OpaRestClient(opaConfiguration, httpClient, ObjectMapperFactory.getInstance().create());
return new OpaClient(new OpaQueryClient(opaRestClient), new OpaDataClient(opaRestClient), new OpaPolicyClient(opaRestClient));
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/bisnode/opa/client/OpaConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package com.bisnode.opa.client;

import java.beans.ConstructorProperties;
import java.net.URI;
import java.net.http.HttpClient;
import java.util.Objects;

/**
* Contains all configuration needed to set up {@link OpaClient}
*/
public final class OpaConfiguration {
private final String url;
private final HttpClient.Version httpVersion;

/**
* @param url base URL to OPA server, containing protocol, and port (eg. http://localhost:8181)
*/
@ConstructorProperties({"url"})
public OpaConfiguration(String url) {
this.url = url;
this.httpVersion = "https".equals(URI.create(url).getScheme()) ?
HttpClient.Version.HTTP_2 :
HttpClient.Version.HTTP_1_1;
}

/**
* @param url base URL to OPA server, containing protocol, and port (eg. http://localhost:8181)
* @param httpVersion preferred HTTP version to use for the client
*/
@ConstructorProperties({"url", "httpVersion"})
public OpaConfiguration(String url, HttpClient.Version httpVersion) {
this.url = url;
this.httpVersion = httpVersion;
}

/**
Expand All @@ -24,6 +40,16 @@ public String getUrl() {
return this.url;
}

/**
* Get HTTP version configured for the client. If not configured will use HTTP2 for "https" scheme
* and HTTP1.1 for "http" scheme.
*
* @return httpVersion configured for use by the client
*/
public HttpClient.Version getHttpVersion() {
return this.httpVersion;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down

0 comments on commit e3c0d73

Please sign in to comment.