Skip to content

Commit

Permalink
Use Closeable HTTP client in order to close the network connection af…
Browse files Browse the repository at this point in the history
…ter use
  • Loading branch information
andreeadracea committed Apr 5, 2024
1 parent d87fc10 commit 99eb30c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 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 All @@ -47,6 +48,8 @@
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Deactivate;

import com.adobe.cq.wcm.core.components.services.embed.OEmbedClient;
import com.adobe.cq.wcm.core.components.services.embed.OEmbedResponse;
Expand Down Expand Up @@ -77,6 +80,8 @@ public class OEmbedClientImpl implements OEmbedClient {
private ObjectMapper mapper = new ObjectMapper();
private static JAXBContext jaxbContext;

private CloseableHttpClient httpClient;

static {
try {
jaxbContext = JAXBContext.newInstance(OEmbedXMLResponseImpl.class);
Expand Down Expand Up @@ -155,22 +160,45 @@ protected OEmbedClientImplConfigurationFactory.Config getConfiguration(String ur
return null;
}

protected InputStream getData(String url) throws IOException, IllegalArgumentException {
RequestConfig rc = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(soTimeout)
@Activate
protected void activate() {
if (connectionTimeout < 0) {
throw new IllegalArgumentException("Connection timeout value cannot be less than 0");
}
if (soTimeout < 0) {
throw new IllegalArgumentException("Socket timeout value cannot be less than 0");
}
RequestConfig rc = RequestConfig.custom()
.setConnectTimeout(connectionTimeout)
.setConnectionRequestTimeout(connectionTimeout)
.setSocketTimeout(soTimeout)
.build();
HttpClient httpClient;

if (httpClientBuilderFactory != null
&& httpClientBuilderFactory.newBuilder() != null) {
&& httpClientBuilderFactory.newBuilder() != null) {
httpClient = httpClientBuilderFactory.newBuilder()
.setDefaultRequestConfig(rc)
.build();
.setDefaultRequestConfig(rc)
.build();
} else {
httpClient = HttpClients.custom()
.setDefaultRequestConfig(rc)
.build();
.setDefaultRequestConfig(rc)
.build();
}

}
@Deactivate
protected void deactivate() throws IOException {
httpClient.close();
}

protected InputStream getData(String url) throws IOException, IllegalArgumentException {
try {
HttpResponse response = httpClient.execute(new HttpGet(url));
LOGGER.debug("GETing data from '{}' succeeded: response: {}", url, response);
return response.getEntity().getContent();
}catch (IOException e) {
LOGGER.error("GETing data from '{}' failed: {}", url, e.getMessage(), e);
}
HttpResponse response = httpClient.execute(new HttpGet(url));
return response.getEntity().getContent();
}

protected String buildURL(String endpoint, String url, String format, Integer maxWidth, Integer maxHeight) throws MalformedURLException {
Expand Down
4 changes: 2 additions & 2 deletions content/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
<execution>
<phase>prepare-package</phase>
<configuration>
<tasks>
<target>
<replace file="target/jcr_root/libs/core/wcm/components/breadcrumb/v2/breadcrumb/.content.xml" token=".core-wcm" value="Core Structure" />
<replace file="target/jcr_root/libs/core/wcm/components/breadcrumb/v3/breadcrumb/.content.xml" token=".core-wcm" value="Core Structure" />
<replace file="target/jcr_root/libs/core/wcm/components/languagenavigation/v1/languagenavigation/.content.xml" token=".core-wcm" value="Core Structure" />
Expand Down Expand Up @@ -317,7 +317,7 @@
<replace file="target/jcr_root/libs/core/wcm/components/form/hidden/v2/hidden/.content.xml" token=".core-wcm-form" value="Core Form" />
<replace file="target/jcr_root/libs/core/wcm/components/form/options/v2/options/.content.xml" token=".core-wcm-form" value="Core Form" />
<replace file="target/jcr_root/libs/core/wcm/components/form/text/v2/text/.content.xml" token=".core-wcm-form" value="Core Form" />
</tasks>
</target>
</configuration>
<goals>
<goal>run</goal>
Expand Down

0 comments on commit 99eb30c

Please sign in to comment.