1
|
package eu.dnetlib.data.mdstore.plugins;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.net.URI;
|
5
|
import java.net.URISyntaxException;
|
6
|
import java.security.KeyManagementException;
|
7
|
import java.security.KeyStoreException;
|
8
|
import java.security.NoSuchAlgorithmException;
|
9
|
import java.util.Map;
|
10
|
import java.util.concurrent.TimeUnit;
|
11
|
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
15
|
import org.apache.http.impl.client.HttpClientBuilder;
|
16
|
import org.apache.http.ssl.SSLContextBuilder;
|
17
|
import org.dom4j.Document;
|
18
|
import org.dom4j.DocumentException;
|
19
|
import org.dom4j.Node;
|
20
|
import org.dom4j.io.SAXReader;
|
21
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
22
|
import org.springframework.web.client.RestTemplate;
|
23
|
|
24
|
import com.mongodb.BasicDBObject;
|
25
|
import com.mongodb.DBObject;
|
26
|
import com.mongodb.client.MongoCollection;
|
27
|
|
28
|
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore;
|
29
|
import eu.dnetlib.rmi.data.MDStoreServiceException;
|
30
|
|
31
|
public abstract class GenericDoiMdstorePlugin extends AbstractIstiMDStorePlugin {
|
32
|
|
33
|
private static final Log log = LogFactory.getLog(GenericDoiMdstorePlugin.class);
|
34
|
|
35
|
private static final int MAX_NUMBER_OF_ATTEMPTS = 10;
|
36
|
private static final int INTERVAL_MILLIS = 20000;
|
37
|
|
38
|
private static final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
|
39
|
private static SSLConnectionSocketFactory sslSocketFactory;
|
40
|
static {
|
41
|
try {
|
42
|
sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true);
|
43
|
sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
|
44
|
} catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
|
45
|
log.error(e);;
|
46
|
}
|
47
|
}
|
48
|
|
49
|
private static final HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder
|
50
|
.create()
|
51
|
.setConnectionTimeToLive(0, TimeUnit.MILLISECONDS)
|
52
|
.setMaxConnPerRoute(1)
|
53
|
.setMaxConnTotal(1)
|
54
|
.disableAutomaticRetries()
|
55
|
.disableConnectionState()
|
56
|
.setSSLSocketFactory(sslSocketFactory)
|
57
|
.build());
|
58
|
|
59
|
@Override
|
60
|
public void process(final MongoMDStore store, final Map<String, String> params) throws MDStoreServiceException {
|
61
|
|
62
|
final MongoCollection<DBObject> collPubs = store.getCollection();
|
63
|
|
64
|
int count = 0;
|
65
|
|
66
|
for (final DBObject obj : collPubs.find()) {
|
67
|
|
68
|
try {
|
69
|
final String recordId = obj.get("id").toString();
|
70
|
final Document doc = (new SAXReader()).read(new StringReader(obj.get("body").toString()));
|
71
|
for (final Object o : doc.selectNodes("//*[local-name()='alternateIdentifier' and @alternateIdentifierType='doi']")) {
|
72
|
final String doi = ((Node) o).getText().trim();
|
73
|
|
74
|
log.debug(" Record " + recordId + " has doi " + doi);
|
75
|
final String response = download(doi);
|
76
|
if ((response != null) && updateDocument(doc, response)) {
|
77
|
collPubs.updateOne(new BasicDBObject("id", recordId), new BasicDBObject("$set", new BasicDBObject("body", doc.asXML())));
|
78
|
count++;
|
79
|
}
|
80
|
}
|
81
|
} catch (final DocumentException e) {
|
82
|
log.warn("Problem parsing a mdstore record");
|
83
|
}
|
84
|
}
|
85
|
|
86
|
log.info("Number of patched records: " + count);
|
87
|
|
88
|
touch(store);
|
89
|
}
|
90
|
|
91
|
abstract protected boolean updateDocument(Document doc, String response);
|
92
|
|
93
|
abstract protected URI prepareURI(String doi) throws URISyntaxException;
|
94
|
|
95
|
private String download(final String doi) {
|
96
|
try {
|
97
|
return fetchUrl(prepareURI(doi), MAX_NUMBER_OF_ATTEMPTS);
|
98
|
} catch (final URISyntaxException e) {
|
99
|
log.error("Error resolving doi: " + doi, e);
|
100
|
return null;
|
101
|
}
|
102
|
}
|
103
|
|
104
|
private static String fetchUrl(final URI url, final int attempts) {
|
105
|
if (attempts == 0) { throw new RuntimeException("Max number of attempts reached, downloading url: " + url); }
|
106
|
|
107
|
try {
|
108
|
return (new RestTemplate(httpRequestFactory)).getForObject(url, String.class);
|
109
|
} catch (final Exception e) {
|
110
|
try {
|
111
|
log.error("Error downloading url: " + url + " - " + e.getMessage());
|
112
|
Thread.sleep(INTERVAL_MILLIS);
|
113
|
return fetchUrl(url, attempts - 1);
|
114
|
} catch (final InterruptedException e1) {
|
115
|
throw new RuntimeException(e1);
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
}
|