1
|
package eu.dnetlib.dli.resolver;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
|
5
|
import eu.dnetlib.dli.DLIUtils;
|
6
|
import eu.dnetlib.dli.resolver.model.CompletionStatus;
|
7
|
import eu.dnetlib.dli.resolver.model.ObjectProvenance;
|
8
|
import eu.dnetlib.dli.resolver.model.ObjectProvisionMode;
|
9
|
import eu.dnetlib.dli.resolver.model.ResolvedObject;
|
10
|
import net.sf.ehcache.Cache;
|
11
|
import net.sf.ehcache.Element;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.apache.http.HttpEntity;
|
16
|
import org.apache.http.client.ResponseHandler;
|
17
|
import org.apache.http.client.methods.HttpGet;
|
18
|
import org.apache.http.impl.client.CloseableHttpClient;
|
19
|
import org.apache.http.impl.client.HttpClients;
|
20
|
import org.apache.http.util.EntityUtils;
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.beans.factory.annotation.Required;
|
23
|
|
24
|
/**
|
25
|
* Created by sandro on 9/8/16.
|
26
|
*/
|
27
|
public abstract class AbstractPIDResolver implements PIDResolver {
|
28
|
|
29
|
private static final Log log = LogFactory.getLog(AbstractPIDResolver.class);
|
30
|
private Cache cache;
|
31
|
private String name;
|
32
|
@Autowired
|
33
|
private ResolverStore storeManager;
|
34
|
private ResponseHandler<String> responseHandler = response -> {
|
35
|
int status = response.getStatusLine().getStatusCode();
|
36
|
if (status >= 200 && status < 300) {
|
37
|
HttpEntity entity = response.getEntity();
|
38
|
return entity != null ? EntityUtils.toString(entity) : null;
|
39
|
} else {
|
40
|
log.debug("Unexpected response status: " + status);
|
41
|
return null;
|
42
|
}
|
43
|
};
|
44
|
|
45
|
public static void setDatasourceProvenace(final ObjectProvenance provenance, final String namespacePrefix) {
|
46
|
try {
|
47
|
provenance.setDatasourceId(DLIUtils.getIdFromDataSourcePrefix(namespacePrefix));
|
48
|
provenance.setDatasource(DLIUtils.getNameFromDataSourcePrefix(namespacePrefix));
|
49
|
provenance.setCompletionStatus(CompletionStatus.complete.toString());
|
50
|
provenance.setProvisionMode(ObjectProvisionMode.resolved.toString());
|
51
|
} catch (Throwable e) {
|
52
|
log.error("Error on assignin datasource ");
|
53
|
}
|
54
|
}
|
55
|
|
56
|
public ResolvedObject retrievePID(final String pid, final String pidType) {
|
57
|
|
58
|
if (!canResolvePid(pidType)) return null;
|
59
|
final Element item = cache.get(storeManager.generateDNetIdentifier(pid, pidType));
|
60
|
|
61
|
if (item != null) return (ResolvedObject) item.getObjectValue();
|
62
|
|
63
|
ResolvedObject resolvedObject = storeManager.getRecord(pid, pidType);
|
64
|
|
65
|
if (resolvedObject != null) {
|
66
|
cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
|
67
|
return resolvedObject;
|
68
|
}
|
69
|
resolvedObject = resolve(pid, pidType);
|
70
|
if (resolvedObject != null) {
|
71
|
storeManager.insertRecord(getName(), resolvedObject);
|
72
|
cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
|
73
|
return resolvedObject;
|
74
|
}
|
75
|
return null;
|
76
|
}
|
77
|
|
78
|
public String retrieveDnetID(final String pid, final String pidType) {
|
79
|
|
80
|
if (!canResolvePid(pidType)) return null;
|
81
|
final String dnetIdentifier = storeManager.generateDNetIdentifier(pid, pidType);
|
82
|
final Element item = cache.get(dnetIdentifier);
|
83
|
|
84
|
if (item != null) return dnetIdentifier;
|
85
|
|
86
|
String resolvedIdentifier = storeManager.getRecordIdentifier(pid, pidType);
|
87
|
|
88
|
if (resolvedIdentifier != null && !StringUtils.isBlank(resolvedIdentifier))
|
89
|
return resolvedIdentifier;
|
90
|
|
91
|
ResolvedObject resolvedObject = resolve(pid, pidType);
|
92
|
if (resolvedObject != null) {
|
93
|
storeManager.insertRecord(getName(), resolvedObject);
|
94
|
cache.put(new Element(dnetIdentifier, resolvedObject));
|
95
|
return dnetIdentifier;
|
96
|
}
|
97
|
|
98
|
return null;
|
99
|
}
|
100
|
|
101
|
public String getName() {
|
102
|
return this.getClass().getSimpleName();
|
103
|
}
|
104
|
|
105
|
public void setName(final String name) {
|
106
|
this.name = name;
|
107
|
}
|
108
|
|
109
|
protected abstract boolean canResolvePid(final String pidType);
|
110
|
|
111
|
protected abstract ResolvedObject resolve(final String pid, final String pidType);
|
112
|
|
113
|
public Cache getCache() {
|
114
|
return cache;
|
115
|
}
|
116
|
|
117
|
@Required
|
118
|
public void setCache(final Cache cache) {
|
119
|
this.cache = cache;
|
120
|
}
|
121
|
|
122
|
protected String requestURL(final String url) {
|
123
|
final CloseableHttpClient httpclient = HttpClients.createDefault();
|
124
|
try {
|
125
|
|
126
|
HttpGet httpGet = new HttpGet(url);
|
127
|
return httpclient.execute(httpGet, responseHandler);
|
128
|
} catch (Throwable e) {
|
129
|
log.debug("Error in getting url " + url, e);
|
130
|
return null;
|
131
|
} finally {
|
132
|
try {
|
133
|
httpclient.close();
|
134
|
} catch (IOException e) {
|
135
|
log.error("Error on closing httpclient", e);
|
136
|
}
|
137
|
}
|
138
|
}
|
139
|
}
|