1
|
package eu.dnetlib.data.utils;
|
2
|
|
3
|
import java.security.KeyManagementException;
|
4
|
import java.security.KeyStoreException;
|
5
|
import java.security.NoSuchAlgorithmException;
|
6
|
import java.util.concurrent.TimeUnit;
|
7
|
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
11
|
import org.apache.http.impl.client.HttpClientBuilder;
|
12
|
import org.apache.http.ssl.SSLContextBuilder;
|
13
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
14
|
import org.springframework.web.client.RestTemplate;
|
15
|
|
16
|
public class RestTemplateFactory {
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(RestTemplateFactory.class);
|
19
|
|
20
|
private static final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
|
21
|
private static SSLConnectionSocketFactory sslSocketFactory;
|
22
|
static {
|
23
|
try {
|
24
|
sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true);
|
25
|
sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
|
26
|
} catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
|
27
|
log.error(e);;
|
28
|
}
|
29
|
}
|
30
|
private static final HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder
|
31
|
.create()
|
32
|
.setConnectionTimeToLive(0, TimeUnit.MILLISECONDS)
|
33
|
.setMaxConnPerRoute(1)
|
34
|
.setMaxConnTotal(1)
|
35
|
.disableAutomaticRetries()
|
36
|
.disableConnectionState()
|
37
|
.setSSLSocketFactory(sslSocketFactory)
|
38
|
.build());
|
39
|
|
40
|
public static RestTemplate newInstance() {
|
41
|
return new RestTemplate(httpRequestFactory);
|
42
|
}
|
43
|
}
|