Skip to content

Commit b9e265d

Browse files
Micheleboychukvins01-4science
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2782-OpenPolicyFinder (pull request DSpace#5668)
DSC-2782: Transizione API SherpaRomeo > Open Policy Finder API Approved-by: Vincenzo Mecca
2 parents 05d244b + 164a554 commit b9e265d

66 files changed

Lines changed: 3277 additions & 2678 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dspace-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@
367367
</exclusions>
368368
</dependency>
369369
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache
370-
Caching dependencies for sherpa service. -->
370+
Caching dependencies for Open Policy Finder service. -->
371371
<dependency>
372372
<groupId>org.springframework.boot</groupId>
373373
<artifactId>spring-boot-starter-cache</artifactId>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.app.client;
9+
10+
import static org.apache.commons.collections4.ListUtils.emptyIfNull;
11+
12+
import java.util.List;
13+
14+
import org.apache.http.HttpRequestInterceptor;
15+
import org.apache.http.HttpResponseInterceptor;
16+
import org.apache.http.client.HttpClient;
17+
import org.apache.http.client.config.RequestConfig;
18+
import org.apache.http.impl.client.CloseableHttpClient;
19+
import org.apache.http.impl.client.HttpClientBuilder;
20+
import org.dspace.services.ConfigurationService;
21+
import org.dspace.utils.DSpace;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
24+
/**
25+
* Factory of {@link HttpClient} with common configurations.
26+
*
27+
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
28+
*
29+
*/
30+
public class DSpaceHttpClientFactory {
31+
32+
@Autowired
33+
private ConfigurationService configurationService;
34+
35+
@Autowired
36+
private DSpaceProxyRoutePlanner proxyRoutePlanner;
37+
38+
@Autowired(required = false)
39+
private List<HttpRequestInterceptor> requestInterceptors;
40+
41+
@Autowired(required = false)
42+
private List<HttpResponseInterceptor> responseInterceptors;
43+
44+
/**
45+
* Get an instance of {@link DSpaceHttpClientFactory} from the Spring context.
46+
* @return the bean instance
47+
*/
48+
public static DSpaceHttpClientFactory getInstance() {
49+
return new DSpace().getSingletonService(DSpaceHttpClientFactory.class);
50+
}
51+
52+
/**
53+
* Build an instance of {@link HttpClient} setting the proxy if configured.
54+
*
55+
* @return the client
56+
*/
57+
public CloseableHttpClient build() {
58+
return build(HttpClientBuilder.create(), true);
59+
}
60+
61+
/**
62+
* return a Builder if an instance of {@link HttpClient} pre-setting the proxy if configured.
63+
*
64+
* @return the client
65+
*/
66+
public HttpClientBuilder builder(boolean setProxy) {
67+
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
68+
if (setProxy) {
69+
clientBuilder.setRoutePlanner(proxyRoutePlanner);
70+
}
71+
getRequestInterceptors().forEach(clientBuilder::addInterceptorLast);
72+
getResponseInterceptors().forEach(clientBuilder::addInterceptorLast);
73+
return clientBuilder;
74+
}
75+
76+
/**
77+
* Build an instance of {@link HttpClient} without setting the proxy, even if
78+
* configured.
79+
*
80+
* @return the client
81+
*/
82+
public CloseableHttpClient buildWithoutProxy() {
83+
return build(HttpClientBuilder.create(), false);
84+
}
85+
86+
/**
87+
* Build an instance of {@link HttpClient} setting the proxy if configured,
88+
* disabling automatic retries and setting the maximum total connection.
89+
*
90+
* @param maxConnTotal the maximum total connection value
91+
* @return the client
92+
*/
93+
public CloseableHttpClient buildWithoutAutomaticRetries(int maxConnTotal) {
94+
HttpClientBuilder clientBuilder = HttpClientBuilder.create()
95+
.disableAutomaticRetries()
96+
.setMaxConnTotal(maxConnTotal);
97+
return build(clientBuilder, true);
98+
}
99+
100+
/**
101+
* Build an instance of {@link HttpClient} setting the proxy if configured with
102+
* the given request configuration.
103+
* @param requestConfig the request configuration
104+
* @return the client
105+
*/
106+
public CloseableHttpClient buildWithRequestConfig(RequestConfig requestConfig) {
107+
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
108+
.setDefaultRequestConfig(requestConfig);
109+
return build(httpClientBuilder, true);
110+
}
111+
112+
private CloseableHttpClient build(HttpClientBuilder clientBuilder, boolean setProxy) {
113+
if (setProxy) {
114+
clientBuilder.setRoutePlanner(proxyRoutePlanner);
115+
}
116+
getRequestInterceptors().forEach(clientBuilder::addInterceptorLast);
117+
getResponseInterceptors().forEach(clientBuilder::addInterceptorLast);
118+
return clientBuilder.build();
119+
}
120+
121+
public ConfigurationService getConfigurationService() {
122+
return configurationService;
123+
}
124+
125+
public void setConfigurationService(ConfigurationService configurationService) {
126+
this.configurationService = configurationService;
127+
}
128+
129+
public List<HttpRequestInterceptor> getRequestInterceptors() {
130+
return emptyIfNull(requestInterceptors);
131+
}
132+
133+
public void setRequestInterceptors(List<HttpRequestInterceptor> requestInterceptors) {
134+
this.requestInterceptors = requestInterceptors;
135+
}
136+
137+
public List<HttpResponseInterceptor> getResponseInterceptors() {
138+
return emptyIfNull(responseInterceptors);
139+
}
140+
141+
public void setResponseInterceptors(List<HttpResponseInterceptor> responseInterceptors) {
142+
this.responseInterceptors = responseInterceptors;
143+
}
144+
145+
public DSpaceProxyRoutePlanner getProxyRoutePlanner() {
146+
return proxyRoutePlanner;
147+
}
148+
149+
public void setProxyRoutePlanner(DSpaceProxyRoutePlanner proxyRoutePlanner) {
150+
this.proxyRoutePlanner = proxyRoutePlanner;
151+
}
152+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* The contents of this file are subject to the license and copyright
3+
* detailed in the LICENSE and NOTICE files at the root of the source
4+
* tree and available online at
5+
*
6+
* http://www.dspace.org/license/
7+
*/
8+
package org.dspace.app.client;
9+
10+
import java.util.Arrays;
11+
12+
import org.apache.commons.lang3.ArrayUtils;
13+
import org.apache.commons.lang3.StringUtils;
14+
import org.apache.http.HttpException;
15+
import org.apache.http.HttpHost;
16+
import org.apache.http.HttpRequest;
17+
import org.apache.http.impl.conn.DefaultRoutePlanner;
18+
import org.apache.http.protocol.HttpContext;
19+
import org.dspace.services.ConfigurationService;
20+
21+
/**
22+
* Extension of {@link DefaultRoutePlanner} that determine the proxy based on
23+
* the configuration service, ignoring configured hosts.
24+
*
25+
* @author Luca Giamminonni (luca.giamminonni at 4science.it)
26+
*
27+
*/
28+
public class DSpaceProxyRoutePlanner extends DefaultRoutePlanner {
29+
30+
private ConfigurationService configurationService;
31+
32+
public DSpaceProxyRoutePlanner(ConfigurationService configurationService) {
33+
super(null);
34+
this.configurationService = configurationService;
35+
}
36+
37+
@Override
38+
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
39+
if (isTargetHostConfiguredToBeIgnored(target)) {
40+
return null;
41+
}
42+
String proxyHost = configurationService.getProperty("http.proxy.host");
43+
String proxyPort = configurationService.getProperty("http.proxy.port");
44+
if (StringUtils.isAnyBlank(proxyHost, proxyPort)) {
45+
return null;
46+
}
47+
try {
48+
return new HttpHost(proxyHost, Integer.parseInt(proxyPort), "http");
49+
} catch (NumberFormatException e) {
50+
throw new RuntimeException("Invalid proxy port configuration: " + proxyPort);
51+
}
52+
}
53+
54+
private boolean isTargetHostConfiguredToBeIgnored(HttpHost target) {
55+
String[] hostsToIgnore = configurationService.getArrayProperty("http.proxy.hosts-to-ignore");
56+
if (ArrayUtils.isEmpty(hostsToIgnore)) {
57+
return false;
58+
}
59+
return Arrays.stream(hostsToIgnore)
60+
.anyMatch(host -> matchesHost(host, target.getHostName()));
61+
}
62+
63+
private boolean matchesHost(String hostPattern, String hostName) {
64+
if (hostName.equals(hostPattern)) {
65+
return true;
66+
} else if (hostPattern.startsWith("*")) {
67+
return hostName.endsWith(StringUtils.removeStart(hostPattern, "*"));
68+
} else if (hostPattern.endsWith("*")) {
69+
return hostName.startsWith(StringUtils.removeEnd(hostPattern, "*"));
70+
}
71+
return false;
72+
}
73+
}

0 commit comments

Comments
 (0)