1
|
package eu.dnetlib.data.mdstore.plugins;
|
2
|
|
3
|
import java.net.URI;
|
4
|
import java.net.URISyntaxException;
|
5
|
import java.security.KeyManagementException;
|
6
|
import java.security.KeyStoreException;
|
7
|
import java.security.NoSuchAlgorithmException;
|
8
|
import java.util.concurrent.TimeUnit;
|
9
|
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
13
|
import org.apache.http.impl.client.HttpClientBuilder;
|
14
|
import org.apache.http.ssl.SSLContextBuilder;
|
15
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
16
|
import org.springframework.web.client.RestTemplate;
|
17
|
|
18
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
19
|
|
20
|
public abstract class GenericDoiMdstorePlugin extends MdRecordPlugin {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(GenericDoiMdstorePlugin.class);
|
23
|
|
24
|
private static final int MAX_NUMBER_OF_ATTEMPTS = 10;
|
25
|
private static final int INTERVAL_MILLIS = 20000;
|
26
|
|
27
|
private static final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
|
28
|
private static SSLConnectionSocketFactory sslSocketFactory;
|
29
|
static {
|
30
|
try {
|
31
|
sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true);
|
32
|
sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
|
33
|
} catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
|
34
|
log.error(e);;
|
35
|
}
|
36
|
}
|
37
|
|
38
|
private static final HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder
|
39
|
.create()
|
40
|
.setConnectionTimeToLive(0, TimeUnit.MILLISECONDS)
|
41
|
.setMaxConnPerRoute(1)
|
42
|
.setMaxConnTotal(1)
|
43
|
.disableAutomaticRetries()
|
44
|
.disableConnectionState()
|
45
|
.setSSLSocketFactory(sslSocketFactory)
|
46
|
.build());
|
47
|
|
48
|
@Override
|
49
|
protected final boolean updateRecord(final MdRecord record) {
|
50
|
for (final String doi : record.getDois()) {
|
51
|
log.debug(" Record " + record.getId() + " has doi " + doi);
|
52
|
final String response = download(doi);
|
53
|
if ((response != null) && updateDocument(record, response)) { return true; }
|
54
|
}
|
55
|
return false;
|
56
|
}
|
57
|
|
58
|
abstract protected boolean updateDocument(MdRecord doc, String response);
|
59
|
|
60
|
abstract protected URI prepareURI(String doi) throws URISyntaxException;
|
61
|
|
62
|
private String download(final String doi) {
|
63
|
try {
|
64
|
return fetchUrl(prepareURI(doi), MAX_NUMBER_OF_ATTEMPTS);
|
65
|
} catch (final URISyntaxException e) {
|
66
|
log.error("Error resolving doi: " + doi, e);
|
67
|
return null;
|
68
|
}
|
69
|
}
|
70
|
|
71
|
private static String fetchUrl(final URI url, final int attempts) {
|
72
|
if (attempts == 0) { throw new RuntimeException("Max number of attempts reached, downloading url: " + url); }
|
73
|
|
74
|
try {
|
75
|
return (new RestTemplate(httpRequestFactory)).getForObject(url, String.class);
|
76
|
} catch (final Exception e) {
|
77
|
try {
|
78
|
log.error("Error downloading url: " + url + " - " + e.getMessage());
|
79
|
Thread.sleep(INTERVAL_MILLIS);
|
80
|
return fetchUrl(url, attempts - 1);
|
81
|
} catch (final InterruptedException e1) {
|
82
|
throw new RuntimeException(e1);
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
|
87
|
}
|