Skip to content

Commit

Permalink
simplified http client config updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Mar 2, 2023
1 parent b8f1cb5 commit a3fc8f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
</developers>

<properties>
<postmark.version>1.9.12</postmark.version>
<postmark.version>1.9.2</postmark.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<jackson.minimum.version>2.9.7</jackson.minimum.version>
<jackson.version>2.14.1</jackson.version>
<jersey.version>2.39</jersey.version>
<junit.jupiter.version>5.8.2</junit.jupiter.version>
<junit.platform.version>1.3.2</junit.platform.version>
</properties>
Expand Down
42 changes: 14 additions & 28 deletions src/main/java/com/postmarkapp/postmark/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
Expand Down Expand Up @@ -32,21 +31,23 @@ public enum DEFAULTS {

private final Map<String,Object> headers;
private CloseableHttpClient client;
// client configuration options like timeouts, forwarding ..
private RequestConfig.Builder clientConfigBuilder;

private boolean secureConnection = true;
private int connectTimeoutSeconds = DEFAULTS.CONNECT_TIMEOUT_SECONDS.value;
private int readTimeoutSeconds = DEFAULTS.READ_TIMEOUT_SECONDS.value;

public HttpClient(Map<String,Object> headers, int connectTimeoutSeconds, int readTimeoutSeconds) {
this.headers = headers;
this.connectTimeoutSeconds = connectTimeoutSeconds;
this.readTimeoutSeconds = readTimeoutSeconds;
buildClientWithCustomConfig();
this.clientConfigBuilder = RequestConfig
.custom()
.setConnectTimeout(Timeout.ofSeconds(connectTimeoutSeconds))
.setResponseTimeout(Timeout.ofSeconds(readTimeoutSeconds));

this.client = buildClient();
}

public HttpClient(Map<String,Object> headers) {
this.headers = headers;
buildClientWithCustomConfig();
this(headers, DEFAULTS.CONNECT_TIMEOUT_SECONDS.value, DEFAULTS.READ_TIMEOUT_SECONDS.value);
}

/**
Expand Down Expand Up @@ -108,13 +109,13 @@ public ClientResponse execute(REQUEST_TYPES requestType, String url, String data
// Setters and Getters

public void setConnectTimeoutSeconds(int connectTimeoutSeconds) {
this.connectTimeoutSeconds = connectTimeoutSeconds;
buildClientWithCustomConfig();
clientConfigBuilder.setConnectTimeout(Timeout.ofSeconds(connectTimeoutSeconds));
this.client = buildClient();
}

public void setReadTimeoutSeconds(int readTimeoutSeconds) {
this.readTimeoutSeconds = readTimeoutSeconds;
buildClientWithCustomConfig();
clientConfigBuilder.setResponseTimeout(Timeout.ofSeconds(readTimeoutSeconds));
this.client = buildClient();
}

public void setSecureConnection(boolean secureConnection) {
Expand All @@ -141,22 +142,7 @@ public CloseableHttpClient getClient() {
* @return initialized HTTP client
*/
private CloseableHttpClient buildClient() {
return HttpClients.createDefault();
}

/**
* Build HTTP client used for requests with custom config settings
*
* @return initialized HTTP client
*/
private void buildClientWithCustomConfig() {
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectTimeout(Timeout.ofSeconds(connectTimeoutSeconds))
.setResponseTimeout(Timeout.ofSeconds(readTimeoutSeconds))
.build();

this.client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
return HttpClientBuilder.create().setDefaultRequestConfig(clientConfigBuilder.build()).build();
}

/**
Expand Down

0 comments on commit a3fc8f7

Please sign in to comment.