1
|
package eu.dnetlib.data.mdstore.plugins;
|
2
|
|
3
|
import java.net.URI;
|
4
|
import java.net.URISyntaxException;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
|
9
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
10
|
import eu.dnetlib.data.utils.HttpFetcher;
|
11
|
|
12
|
public abstract class GenericDoiMdstorePlugin extends MdRecordPlugin {
|
13
|
|
14
|
private static final Log log = LogFactory.getLog(GenericDoiMdstorePlugin.class);
|
15
|
|
16
|
private String accessToken;
|
17
|
|
18
|
@Override
|
19
|
protected final boolean updateRecord(final String recordId, final MdRecord record) {
|
20
|
for (final String doi : record.getDois()) {
|
21
|
log.debug(" Record " + record.getId() + " has doi " + doi);
|
22
|
final String response = download(doi);
|
23
|
if ((response != null) && updateDocument(record, response)) { return true; }
|
24
|
}
|
25
|
return false;
|
26
|
}
|
27
|
|
28
|
abstract protected boolean updateDocument(MdRecord doc, String response);
|
29
|
|
30
|
abstract protected URI prepareURI(String doi) throws URISyntaxException;
|
31
|
|
32
|
private String download(final String doi) {
|
33
|
try {
|
34
|
return HttpFetcher.fetch(prepareURI(doi), accessToken);
|
35
|
} catch (final URISyntaxException e) {
|
36
|
log.error("Error resolving doi: " + doi, e);
|
37
|
return null;
|
38
|
} finally {
|
39
|
try {
|
40
|
Thread.sleep(1000);
|
41
|
} catch (final InterruptedException e) {
|
42
|
log.warn("Error during sleep", e);
|
43
|
}
|
44
|
}
|
45
|
}
|
46
|
|
47
|
public String getAccessToken() {
|
48
|
return accessToken;
|
49
|
}
|
50
|
|
51
|
public void setAccessToken(final String accessToken) {
|
52
|
this.accessToken = accessToken;
|
53
|
}
|
54
|
|
55
|
}
|