Skip to content

Commit

Permalink
Catch a few more outgoing connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwoodiupui committed Sep 13, 2024
1 parent e802978 commit 787b16b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> 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();


/**
Expand Down Expand Up @@ -94,7 +92,7 @@ public int perform(DSpaceObject dso) throws IOException {
protected List<String> getURLs(Item item) {
// Get URIs from anyschema.anyelement.uri.*
List<MetadataValue> urls = itemService.getMetadata(item, Item.ANY, Item.ANY, "uri", Item.ANY);
ArrayList<String> theURLs = new ArrayList<String>();
ArrayList<String> theURLs = new ArrayList<>();
for (MetadataValue url : urls) {
theURLs.add(url.getValue());
}
Expand Down Expand Up @@ -131,15 +129,16 @@ 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();

return code;

} catch (IOException ioe) {
// Must be a bad URL
log.debug("Bad link: " + ioe.getMessage());
log.debug("Bad link: {}", ioe::getMessage);
return 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -53,8 +53,15 @@ public class LiveImportClientImpl implements LiveImportClient {
@Override
public String executeHttpGetRequest(int timeout, String URL, Map<String, Map<String, String>> 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);
Expand All @@ -75,7 +82,7 @@ public String executeHttpGetRequest(int timeout, String URL, Map<String, Map<Str
if (log.isDebugEnabled()) {
log.debug("Performing GET request to \"" + uri + "\"...");
}
HttpResponse httpResponse = httpClient.execute(method);
HttpResponse httpResponse = theClient.execute(method);
if (isNotSuccessfull(httpResponse)) {
throw new RuntimeException("The request failed with: " + getStatusCode(httpResponse) + " code, reason= "
+ httpResponse.getStatusLine().getReasonPhrase());
Expand All @@ -95,9 +102,15 @@ public String executeHttpGetRequest(int timeout, String URL, Map<String, Map<Str
@Override
public String executeHttpPostRequest(String URL, Map<String, Map<String, String>> params, String entry) {
HttpPost method = null;
try (CloseableHttpClient httpClient = Optional.ofNullable(this.httpClient)
.orElseGet(HttpClients::createDefault)) {

CloseableHttpClient theClient;
if (null == httpClient) {
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
ProxyUtils.addProxy(clientBuilder);
theClient = clientBuilder.build();
} else {
theClient = httpClient;
}
try (theClient) {
Builder requestConfigBuilder = RequestConfig.custom();
RequestConfig defaultRequestConfig = requestConfigBuilder.build();

Expand All @@ -113,7 +126,7 @@ public String executeHttpPostRequest(String URL, Map<String, Map<String, String>
if (log.isDebugEnabled()) {
log.debug("Performing POST request to \"" + uri + "\"..." );
}
HttpResponse httpResponse = httpClient.execute(method);
HttpResponse httpResponse = theClient.execute(method);
if (isNotSuccessfull(httpResponse)) {
throw new RuntimeException();
}
Expand Down Expand Up @@ -142,7 +155,7 @@ private void configureProxy(HttpRequestBase method, RequestConfig defaultRequest

/**
* Allows to set the header parameters to the HTTP Post method
*
*
* @param method HttpPost method
* @param params This map contains the header params to be included in the request.
*/
Expand All @@ -157,7 +170,7 @@ private void setHeaderParams(HttpPost method, Map<String, Map<String, String>> 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)
Expand Down

0 comments on commit 787b16b

Please sign in to comment.