mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 07:23:08 +00:00
Merge pull request #10639 from 4Science/task/main/DURACOM-109
Proxy Configuration and Connection Leak using HTTP Clients
This commit is contained in:
@@ -7,4 +7,5 @@
|
|||||||
<!-- TODO: We should have these turned on. But, currently there's a known bug with indentation checks
|
<!-- TODO: We should have these turned on. But, currently there's a known bug with indentation checks
|
||||||
on JMockIt Expectations blocks and similar. See https://github.com/checkstyle/checkstyle/issues/3739 -->
|
on JMockIt Expectations blocks and similar. See https://github.com/checkstyle/checkstyle/issues/3739 -->
|
||||||
<suppress checks="Indentation" files="src[/\\]test[/\\]java"/>
|
<suppress checks="Indentation" files="src[/\\]test[/\\]java"/>
|
||||||
|
<suppress checks="Regexp" files="DSpaceHttpClientFactory\.java"/>
|
||||||
</suppressions>
|
</suppressions>
|
||||||
|
@@ -136,5 +136,22 @@ For more information on CheckStyle configurations below, see: http://checkstyle.
|
|||||||
<module name="OneStatementPerLine"/>
|
<module name="OneStatementPerLine"/>
|
||||||
<!-- Require that "catch" statements are not empty (must at least contain a comment) -->
|
<!-- Require that "catch" statements are not empty (must at least contain a comment) -->
|
||||||
<module name="EmptyCatchBlock"/>
|
<module name="EmptyCatchBlock"/>
|
||||||
|
|
||||||
|
<!-- Require to use DSpaceHttpClientFactory.getClient() statement instead of creating directly the client -->
|
||||||
|
<module name="Regexp">
|
||||||
|
<property name="format" value="HttpClientBuilder\.create\s*\(\s*\)" />
|
||||||
|
<property name="message" value="Use DSpaceHttpClientFactory.getClient() instead of HttpClientBuilder.create()" />
|
||||||
|
<property name="illegalPattern" value="true"/>
|
||||||
|
<property name="ignoreComments" value="true"/>
|
||||||
|
</module>
|
||||||
|
<!-- Require to use DSpaceHttpClientFactory.getClient() statement instead of creating directly the client -->
|
||||||
|
<module name="Regexp">
|
||||||
|
<property name="format" value="HttpClients\.createDefault\s*\(\s*\)" />
|
||||||
|
<property name="message" value="Use DSpaceHttpClientFactory.getClient() instead of HttpClients.createDefault()" />
|
||||||
|
<property name="illegalPattern" value="true"/>
|
||||||
|
<property name="ignoreComments" value="true"/>
|
||||||
|
</module>
|
||||||
|
|
||||||
|
|
||||||
</module>
|
</module>
|
||||||
</module>
|
</module>
|
||||||
|
@@ -774,6 +774,7 @@
|
|||||||
<version>1.19.0</version>
|
<version>1.19.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Used for Solr core export/import -->
|
<!-- Used for Solr core export/import -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.opencsv</groupId>
|
<groupId>com.opencsv</groupId>
|
||||||
@@ -942,5 +943,10 @@
|
|||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>mockwebserver</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@@ -0,0 +1,152 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
package org.dspace.app.client;
|
||||||
|
|
||||||
|
import static org.apache.commons.collections4.ListUtils.emptyIfNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.HttpResponseInterceptor;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
|
import org.dspace.utils.DSpace;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory of {@link HttpClient} with common configurations.
|
||||||
|
*
|
||||||
|
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DSpaceHttpClientFactory {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigurationService configurationService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DSpaceProxyRoutePlanner proxyRoutePlanner;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private List<HttpRequestInterceptor> requestInterceptors;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private List<HttpResponseInterceptor> responseInterceptors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an instance of {@link DSpaceHttpClientFactory} from the Spring context.
|
||||||
|
* @return the bean instance
|
||||||
|
*/
|
||||||
|
public static DSpaceHttpClientFactory getInstance() {
|
||||||
|
return new DSpace().getSingletonService(DSpaceHttpClientFactory.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build an instance of {@link HttpClient} setting the proxy if configured.
|
||||||
|
*
|
||||||
|
* @return the client
|
||||||
|
*/
|
||||||
|
public CloseableHttpClient build() {
|
||||||
|
return build(HttpClientBuilder.create(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return a Builder if an instance of {@link HttpClient} pre-setting the proxy if configured.
|
||||||
|
*
|
||||||
|
* @return the client
|
||||||
|
*/
|
||||||
|
public HttpClientBuilder builder(boolean setProxy) {
|
||||||
|
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
|
||||||
|
if (setProxy) {
|
||||||
|
clientBuilder.setRoutePlanner(proxyRoutePlanner);
|
||||||
|
}
|
||||||
|
getRequestInterceptors().forEach(clientBuilder::addInterceptorLast);
|
||||||
|
getResponseInterceptors().forEach(clientBuilder::addInterceptorLast);
|
||||||
|
return clientBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build an instance of {@link HttpClient} without setting the proxy, even if
|
||||||
|
* configured.
|
||||||
|
*
|
||||||
|
* @return the client
|
||||||
|
*/
|
||||||
|
public CloseableHttpClient buildWithoutProxy() {
|
||||||
|
return build(HttpClientBuilder.create(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build an instance of {@link HttpClient} setting the proxy if configured,
|
||||||
|
* disabling automatic retries and setting the maximum total connection.
|
||||||
|
*
|
||||||
|
* @param maxConnTotal the maximum total connection value
|
||||||
|
* @return the client
|
||||||
|
*/
|
||||||
|
public CloseableHttpClient buildWithoutAutomaticRetries(int maxConnTotal) {
|
||||||
|
HttpClientBuilder clientBuilder = HttpClientBuilder.create()
|
||||||
|
.disableAutomaticRetries()
|
||||||
|
.setMaxConnTotal(maxConnTotal);
|
||||||
|
return build(clientBuilder, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build an instance of {@link HttpClient} setting the proxy if configured with
|
||||||
|
* the given request configuration.
|
||||||
|
* @param requestConfig the request configuration
|
||||||
|
* @return the client
|
||||||
|
*/
|
||||||
|
public CloseableHttpClient buildWithRequestConfig(RequestConfig requestConfig) {
|
||||||
|
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
|
||||||
|
.setDefaultRequestConfig(requestConfig);
|
||||||
|
return build(httpClientBuilder, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CloseableHttpClient build(HttpClientBuilder clientBuilder, boolean setProxy) {
|
||||||
|
if (setProxy) {
|
||||||
|
clientBuilder.setRoutePlanner(proxyRoutePlanner);
|
||||||
|
}
|
||||||
|
getRequestInterceptors().forEach(clientBuilder::addInterceptorLast);
|
||||||
|
getResponseInterceptors().forEach(clientBuilder::addInterceptorLast);
|
||||||
|
return clientBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigurationService getConfigurationService() {
|
||||||
|
return configurationService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfigurationService(ConfigurationService configurationService) {
|
||||||
|
this.configurationService = configurationService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<HttpRequestInterceptor> getRequestInterceptors() {
|
||||||
|
return emptyIfNull(requestInterceptors);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequestInterceptors(List<HttpRequestInterceptor> requestInterceptors) {
|
||||||
|
this.requestInterceptors = requestInterceptors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<HttpResponseInterceptor> getResponseInterceptors() {
|
||||||
|
return emptyIfNull(responseInterceptors);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponseInterceptors(List<HttpResponseInterceptor> responseInterceptors) {
|
||||||
|
this.responseInterceptors = responseInterceptors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DSpaceProxyRoutePlanner getProxyRoutePlanner() {
|
||||||
|
return proxyRoutePlanner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProxyRoutePlanner(DSpaceProxyRoutePlanner proxyRoutePlanner) {
|
||||||
|
this.proxyRoutePlanner = proxyRoutePlanner;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,73 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
package org.dspace.app.client;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.http.HttpException;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.HttpRequest;
|
||||||
|
import org.apache.http.impl.conn.DefaultRoutePlanner;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension of {@link DefaultRoutePlanner} that determine the proxy based on
|
||||||
|
* the configuration service, ignoring configured hosts.
|
||||||
|
*
|
||||||
|
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DSpaceProxyRoutePlanner extends DefaultRoutePlanner {
|
||||||
|
|
||||||
|
private ConfigurationService configurationService;
|
||||||
|
|
||||||
|
public DSpaceProxyRoutePlanner(ConfigurationService configurationService) {
|
||||||
|
super(null);
|
||||||
|
this.configurationService = configurationService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
|
||||||
|
if (isTargetHostConfiguredToBeIgnored(target)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String proxyHost = configurationService.getProperty("http.proxy.host");
|
||||||
|
String proxyPort = configurationService.getProperty("http.proxy.port");
|
||||||
|
if (StringUtils.isAnyBlank(proxyHost, proxyPort)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new HttpHost(proxyHost, Integer.parseInt(proxyPort), "http");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new RuntimeException("Invalid proxy port configuration: " + proxyPort);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isTargetHostConfiguredToBeIgnored(HttpHost target) {
|
||||||
|
String[] hostsToIgnore = configurationService.getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
if (ArrayUtils.isEmpty(hostsToIgnore)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Arrays.stream(hostsToIgnore)
|
||||||
|
.anyMatch(host -> matchesHost(host, target.getHostName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean matchesHost(String hostPattern, String hostName) {
|
||||||
|
if (hostName.equals(hostPattern)) {
|
||||||
|
return true;
|
||||||
|
} else if (hostPattern.startsWith("*")) {
|
||||||
|
return hostName.endsWith(StringUtils.removeStart(hostPattern, "*"));
|
||||||
|
} else if (hostPattern.endsWith("*")) {
|
||||||
|
return hostName.startsWith(StringUtils.removeEnd(hostPattern, "*"));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@@ -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.HttpClientBuilder;
|
|
||||||
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.app.ldn.model.Notification;
|
import org.dspace.app.ldn.model.Notification;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
@@ -34,22 +34,14 @@ public class SendLDNMessageAction implements LDNAction {
|
|||||||
|
|
||||||
private static final Logger log = LogManager.getLogger(SendLDNMessageAction.class);
|
private static final Logger log = LogManager.getLogger(SendLDNMessageAction.class);
|
||||||
|
|
||||||
private CloseableHttpClient client = null;
|
private CloseableHttpClient client;
|
||||||
|
|
||||||
public SendLDNMessageAction() {
|
public SendLDNMessageAction() {
|
||||||
HttpClientBuilder builder = HttpClientBuilder.create();
|
|
||||||
client = builder
|
|
||||||
.disableAutomaticRetries()
|
|
||||||
.setMaxConnTotal(5)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SendLDNMessageAction(CloseableHttpClient client) {
|
public SendLDNMessageAction(CloseableHttpClient client) {
|
||||||
this();
|
|
||||||
if (client != null) {
|
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LDNActionStatus execute(Context context, Notification notification, Item item) throws Exception {
|
public LDNActionStatus execute(Context context, Notification notification, Item item) throws Exception {
|
||||||
@@ -66,9 +58,10 @@ public class SendLDNMessageAction implements LDNAction {
|
|||||||
// NOTE: Github believes there is a "Potential server-side request forgery due to a user-provided value"
|
// NOTE: Github believes there is a "Potential server-side request forgery due to a user-provided value"
|
||||||
// This is a false positive because the LDN Service URL is configured by the user from DSpace.
|
// This is a false positive because the LDN Service URL is configured by the user from DSpace.
|
||||||
// See the frontend configuration at [dspace.ui.url]/admin/ldn/services
|
// See the frontend configuration at [dspace.ui.url]/admin/ldn/services
|
||||||
try (
|
if (client == null) {
|
||||||
CloseableHttpResponse response = client.execute(httpPost);
|
client = DSpaceHttpClientFactory.getInstance().buildWithoutAutomaticRetries(5);
|
||||||
) {
|
}
|
||||||
|
try (CloseableHttpResponse response = client.execute(httpPost)) {
|
||||||
if (isSuccessful(response.getStatusLine().getStatusCode())) {
|
if (isSuccessful(response.getStatusLine().getStatusCode())) {
|
||||||
result = LDNActionStatus.CONTINUE;
|
result = LDNActionStatus.CONTINUE;
|
||||||
} else if (isRedirect(response.getStatusLine().getStatusCode())) {
|
} else if (isRedirect(response.getStatusLine().getStatusCode())) {
|
||||||
@@ -77,6 +70,7 @@ public class SendLDNMessageAction implements LDNAction {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
}
|
}
|
||||||
|
client.close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,9 +85,9 @@ public class SendLDNMessageAction implements LDNAction {
|
|||||||
statusCode == HttpStatus.SC_TEMPORARY_REDIRECT;
|
statusCode == HttpStatus.SC_TEMPORARY_REDIRECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LDNActionStatus handleRedirect(CloseableHttpResponse oldresponse,
|
private LDNActionStatus handleRedirect(CloseableHttpResponse oldResponse,
|
||||||
HttpPost request) throws HttpException {
|
HttpPost request) throws HttpException {
|
||||||
Header[] urls = oldresponse.getHeaders(HttpHeaders.LOCATION);
|
Header[] urls = oldResponse.getHeaders(HttpHeaders.LOCATION);
|
||||||
String url = urls.length > 0 && urls[0] != null ? urls[0].getValue() : null;
|
String url = urls.length > 0 && urls[0] != null ? urls[0].getValue() : null;
|
||||||
if (url == null) {
|
if (url == null) {
|
||||||
throw new HttpException("Error following redirect, unable to reach"
|
throw new HttpException("Error following redirect, unable to reach"
|
||||||
@@ -102,17 +96,14 @@ public class SendLDNMessageAction implements LDNAction {
|
|||||||
LDNActionStatus result = LDNActionStatus.ABORT;
|
LDNActionStatus result = LDNActionStatus.ABORT;
|
||||||
try {
|
try {
|
||||||
request.setURI(new URI(url));
|
request.setURI(new URI(url));
|
||||||
try (
|
try (CloseableHttpResponse response = client.execute(request)) {
|
||||||
CloseableHttpResponse response = client.execute(request);
|
|
||||||
) {
|
|
||||||
if (isSuccessful(response.getStatusLine().getStatusCode())) {
|
if (isSuccessful(response.getStatusLine().getStatusCode())) {
|
||||||
return LDNActionStatus.CONTINUE;
|
result = LDNActionStatus.CONTINUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error following redirect:", e);
|
log.error("Error following redirect:", e);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
return LDNActionStatus.ABORT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -17,15 +17,15 @@ import jakarta.annotation.PostConstruct;
|
|||||||
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.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
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.client.methods.HttpGet;
|
||||||
import org.apache.http.client.utils.URIBuilder;
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
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.app.sherpa.v2.SHERPAPublisherResponse;
|
import org.dspace.app.sherpa.v2.SHERPAPublisherResponse;
|
||||||
import org.dspace.app.sherpa.v2.SHERPAResponse;
|
import org.dspace.app.sherpa.v2.SHERPAResponse;
|
||||||
import org.dspace.app.sherpa.v2.SHERPAUtils;
|
import org.dspace.app.sherpa.v2.SHERPAUtils;
|
||||||
@@ -45,8 +45,6 @@ import org.springframework.cache.annotation.Cacheable;
|
|||||||
*/
|
*/
|
||||||
public class SHERPAService {
|
public class SHERPAService {
|
||||||
|
|
||||||
private CloseableHttpClient client = null;
|
|
||||||
|
|
||||||
private int maxNumberOfTries;
|
private int maxNumberOfTries;
|
||||||
private long sleepBetweenTimeouts;
|
private long sleepBetweenTimeouts;
|
||||||
private int timeout = 5000;
|
private int timeout = 5000;
|
||||||
@@ -59,19 +57,6 @@ public class SHERPAService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
ConfigurationService configurationService;
|
ConfigurationService configurationService;
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new HTTP builder with sensible defaults in constructor
|
|
||||||
*/
|
|
||||||
public SHERPAService() {
|
|
||||||
HttpClientBuilder builder = HttpClientBuilder.create();
|
|
||||||
// httpclient 4.3+ doesn't appear to have any sensible defaults any more. Setting conservative defaults as
|
|
||||||
// not to hammer the SHERPA service too much.
|
|
||||||
client = builder
|
|
||||||
.disableAutomaticRetries()
|
|
||||||
.setMaxConnTotal(5)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Complete initialization of the Bean.
|
* Complete initialization of the Bean.
|
||||||
*/
|
*/
|
||||||
@@ -132,14 +117,14 @@ public class SHERPAService {
|
|||||||
timeout,
|
timeout,
|
||||||
sleepBetweenTimeouts));
|
sleepBetweenTimeouts));
|
||||||
|
|
||||||
try {
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().buildWithoutAutomaticRetries(5)) {
|
||||||
Thread.sleep(sleepBetweenTimeouts);
|
Thread.sleep(sleepBetweenTimeouts);
|
||||||
|
|
||||||
// Construct a default HTTP method (first result)
|
// Construct a default HTTP method (first result)
|
||||||
method = constructHttpGet(type, field, predicate, value, start, limit);
|
method = constructHttpGet(type, field, predicate, value, start, limit);
|
||||||
|
|
||||||
// Execute the method
|
// Execute the method
|
||||||
HttpResponse response = client.execute(method);
|
try (CloseableHttpResponse response = client.execute(method)) {
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
log.debug(response.getStatusLine().getStatusCode() + ": "
|
log.debug(response.getStatusLine().getStatusCode() + ": "
|
||||||
@@ -173,6 +158,7 @@ public class SHERPAService {
|
|||||||
log.debug("Empty SHERPA response body for query on " + value);
|
log.debug("Empty SHERPA response body for query on " + value);
|
||||||
sherpaResponse = new SHERPAPublisherResponse("SHERPA/RoMEO returned no response");
|
sherpaResponse = new SHERPAPublisherResponse("SHERPA/RoMEO returned no response");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
String errorMessage = "Error building SHERPA v2 API URI: " + e.getMessage();
|
String errorMessage = "Error building SHERPA v2 API URI: " + e.getMessage();
|
||||||
log.error(errorMessage, e);
|
log.error(errorMessage, e);
|
||||||
@@ -235,14 +221,14 @@ public class SHERPAService {
|
|||||||
timeout,
|
timeout,
|
||||||
sleepBetweenTimeouts));
|
sleepBetweenTimeouts));
|
||||||
|
|
||||||
try {
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().buildWithoutAutomaticRetries(5)) {
|
||||||
Thread.sleep(sleepBetweenTimeouts);
|
Thread.sleep(sleepBetweenTimeouts);
|
||||||
|
|
||||||
// Construct a default HTTP method (first result)
|
// Construct a default HTTP method (first result)
|
||||||
method = constructHttpGet(type, field, predicate, value, start, limit);
|
method = constructHttpGet(type, field, predicate, value, start, limit);
|
||||||
|
|
||||||
// Execute the method
|
// Execute the method
|
||||||
HttpResponse response = client.execute(method);
|
try (CloseableHttpResponse response = client.execute(method)) {
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
log.debug(response.getStatusLine().getStatusCode() + ": "
|
log.debug(response.getStatusLine().getStatusCode() + ": "
|
||||||
@@ -275,6 +261,7 @@ public class SHERPAService {
|
|||||||
log.debug("Empty SHERPA response body for query on " + value);
|
log.debug("Empty SHERPA response body for query on " + value);
|
||||||
sherpaResponse = new SHERPAResponse("SHERPA/RoMEO returned no response");
|
sherpaResponse = new SHERPAResponse("SHERPA/RoMEO returned no response");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
String errorMessage = "Error building SHERPA v2 API URI: " + e.getMessage();
|
String errorMessage = "Error building SHERPA v2 API URI: " + e.getMessage();
|
||||||
log.error(errorMessage, e);
|
log.error(errorMessage, e);
|
||||||
|
@@ -13,12 +13,12 @@ import java.time.Instant;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpHead;
|
import org.apache.http.client.methods.HttpHead;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.dspace.app.client.DSpaceHttpClientFactory;
|
||||||
import org.dspace.app.util.dao.WebAppDAO;
|
import org.dspace.app.util.dao.WebAppDAO;
|
||||||
import org.dspace.app.util.service.WebAppService;
|
import org.dspace.app.util.service.WebAppService;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
@@ -77,8 +77,8 @@ public class WebAppServiceImpl implements WebAppService {
|
|||||||
for (WebApp app : webApps) {
|
for (WebApp app : webApps) {
|
||||||
method = new HttpHead(app.getUrl());
|
method = new HttpHead(app.getUrl());
|
||||||
int status;
|
int status;
|
||||||
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpResponse response = client.execute(method);
|
CloseableHttpResponse response = client.execute(method);
|
||||||
status = response.getStatusLine().getStatusCode();
|
status = response.getStatusLine().getStatusCode();
|
||||||
}
|
}
|
||||||
if (status != HttpStatus.SC_OK) {
|
if (status != HttpStatus.SC_OK) {
|
||||||
|
@@ -22,12 +22,13 @@ import org.apache.commons.io.IOUtils;
|
|||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpUriRequest;
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
import org.apache.http.client.methods.RequestBuilder;
|
import org.apache.http.client.methods.RequestBuilder;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
import org.dspace.app.client.DSpaceHttpClientFactory;
|
||||||
import org.dspace.authenticate.oidc.OidcClient;
|
import org.dspace.authenticate.oidc.OidcClient;
|
||||||
import org.dspace.authenticate.oidc.OidcClientException;
|
import org.dspace.authenticate.oidc.OidcClientException;
|
||||||
import org.dspace.authenticate.oidc.model.OidcTokenResponseDTO;
|
import org.dspace.authenticate.oidc.model.OidcTokenResponseDTO;
|
||||||
@@ -83,21 +84,17 @@ public class OidcClientImpl implements OidcClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T> T executeAndParseJson(HttpUriRequest httpUriRequest, Class<T> clazz) {
|
private <T> T executeAndParseJson(HttpUriRequest httpUriRequest, Class<T> clazz) {
|
||||||
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpClient client = HttpClientBuilder.create().build();
|
|
||||||
|
|
||||||
return executeAndReturns(() -> {
|
return executeAndReturns(() -> {
|
||||||
|
CloseableHttpResponse response = client.execute(httpUriRequest);
|
||||||
HttpResponse response = client.execute(httpUriRequest);
|
|
||||||
|
|
||||||
if (isNotSuccessfull(response)) {
|
if (isNotSuccessfull(response)) {
|
||||||
throw new OidcClientException(getStatusCode(response), formatErrorMessage(response));
|
throw new OidcClientException(getStatusCode(response), formatErrorMessage(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
return objectMapper.readValue(getContent(response), clazz);
|
return objectMapper.readValue(getContent(response), clazz);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> T executeAndReturns(ThrowingSupplier<T, Exception> supplier) {
|
private <T> T executeAndReturns(ThrowingSupplier<T, Exception> supplier) {
|
||||||
|
@@ -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;
|
||||||
@@ -1310,13 +1312,12 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester {
|
|||||||
if (params.getBooleanProperty("manifestOnly", false)) {
|
if (params.getBooleanProperty("manifestOnly", false)) {
|
||||||
// NOTE: since we are only dealing with a METS manifest,
|
// NOTE: since we are only dealing with a METS manifest,
|
||||||
// we will assume all external files are available via URLs.
|
// we will assume all external files are available via URLs.
|
||||||
try {
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
// attempt to open a connection to given URL
|
// attempt to open a connection to given URL
|
||||||
URL fileURL = new URL(path);
|
try (CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(path))) {
|
||||||
URLConnection connection = fileURL.openConnection();
|
|
||||||
|
|
||||||
// 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 '"
|
||||||
|
@@ -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;
|
||||||
@@ -135,24 +139,20 @@ public class BasicLinkChecker extends AbstractCurationTask {
|
|||||||
* @return The HTTP response code (e.g. 200 / 301 / 404 / 500)
|
* @return The HTTP response code (e.g. 200 / 301 / 404 / 500)
|
||||||
*/
|
*/
|
||||||
protected int getResponseStatus(String url, int redirects) {
|
protected int getResponseStatus(String url, int redirects) {
|
||||||
try {
|
RequestConfig config = RequestConfig.custom().setRedirectsEnabled(true).build();
|
||||||
URL theURL = new URL(url);
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().buildWithRequestConfig(config)) {
|
||||||
HttpURLConnection connection = (HttpURLConnection) theURL.openConnection();
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(url));
|
||||||
connection.setInstanceFollowRedirects(true);
|
int statusCode = httpResponse.getStatusLine().getStatusCode();
|
||||||
int statusCode = connection.getResponseCode();
|
|
||||||
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();
|
String newUrl = httpResponse.getFirstHeader("Location").getValue();
|
||||||
String newUrl = connection.getHeaderField("Location");
|
|
||||||
if (newUrl != null && (maxRedirect >= redirects || maxRedirect == -1)) {
|
if (newUrl != null && (maxRedirect >= redirects || maxRedirect == -1)) {
|
||||||
redirects++;
|
redirects++;
|
||||||
return getResponseStatus(newUrl, redirects);
|
return getResponseStatus(newUrl, redirects);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return statusCode;
|
return statusCode;
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
// Must be a bad URL
|
// Must be a bad URL
|
||||||
log.debug("Bad link: " + ioe.getMessage());
|
log.debug("Bad link: " + ioe.getMessage());
|
||||||
|
@@ -30,13 +30,13 @@ import javax.xml.xpath.XPathExpressionException;
|
|||||||
import javax.xml.xpath.XPathFactory;
|
import javax.xml.xpath.XPathFactory;
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
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.authorize.AuthorizeException;
|
import org.dspace.authorize.AuthorizeException;
|
||||||
import org.dspace.content.DSpaceObject;
|
import org.dspace.content.DSpaceObject;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
@@ -255,14 +255,13 @@ public class MetadataWebService extends AbstractCurationTask implements Namespac
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected int callService(String value, Item item, StringBuilder resultSb) throws IOException {
|
protected int callService(String value, Item item, StringBuilder resultSb) throws IOException {
|
||||||
|
|
||||||
String callUrl = urlTemplate.replaceAll("\\{" + templateParam + "\\}", value);
|
String callUrl = urlTemplate.replaceAll("\\{" + templateParam + "\\}", value);
|
||||||
CloseableHttpClient client = HttpClientBuilder.create().build();
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpGet req = new HttpGet(callUrl);
|
HttpGet req = new HttpGet(callUrl);
|
||||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||||
req.addHeader(entry.getKey(), entry.getValue());
|
req.addHeader(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
HttpResponse resp = client.execute(req);
|
try (CloseableHttpResponse resp = client.execute(req)) {
|
||||||
int status = Curator.CURATE_ERROR;
|
int status = Curator.CURATE_ERROR;
|
||||||
int statusCode = resp.getStatusLine().getStatusCode();
|
int statusCode = resp.getStatusLine().getStatusCode();
|
||||||
if (statusCode == HttpStatus.SC_OK) {
|
if (statusCode == HttpStatus.SC_OK) {
|
||||||
@@ -271,8 +270,8 @@ public class MetadataWebService extends AbstractCurationTask implements Namespac
|
|||||||
// boiler-plate handling taken from Apache 4.1 javadoc
|
// boiler-plate handling taken from Apache 4.1 javadoc
|
||||||
InputStream instream = entity.getContent();
|
InputStream instream = entity.getContent();
|
||||||
try {
|
try {
|
||||||
// This next line triggers a false-positive XXE warning from LGTM, even though we disallow DTD
|
// This next line triggers a false-positive XXE warning from LGTM, even though
|
||||||
// parsing during initialization of docBuilder in init()
|
// we disallow DTD parsing during initialization of docBuilder in init()
|
||||||
Document doc = docBuilder.parse(instream); // lgtm [java/xxe]
|
Document doc = docBuilder.parse(instream); // lgtm [java/xxe]
|
||||||
status = processResponse(doc, item, resultSb);
|
status = processResponse(doc, item, resultSb);
|
||||||
} catch (SAXException saxE) {
|
} catch (SAXException saxE) {
|
||||||
@@ -289,10 +288,6 @@ public class MetadataWebService extends AbstractCurationTask implements Namespac
|
|||||||
// Closing the input stream will trigger connection release
|
// Closing the input stream will trigger connection release
|
||||||
instream.close();
|
instream.close();
|
||||||
}
|
}
|
||||||
// When HttpClient instance is no longer needed,
|
|
||||||
// shut down the connection manager to ensure
|
|
||||||
// immediate deallocation of all system resources
|
|
||||||
client.close();
|
|
||||||
} else {
|
} else {
|
||||||
log.error(" obtained no valid service response");
|
log.error(" obtained no valid service response");
|
||||||
resultSb.append("no service response");
|
resultSb.append("no service response");
|
||||||
@@ -303,6 +298,8 @@ public class MetadataWebService extends AbstractCurationTask implements Namespac
|
|||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected int processResponse(Document doc, Item item, StringBuilder resultSb) throws IOException {
|
protected int processResponse(Document doc, Item item, StringBuilder resultSb) throws IOException {
|
||||||
boolean update = false;
|
boolean update = false;
|
||||||
|
@@ -12,12 +12,12 @@ import java.net.URLEncoder;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
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.dspace.services.factory.DSpaceServicesFactory;
|
import org.dspace.services.factory.DSpaceServicesFactory;
|
||||||
|
|
||||||
@@ -60,11 +60,10 @@ public class MicrosoftTranslator extends AbstractTranslator {
|
|||||||
String url = baseUrl + "?appId=" + apiKey;
|
String url = baseUrl + "?appId=" + apiKey;
|
||||||
url += "&to=" + to + "&from=" + from + "&text=" + text;
|
url += "&to=" + to + "&from=" + from + "&text=" + text;
|
||||||
|
|
||||||
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpGet hm = new HttpGet(url);
|
HttpGet hm = new HttpGet(url);
|
||||||
HttpResponse httpResponse = client.execute(hm);
|
try (CloseableHttpResponse httpResponse = client.execute(hm)) {
|
||||||
log.debug("Response code from API call is " + httpResponse);
|
log.debug("Response code from API call is " + httpResponse);
|
||||||
|
|
||||||
if (httpResponse.getStatusLine().getStatusCode() == 200) {
|
if (httpResponse.getStatusLine().getStatusCode() == 200) {
|
||||||
String response = IOUtils.toString(httpResponse.getEntity().getContent(),
|
String response = IOUtils.toString(httpResponse.getEntity().getContent(),
|
||||||
StandardCharsets.ISO_8859_1);
|
StandardCharsets.ISO_8859_1);
|
||||||
@@ -74,8 +73,7 @@ public class MicrosoftTranslator extends AbstractTranslator {
|
|||||||
translatedText = response;
|
translatedText = response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return translatedText;
|
return translatedText;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -17,15 +17,15 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
|
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.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
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.eperson.service.CaptchaService;
|
import org.dspace.eperson.service.CaptchaService;
|
||||||
import org.dspace.services.ConfigurationService;
|
import org.dspace.services.ConfigurationService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -82,18 +82,17 @@ public class CaptchaServiceImpl implements CaptchaService {
|
|||||||
throw new RuntimeException(e.getMessage(), e);
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpResponse httpResponse;
|
|
||||||
GoogleCaptchaResponse googleResponse;
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
try {
|
try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
|
||||||
httpResponse = httpClient.execute(httpPost);
|
GoogleCaptchaResponse googleResponse = objectMapper.readValue(httpResponse.getEntity().getContent(),
|
||||||
googleResponse = objectMapper.readValue(httpResponse.getEntity().getContent(), GoogleCaptchaResponse.class);
|
GoogleCaptchaResponse.class);
|
||||||
|
validateGoogleResponse(googleResponse, action);
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
throw new RuntimeException("Error during verify google recaptcha site", e);
|
throw new RuntimeException("Error during verify google recaptcha site", e);
|
||||||
}
|
}
|
||||||
validateGoogleResponse(googleResponse, action);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean responseSanityCheck(String response) {
|
private boolean responseSanityCheck(String response) {
|
||||||
|
@@ -27,13 +27,13 @@ import org.apache.http.HttpStatus;
|
|||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.NoHttpResponseException;
|
import org.apache.http.NoHttpResponseException;
|
||||||
import org.apache.http.StatusLine;
|
import org.apache.http.StatusLine;
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
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.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.dspace.app.client.DSpaceHttpClientFactory;
|
||||||
import org.dspace.app.util.Util;
|
import org.dspace.app.util.Util;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -120,7 +120,7 @@ public class OpenaireRestConnector {
|
|||||||
params.add(new BasicNameValuePair("grant_type", "client_credentials"));
|
params.add(new BasicNameValuePair("grant_type", "client_credentials"));
|
||||||
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||||||
|
|
||||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpResponse getResponse = httpClient.execute(httpPost);
|
HttpResponse getResponse = httpClient.execute(httpPost);
|
||||||
|
|
||||||
JSONObject responseObject = null;
|
JSONObject responseObject = null;
|
||||||
@@ -141,12 +141,13 @@ public class OpenaireRestConnector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (responseObject == null || !responseObject.has("access_token") || !responseObject.has("expires_in")) {
|
if (responseObject == null || !responseObject.has("access_token") || !responseObject.has("expires_in")) {
|
||||||
throw new IOException("Unable to grab the access token using provided service url, client id and secret");
|
throw new IOException("Unable to grab the access token using provided service url, " +
|
||||||
|
"client id and secret");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OpenaireRestToken(responseObject.get("access_token").toString(),
|
return new OpenaireRestToken(responseObject.get("access_token").toString(),
|
||||||
Long.valueOf(responseObject.get("expires_in").toString()));
|
Long.valueOf(responseObject.get("expires_in").toString()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,7 +172,7 @@ public class OpenaireRestConnector {
|
|||||||
httpGet.addHeader("Authorization", "Bearer " + accessToken);
|
httpGet.addHeader("Authorization", "Bearer " + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
getResponse = httpClient.execute(httpGet);
|
getResponse = httpClient.execute(httpGet);
|
||||||
|
|
||||||
StatusLine status = getResponse.getStatusLine();
|
StatusLine status = getResponse.getStatusLine();
|
||||||
@@ -192,8 +193,8 @@ public class OpenaireRestConnector {
|
|||||||
if (limitMax.length > 0) {
|
if (limitMax.length > 0) {
|
||||||
limitMsg = limitMsg.concat(" of " + limitMax[0].getValue());
|
limitMsg = limitMsg.concat(" of " + limitMax[0].getValue());
|
||||||
}
|
}
|
||||||
getGotError(
|
getGotError(new NoHttpResponseException(status.getReasonPhrase() + " with usage limit "
|
||||||
new NoHttpResponseException(status.getReasonPhrase() + " with usage limit " + limitMsg),
|
+ limitMsg),
|
||||||
url + '/' + file);
|
url + '/' + file);
|
||||||
} else {
|
} else {
|
||||||
// 429 - Rate limit abuse
|
// 429 - Rate limit abuse
|
||||||
@@ -207,6 +208,7 @@ public class OpenaireRestConnector {
|
|||||||
|
|
||||||
// do not close this httpClient
|
// do not close this httpClient
|
||||||
result = getResponse.getEntity().getContent();
|
result = getResponse.getEntity().getContent();
|
||||||
|
}
|
||||||
} catch (MalformedURLException e1) {
|
} catch (MalformedURLException e1) {
|
||||||
getGotError(e1, url + '/' + file);
|
getGotError(e1, url + '/' + file);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@@ -7,17 +7,18 @@
|
|||||||
*/
|
*/
|
||||||
package org.dspace.external;
|
package org.dspace.external;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Antoine Snyers (antoine at atmire.com)
|
* @author Antoine Snyers (antoine at atmire.com)
|
||||||
@@ -39,7 +40,7 @@ public class OrcidRestConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InputStream get(String path, String accessToken) {
|
public InputStream get(String path, String accessToken) {
|
||||||
HttpResponse getResponse = null;
|
CloseableHttpResponse getResponse = null;
|
||||||
InputStream result = null;
|
InputStream result = null;
|
||||||
path = trimSlashes(path);
|
path = trimSlashes(path);
|
||||||
|
|
||||||
@@ -49,11 +50,13 @@ public class OrcidRestConnector {
|
|||||||
httpGet.addHeader("Content-Type", "application/vnd.orcid+xml");
|
httpGet.addHeader("Content-Type", "application/vnd.orcid+xml");
|
||||||
httpGet.addHeader("Authorization","Bearer " + accessToken);
|
httpGet.addHeader("Authorization","Bearer " + accessToken);
|
||||||
}
|
}
|
||||||
try {
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
|
||||||
getResponse = httpClient.execute(httpGet);
|
getResponse = httpClient.execute(httpGet);
|
||||||
//do not close this httpClient
|
try (InputStream responseStream = getResponse.getEntity().getContent()) {
|
||||||
result = getResponse.getEntity().getContent();
|
// Read all the content of the response stream into a byte array to prevent TruncatedChunkException
|
||||||
|
byte[] content = responseStream.readAllBytes();
|
||||||
|
result = new ByteArrayInputStream(content);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
getGotError(e, fullPath);
|
getGotError(e, fullPath);
|
||||||
}
|
}
|
||||||
|
@@ -169,13 +169,7 @@ public class OrcidV3AuthorDataProvider extends AbstractExternalDataProvider {
|
|||||||
}
|
}
|
||||||
initializeAccessToken();
|
initializeAccessToken();
|
||||||
InputStream bioDocument = orcidRestConnector.get(id + ((id.endsWith("/person")) ? "" : "/person"), accessToken);
|
InputStream bioDocument = orcidRestConnector.get(id + ((id.endsWith("/person")) ? "" : "/person"), accessToken);
|
||||||
Person person = converter.convertSinglePerson(bioDocument);
|
return converter.convertSinglePerson(bioDocument);
|
||||||
try {
|
|
||||||
bioDocument.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return person;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -220,11 +214,6 @@ public class OrcidV3AuthorDataProvider extends AbstractExternalDataProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
bioDocument.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return bios.stream().map(bio -> convertToExternalDataObject(bio)).collect(Collectors.toList());
|
return bios.stream().map(bio -> convertToExternalDataObject(bio)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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
|
||||||
|
@@ -36,10 +36,10 @@ import org.apache.http.entity.ContentType;
|
|||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
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.authorize.AuthorizeException;
|
import org.dspace.authorize.AuthorizeException;
|
||||||
import org.dspace.content.DSpaceObject;
|
import org.dspace.content.DSpaceObject;
|
||||||
import org.dspace.content.crosswalk.CrosswalkException;
|
import org.dspace.content.crosswalk.CrosswalkException;
|
||||||
@@ -719,7 +719,7 @@ public class DataCiteConnector
|
|||||||
httpContext.setCredentialsProvider(credentialsProvider);
|
httpContext.setCredentialsProvider(credentialsProvider);
|
||||||
|
|
||||||
HttpEntity entity = null;
|
HttpEntity entity = null;
|
||||||
try ( CloseableHttpClient httpclient = HttpClientBuilder.create().build(); ) {
|
try (CloseableHttpClient httpclient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpResponse response = httpclient.execute(req, httpContext);
|
HttpResponse response = httpclient.execute(req, httpContext);
|
||||||
|
|
||||||
StatusLine status = response.getStatusLine();
|
StatusLine status = response.getStatusLine();
|
||||||
|
@@ -26,9 +26,9 @@ import org.apache.http.client.protocol.HttpClientContext;
|
|||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
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.identifier.DOI;
|
import org.dspace.identifier.DOI;
|
||||||
import org.dspace.identifier.IdentifierException;
|
import org.dspace.identifier.IdentifierException;
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ public class EZIDRequest {
|
|||||||
this.authority = authority;
|
this.authority = authority;
|
||||||
}
|
}
|
||||||
|
|
||||||
client = HttpClientBuilder.create().build();
|
client = DSpaceHttpClientFactory.getInstance().build();
|
||||||
httpContext = HttpClientContext.create();
|
httpContext = HttpClientContext.create();
|
||||||
if (null != username) {
|
if (null != username) {
|
||||||
URI uri = new URI(scheme, host, path, null);
|
URI uri = new URI(scheme, host, path, null);
|
||||||
@@ -124,7 +124,7 @@ public class EZIDRequest {
|
|||||||
this.authority = authority;
|
this.authority = authority;
|
||||||
}
|
}
|
||||||
|
|
||||||
client = HttpClientBuilder.create().build();
|
client = DSpaceHttpClientFactory.getInstance().build();
|
||||||
httpContext = HttpClientContext.create();
|
httpContext = HttpClientContext.create();
|
||||||
if (null != username) {
|
if (null != username) {
|
||||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
|
@@ -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,10 @@ 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 (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
url = new URL(path);
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(path));
|
||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
|
||||||
con.setRequestMethod("GET");
|
|
||||||
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) {
|
||||||
|
@@ -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,11 @@ 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);
|
|
||||||
|
|
||||||
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 +63,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 +86,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 +116,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
|
||||||
*
|
*
|
||||||
|
@@ -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;
|
||||||
@@ -28,9 +25,9 @@ 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.entity.mime.MultipartEntityBuilder;
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
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.jdom2.Attribute;
|
import org.jdom2.Attribute;
|
||||||
import org.jdom2.Document;
|
import org.jdom2.Document;
|
||||||
@@ -70,12 +67,7 @@ public class CCLicenseConnectorServiceImpl implements CCLicenseConnectorService,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
HttpClientBuilder builder = HttpClientBuilder.create();
|
client = DSpaceHttpClientFactory.getInstance().buildWithoutAutomaticRetries(5);
|
||||||
|
|
||||||
client = builder
|
|
||||||
.disableAutomaticRetries()
|
|
||||||
.setMaxConnTotal(5)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// disallow DTD parsing to ensure no XXE attacks can occur.
|
// disallow DTD parsing to ensure no XXE attacks can occur.
|
||||||
// See https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
|
// See https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
|
||||||
@@ -333,23 +325,13 @@ 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;
|
||||||
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
URL request_url;
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpPost(issueUrl));
|
||||||
try {
|
|
||||||
request_url = new URL(issueUrl);
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
URLConnection connection = request_url.openConnection();
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
try {
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
|
@@ -35,13 +35,14 @@ import org.apache.http.HttpEntity;
|
|||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpUriRequest;
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
import org.apache.http.client.methods.RequestBuilder;
|
import org.apache.http.client.methods.RequestBuilder;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
import org.dspace.app.client.DSpaceHttpClientFactory;
|
||||||
import org.dspace.orcid.OrcidToken;
|
import org.dspace.orcid.OrcidToken;
|
||||||
import org.dspace.orcid.exception.OrcidClientException;
|
import org.dspace.orcid.exception.OrcidClientException;
|
||||||
import org.dspace.orcid.model.OrcidEntityType;
|
import org.dspace.orcid.model.OrcidEntityType;
|
||||||
@@ -254,10 +255,8 @@ public class OrcidClientImpl implements OrcidClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void executeSuccessful(HttpUriRequest httpUriRequest) {
|
private void executeSuccessful(HttpUriRequest httpUriRequest) {
|
||||||
try {
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpClient client = HttpClientBuilder.create().build();
|
CloseableHttpResponse response = client.execute(httpUriRequest);
|
||||||
HttpResponse response = client.execute(httpUriRequest);
|
|
||||||
|
|
||||||
if (isNotSuccessfull(response)) {
|
if (isNotSuccessfull(response)) {
|
||||||
throw new OrcidClientException(
|
throw new OrcidClientException(
|
||||||
getStatusCode(response),
|
getStatusCode(response),
|
||||||
@@ -272,21 +271,17 @@ public class OrcidClientImpl implements OrcidClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T> T executeAndParseJson(HttpUriRequest httpUriRequest, Class<T> clazz) {
|
private <T> T executeAndParseJson(HttpUriRequest httpUriRequest, Class<T> clazz) {
|
||||||
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpClient client = HttpClientBuilder.create().build();
|
|
||||||
|
|
||||||
return executeAndReturns(() -> {
|
return executeAndReturns(() -> {
|
||||||
|
CloseableHttpResponse response = client.execute(httpUriRequest);
|
||||||
HttpResponse response = client.execute(httpUriRequest);
|
|
||||||
|
|
||||||
if (isNotSuccessfull(response)) {
|
if (isNotSuccessfull(response)) {
|
||||||
throw new OrcidClientException(getStatusCode(response), formatErrorMessage(response));
|
throw new OrcidClientException(getStatusCode(response), formatErrorMessage(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
return objectMapper.readValue(response.getEntity().getContent(), clazz);
|
return objectMapper.readValue(response.getEntity().getContent(), clazz);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -301,44 +296,37 @@ public class OrcidClientImpl implements OrcidClient {
|
|||||||
* @throws OrcidClientException if the incoming response is not successful
|
* @throws OrcidClientException if the incoming response is not successful
|
||||||
*/
|
*/
|
||||||
private <T> T executeAndUnmarshall(HttpUriRequest httpUriRequest, boolean handleNotFoundAsNull, Class<T> clazz) {
|
private <T> T executeAndUnmarshall(HttpUriRequest httpUriRequest, boolean handleNotFoundAsNull, Class<T> clazz) {
|
||||||
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
HttpClient client = HttpClientBuilder.create().build();
|
|
||||||
|
|
||||||
return executeAndReturns(() -> {
|
return executeAndReturns(() -> {
|
||||||
|
CloseableHttpResponse response = client.execute(httpUriRequest);
|
||||||
HttpResponse response = client.execute(httpUriRequest);
|
|
||||||
|
|
||||||
if (handleNotFoundAsNull && isNotFound(response)) {
|
if (handleNotFoundAsNull && isNotFound(response)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNotSuccessfull(response)) {
|
if (isNotSuccessfull(response)) {
|
||||||
throw new OrcidClientException(getStatusCode(response), formatErrorMessage(response));
|
throw new OrcidClientException(getStatusCode(response), formatErrorMessage(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
return unmarshall(response.getEntity(), clazz);
|
return unmarshall(response.getEntity(), clazz);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private OrcidResponse execute(HttpUriRequest httpUriRequest, boolean handleNotFoundAsNull) {
|
private OrcidResponse execute(HttpUriRequest httpUriRequest, boolean handleNotFoundAsNull) {
|
||||||
HttpClient client = HttpClientBuilder.create().build();
|
try (CloseableHttpClient client = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
|
|
||||||
return executeAndReturns(() -> {
|
return executeAndReturns(() -> {
|
||||||
|
CloseableHttpResponse response = client.execute(httpUriRequest);
|
||||||
HttpResponse response = client.execute(httpUriRequest);
|
|
||||||
|
|
||||||
if (handleNotFoundAsNull && isNotFound(response)) {
|
if (handleNotFoundAsNull && isNotFound(response)) {
|
||||||
return new OrcidResponse(getStatusCode(response), null, getContent(response));
|
return new OrcidResponse(getStatusCode(response), null, getContent(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNotSuccessfull(response)) {
|
if (isNotSuccessfull(response)) {
|
||||||
throw new OrcidClientException(getStatusCode(response), formatErrorMessage(response));
|
throw new OrcidClientException(getStatusCode(response), formatErrorMessage(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OrcidResponse(getStatusCode(response), getPutCode(response), getContent(response));
|
return new OrcidResponse(getStatusCode(response), getPutCode(response), getContent(response));
|
||||||
|
|
||||||
});
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> T executeAndReturns(ThrowingSupplier<T, Exception> supplier) {
|
private <T> T executeAndReturns(ThrowingSupplier<T, Exception> supplier) {
|
||||||
|
@@ -20,7 +20,7 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.dspace.app.client.DSpaceHttpClientFactory;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,7 +97,7 @@ public final class OrcidFactoryUtils {
|
|||||||
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
HttpResponse response;
|
HttpResponse response;
|
||||||
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
|
||||||
response = httpClient.execute(httpPost);
|
response = httpClient.execute(httpPost);
|
||||||
}
|
}
|
||||||
JSONObject responseObject = null;
|
JSONObject responseObject = null;
|
||||||
|
@@ -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;
|
||||||
@@ -114,20 +114,19 @@ public class QAEventActionServiceImpl implements QAEventActionService {
|
|||||||
* Make acknowledgement to the configured urls for the event status.
|
* Make acknowledgement to the configured urls for the event status.
|
||||||
*/
|
*/
|
||||||
private void makeAcknowledgement(String eventId, String source, String status) {
|
private void makeAcknowledgement(String eventId, String source, String status) {
|
||||||
String[] ackwnoledgeCallbacks = configurationService
|
String[] acknowledgeCallbacks = configurationService
|
||||||
.getArrayProperty("qaevents." + source + ".acknowledge-url");
|
.getArrayProperty("qaevents." + source + ".acknowledge-url");
|
||||||
if (ackwnoledgeCallbacks != null) {
|
if (acknowledgeCallbacks != null) {
|
||||||
for (String ackwnoledgeCallback : ackwnoledgeCallbacks) {
|
for (String acknowledgeCallback : acknowledgeCallbacks) {
|
||||||
if (StringUtils.isNotBlank(ackwnoledgeCallback)) {
|
if (StringUtils.isNotBlank(acknowledgeCallback)) {
|
||||||
ObjectNode node = jsonMapper.createObjectNode();
|
ObjectNode node = jsonMapper.createObjectNode();
|
||||||
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();
|
try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().buildWithoutProxy()) {
|
||||||
HttpPost postMethod = new HttpPost(ackwnoledgeCallback);
|
HttpPost postMethod = new HttpPost(acknowledgeCallback);
|
||||||
postMethod.setEntity(requestEntity);
|
postMethod.setEntity(requestEntity);
|
||||||
try {
|
httpClient.execute(postMethod);
|
||||||
httpclient.execute(postMethod);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
@@ -19,11 +19,11 @@ import org.apache.http.HttpResponse;
|
|||||||
import org.apache.http.conn.ConnectionKeepAliveStrategy;
|
import org.apache.http.conn.ConnectionKeepAliveStrategy;
|
||||||
import org.apache.http.conn.HttpClientConnectionManager;
|
import org.apache.http.conn.HttpClientConnectionManager;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
import org.apache.http.message.BasicHeaderElementIterator;
|
import org.apache.http.message.BasicHeaderElementIterator;
|
||||||
import org.apache.http.protocol.HTTP;
|
import org.apache.http.protocol.HTTP;
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.dspace.app.client.DSpaceHttpClientFactory;
|
||||||
import org.dspace.services.ConfigurationService;
|
import org.dspace.services.ConfigurationService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -112,7 +112,7 @@ public class HttpConnectionPoolService {
|
|||||||
* @return the client.
|
* @return the client.
|
||||||
*/
|
*/
|
||||||
public CloseableHttpClient getClient() {
|
public CloseableHttpClient getClient() {
|
||||||
CloseableHttpClient httpClient = HttpClientBuilder.create()
|
CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().builder(true).create()
|
||||||
.setKeepAliveStrategy(keepAliveStrategy)
|
.setKeepAliveStrategy(keepAliveStrategy)
|
||||||
.setConnectionManager(connManager)
|
.setConnectionManager(connManager)
|
||||||
.build();
|
.build();
|
||||||
|
@@ -54,7 +54,6 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
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.apache.solr.client.solrj.SolrClient;
|
import org.apache.solr.client.solrj.SolrClient;
|
||||||
@@ -84,6 +83,7 @@ import org.apache.solr.common.params.MapSolrParams;
|
|||||||
import org.apache.solr.common.params.ModifiableSolrParams;
|
import org.apache.solr.common.params.ModifiableSolrParams;
|
||||||
import org.apache.solr.common.params.ShardParams;
|
import org.apache.solr.common.params.ShardParams;
|
||||||
import org.apache.solr.common.util.NamedList;
|
import org.apache.solr.common.util.NamedList;
|
||||||
|
import org.dspace.app.client.DSpaceHttpClientFactory;
|
||||||
import org.dspace.authorize.service.AuthorizeService;
|
import org.dspace.authorize.service.AuthorizeService;
|
||||||
import org.dspace.content.Bitstream;
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.content.Bundle;
|
import org.dspace.content.Bundle;
|
||||||
@@ -1208,7 +1208,7 @@ public class SolrLoggerServiceImpl implements SolrLoggerService, InitializingBea
|
|||||||
+ "."
|
+ "."
|
||||||
+ i
|
+ i
|
||||||
+ ".csv");
|
+ ".csv");
|
||||||
try ( CloseableHttpClient hc = HttpClientBuilder.create().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 !
|
||||||
@@ -1350,7 +1350,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 = HttpClientBuilder.create().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);
|
||||||
|
@@ -15,13 +15,13 @@ import java.time.LocalDate;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
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.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
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.core.Context;
|
import org.dspace.core.Context;
|
||||||
import org.dspace.statistics.export.OpenURLTracker;
|
import org.dspace.statistics.export.OpenURLTracker;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -69,16 +69,16 @@ public class OpenUrlServiceImpl implements OpenUrlService {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected int getResponseCodeFromUrl(final String urlStr) throws IOException {
|
protected int getResponseCodeFromUrl(final String urlStr) throws IOException {
|
||||||
|
try (CloseableHttpClient httpClient = getHttpClient(getHttpClientRequestConfig())) {
|
||||||
HttpGet httpGet = new HttpGet(urlStr);
|
HttpGet httpGet = new HttpGet(urlStr);
|
||||||
HttpClient httpClient = getHttpClient(getHttpClientRequestConfig());
|
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
|
||||||
HttpResponse httpResponse = httpClient.execute(httpGet);
|
|
||||||
return httpResponse.getStatusLine().getStatusCode();
|
return httpResponse.getStatusLine().getStatusCode();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected HttpClient getHttpClient(RequestConfig requestConfig) {
|
protected CloseableHttpClient getHttpClient(RequestConfig requestConfig) {
|
||||||
return HttpClientBuilder.create()
|
return DSpaceHttpClientFactory.getInstance().buildWithRequestConfig(requestConfig);
|
||||||
.setDefaultRequestConfig(requestConfig)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected RequestConfig getHttpClientRequestConfig() {
|
protected RequestConfig getHttpClientRequestConfig() {
|
||||||
|
@@ -0,0 +1,256 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
package org.dspace.app.client;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.arrayWithSize;
|
||||||
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoInteractions;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
import okhttp3.mockwebserver.MockResponse;
|
||||||
|
import okhttp3.mockwebserver.MockWebServer;
|
||||||
|
import okhttp3.mockwebserver.RecordedRequest;
|
||||||
|
import org.apache.http.HttpRequestInterceptor;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.HttpResponseInterceptor;
|
||||||
|
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.conn.routing.HttpRoute;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for {@link DSpaceHttpClientFactory}.
|
||||||
|
*
|
||||||
|
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class DSpaceHttpClientFactoryTest {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private DSpaceHttpClientFactory httpClientFactory;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ConfigurationService configurationService;
|
||||||
|
|
||||||
|
private MockWebServer mockProxy;
|
||||||
|
|
||||||
|
private MockWebServer mockServer;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
this.httpClientFactory.setProxyRoutePlanner(new DSpaceProxyRoutePlanner(configurationService));
|
||||||
|
this.mockProxy = new MockWebServer();
|
||||||
|
this.mockProxy.enqueue(new MockResponse().setResponseCode(200).addHeader("From", "Proxy"));
|
||||||
|
this.mockServer = new MockWebServer();
|
||||||
|
this.mockServer.enqueue(new MockResponse().setResponseCode(200).addHeader("From", "Server"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithProxyConfigured() throws Exception {
|
||||||
|
setHttpProxyOnConfigurationService();
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.build();
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(httpResponse.getHeaders("From"), arrayWithSize(1));
|
||||||
|
assertThat(httpResponse.getHeaders("From")[0].getValue(), is("Proxy"));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(1));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
RecordedRequest request = mockProxy.takeRequest(100, TimeUnit.MILLISECONDS);
|
||||||
|
assertThat(request, notNullValue());
|
||||||
|
assertThat(request.getRequestUrl(), is(mockProxy.url("")));
|
||||||
|
assertThat(request.getRequestLine(), is("GET " + mockServer.url("").toString() + " HTTP/1.1"));
|
||||||
|
verify(configurationService).getProperty("http.proxy.host");
|
||||||
|
verify(configurationService).getProperty("http.proxy.port");
|
||||||
|
verify(configurationService).getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
verifyNoMoreInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithProxyConfiguredAndHostToIgnoreSet() throws Exception {
|
||||||
|
setHttpProxyOnConfigurationService(mockServer.getHostName());
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.build();
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(httpResponse.getHeaders("From"), arrayWithSize(1));
|
||||||
|
assertThat(httpResponse.getHeaders("From")[0].getValue(), is("Server"));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(1));
|
||||||
|
verify(configurationService).getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
verifyNoMoreInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithProxyConfiguredAndHostPrefixToIgnoreSet() throws Exception {
|
||||||
|
setHttpProxyOnConfigurationService("local*", "www.test.com");
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.build();
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(httpResponse.getHeaders("From"), arrayWithSize(1));
|
||||||
|
assertThat(httpResponse.getHeaders("From")[0].getValue(), is("Server"));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(1));
|
||||||
|
verify(configurationService).getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
verifyNoMoreInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithProxyConfiguredAndHostSuffixToIgnoreSet() throws Exception {
|
||||||
|
setHttpProxyOnConfigurationService("www.test.com", "*host");
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.build();
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(httpResponse.getHeaders("From"), arrayWithSize(1));
|
||||||
|
assertThat(httpResponse.getHeaders("From")[0].getValue(), is("Server"));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(1));
|
||||||
|
verify(configurationService).getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
verifyNoMoreInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithoutConfiguredProxy() throws Exception {
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.build();
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(httpResponse.getHeaders("From"), arrayWithSize(1));
|
||||||
|
assertThat(httpResponse.getHeaders("From")[0].getValue(), is("Server"));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(1));
|
||||||
|
RecordedRequest request = mockServer.takeRequest(100, TimeUnit.MILLISECONDS);
|
||||||
|
assertThat(request, notNullValue());
|
||||||
|
assertThat(request.getRequestUrl(), is(mockServer.url("")));
|
||||||
|
assertThat(request.getRequestLine(), is("GET / HTTP/1.1"));
|
||||||
|
verify(configurationService).getProperty("http.proxy.host");
|
||||||
|
verify(configurationService).getProperty("http.proxy.port");
|
||||||
|
verify(configurationService).getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
verifyNoMoreInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithoutProxy() throws Exception {
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.buildWithoutProxy();
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(1));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
RecordedRequest request = mockServer.takeRequest(100, TimeUnit.MILLISECONDS);
|
||||||
|
assertThat(request, notNullValue());
|
||||||
|
assertThat(request.getRequestUrl(), is(mockServer.url("")));
|
||||||
|
assertThat(request.getRequestLine(), is("GET / HTTP/1.1"));
|
||||||
|
verifyNoInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithoutAutomaticRetries() throws Exception {
|
||||||
|
setHttpProxyOnConfigurationService("www.test.com");
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.buildWithoutAutomaticRetries(10);
|
||||||
|
httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(1));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
RecordedRequest request = mockProxy.takeRequest(100, TimeUnit.MILLISECONDS);
|
||||||
|
assertThat(request, notNullValue());
|
||||||
|
assertThat(request.getRequestUrl(), is(mockProxy.url("")));
|
||||||
|
assertThat(request.getRequestLine(), is("GET " + mockServer.url("").toString() + " HTTP/1.1"));
|
||||||
|
verify(configurationService).getProperty("http.proxy.host");
|
||||||
|
verify(configurationService).getProperty("http.proxy.port");
|
||||||
|
verify(configurationService).getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
verifyNoMoreInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithHttpRequestInterceptor() throws Exception {
|
||||||
|
setHttpProxyOnConfigurationService("*test.com", "www.dspace.com");
|
||||||
|
AtomicReference<HttpContext> contextReference = new AtomicReference<HttpContext>();
|
||||||
|
HttpRequestInterceptor interceptor = (request, context) -> contextReference.set(context);
|
||||||
|
httpClientFactory.setRequestInterceptors(List.of(interceptor));
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.build();
|
||||||
|
httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(1));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
HttpContext httpContext = contextReference.get();
|
||||||
|
assertThat(httpContext, notNullValue());
|
||||||
|
Object httpRouteObj = httpContext.getAttribute("http.route");
|
||||||
|
assertThat(httpRouteObj, notNullValue());
|
||||||
|
assertThat(httpRouteObj, instanceOf(HttpRoute.class));
|
||||||
|
HttpRoute httpRoute = (HttpRoute) httpRouteObj;
|
||||||
|
assertThat(httpRoute.getHopCount(), is(2));
|
||||||
|
assertThat(httpRoute.getHopTarget(0).getPort(), is(mockProxy.getPort()));
|
||||||
|
assertThat(httpRoute.getHopTarget(1).getPort(), is(mockServer.getPort()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithHttpResponseInterceptor() throws Exception {
|
||||||
|
AtomicReference<HttpResponse> responseReference = new AtomicReference<HttpResponse>();
|
||||||
|
HttpResponseInterceptor responseInterceptor = (response, context) -> responseReference.set(response);
|
||||||
|
httpClientFactory.setResponseInterceptors(List.of(responseInterceptor));
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.build();
|
||||||
|
httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(0));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(1));
|
||||||
|
HttpResponse httpResponse = responseReference.get();
|
||||||
|
assertThat(httpResponse, notNullValue());
|
||||||
|
assertThat(httpResponse.getHeaders("From"), arrayWithSize(1));
|
||||||
|
assertThat(httpResponse.getHeaders("From")[0].getValue(), is("Server"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildWithRequestConfig() throws Exception {
|
||||||
|
setHttpProxyOnConfigurationService();
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectTimeout(2500)
|
||||||
|
.build();
|
||||||
|
AtomicReference<HttpContext> contextReference = new AtomicReference<HttpContext>();
|
||||||
|
HttpRequestInterceptor interceptor = (request, context) -> contextReference.set(context);
|
||||||
|
httpClientFactory.setRequestInterceptors(List.of(interceptor));
|
||||||
|
CloseableHttpClient httpClient = httpClientFactory.buildWithRequestConfig(requestConfig);
|
||||||
|
httpClient.execute(new HttpGet(mockServer.url("").toString()));
|
||||||
|
assertThat(mockProxy.getRequestCount(), is(1));
|
||||||
|
assertThat(mockServer.getRequestCount(), is(0));
|
||||||
|
HttpContext httpContext = contextReference.get();
|
||||||
|
assertThat(httpContext, notNullValue());
|
||||||
|
Object httpRequestConfigObj = httpContext.getAttribute("http.request-config");
|
||||||
|
assertThat(httpRequestConfigObj, notNullValue());
|
||||||
|
assertThat(httpRequestConfigObj, instanceOf(RequestConfig.class));
|
||||||
|
assertThat(((RequestConfig) httpRequestConfigObj).getConnectTimeout(), is(2500));
|
||||||
|
verify(configurationService).getProperty("http.proxy.host");
|
||||||
|
verify(configurationService).getProperty("http.proxy.port");
|
||||||
|
verify(configurationService).getArrayProperty("http.proxy.hosts-to-ignore");
|
||||||
|
verifyNoMoreInteractions(configurationService);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setHttpProxyOnConfigurationService(String... hostsToIgnore) {
|
||||||
|
when(configurationService.getProperty("http.proxy.host")).thenReturn(mockProxy.getHostName());
|
||||||
|
when(configurationService.getProperty("http.proxy.port")).thenReturn(String.valueOf(mockProxy.getPort()));
|
||||||
|
when(configurationService.getArrayProperty("http.proxy.hosts-to-ignore")).thenReturn(hostsToIgnore);
|
||||||
|
}
|
||||||
|
}
|
@@ -25,9 +25,9 @@ import java.sql.SQLException;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.StatusLine;
|
import org.apache.http.StatusLine;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
import org.dspace.statistics.export.OpenURLTracker;
|
import org.dspace.statistics.export.OpenURLTracker;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -52,7 +52,7 @@ public class OpenUrlServiceImplTest {
|
|||||||
private FailedOpenURLTrackerService failedOpenURLTrackerService;
|
private FailedOpenURLTrackerService failedOpenURLTrackerService;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private HttpClient httpClient;
|
private CloseableHttpClient httpClient;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
@@ -71,11 +71,11 @@ public class OpenUrlServiceImplTest {
|
|||||||
* @param statusCode the http status code to use in the mock.
|
* @param statusCode the http status code to use in the mock.
|
||||||
* @return a mocked http response.
|
* @return a mocked http response.
|
||||||
*/
|
*/
|
||||||
protected HttpResponse createMockHttpResponse(int statusCode) {
|
protected CloseableHttpResponse createMockHttpResponse(int statusCode) {
|
||||||
StatusLine statusLine = mock(StatusLine.class);
|
StatusLine statusLine = mock(StatusLine.class);
|
||||||
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
when(statusLine.getStatusCode()).thenReturn(statusCode);
|
||||||
|
|
||||||
HttpResponse httpResponse = mock(HttpResponse.class);
|
CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class);
|
||||||
when(httpResponse.getStatusLine()).thenReturn(statusLine);
|
when(httpResponse.getStatusLine()).thenReturn(statusLine);
|
||||||
|
|
||||||
return httpResponse;
|
return httpResponse;
|
||||||
|
@@ -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
|
||||||
|
@@ -162,6 +162,9 @@
|
|||||||
<bean class="org.dspace.authorize.ValidatePasswordServiceImpl"/>
|
<bean class="org.dspace.authorize.ValidatePasswordServiceImpl"/>
|
||||||
<bean class="org.dspace.authorize.RegexPasswordValidator" />
|
<bean class="org.dspace.authorize.RegexPasswordValidator" />
|
||||||
|
|
||||||
|
<bean id="org.dspace.app.client.DSpaceHttpClientFactory" class="org.dspace.app.client.DSpaceHttpClientFactory"/>
|
||||||
|
<bean class="org.dspace.app.client.DSpaceProxyRoutePlanner"/>
|
||||||
|
|
||||||
<bean class="org.dspace.supervision.SupervisionOrderServiceImpl"/>
|
<bean class="org.dspace.supervision.SupervisionOrderServiceImpl"/>
|
||||||
|
|
||||||
<bean class="org.dspace.content.ItemFilterServiceImpl"/>
|
<bean class="org.dspace.content.ItemFilterServiceImpl"/>
|
||||||
|
6
pom.xml
6
pom.xml
@@ -1746,6 +1746,12 @@
|
|||||||
<version>2.3.232</version>
|
<version>2.3.232</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>mockwebserver</artifactId>
|
||||||
|
<version>4.12.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Findbugs annotations -->
|
<!-- Findbugs annotations -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
Reference in New Issue
Block a user