|
7 | 7 | import java.util.ArrayList; |
8 | 8 | import java.util.List; |
9 | 9 | import java.util.concurrent.TimeUnit; |
| 10 | +import java.util.stream.Collectors; |
10 | 11 | import javax.net.ssl.SSLSocketFactory; |
11 | 12 | import javax.net.ssl.X509TrustManager; |
12 | 13 | import lombok.Getter; |
@@ -55,13 +56,19 @@ protected FlagsmithConfig(Builder builder) { |
55 | 56 | .writeTimeout(builder.writeTimeoutMillis, TimeUnit.MILLISECONDS) |
56 | 57 | .readTimeout(builder.readTimeoutMillis, TimeUnit.MILLISECONDS); |
57 | 58 | if (builder.sslSocketFactory != null && builder.trustManager != null) { |
58 | | - httpBuilder = httpBuilder.sslSocketFactory(builder.sslSocketFactory, builder.trustManager); |
| 59 | + httpBuilder.sslSocketFactory(builder.sslSocketFactory, builder.trustManager); |
59 | 60 | } |
60 | 61 | for (final Interceptor interceptor : builder.interceptors) { |
61 | | - httpBuilder = httpBuilder.addInterceptor(interceptor); |
| 62 | + httpBuilder.addInterceptor(interceptor); |
62 | 63 | } |
63 | 64 | if (builder.proxy != null) { |
64 | | - httpBuilder = httpBuilder.proxy(builder.proxy); |
| 65 | + httpBuilder.proxy(builder.proxy); |
| 66 | + } |
| 67 | + if (!builder.supportedProtocols.isEmpty()) { |
| 68 | + httpBuilder.protocols( |
| 69 | + builder.supportedProtocols.stream() |
| 70 | + .map(Protocol::internalProtocol) |
| 71 | + .collect(Collectors.toList())); |
65 | 72 | } |
66 | 73 | this.httpClient = httpBuilder.build(); |
67 | 74 |
|
@@ -97,6 +104,7 @@ public static class Builder { |
97 | 104 | private int connectTimeoutMillis = DEFAULT_CONNECT_TIMEOUT_MILLIS; |
98 | 105 | private int writeTimeoutMillis = DEFAULT_WRITE_TIMEOUT_MILLIS; |
99 | 106 | private int readTimeoutMillis = DEFAULT_READ_TIMEOUT_MILLIS; |
| 107 | + private List<Protocol> supportedProtocols = new ArrayList<>(); |
100 | 108 | private Retry retries = new Retry(3); |
101 | 109 | private SSLSocketFactory sslSocketFactory; |
102 | 110 | private X509TrustManager trustManager; |
@@ -216,8 +224,8 @@ public Builder withLocalEvaluation(Boolean localEvaluation) { |
216 | 224 | } |
217 | 225 |
|
218 | 226 | /** |
219 | | - * set environment refresh rate with polling manager. Only needed when local |
220 | | - * evaluation is true. |
| 227 | + * set environment refresh rate with polling manager. Only needed when local evaluation is |
| 228 | + * true. |
221 | 229 | * |
222 | 230 | * @param seconds seconds |
223 | 231 | */ |
@@ -267,8 +275,35 @@ public Builder withOfflineHandler(IOfflineHandler offlineHandler) { |
267 | 275 | return this; |
268 | 276 | } |
269 | 277 |
|
| 278 | + /** |
| 279 | + * Specify the list of protocols supported for calls to the server. |
| 280 | + * @param supportedProtocols the list of supported protocols |
| 281 | + * @return |
| 282 | + */ |
| 283 | + public Builder withSupportedProtocols(List<Protocol> supportedProtocols) { |
| 284 | + this.supportedProtocols.clear(); |
| 285 | + this.supportedProtocols.addAll(supportedProtocols); |
| 286 | + return this; |
| 287 | + } |
| 288 | + |
270 | 289 | public FlagsmithConfig build() { |
271 | 290 | return new FlagsmithConfig(this); |
272 | 291 | } |
273 | 292 | } |
| 293 | + |
| 294 | + // This enum prevents leakage of the underlying HTTP client implementation details. |
| 295 | + enum Protocol { |
| 296 | + HTTP_1_1(okhttp3.Protocol.HTTP_1_1), |
| 297 | + HTTP_2(okhttp3.Protocol.HTTP_2); |
| 298 | + |
| 299 | + private final okhttp3.Protocol protocol; |
| 300 | + |
| 301 | + Protocol(okhttp3.Protocol protocol) { |
| 302 | + this.protocol = protocol; |
| 303 | + } |
| 304 | + |
| 305 | + okhttp3.Protocol internalProtocol() { |
| 306 | + return protocol; |
| 307 | + } |
| 308 | + } |
274 | 309 | } |
0 commit comments