From 2903728e9b9dd7297bb18ee39d5377dd24d1343a Mon Sep 17 00:00:00 2001 From: Bruce Downs Date: Sun, 24 Nov 2024 19:46:16 -0700 Subject: [PATCH 1/4] expose backward compatible connection mgr * add HttpClientConnectionManager to SpotifyHttpManager.builder * add getConnectionManager to handle default --- .../spotify/SpotifyHttpManager.java | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java index deaf04320..6e9e1eeaf 100644 --- a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java +++ b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java @@ -20,6 +20,7 @@ import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.core5.http.*; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.util.Timeout; @@ -75,14 +76,6 @@ public SpotifyHttpManager(Builder builder) { ); } - ConnectionConfig connectionConfig = ConnectionConfig - .custom() - .setConnectTimeout(builder.connectTimeout != null - ? Timeout.ofMilliseconds(builder.connectTimeout) - : ConnectionConfig.DEFAULT.getConnectTimeout()) - .build(); - BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); - connectionManager.setConnectionConfig(connectionConfig); RequestConfig requestConfig = RequestConfig .custom() .setCookieSpec(StandardCookieSpec.STRICT) @@ -98,7 +91,7 @@ public SpotifyHttpManager(Builder builder) { this.httpClient = HttpClients .custom() .disableContentCompression() - .setConnectionManager(connectionManager) + .setConnectionManager(builder.getConnectionManager()) .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(requestConfig) .setProxy(proxy) @@ -109,7 +102,7 @@ public SpotifyHttpManager(Builder builder) { .custom() .setCacheConfig(cacheConfig) .disableContentCompression() - .setConnectionManager(connectionManager) + .setConnectionManager(builder.getConnectionManager()) .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(requestConfig) .setProxy(proxy) @@ -366,6 +359,7 @@ public static class Builder { private Integer connectionRequestTimeout; private Integer connectTimeout; private Integer socketTimeout; + private HttpClientConnectionManager connectionManager; public Builder setProxy(HttpHost proxy) { this.proxy = proxy; @@ -402,6 +396,26 @@ public Builder setSocketTimeout(Integer socketTimeout) { return this; } + public Builder setConnectionManager(HttpClientConnectionManager connectionManager) { + this.connectionManager = connectionManager; + return this; + } + + HttpClientConnectionManager getConnectionManager() { + if (connectionManager == null) { + BasicHttpClientConnectionManager basicHttpClientConnectionManager = new BasicHttpClientConnectionManager(); + basicHttpClientConnectionManager.setConnectionConfig(ConnectionConfig + .custom() + .setConnectTimeout(this.connectTimeout != null ? + Timeout.ofMilliseconds(this.connectTimeout) : + ConnectionConfig.DEFAULT.getConnectTimeout()) + .build()); + this.connectionManager = basicHttpClientConnectionManager; + } + + return connectionManager; + } + public SpotifyHttpManager build() { return new SpotifyHttpManager(this); } From 2940900dd742c3728c08edca481060413518288f Mon Sep 17 00:00:00 2001 From: Bruce Downs Date: Wed, 27 Nov 2024 23:03:06 -0700 Subject: [PATCH 2/4] no need for this scope --- src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java index 6e9e1eeaf..850c2b256 100644 --- a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java +++ b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java @@ -410,7 +410,7 @@ HttpClientConnectionManager getConnectionManager() { Timeout.ofMilliseconds(this.connectTimeout) : ConnectionConfig.DEFAULT.getConnectTimeout()) .build()); - this.connectionManager = basicHttpClientConnectionManager; + connectionManager = basicHttpClientConnectionManager; } return connectionManager; From e02b0c1f077716e767476550f6c9107eb1dc4677 Mon Sep 17 00:00:00 2001 From: Bruce Downs Date: Wed, 27 Nov 2024 23:08:11 -0700 Subject: [PATCH 3/4] unnecessary this --- .../java/se/michaelthelin/spotify/SpotifyHttpManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java index 850c2b256..49bc9c7d1 100644 --- a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java +++ b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java @@ -406,8 +406,8 @@ HttpClientConnectionManager getConnectionManager() { BasicHttpClientConnectionManager basicHttpClientConnectionManager = new BasicHttpClientConnectionManager(); basicHttpClientConnectionManager.setConnectionConfig(ConnectionConfig .custom() - .setConnectTimeout(this.connectTimeout != null ? - Timeout.ofMilliseconds(this.connectTimeout) : + .setConnectTimeout(connectTimeout != null ? + Timeout.ofMilliseconds(connectTimeout) : ConnectionConfig.DEFAULT.getConnectTimeout()) .build()); connectionManager = basicHttpClientConnectionManager; From c514b65a279ffa53d5430ece6b444aa63fd6a59d Mon Sep 17 00:00:00 2001 From: Jonas Thelemann Date: Mon, 2 Dec 2024 04:31:57 +0100 Subject: [PATCH 4/4] feat(http-manager): keep current setter-only builder pattern --- .../spotify/SpotifyHttpManager.java | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java index 49bc9c7d1..3e5a30023 100644 --- a/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java +++ b/src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java @@ -39,6 +39,7 @@ public class SpotifyHttpManager implements IHttpManager { private static final Gson GSON = new Gson(); private final CloseableHttpClient httpClient; private final CloseableHttpClient httpClientCaching; + private final HttpClientConnectionManager connectionManager; private final HttpHost proxy; private final UsernamePasswordCredentials proxyCredentials; private final Integer cacheMaxEntries; @@ -76,6 +77,19 @@ public SpotifyHttpManager(Builder builder) { ); } + if (builder.connectionManager != null) { + this.connectionManager = builder.connectionManager; + } else { + BasicHttpClientConnectionManager basicHttpClientConnectionManager = new BasicHttpClientConnectionManager(); + basicHttpClientConnectionManager.setConnectionConfig(ConnectionConfig + .custom() + .setConnectTimeout(connectTimeout != null ? + Timeout.ofMilliseconds(connectTimeout) : + ConnectionConfig.DEFAULT.getConnectTimeout()) + .build()); + this.connectionManager = basicHttpClientConnectionManager; + } + RequestConfig requestConfig = RequestConfig .custom() .setCookieSpec(StandardCookieSpec.STRICT) @@ -91,7 +105,7 @@ public SpotifyHttpManager(Builder builder) { this.httpClient = HttpClients .custom() .disableContentCompression() - .setConnectionManager(builder.getConnectionManager()) + .setConnectionManager(connectionManager) .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(requestConfig) .setProxy(proxy) @@ -102,7 +116,7 @@ public SpotifyHttpManager(Builder builder) { .custom() .setCacheConfig(cacheConfig) .disableContentCompression() - .setConnectionManager(builder.getConnectionManager()) + .setConnectionManager(connectionManager) .setDefaultCredentialsProvider(credentialsProvider) .setDefaultRequestConfig(requestConfig) .setProxy(proxy) @@ -121,6 +135,10 @@ public static URI makeUri(String uriString) { } } + public HttpClientConnectionManager getConnectionManager() { + return connectionManager; + } + public HttpHost getProxy() { return proxy; } @@ -352,6 +370,7 @@ private String getResponseBody(CloseableHttpResponse httpResponse) throws } public static class Builder { + private HttpClientConnectionManager connectionManager; private HttpHost proxy; private UsernamePasswordCredentials proxyCredentials; private Integer cacheMaxEntries; @@ -359,7 +378,11 @@ public static class Builder { private Integer connectionRequestTimeout; private Integer connectTimeout; private Integer socketTimeout; - private HttpClientConnectionManager connectionManager; + + public Builder setConnectionManager(HttpClientConnectionManager connectionManager) { + this.connectionManager = connectionManager; + return this; + } public Builder setProxy(HttpHost proxy) { this.proxy = proxy; @@ -396,26 +419,6 @@ public Builder setSocketTimeout(Integer socketTimeout) { return this; } - public Builder setConnectionManager(HttpClientConnectionManager connectionManager) { - this.connectionManager = connectionManager; - return this; - } - - HttpClientConnectionManager getConnectionManager() { - if (connectionManager == null) { - BasicHttpClientConnectionManager basicHttpClientConnectionManager = new BasicHttpClientConnectionManager(); - basicHttpClientConnectionManager.setConnectionConfig(ConnectionConfig - .custom() - .setConnectTimeout(connectTimeout != null ? - Timeout.ofMilliseconds(connectTimeout) : - ConnectionConfig.DEFAULT.getConnectTimeout()) - .build()); - connectionManager = basicHttpClientConnectionManager; - } - - return connectionManager; - } - public SpotifyHttpManager build() { return new SpotifyHttpManager(this); }