[DURACOM-109] Continued configuring proxy for other classes

This commit is contained in:
Elios Buzo
2025-04-17 17:55:05 +02:00
parent 9c4422e2ee
commit 75b98f90ca
10 changed files with 43 additions and 71 deletions

View File

@@ -93,7 +93,7 @@ public class DSpaceHttpClientFactory {
public CloseableHttpClient buildWithoutAutomaticRetries(int maxConnTotal) { public CloseableHttpClient buildWithoutAutomaticRetries(int maxConnTotal) {
HttpClientBuilder clientBuilder = HttpClientBuilder.create() HttpClientBuilder clientBuilder = HttpClientBuilder.create()
.disableAutomaticRetries() .disableAutomaticRetries()
.setMaxConnTotal(5); .setMaxConnTotal(maxConnTotal);
return build(clientBuilder, true); return build(clientBuilder, true);
} }

View File

@@ -11,8 +11,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@@ -21,7 +19,11 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream; import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat; import org.dspace.content.BitstreamFormat;
@@ -1312,11 +1314,11 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester {
// we will assume all external files are available via URLs. // we will assume all external files are available via URLs.
try { try {
// attempt to open a connection to given URL // attempt to open a connection to given URL
URL fileURL = new URL(path); CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build();
URLConnection connection = fileURL.openConnection(); CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(path));
// open stream to access file contents // open stream to access file contents
return connection.getInputStream(); return httpResponse.getEntity().getContent();
} catch (IOException io) { } catch (IOException io) {
log log
.error("Unable to retrieve external file from URL '" .error("Unable to retrieve external file from URL '"

View File

@@ -9,11 +9,15 @@ package org.dspace.ctask.general;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory;
import org.dspace.content.DSpaceObject; import org.dspace.content.DSpaceObject;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.content.MetadataValue; import org.dspace.content.MetadataValue;
@@ -136,15 +140,15 @@ public class BasicLinkChecker extends AbstractCurationTask {
*/ */
protected int getResponseStatus(String url, int redirects) { protected int getResponseStatus(String url, int redirects) {
try { try {
URL theURL = new URL(url); RequestConfig config = RequestConfig.custom().setRedirectsEnabled(true).build();
HttpURLConnection connection = (HttpURLConnection) theURL.openConnection(); CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().buildWithRequestConfig(config);
connection.setInstanceFollowRedirects(true); CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(url));
int statusCode = connection.getResponseCode(); int statusCode = httpResponse.getStatusLine().getStatusCode();
int maxRedirect = configurationService.getIntProperty("curate.checklinks.max-redirect", 0); int maxRedirect = configurationService.getIntProperty("curate.checklinks.max-redirect", 0);
if ((statusCode == HttpURLConnection.HTTP_MOVED_TEMP || statusCode == HttpURLConnection.HTTP_MOVED_PERM || if ((statusCode == HttpURLConnection.HTTP_MOVED_TEMP || statusCode == HttpURLConnection.HTTP_MOVED_PERM ||
statusCode == HttpURLConnection.HTTP_SEE_OTHER)) { statusCode == HttpURLConnection.HTTP_SEE_OTHER)) {
connection.disconnect(); httpClient.close();
String newUrl = connection.getHeaderField("Location"); String newUrl = httpResponse.getFirstHeader("Location").getValue();
if (newUrl != null && (maxRedirect >= redirects || maxRedirect == -1)) { if (newUrl != null && (maxRedirect >= redirects || maxRedirect == -1)) {
redirects++; redirects++;
return getResponseStatus(newUrl, redirects); return getResponseStatus(newUrl, redirects);

View File

@@ -18,9 +18,9 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory;
import org.dspace.google.GoogleAnalyticsEvent; import org.dspace.google.GoogleAnalyticsEvent;
/** /**
@@ -42,7 +42,7 @@ public class GoogleAnalyticsClientImpl implements GoogleAnalyticsClient {
public GoogleAnalyticsClientImpl(String keyPrefix, GoogleAnalyticsClientRequestBuilder requestBuilder) { public GoogleAnalyticsClientImpl(String keyPrefix, GoogleAnalyticsClientRequestBuilder requestBuilder) {
this.keyPrefix = keyPrefix; this.keyPrefix = keyPrefix;
this.requestBuilder = requestBuilder; this.requestBuilder = requestBuilder;
this.httpclient = HttpClients.createDefault(); this.httpclient = DSpaceHttpClientFactory.getInstance().build();
} }
@Override @Override

View File

@@ -12,12 +12,14 @@ import static org.dspace.iiif.canvasdimension.Util.checkDimensions;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory;
import org.dspace.content.Bitstream; import org.dspace.content.Bitstream;
import org.dspace.iiif.util.IIIFSharedUtils; import org.dspace.iiif.util.IIIFSharedUtils;
@@ -35,14 +37,11 @@ public class IIIFApiQueryServiceImpl implements IIIFApiQueryService {
public int[] getImageDimensions(Bitstream bitstream) { public int[] getImageDimensions(Bitstream bitstream) {
int[] arr = new int[2]; int[] arr = new int[2];
String path = IIIFSharedUtils.getInfoJsonPath(bitstream); String path = IIIFSharedUtils.getInfoJsonPath(bitstream);
URL url;
BufferedReader in = null; BufferedReader in = null;
try { try {
url = new URL(path); CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build();
HttpURLConnection con = (HttpURLConnection) url.openConnection(); CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(path));
con.setRequestMethod("GET"); in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine; String inputLine;
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) { while ((inputLine = in.readLine()) != null) {

View File

@@ -17,19 +17,16 @@ import java.util.Optional;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory;
import org.dspace.services.ConfigurationService; import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -53,16 +50,12 @@ public class LiveImportClientImpl implements LiveImportClient {
@Override @Override
public String executeHttpGetRequest(int timeout, String URL, Map<String, Map<String, String>> params) { public String executeHttpGetRequest(int timeout, String URL, Map<String, Map<String, String>> params) {
HttpGet method = null; HttpGet method = null;
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(timeout).build();
try (CloseableHttpClient httpClient = Optional.ofNullable(this.httpClient) try (CloseableHttpClient httpClient = Optional.ofNullable(this.httpClient)
.orElseGet(HttpClients::createDefault)) { .orElse(DSpaceHttpClientFactory.getInstance().buildWithRequestConfig(config))) {
Builder requestConfigBuilder = RequestConfig.custom();
requestConfigBuilder.setConnectionRequestTimeout(timeout);
RequestConfig defaultRequestConfig = requestConfigBuilder.build();
String uri = buildUrl(URL, params.get(URI_PARAMETERS)); String uri = buildUrl(URL, params.get(URI_PARAMETERS));
method = new HttpGet(uri); method = new HttpGet(uri);
method.setConfig(defaultRequestConfig); method.setConfig(config);
Map<String, String> headerParams = params.get(HEADER_PARAMETERS); Map<String, String> headerParams = params.get(HEADER_PARAMETERS);
if (MapUtils.isNotEmpty(headerParams)) { if (MapUtils.isNotEmpty(headerParams)) {
@@ -71,7 +64,6 @@ public class LiveImportClientImpl implements LiveImportClient {
} }
} }
configureProxy(method, defaultRequestConfig);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Performing GET request to \"" + uri + "\"..."); log.debug("Performing GET request to \"" + uri + "\"...");
} }
@@ -95,21 +87,17 @@ public class LiveImportClientImpl implements LiveImportClient {
@Override @Override
public String executeHttpPostRequest(String URL, Map<String, Map<String, String>> params, String entry) { public String executeHttpPostRequest(String URL, Map<String, Map<String, String>> params, String entry) {
HttpPost method = null; HttpPost method = null;
RequestConfig config = RequestConfig.custom().build();
try (CloseableHttpClient httpClient = Optional.ofNullable(this.httpClient) try (CloseableHttpClient httpClient = Optional.ofNullable(this.httpClient)
.orElseGet(HttpClients::createDefault)) { .orElse(DSpaceHttpClientFactory.getInstance().buildWithRequestConfig(config))) {
Builder requestConfigBuilder = RequestConfig.custom();
RequestConfig defaultRequestConfig = requestConfigBuilder.build();
String uri = buildUrl(URL, params.get(URI_PARAMETERS)); String uri = buildUrl(URL, params.get(URI_PARAMETERS));
method = new HttpPost(uri); method = new HttpPost(uri);
method.setConfig(defaultRequestConfig);
if (StringUtils.isNotBlank(entry)) { if (StringUtils.isNotBlank(entry)) {
method.setEntity(new StringEntity(entry)); method.setEntity(new StringEntity(entry));
} }
setHeaderParams(method, params); setHeaderParams(method, params);
configureProxy(method, defaultRequestConfig);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Performing POST request to \"" + uri + "\"..." ); log.debug("Performing POST request to \"" + uri + "\"..." );
} }
@@ -129,17 +117,6 @@ public class LiveImportClientImpl implements LiveImportClient {
return StringUtils.EMPTY; return StringUtils.EMPTY;
} }
private void configureProxy(HttpRequestBase method, RequestConfig defaultRequestConfig) {
String proxyHost = configurationService.getProperty("http.proxy.host");
String proxyPort = configurationService.getProperty("http.proxy.port");
if (StringUtils.isNotBlank(proxyHost) && StringUtils.isNotBlank(proxyPort)) {
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setProxy(new HttpHost(proxyHost, Integer.parseInt(proxyPort), "http"))
.build();
method.setConfig(requestConfig);
}
}
/** /**
* Allows to set the header parameters to the HTTP Post method * Allows to set the header parameters to the HTTP Post method
* *

View File

@@ -10,9 +10,6 @@ package org.dspace.license;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@@ -328,23 +325,14 @@ public class CCLicenseConnectorServiceImpl implements CCLicenseConnectorService,
@Override @Override
public Document retrieveLicenseRDFDoc(String licenseURI) throws IOException { public Document retrieveLicenseRDFDoc(String licenseURI) throws IOException {
String ccLicenseUrl = configurationService.getProperty("cc.api.rooturl"); String ccLicenseUrl = configurationService.getProperty("cc.api.rooturl");
String issueUrl = ccLicenseUrl + "/details?license-uri=" + licenseURI; String issueUrl = ccLicenseUrl + "/details?license-uri=" + licenseURI;
URL request_url;
try {
request_url = new URL(issueUrl);
} catch (MalformedURLException e) {
return null;
}
URLConnection connection = request_url.openConnection();
connection.setDoOutput(true);
try { try {
CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build();
CloseableHttpResponse httpResponse = httpClient.execute(new HttpPost(issueUrl));
// parsing document from input stream // parsing document from input stream
InputStream stream = connection.getInputStream(); InputStream stream = httpResponse.getEntity().getContent();
Document doc = parser.build(stream); Document doc = parser.build(stream);
return doc; return doc;
} catch (Exception e) { } catch (Exception e) {
log.error("Error while retrieving the license document for URI: " + licenseURI, e); log.error("Error while retrieving the license document for URI: " + licenseURI, e);
} }

View File

@@ -22,9 +22,9 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.content.QAEvent; import org.dspace.content.QAEvent;
import org.dspace.content.service.ItemService; import org.dspace.content.service.ItemService;
@@ -123,7 +123,7 @@ public class QAEventActionServiceImpl implements QAEventActionService {
node.put("eventId", eventId); node.put("eventId", eventId);
node.put("status", status); node.put("status", status);
StringEntity requestEntity = new StringEntity(node.toString(), ContentType.APPLICATION_JSON); StringEntity requestEntity = new StringEntity(node.toString(), ContentType.APPLICATION_JSON);
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = DSpaceHttpClientFactory.getInstance().buildWithoutProxy();
HttpPost postMethod = new HttpPost(ackwnoledgeCallback); HttpPost postMethod = new HttpPost(ackwnoledgeCallback);
postMethod.setEntity(requestEntity); postMethod.setEntity(requestEntity);
try { try {

View File

@@ -1223,7 +1223,7 @@ public class SolrLoggerServiceImpl implements SolrLoggerService, InitializingBea
+ "." + "."
+ i + i
+ ".csv"); + ".csv");
try (CloseableHttpClient hc = DSpaceHttpClientFactory.getInstance().build()) { try (CloseableHttpClient hc = DSpaceHttpClientFactory.getInstance().buildWithoutProxy()) {
HttpResponse response = hc.execute(get); HttpResponse response = hc.execute(get);
csvInputstream = response.getEntity().getContent(); csvInputstream = response.getEntity().getContent();
//Write the csv output to a file ! //Write the csv output to a file !
@@ -1365,7 +1365,7 @@ public class SolrLoggerServiceImpl implements SolrLoggerService, InitializingBea
HttpGet get = new HttpGet(solrRequestUrl); HttpGet get = new HttpGet(solrRequestUrl);
List<String[]> rows; List<String[]> rows;
try (CloseableHttpClient hc = DSpaceHttpClientFactory.getInstance().build()) { try (CloseableHttpClient hc = DSpaceHttpClientFactory.getInstance().buildWithoutProxy()) {
HttpResponse response = hc.execute(get); HttpResponse response = hc.execute(get);
InputStream csvOutput = response.getEntity().getContent(); InputStream csvOutput = response.getEntity().getContent();
Reader csvReader = new InputStreamReader(csvOutput); Reader csvReader = new InputStreamReader(csvOutput);

View File

@@ -411,6 +411,8 @@ http.proxy.host =
# port number of proxy server # port number of proxy server
http.proxy.port = http.proxy.port =
http.proxy.hosts-to-ignore = 127.0.0.1, localhost
# If enabled, the logging and the Solr statistics system will look for an X-Forwarded-For header. # If enabled, the logging and the Solr statistics system will look for an X-Forwarded-For header.
# If it finds it, it will use this for the user IP address. # If it finds it, it will use this for the user IP address.
# NOTE: This is required to be enabled if you plan to use the Angular UI, as the server-side rendering provided in # NOTE: This is required to be enabled if you plan to use the Angular UI, as the server-side rendering provided in