Skip to content

Commit

Permalink
TNT-45922 Adding custom http client support for default target client (
Browse files Browse the repository at this point in the history
…#67)

* adding custom http client support for default target client

* removing second test and changing exposed client type

* changing httpClient config

Co-authored-by: Eric Fichtel <[email protected]>
  • Loading branch information
ericfichtel and eric-fichtel-adobe authored Oct 20, 2022
1 parent e9e1579 commit 6f3a136
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/adobe/target/edge/client/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Objects;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.HttpClient;

public class ClientConfig {

Expand Down Expand Up @@ -47,6 +48,7 @@ public class ClientConfig {
private byte[] onDeviceArtifactPayload;
private boolean telemetryEnabled;
private List<String> onDeviceAllMatchingRulesMboxes;
private HttpClient httpClient;

public String getClient() {
return client;
Expand Down Expand Up @@ -92,6 +94,10 @@ public HttpRequestInterceptor getRequestInterceptor() {
return requestInterceptor;
}

public HttpClient getHttpClient() {
return httpClient;
}

public String getUrl(String locationHint) {
if (isNotEmpty(locationHint)) {
return clusterUrlPrefix + locationHint + clusterUrlSuffix;
Expand Down Expand Up @@ -177,6 +183,7 @@ public static final class ClientConfigBuilder {
private byte[] onDeviceArtifactPayload;
private boolean telemetryEnabled = true;
private List<String> onDeviceAllMatchingRulesMboxes;
private HttpClient httpClient;

private ClientConfigBuilder() {}

Expand Down Expand Up @@ -294,6 +301,11 @@ public ClientConfigBuilder onDeviceAllMatchingRulesMboxes(List<String> mboxes) {
return this;
}

public ClientConfigBuilder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

public ClientConfig build() {
ClientConfig clientConfig = new ClientConfig();
Objects.requireNonNull(organizationId, "organization id cannot be null");
Expand Down Expand Up @@ -323,6 +335,7 @@ public ClientConfig build() {
clientConfig.onDeviceArtifactPayload = this.onDeviceArtifactPayload;
clientConfig.onDeviceAllMatchingRulesMboxes = this.onDeviceAllMatchingRulesMboxes;
clientConfig.telemetryEnabled = this.telemetryEnabled;
clientConfig.httpClient = this.httpClient;
return clientConfig;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import kong.unirest.UnirestInstance;
import kong.unirest.apache.ApacheClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -76,6 +77,12 @@ public DefaultTargetHttpClient(ClientConfig clientConfig) {
unirestInstance.config().proxy(proxyConfig.getHost(), proxyConfig.getPort());
}
}

if (clientConfig.getHttpClient() != null) {
unirestInstance
.config()
.httpClient(new ApacheClient(clientConfig.getHttpClient(), unirestInstance.config()));
}
}

private ObjectMapper getObjectMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import kong.unirest.HttpResponse;
import kong.unirest.Proxy;
import kong.unirest.RawResponse;
import kong.unirest.UnirestInstance;
import javax.net.ssl.SSLContext;
import kong.unirest.*;
import org.apache.http.HttpStatus;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
Expand Down Expand Up @@ -93,6 +95,18 @@ void testProxyConfigSetWithAuthentication() {
targetClient.close();
}

@Test
void testConfigSetWithSSLFactory() throws Exception {
SSLContext context = SSLContextBuilder.create().build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(context);
CloseableHttpClient httpClient =
HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
ClientConfig clientConfig =
ClientConfig.builder().organizationId(TEST_ORG_ID).httpClient(httpClient).build();
DefaultTargetHttpClient targetClient = new DefaultTargetHttpClient(clientConfig);
assertEquals(targetClient.getUnirestInstance().config().getClient().getClient(), httpClient);
}

@Test
void testExecute() throws NoSuchFieldException {
ClientConfig clientConfig =
Expand Down

0 comments on commit 6f3a136

Please sign in to comment.