diff --git a/src/main/java/fi/aalto/cs/apluscourses/utils/CoursesClient.java b/src/main/java/fi/aalto/cs/apluscourses/utils/CoursesClient.java index d9d52351b..68e720f4c 100644 --- a/src/main/java/fi/aalto/cs/apluscourses/utils/CoursesClient.java +++ b/src/main/java/fi/aalto/cs/apluscourses/utils/CoursesClient.java @@ -15,7 +15,7 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; -import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; @@ -24,7 +24,6 @@ import org.apache.http.entity.mime.content.ContentBody; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; -import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.http.util.VersionInfo; @@ -44,6 +43,8 @@ public class CoursesClient { private static final AtomicReference userAgent = new AtomicReference<>(null); + private static HttpClient httpClient; + /** * Makes a GET request to the given URL and returns the response body in a * {@link ByteArrayInputStream}. @@ -213,11 +214,9 @@ public static T post(@NotNull URL url, authentication.addToRequest(request); } - try (CloseableHttpClient client = createClient(); - CloseableHttpResponse response = client.execute(request)) { - requireSuccessStatusCode(response); - return mapper.map(response); - } + HttpResponse response = getClient().execute(request); + requireSuccessStatusCode(response); + return mapper.map(response); } @NotNull @@ -240,11 +239,9 @@ private static ContentBody getContentBody(@NotNull Object value) { */ private static T mapResponse(@NotNull HttpUriRequest request, @NotNull ResponseMapper mapper) throws IOException { - try (CloseableHttpClient client = createClient(); - CloseableHttpResponse response = client.execute(request)) { - requireSuccessStatusCode(response); - return mapper.map(response); - } + HttpResponse response = getClient().execute(request); + requireSuccessStatusCode(response); + return mapper.map(response); } /** @@ -253,11 +250,9 @@ private static T mapResponse(@NotNull HttpUriRequest request, */ private static void consumeResponse(@NotNull HttpUriRequest request, @NotNull ResponseConsumer consumer) throws IOException { - try (CloseableHttpClient client = createClient(); - CloseableHttpResponse response = client.execute(request)) { - requireSuccessStatusCode(response); - consumer.consume(response); - } + HttpResponse response = getClient().execute(request); + requireSuccessStatusCode(response); + consumer.consume(response); } /** @@ -309,8 +304,11 @@ private static void requireResponseEntity(@NotNull HttpResponse response) } @NotNull - private static CloseableHttpClient createClient() { - return HttpClients.custom().setUserAgent(getUserAgent()).build(); + private static synchronized HttpClient getClient() { + if (httpClient == null) { + httpClient = HttpClients.custom().setUserAgent(getUserAgent()).build(); + } + return httpClient; } @NotNull