Skip to content

Commit a7ee9bd

Browse files
[FIX] HTTP Client: use singleton instance
1 parent 8fa0873 commit a7ee9bd

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

basex-api/src/test/java/org/basex/http/HTTPTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected static String send(final int status, final String method, final InputS
215215
if(type != null) builder.setHeader(HTTPText.CONTENT_TYPE, type.toString());
216216

217217
try {
218-
final HttpClient client = IOUrl.clientBuilder(true).build();
218+
final HttpClient client = IOUrl.client(true);
219219
final HttpResponse<String> response = client.send(builder.build(),
220220
HttpResponse.BodyHandlers.ofString());
221221
final String body = response.body();

basex-core/src/main/java/org/basex/io/IOUrl.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public final class IOUrl extends IO {
3030
private static final HashMap<Integer, String> REASONS = new HashMap<>();
3131
/** Optional SSL context for ignoring certificates. */
3232
private static SSLContext ssl;
33-
/** Global HTTP client instance. */
34-
private static HttpClient client;
33+
/** Cached HTTP client instances. */
34+
private static final HttpClient[] CLIENTS = new HttpClient[2];
3535

3636
static {
3737
REASONS.put(100, "Continue");
@@ -134,7 +134,7 @@ public InputStream inputStream() throws IOException {
134134
* @throws IOException I/O exception
135135
*/
136136
public HttpResponse<InputStream> response() throws IOException {
137-
if(client == null) client = clientBuilder(true).build();
137+
final HttpClient client = client(true);
138138

139139
final HttpResponse<InputStream> response;
140140
try {
@@ -166,14 +166,18 @@ public HttpResponse<InputStream> response() throws IOException {
166166
}
167167

168168
/**
169-
* Returns a unified HTTP client builder.
169+
* Returns a singleton HTTP client instance.
170170
* @param redirect follow redirects
171171
* @return client builder
172172
*/
173-
public static HttpClient.Builder clientBuilder(final boolean redirect) {
174-
final HttpClient.Builder cb = HttpClient.newBuilder();
175-
if(ssl != null) cb.sslContext(ssl).connectTimeout(Duration.ofMinutes(1));
176-
return cb.followRedirects(redirect ? Redirect.ALWAYS : Redirect.NEVER);
173+
public static HttpClient client(final boolean redirect) {
174+
final int i = redirect ? 1 : 0;
175+
if(CLIENTS[i] == null) {
176+
final HttpClient.Builder cb = HttpClient.newBuilder();
177+
if(ssl != null) cb.sslContext(ssl).connectTimeout(Duration.ofMinutes(1));
178+
CLIENTS[i] = cb.followRedirects(redirect ? Redirect.ALWAYS : Redirect.NEVER).build();
179+
}
180+
return CLIENTS[i];
177181
}
178182

179183
/**

basex-core/src/main/java/org/basex/util/http/Client.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private static HttpResponse<InputStream> send(final URI uri, final Request reque
134134
}
135135

136136
final String fw = request.attribute(FOLLOW_REDIRECT);
137-
final HttpClient client = IOUrl.clientBuilder(fw == null || Strings.toBoolean(fw)).build();
137+
final HttpClient client = IOUrl.client(fw == null || Strings.toBoolean(fw));
138138
final BodyHandler<InputStream> handler = HttpResponse.BodyHandlers.ofInputStream();
139139

140140
// send request (with optional authorization)

0 commit comments

Comments
 (0)