From 787b16bf52c4e19fb171553778cca9420dabd010 Mon Sep 17 00:00:00 2001 From: "Mark H. Wood" Date: Fri, 13 Sep 2024 17:03:50 -0400 Subject: [PATCH] Catch a few more outgoing connections. --- .../packager/AbstractMETSIngester.java | 3 +- .../ctask/general/BasicLinkChecker.java | 13 ++++--- .../service/LiveImportClientImpl.java | 35 +++++++++++++------ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java b/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java index 0ed0abe21825..5779bd3407bd 100644 --- a/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java +++ b/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java @@ -49,6 +49,7 @@ import org.dspace.handle.service.HandleService; import org.dspace.services.ConfigurationService; import org.dspace.services.factory.DSpaceServicesFactory; +import org.dspace.util.ProxyUtils; import org.dspace.workflow.WorkflowException; import org.dspace.workflow.factory.WorkflowServiceFactory; import org.jdom2.Element; @@ -1313,7 +1314,7 @@ protected static InputStream getFileInputStream(File pkgFile, try { // attempt to open a connection to given URL URL fileURL = new URL(path); - URLConnection connection = fileURL.openConnection(); + URLConnection connection = fileURL.openConnection(ProxyUtils.getProxy()); // open stream to access file contents return connection.getInputStream(); diff --git a/dspace-api/src/main/java/org/dspace/ctask/general/BasicLinkChecker.java b/dspace-api/src/main/java/org/dspace/ctask/general/BasicLinkChecker.java index 756d65a0ad09..5a2c952ba541 100644 --- a/dspace-api/src/main/java/org/dspace/ctask/general/BasicLinkChecker.java +++ b/dspace-api/src/main/java/org/dspace/ctask/general/BasicLinkChecker.java @@ -19,6 +19,7 @@ import org.dspace.content.MetadataValue; import org.dspace.curate.AbstractCurationTask; import org.dspace.curate.Curator; +import org.dspace.util.ProxyUtils; /** * A basic link checker that is designed to be extended. By default this link checker @@ -36,11 +37,8 @@ public class BasicLinkChecker extends AbstractCurationTask { // The status of the link checking of this item private int status = Curator.CURATE_UNSET; - // The results of link checking this item - private List results = null; - // The log4j logger for this class - private static Logger log = org.apache.logging.log4j.LogManager.getLogger(BasicLinkChecker.class); + private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(); /** @@ -94,7 +92,7 @@ public int perform(DSpaceObject dso) throws IOException { protected List getURLs(Item item) { // Get URIs from anyschema.anyelement.uri.* List urls = itemService.getMetadata(item, Item.ANY, Item.ANY, "uri", Item.ANY); - ArrayList theURLs = new ArrayList(); + ArrayList theURLs = new ArrayList<>(); for (MetadataValue url : urls) { theURLs.add(url.getValue()); } @@ -131,7 +129,8 @@ protected boolean checkURL(String url, StringBuilder results) { protected int getResponseStatus(String url) { try { URL theURL = new URL(url); - HttpURLConnection connection = (HttpURLConnection) theURL.openConnection(); + HttpURLConnection connection + = (HttpURLConnection) theURL.openConnection(ProxyUtils.getProxy()); int code = connection.getResponseCode(); connection.disconnect(); @@ -139,7 +138,7 @@ protected int getResponseStatus(String url) { } catch (IOException ioe) { // Must be a bad URL - log.debug("Bad link: " + ioe.getMessage()); + log.debug("Bad link: {}", ioe::getMessage); return 0; } } diff --git a/dspace-api/src/main/java/org/dspace/importer/external/liveimportclient/service/LiveImportClientImpl.java b/dspace-api/src/main/java/org/dspace/importer/external/liveimportclient/service/LiveImportClientImpl.java index 1a8a7a7861ed..58809cbd58d4 100644 --- a/dspace-api/src/main/java/org/dspace/importer/external/liveimportclient/service/LiveImportClientImpl.java +++ b/dspace-api/src/main/java/org/dspace/importer/external/liveimportclient/service/LiveImportClientImpl.java @@ -12,7 +12,6 @@ import java.nio.charset.Charset; import java.util.Map; import java.util.Objects; -import java.util.Optional; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.IOUtils; @@ -27,10 +26,11 @@ import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.dspace.services.ConfigurationService; +import org.dspace.util.ProxyUtils; import org.springframework.beans.factory.annotation.Autowired; /** @@ -53,8 +53,15 @@ public class LiveImportClientImpl implements LiveImportClient { @Override public String executeHttpGetRequest(int timeout, String URL, Map> params) { HttpGet method = null; - try (CloseableHttpClient httpClient = Optional.ofNullable(this.httpClient) - .orElseGet(HttpClients::createDefault)) { + CloseableHttpClient theClient; + if (null != httpClient) { + theClient = httpClient; + } else { + HttpClientBuilder clientBuilder = HttpClientBuilder.create(); + ProxyUtils.addProxy(clientBuilder); + theClient = clientBuilder.build(); + } + try (theClient) { Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout(timeout); @@ -75,7 +82,7 @@ public String executeHttpGetRequest(int timeout, String URL, Map> p /** * This method allows you to add the parameters contained in the requestParams map to the URL - * + * * @param URL URL * @param requestParams This map contains the parameters to be included in the request. * Each parameter will be added to the url?(key=value)