1
|
package eu.dnetlib.data.collector.plugins.doiresolver;
|
2
|
|
3
|
import eu.dnetlib.data.collector.plugins.HttpConnector;
|
4
|
import eu.dnetlib.data.collector.plugins.utils.JsonUtils;
|
5
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
6
|
import org.apache.commons.io.IOUtils;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.util.regex.Pattern;
|
14
|
|
15
|
public class CrossrefResolver implements DOIResolver{
|
16
|
|
17
|
private static final Log log = LogFactory.getLog(CrossrefResolver.class);
|
18
|
private String baseURL = "https://api.crossref.org/works/";
|
19
|
|
20
|
public static Pattern CROSSREF_DOI_REGEXP = Pattern.compile("^10\\.\\d{4,9}/[-._;()/:A-Z0-9]+$", Pattern.CASE_INSENSITIVE);
|
21
|
|
22
|
|
23
|
@Autowired
|
24
|
private HttpConnector httpConnector;
|
25
|
|
26
|
@Override
|
27
|
public String resolve(String doi) {
|
28
|
if(isValid(doi)) {
|
29
|
try {
|
30
|
InputStream is = httpConnector.getInputSourceAsStream(getBaseURL() + doi);
|
31
|
return asXml(IOUtils.toString(is));
|
32
|
} catch (IOException | CollectorServiceException e) {
|
33
|
log.error("Cannot resolve doi " + doi + " Exception: " + e);
|
34
|
return null;
|
35
|
}
|
36
|
}
|
37
|
else {
|
38
|
log.debug("Input does not look like a valid DOI " + doi);
|
39
|
return null;
|
40
|
}
|
41
|
}
|
42
|
|
43
|
protected boolean isValid(String doi){
|
44
|
return CROSSREF_DOI_REGEXP.matcher(doi).matches();
|
45
|
}
|
46
|
|
47
|
protected String asXml(final String record){
|
48
|
return new JsonUtils().convertToXML(record);
|
49
|
}
|
50
|
|
51
|
public String getBaseURL() {
|
52
|
return baseURL;
|
53
|
}
|
54
|
|
55
|
public void setBaseURL(final String baseURL) {
|
56
|
this.baseURL = baseURL;
|
57
|
}
|
58
|
|
59
|
public HttpConnector getHttpConnector() {
|
60
|
return httpConnector;
|
61
|
}
|
62
|
|
63
|
public void setHttpConnector(HttpConnector httpConnector) {
|
64
|
this.httpConnector = httpConnector;
|
65
|
}
|
66
|
}
|