From 3b3a04ff0edb04ab1c01b4b731054463d9ee406b Mon Sep 17 00:00:00 2001 From: Paul Hsieh Date: Fri, 6 Sep 2024 13:31:54 +0800 Subject: [PATCH 1/3] Allow customized interceptors --- pom.xml | 2 +- .../java/com/taboola/rest/api/RestAPIClient.java | 12 ++++++++++-- .../rest/api/internal/CommunicationFactory.java | 9 ++++++--- .../api/internal/config/CommunicationConfig.java | 11 ++++++++++- .../rest/api/internal/CommunicationFactoryTest.java | 2 +- 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index d8e8371..f56f421 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.taboola api-java-client-core - 1.0.6 + 1.0.7 ${project.groupId}:${project.artifactId} API Java client core diff --git a/src/main/java/com/taboola/rest/api/RestAPIClient.java b/src/main/java/com/taboola/rest/api/RestAPIClient.java index e6e7b32..bec80b4 100644 --- a/src/main/java/com/taboola/rest/api/RestAPIClient.java +++ b/src/main/java/com/taboola/rest/api/RestAPIClient.java @@ -1,5 +1,7 @@ package com.taboola.rest.api; +import okhttp3.Interceptor; + import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -51,7 +53,7 @@ public static RestAPIClientBuilder builder() { } public static class RestAPIClientBuilder { - private static final String VERSION = "1.0.6"; + private static final String VERSION = "1.0.7"; private static final Integer DEFAULT_MAX_IDLE_CONNECTIONS = 5; private static final Long DEFAULT_KEEP_ALIVE_DURATION_MILLIS = 300_000L; private static final SerializationConfig DEFAULT_SERIALIZATION_CONFIG = new SerializationConfig(); @@ -78,6 +80,7 @@ public static class RestAPIClientBuilder { private final StringResponseFactories stringResponseFactories = new StringResponseFactories(); private HttpLoggingLevel loggingLevel; private CommunicationInterceptor communicationInterceptor; + private List interceptors = new ArrayList<>(); public RestAPIClientBuilder setLoggingLevel(HttpLoggingLevel loggingLevel) { this.loggingLevel = loggingLevel; @@ -164,12 +167,17 @@ public RestAPIClientBuilder addStringBodyResponseFactory(Class clazz, StringR return this; } + public RestAPIClientBuilder addInterceptor(Interceptor interceptor) { + interceptors.add(interceptor); + return this; + } + public RestAPIClient build() { organizeState(); String finalUserAgent = String.format("%s/%s/%s (%s)", userAgentPrefix, restAPIVersion, VERSION, userAgentSuffix); Collection headers = getAllHeaders(this.headers, finalUserAgent); CommunicationConfig config = new CommunicationConfig(baseUrl, connectionTimeoutMillis, readTimeoutMillis, writeTimeoutMillis, maxIdleConnections, - keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor); + keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor, interceptors); return new RestAPIClient(new CommunicationFactory(config)); } diff --git a/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java b/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java index ec86dfb..0409566 100644 --- a/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java +++ b/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java @@ -56,7 +56,7 @@ private Retrofit.Builder createRetrofitBuilder(CommunicationConfig config) { } private OkHttpClient createOkHttpClient(CommunicationConfig config) { - return new OkHttpClient.Builder() + OkHttpClient.Builder builder = new OkHttpClient.Builder() .addInterceptor(new HeadersInterceptor(config.getHeaders())) .addInterceptor(createLoggingInterceptor(config)) .addInterceptor(new ImmutableRequestResponseInterceptor(config.getCommunicationInterceptor())) @@ -64,8 +64,11 @@ private OkHttpClient createOkHttpClient(CommunicationConfig config) { .writeTimeout(config.getWriteTimeoutMillis(), TimeUnit.MILLISECONDS) .connectTimeout(config.getConnectionTimeoutMillis(), TimeUnit.MILLISECONDS) .connectionPool(new ConnectionPool(config.getMaxIdleConnections(), - config.getKeepAliveDurationMillis(), TimeUnit.MILLISECONDS)) - .build(); + config.getKeepAliveDurationMillis(), TimeUnit.MILLISECONDS)); + + config.getInterceptors().forEach(builder::addInterceptor); + + return builder.build(); } diff --git a/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java b/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java index d6c89d3..ef17df2 100644 --- a/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java +++ b/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java @@ -1,6 +1,8 @@ package com.taboola.rest.api.internal.config; +import okhttp3.Interceptor; + import java.util.Collection; import com.fasterxml.jackson.databind.ObjectMapper; @@ -31,12 +33,13 @@ public class CommunicationConfig { private final StringResponseFactories stringResponseFactories; private final HttpLoggingLevel loggingLevel; private final CommunicationInterceptor communicationInterceptor; + private final Collection interceptors; public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long readTimeoutMillis, Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis, Collection headers, boolean debug, ExceptionFactory exceptionFactory, ObjectMapper objectMapper, StringResponseFactories stringResponseFactories, HttpLoggingLevel loggingLevel, - CommunicationInterceptor communicationInterceptor) { + CommunicationInterceptor communicationInterceptor, Collection interceptors) { this.baseUrl = baseUrl; this.connectionTimeoutMillis = connectionTimeoutMillis; this.readTimeoutMillis = readTimeoutMillis; @@ -50,6 +53,7 @@ public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long re this.stringResponseFactories = stringResponseFactories; this.loggingLevel = loggingLevel; this.communicationInterceptor = communicationInterceptor; + this.interceptors = interceptors; } public String getBaseUrl() { @@ -104,6 +108,10 @@ public CommunicationInterceptor getCommunicationInterceptor() { return communicationInterceptor; } + public Collection getInterceptors() { + return interceptors; + } + @Override public String toString() { return "CommunicationConfig{" + @@ -120,6 +128,7 @@ public String toString() { ", stringResponseFactories=" + stringResponseFactories + ", loggingLevel=" + loggingLevel + ", communicationInterceptor=" + communicationInterceptor + + ", interceptors=" + interceptors + '}'; } } diff --git a/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java b/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java index d7a5c8f..61f8674 100644 --- a/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java +++ b/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java @@ -39,7 +39,7 @@ public void beforeTest() { CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost", 1L, 1L, 1L, 1, 60L, Collections.singleton(new UserAgentHeader("Dummy-Agent")),true, new DefaultExceptionFactory(), - new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor()); + new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor(), Collections.emptyList()); testInstance = new CommunicationFactory(communicationConfig); } From 056e7d8d30ee340f78d4ca70b476ecacf9b843a2 Mon Sep 17 00:00:00 2001 From: Paul Hsieh Date: Tue, 10 Sep 2024 09:14:51 +0800 Subject: [PATCH 2/3] Remove customized interceptors and add RequestHeaders --- .../com/taboola/rest/api/RestAPIClient.java | 13 ++++++------ .../api/internal/CommunicationFactory.java | 11 ++++------ .../internal/config/CommunicationConfig.java | 15 +++++++------ .../interceptors/HeadersInterceptor.java | 21 +++++++++++++++++-- .../rest/api/model/RequestHeaders.java | 7 +++++++ .../internal/CommunicationFactoryTest.java | 4 ++-- 6 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/taboola/rest/api/model/RequestHeaders.java diff --git a/src/main/java/com/taboola/rest/api/RestAPIClient.java b/src/main/java/com/taboola/rest/api/RestAPIClient.java index bec80b4..ffe0f52 100644 --- a/src/main/java/com/taboola/rest/api/RestAPIClient.java +++ b/src/main/java/com/taboola/rest/api/RestAPIClient.java @@ -1,7 +1,5 @@ package com.taboola.rest.api; -import okhttp3.Interceptor; - import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -24,6 +22,7 @@ import com.taboola.rest.api.model.HttpLoggingLevel; import com.taboola.rest.api.model.NoOpCommunicationInterceptor; import com.taboola.rest.api.model.RequestHeader; +import com.taboola.rest.api.model.RequestHeaders; import com.taboola.rest.api.model.StringResponseFactory; /** @@ -80,7 +79,7 @@ public static class RestAPIClientBuilder { private final StringResponseFactories stringResponseFactories = new StringResponseFactories(); private HttpLoggingLevel loggingLevel; private CommunicationInterceptor communicationInterceptor; - private List interceptors = new ArrayList<>(); + private RequestHeaders requestHeaders; public RestAPIClientBuilder setLoggingLevel(HttpLoggingLevel loggingLevel) { this.loggingLevel = loggingLevel; @@ -167,8 +166,8 @@ public RestAPIClientBuilder addStringBodyResponseFactory(Class clazz, StringR return this; } - public RestAPIClientBuilder addInterceptor(Interceptor interceptor) { - interceptors.add(interceptor); + public RestAPIClientBuilder setRequestHeaders(RequestHeaders requestHeaders) { + this.requestHeaders = requestHeaders; return this; } @@ -177,7 +176,7 @@ public RestAPIClient build() { String finalUserAgent = String.format("%s/%s/%s (%s)", userAgentPrefix, restAPIVersion, VERSION, userAgentSuffix); Collection headers = getAllHeaders(this.headers, finalUserAgent); CommunicationConfig config = new CommunicationConfig(baseUrl, connectionTimeoutMillis, readTimeoutMillis, writeTimeoutMillis, maxIdleConnections, - keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor, interceptors); + keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor, requestHeaders); return new RestAPIClient(new CommunicationFactory(config)); } @@ -249,7 +248,7 @@ private void organizeState() { loggingLevel = HttpLoggingLevel.BASIC; } - if(communicationInterceptor == null) { + if (communicationInterceptor == null) { communicationInterceptor = DEFAULT_COMMUNICATION_INTERCEPTOR; } } diff --git a/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java b/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java index 0409566..e282609 100644 --- a/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java +++ b/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java @@ -56,19 +56,16 @@ private Retrofit.Builder createRetrofitBuilder(CommunicationConfig config) { } private OkHttpClient createOkHttpClient(CommunicationConfig config) { - OkHttpClient.Builder builder = new OkHttpClient.Builder() - .addInterceptor(new HeadersInterceptor(config.getHeaders())) + return new OkHttpClient.Builder() + .addInterceptor(new HeadersInterceptor(config.getHeaders(), config.getRequestHeaders())) .addInterceptor(createLoggingInterceptor(config)) .addInterceptor(new ImmutableRequestResponseInterceptor(config.getCommunicationInterceptor())) .readTimeout(config.getReadTimeoutMillis(), TimeUnit.MILLISECONDS) .writeTimeout(config.getWriteTimeoutMillis(), TimeUnit.MILLISECONDS) .connectTimeout(config.getConnectionTimeoutMillis(), TimeUnit.MILLISECONDS) .connectionPool(new ConnectionPool(config.getMaxIdleConnections(), - config.getKeepAliveDurationMillis(), TimeUnit.MILLISECONDS)); - - config.getInterceptors().forEach(builder::addInterceptor); - - return builder.build(); + config.getKeepAliveDurationMillis(), TimeUnit.MILLISECONDS)) + .build(); } diff --git a/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java b/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java index ef17df2..f6c352d 100644 --- a/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java +++ b/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java @@ -1,8 +1,6 @@ package com.taboola.rest.api.internal.config; -import okhttp3.Interceptor; - import java.util.Collection; import com.fasterxml.jackson.databind.ObjectMapper; @@ -11,6 +9,7 @@ import com.taboola.rest.api.model.CommunicationInterceptor; import com.taboola.rest.api.model.HttpLoggingLevel; import com.taboola.rest.api.model.RequestHeader; +import com.taboola.rest.api.model.RequestHeaders; /** * Created by vladi @@ -33,13 +32,13 @@ public class CommunicationConfig { private final StringResponseFactories stringResponseFactories; private final HttpLoggingLevel loggingLevel; private final CommunicationInterceptor communicationInterceptor; - private final Collection interceptors; + private final RequestHeaders requestHeaders; public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long readTimeoutMillis, Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis, Collection headers, boolean debug, ExceptionFactory exceptionFactory, ObjectMapper objectMapper, StringResponseFactories stringResponseFactories, HttpLoggingLevel loggingLevel, - CommunicationInterceptor communicationInterceptor, Collection interceptors) { + CommunicationInterceptor communicationInterceptor, RequestHeaders requestHeaders) { this.baseUrl = baseUrl; this.connectionTimeoutMillis = connectionTimeoutMillis; this.readTimeoutMillis = readTimeoutMillis; @@ -53,7 +52,7 @@ public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long re this.stringResponseFactories = stringResponseFactories; this.loggingLevel = loggingLevel; this.communicationInterceptor = communicationInterceptor; - this.interceptors = interceptors; + this.requestHeaders = requestHeaders; } public String getBaseUrl() { @@ -108,8 +107,8 @@ public CommunicationInterceptor getCommunicationInterceptor() { return communicationInterceptor; } - public Collection getInterceptors() { - return interceptors; + public RequestHeaders getRequestHeaders() { + return requestHeaders; } @Override @@ -128,7 +127,7 @@ public String toString() { ", stringResponseFactories=" + stringResponseFactories + ", loggingLevel=" + loggingLevel + ", communicationInterceptor=" + communicationInterceptor + - ", interceptors=" + interceptors + + ", requestHeaders=" + requestHeaders + '}'; } } diff --git a/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java b/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java index 9940e20..6e71f80 100644 --- a/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java +++ b/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java @@ -2,8 +2,10 @@ import java.io.IOException; import java.util.Collection; +import java.util.Map; import com.taboola.rest.api.model.RequestHeader; +import com.taboola.rest.api.model.RequestHeaders; import okhttp3.Interceptor; import okhttp3.Request; @@ -14,21 +16,36 @@ */ public class HeadersInterceptor implements Interceptor { private final Collection headers; + private final RequestHeaders requestHeaders; - public HeadersInterceptor(Collection headers) { + public HeadersInterceptor(Collection headers, RequestHeaders requestHeaders) { this.headers = headers; + this.requestHeaders = requestHeaders; } @Override public Response intercept(Chain chain) throws IOException { Request.Builder builder = chain.request().newBuilder(); + headers.stream() .filter(this::validateHeader) .forEach(header -> builder.header(header.getName(), header.getValue())); + + if (requestHeaders != null) { + requestHeaders.get() + .entrySet() + .stream().filter(this::validateHeader) + .forEach(entry -> builder.header(entry.getKey(), entry.getValue())); + } + return chain.proceed(builder.build()); } - private boolean validateHeader(RequestHeader header){ + private boolean validateHeader(RequestHeader header) { return header.getName() != null && header.getValue() != null; } + + private boolean validateHeader(Map.Entry entry) { + return entry.getKey() != null && entry.getValue() != null; + } } diff --git a/src/main/java/com/taboola/rest/api/model/RequestHeaders.java b/src/main/java/com/taboola/rest/api/model/RequestHeaders.java new file mode 100644 index 0000000..547bdfc --- /dev/null +++ b/src/main/java/com/taboola/rest/api/model/RequestHeaders.java @@ -0,0 +1,7 @@ +package com.taboola.rest.api.model; + +import java.util.Map; +import java.util.function.Supplier; + +public interface RequestHeaders extends Supplier> { +} diff --git a/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java b/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java index 61f8674..057aeb2 100644 --- a/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java +++ b/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java @@ -38,8 +38,8 @@ public interface TestEndpoint { public void beforeTest() { CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost", 1L, 1L, 1L, 1, 60L, - Collections.singleton(new UserAgentHeader("Dummy-Agent")),true, new DefaultExceptionFactory(), - new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor(), Collections.emptyList()); + Collections.singleton(new UserAgentHeader("Dummy-Agent")), true, new DefaultExceptionFactory(), + new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor(), null); testInstance = new CommunicationFactory(communicationConfig); } From 755e6e60c419c797db53bb5084830a0908b63f7a Mon Sep 17 00:00:00 2001 From: Paul Hsieh Date: Thu, 12 Sep 2024 11:22:56 +0800 Subject: [PATCH 3/3] Add RequestHeadersSupplier --- .../com/taboola/rest/api/RestAPIClient.java | 19 +++++++------ .../api/internal/CommunicationFactory.java | 2 +- .../internal/config/CommunicationConfig.java | 24 ++++++---------- .../interceptors/HeadersInterceptor.java | 28 ++++--------------- .../model/MultiRequestHeadersSupplier.java | 24 ++++++++++++++++ .../rest/api/model/RequestHeaders.java | 7 ----- .../api/model/RequestHeadersSupplier.java | 7 +++++ .../internal/CommunicationFactoryTest.java | 4 +-- 8 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/taboola/rest/api/model/MultiRequestHeadersSupplier.java delete mode 100644 src/main/java/com/taboola/rest/api/model/RequestHeaders.java create mode 100644 src/main/java/com/taboola/rest/api/model/RequestHeadersSupplier.java diff --git a/src/main/java/com/taboola/rest/api/RestAPIClient.java b/src/main/java/com/taboola/rest/api/RestAPIClient.java index ffe0f52..c8f0152 100644 --- a/src/main/java/com/taboola/rest/api/RestAPIClient.java +++ b/src/main/java/com/taboola/rest/api/RestAPIClient.java @@ -20,9 +20,10 @@ import com.taboola.rest.api.internal.serialization.SerializationMapperCreator; import com.taboola.rest.api.model.CommunicationInterceptor; import com.taboola.rest.api.model.HttpLoggingLevel; +import com.taboola.rest.api.model.MultiRequestHeadersSupplier; import com.taboola.rest.api.model.NoOpCommunicationInterceptor; import com.taboola.rest.api.model.RequestHeader; -import com.taboola.rest.api.model.RequestHeaders; +import com.taboola.rest.api.model.RequestHeadersSupplier; import com.taboola.rest.api.model.StringResponseFactory; /** @@ -71,6 +72,7 @@ public static class RestAPIClientBuilder { private Boolean debug; private SerializationConfig serializationConfig; private Collection headers; + private RequestHeadersSupplier headersSupplier; private String userAgentPrefix; private String userAgentSuffix; private String restAPIVersion; @@ -79,7 +81,6 @@ public static class RestAPIClientBuilder { private final StringResponseFactories stringResponseFactories = new StringResponseFactories(); private HttpLoggingLevel loggingLevel; private CommunicationInterceptor communicationInterceptor; - private RequestHeaders requestHeaders; public RestAPIClientBuilder setLoggingLevel(HttpLoggingLevel loggingLevel) { this.loggingLevel = loggingLevel; @@ -141,6 +142,11 @@ public RestAPIClientBuilder setHeaders(Collection headers) { return this; } + public RestAPIClientBuilder setHeadersSupplier(RequestHeadersSupplier headersSupplier) { + this.headersSupplier = headersSupplier; + return this; + } + public RestAPIClientBuilder setAPIVersion(String restAPIVersion) { this.restAPIVersion = restAPIVersion; return this; @@ -166,17 +172,14 @@ public RestAPIClientBuilder addStringBodyResponseFactory(Class clazz, StringR return this; } - public RestAPIClientBuilder setRequestHeaders(RequestHeaders requestHeaders) { - this.requestHeaders = requestHeaders; - return this; - } - public RestAPIClient build() { organizeState(); String finalUserAgent = String.format("%s/%s/%s (%s)", userAgentPrefix, restAPIVersion, VERSION, userAgentSuffix); Collection headers = getAllHeaders(this.headers, finalUserAgent); + RequestHeadersSupplier multiRequestHeadersSupplier = new MultiRequestHeadersSupplier(() -> headers, headersSupplier); + CommunicationConfig config = new CommunicationConfig(baseUrl, connectionTimeoutMillis, readTimeoutMillis, writeTimeoutMillis, maxIdleConnections, - keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor, requestHeaders); + keepAliveDurationMillis, multiRequestHeadersSupplier, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel, communicationInterceptor); return new RestAPIClient(new CommunicationFactory(config)); } diff --git a/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java b/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java index e282609..6aab8a8 100644 --- a/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java +++ b/src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java @@ -57,7 +57,7 @@ private Retrofit.Builder createRetrofitBuilder(CommunicationConfig config) { private OkHttpClient createOkHttpClient(CommunicationConfig config) { return new OkHttpClient.Builder() - .addInterceptor(new HeadersInterceptor(config.getHeaders(), config.getRequestHeaders())) + .addInterceptor(new HeadersInterceptor(config.getRequestHeadersSupplier())) .addInterceptor(createLoggingInterceptor(config)) .addInterceptor(new ImmutableRequestResponseInterceptor(config.getCommunicationInterceptor())) .readTimeout(config.getReadTimeoutMillis(), TimeUnit.MILLISECONDS) diff --git a/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java b/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java index f6c352d..034d1a8 100644 --- a/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java +++ b/src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java @@ -8,8 +8,7 @@ import com.taboola.rest.api.internal.StringResponseFactories; import com.taboola.rest.api.model.CommunicationInterceptor; import com.taboola.rest.api.model.HttpLoggingLevel; -import com.taboola.rest.api.model.RequestHeader; -import com.taboola.rest.api.model.RequestHeaders; +import com.taboola.rest.api.model.RequestHeadersSupplier; /** * Created by vladi @@ -26,33 +25,31 @@ public class CommunicationConfig { private final int maxIdleConnections; private final long keepAliveDurationMillis; private final boolean debug; - private final Collection headers; + private final RequestHeadersSupplier requestHeadersSupplier; private final ExceptionFactory exceptionFactory; private final ObjectMapper objectMapper; private final StringResponseFactories stringResponseFactories; private final HttpLoggingLevel loggingLevel; private final CommunicationInterceptor communicationInterceptor; - private final RequestHeaders requestHeaders; public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long readTimeoutMillis, Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis, - Collection headers, boolean debug, ExceptionFactory exceptionFactory, + RequestHeadersSupplier requestHeadersSupplier, boolean debug, ExceptionFactory exceptionFactory, ObjectMapper objectMapper, StringResponseFactories stringResponseFactories, HttpLoggingLevel loggingLevel, - CommunicationInterceptor communicationInterceptor, RequestHeaders requestHeaders) { + CommunicationInterceptor communicationInterceptor) { this.baseUrl = baseUrl; this.connectionTimeoutMillis = connectionTimeoutMillis; this.readTimeoutMillis = readTimeoutMillis; this.writeTimeoutMillis = writeTimeoutMillis; this.maxIdleConnections = maxIdleConnections; this.keepAliveDurationMillis = keepAliveDurationMillis; - this.headers = headers; + this.requestHeadersSupplier = requestHeadersSupplier; this.debug = debug; this.exceptionFactory = exceptionFactory; this.objectMapper = objectMapper; this.stringResponseFactories = stringResponseFactories; this.loggingLevel = loggingLevel; this.communicationInterceptor = communicationInterceptor; - this.requestHeaders = requestHeaders; } public String getBaseUrl() { @@ -79,8 +76,8 @@ public long getKeepAliveDurationMillis() { return keepAliveDurationMillis; } - public Collection getHeaders() { - return headers; + public RequestHeadersSupplier getRequestHeadersSupplier() { + return requestHeadersSupplier; } public boolean isDebug() { @@ -107,10 +104,6 @@ public CommunicationInterceptor getCommunicationInterceptor() { return communicationInterceptor; } - public RequestHeaders getRequestHeaders() { - return requestHeaders; - } - @Override public String toString() { return "CommunicationConfig{" + @@ -121,13 +114,12 @@ public String toString() { ", maxIdleConnections=" + maxIdleConnections + ", keepAliveDurationMillis=" + keepAliveDurationMillis + ", debug=" + debug + - ", headers=" + headers + + ", requestHeadersSupplier=" + requestHeadersSupplier + ", exceptionFactory=" + exceptionFactory + ", objectMapper=" + objectMapper + ", stringResponseFactories=" + stringResponseFactories + ", loggingLevel=" + loggingLevel + ", communicationInterceptor=" + communicationInterceptor + - ", requestHeaders=" + requestHeaders + '}'; } } diff --git a/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java b/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java index 6e71f80..3928faf 100644 --- a/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java +++ b/src/main/java/com/taboola/rest/api/internal/interceptors/HeadersInterceptor.java @@ -1,11 +1,9 @@ package com.taboola.rest.api.internal.interceptors; import java.io.IOException; -import java.util.Collection; -import java.util.Map; import com.taboola.rest.api.model.RequestHeader; -import com.taboola.rest.api.model.RequestHeaders; +import com.taboola.rest.api.model.RequestHeadersSupplier; import okhttp3.Interceptor; import okhttp3.Request; @@ -15,37 +13,23 @@ * Created on 10/21/20. */ public class HeadersInterceptor implements Interceptor { - private final Collection headers; - private final RequestHeaders requestHeaders; + private final RequestHeadersSupplier requestHeadersSupplier; - public HeadersInterceptor(Collection headers, RequestHeaders requestHeaders) { - this.headers = headers; - this.requestHeaders = requestHeaders; + public HeadersInterceptor(RequestHeadersSupplier requestHeadersSupplier) { + this.requestHeadersSupplier = requestHeadersSupplier; } @Override public Response intercept(Chain chain) throws IOException { Request.Builder builder = chain.request().newBuilder(); - - headers.stream() + requestHeadersSupplier.get() + .stream() .filter(this::validateHeader) .forEach(header -> builder.header(header.getName(), header.getValue())); - - if (requestHeaders != null) { - requestHeaders.get() - .entrySet() - .stream().filter(this::validateHeader) - .forEach(entry -> builder.header(entry.getKey(), entry.getValue())); - } - return chain.proceed(builder.build()); } private boolean validateHeader(RequestHeader header) { return header.getName() != null && header.getValue() != null; } - - private boolean validateHeader(Map.Entry entry) { - return entry.getKey() != null && entry.getValue() != null; - } } diff --git a/src/main/java/com/taboola/rest/api/model/MultiRequestHeadersSupplier.java b/src/main/java/com/taboola/rest/api/model/MultiRequestHeadersSupplier.java new file mode 100644 index 0000000..9dbfe87 --- /dev/null +++ b/src/main/java/com/taboola/rest/api/model/MultiRequestHeadersSupplier.java @@ -0,0 +1,24 @@ +package com.taboola.rest.api.model; + +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class MultiRequestHeadersSupplier implements RequestHeadersSupplier { + private final Collection suppliers; + + public MultiRequestHeadersSupplier(RequestHeadersSupplier... suppliers) { + this.suppliers = Stream.of(suppliers == null ? new RequestHeadersSupplier[0] : suppliers) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + @Override + public Collection get() { + return suppliers.stream() + .map(RequestHeadersSupplier::get) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/com/taboola/rest/api/model/RequestHeaders.java b/src/main/java/com/taboola/rest/api/model/RequestHeaders.java deleted file mode 100644 index 547bdfc..0000000 --- a/src/main/java/com/taboola/rest/api/model/RequestHeaders.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.taboola.rest.api.model; - -import java.util.Map; -import java.util.function.Supplier; - -public interface RequestHeaders extends Supplier> { -} diff --git a/src/main/java/com/taboola/rest/api/model/RequestHeadersSupplier.java b/src/main/java/com/taboola/rest/api/model/RequestHeadersSupplier.java new file mode 100644 index 0000000..2f99461 --- /dev/null +++ b/src/main/java/com/taboola/rest/api/model/RequestHeadersSupplier.java @@ -0,0 +1,7 @@ +package com.taboola.rest.api.model; + +import java.util.Collection; +import java.util.function.Supplier; + +public interface RequestHeadersSupplier extends Supplier> { +} diff --git a/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java b/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java index 057aeb2..745baf1 100644 --- a/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java +++ b/src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java @@ -38,8 +38,8 @@ public interface TestEndpoint { public void beforeTest() { CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost", 1L, 1L, 1L, 1, 60L, - Collections.singleton(new UserAgentHeader("Dummy-Agent")), true, new DefaultExceptionFactory(), - new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor(), null); + () -> Collections.singleton(new UserAgentHeader("Dummy-Agent")), true, new DefaultExceptionFactory(), + new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC, new NoOpCommunicationInterceptor()); testInstance = new CommunicationFactory(communicationConfig); }