Skip to content

Commit

Permalink
OEmbedClientImpl leaks network connections (#2715)
Browse files Browse the repository at this point in the history
* Using try-with-resources in order to close the HttpClient

---------

Co-authored-by: Stefan Popescu <spopescu@adobe.com>
  • Loading branch information
andreeadracea and spopescu committed Apr 8, 2024
1 parent 7267867 commit 34ee437
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.osgi.framework.Constants;
Expand Down Expand Up @@ -158,18 +159,21 @@ protected OEmbedClientImplConfigurationFactory.Config getConfiguration(String ur
protected InputStream getData(String url) throws IOException, IllegalArgumentException {
RequestConfig rc = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(soTimeout)
.build();
HttpClient httpClient;
HttpResponse response;
if (httpClientBuilderFactory != null
&& httpClientBuilderFactory.newBuilder() != null) {
httpClient = httpClientBuilderFactory.newBuilder()
try (CloseableHttpClient httpClient = httpClientBuilderFactory.newBuilder()
.setDefaultRequestConfig(rc)
.build();
.build()){
response = httpClient.execute(new HttpGet(url));
}
} else {
httpClient = HttpClients.custom()
try (CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(rc)
.build();
.build()){
response = httpClient.execute(new HttpGet(url));
}
}
HttpResponse response = httpClient.execute(new HttpGet(url));
return response.getEntity().getContent();
}

Expand Down

0 comments on commit 34ee437

Please sign in to comment.