Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HTTP/1.1 unless HTTPS scheme is in URL #32

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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