Skip to content

Commit c514b65

Browse files
committed
feat(http-manager): keep current setter-only builder pattern
1 parent e02b0c1 commit c514b65

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class SpotifyHttpManager implements IHttpManager {
3939
private static final Gson GSON = new Gson();
4040
private final CloseableHttpClient httpClient;
4141
private final CloseableHttpClient httpClientCaching;
42+
private final HttpClientConnectionManager connectionManager;
4243
private final HttpHost proxy;
4344
private final UsernamePasswordCredentials proxyCredentials;
4445
private final Integer cacheMaxEntries;
@@ -76,6 +77,19 @@ public SpotifyHttpManager(Builder builder) {
7677
);
7778
}
7879

80+
if (builder.connectionManager != null) {
81+
this.connectionManager = builder.connectionManager;
82+
} else {
83+
BasicHttpClientConnectionManager basicHttpClientConnectionManager = new BasicHttpClientConnectionManager();
84+
basicHttpClientConnectionManager.setConnectionConfig(ConnectionConfig
85+
.custom()
86+
.setConnectTimeout(connectTimeout != null ?
87+
Timeout.ofMilliseconds(connectTimeout) :
88+
ConnectionConfig.DEFAULT.getConnectTimeout())
89+
.build());
90+
this.connectionManager = basicHttpClientConnectionManager;
91+
}
92+
7993
RequestConfig requestConfig = RequestConfig
8094
.custom()
8195
.setCookieSpec(StandardCookieSpec.STRICT)
@@ -91,7 +105,7 @@ public SpotifyHttpManager(Builder builder) {
91105
this.httpClient = HttpClients
92106
.custom()
93107
.disableContentCompression()
94-
.setConnectionManager(builder.getConnectionManager())
108+
.setConnectionManager(connectionManager)
95109
.setDefaultCredentialsProvider(credentialsProvider)
96110
.setDefaultRequestConfig(requestConfig)
97111
.setProxy(proxy)
@@ -102,7 +116,7 @@ public SpotifyHttpManager(Builder builder) {
102116
.custom()
103117
.setCacheConfig(cacheConfig)
104118
.disableContentCompression()
105-
.setConnectionManager(builder.getConnectionManager())
119+
.setConnectionManager(connectionManager)
106120
.setDefaultCredentialsProvider(credentialsProvider)
107121
.setDefaultRequestConfig(requestConfig)
108122
.setProxy(proxy)
@@ -121,6 +135,10 @@ public static URI makeUri(String uriString) {
121135
}
122136
}
123137

138+
public HttpClientConnectionManager getConnectionManager() {
139+
return connectionManager;
140+
}
141+
124142
public HttpHost getProxy() {
125143
return proxy;
126144
}
@@ -352,14 +370,19 @@ private String getResponseBody(CloseableHttpResponse httpResponse) throws
352370
}
353371

354372
public static class Builder {
373+
private HttpClientConnectionManager connectionManager;
355374
private HttpHost proxy;
356375
private UsernamePasswordCredentials proxyCredentials;
357376
private Integer cacheMaxEntries;
358377
private Integer cacheMaxObjectSize;
359378
private Integer connectionRequestTimeout;
360379
private Integer connectTimeout;
361380
private Integer socketTimeout;
362-
private HttpClientConnectionManager connectionManager;
381+
382+
public Builder setConnectionManager(HttpClientConnectionManager connectionManager) {
383+
this.connectionManager = connectionManager;
384+
return this;
385+
}
363386

364387
public Builder setProxy(HttpHost proxy) {
365388
this.proxy = proxy;
@@ -396,26 +419,6 @@ public Builder setSocketTimeout(Integer socketTimeout) {
396419
return this;
397420
}
398421

399-
public Builder setConnectionManager(HttpClientConnectionManager connectionManager) {
400-
this.connectionManager = connectionManager;
401-
return this;
402-
}
403-
404-
HttpClientConnectionManager getConnectionManager() {
405-
if (connectionManager == null) {
406-
BasicHttpClientConnectionManager basicHttpClientConnectionManager = new BasicHttpClientConnectionManager();
407-
basicHttpClientConnectionManager.setConnectionConfig(ConnectionConfig
408-
.custom()
409-
.setConnectTimeout(connectTimeout != null ?
410-
Timeout.ofMilliseconds(connectTimeout) :
411-
ConnectionConfig.DEFAULT.getConnectTimeout())
412-
.build());
413-
connectionManager = basicHttpClientConnectionManager;
414-
}
415-
416-
return connectionManager;
417-
}
418-
419422
public SpotifyHttpManager build() {
420423
return new SpotifyHttpManager(this);
421424
}

0 commit comments

Comments
 (0)