1
|
package eu.dnetlib.pid.resolver;
|
2
|
|
3
|
import eu.dnetlib.pid.resolver.model.ResolvedObject;
|
4
|
import eu.dnetlib.pid.resolver.store.ResolverStore;
|
5
|
import net.sf.ehcache.Cache;
|
6
|
import net.sf.ehcache.Element;
|
7
|
import org.apache.commons.lang3.StringUtils;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.apache.http.HttpEntity;
|
11
|
import org.apache.http.client.ResponseHandler;
|
12
|
import org.apache.http.client.config.RequestConfig;
|
13
|
import org.apache.http.client.methods.HttpGet;
|
14
|
import org.apache.http.impl.client.CloseableHttpClient;
|
15
|
import org.apache.http.impl.client.HttpClientBuilder;
|
16
|
import org.apache.http.util.EntityUtils;
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.beans.factory.annotation.Required;
|
19
|
|
20
|
import java.io.IOException;
|
21
|
|
22
|
/**
|
23
|
* Created by sandro on 9/8/16.
|
24
|
*/
|
25
|
public abstract class AbstractPIDResolver implements PIDResolver {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(AbstractPIDResolver.class);
|
28
|
private Cache cache;
|
29
|
protected String name;
|
30
|
protected int order;
|
31
|
|
32
|
protected boolean availableOffline;
|
33
|
|
34
|
@Autowired
|
35
|
private ResolverStore storeManager;
|
36
|
|
37
|
private ResponseHandler<String> responseHandler = response -> {
|
38
|
int status = response.getStatusLine().getStatusCode();
|
39
|
if (status >= 200 && status < 300) {
|
40
|
HttpEntity entity = response.getEntity();
|
41
|
return entity != null ? EntityUtils.toString(entity) : null;
|
42
|
} else {
|
43
|
log.debug("Unexpected response status: " + status);
|
44
|
return null;
|
45
|
}
|
46
|
};
|
47
|
|
48
|
// public abstract void setDatasourceProvenance(final ObjectProvenance provenance, final String namespacePrefix);
|
49
|
|
50
|
public ResolvedObject retrievePID(final String pid, final String pidType, boolean offline) {
|
51
|
if (offline && !isAvailableOffline())
|
52
|
return null;
|
53
|
if (!canResolvePid(pidType)) return null;
|
54
|
final String dnetIdentifier = storeManager.generateDNetIdentifier(pid, pidType);
|
55
|
final Element item = cache.get(dnetIdentifier);
|
56
|
|
57
|
if (item != null) return (ResolvedObject) item.getObjectValue();
|
58
|
|
59
|
ResolvedObject resolvedObject = storeManager.getRecord(dnetIdentifier);
|
60
|
|
61
|
if (resolvedObject != null) {
|
62
|
cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
|
63
|
return resolvedObject;
|
64
|
}
|
65
|
|
66
|
log.debug("Cache miss for pid:" + pid + " and type :" + pidType);
|
67
|
resolvedObject = resolve(pid, pidType);
|
68
|
if (resolvedObject != null) {
|
69
|
storeManager.insertRecord(getName(), resolvedObject);
|
70
|
cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
|
71
|
return resolvedObject;
|
72
|
}
|
73
|
return null;
|
74
|
}
|
75
|
|
76
|
public String retrieveDnetID(final String pid, final String pidType, boolean offline) {
|
77
|
if (offline && !isAvailableOffline())
|
78
|
return null;
|
79
|
if (!canResolvePid(pidType)) return null;
|
80
|
final String dnetIdentifier = storeManager.generateDNetIdentifier(pid, pidType);
|
81
|
final Element item = cache.get(dnetIdentifier);
|
82
|
|
83
|
if (item != null) return dnetIdentifier;
|
84
|
|
85
|
String resolvedIdentifier = storeManager.getRecordIdentifier(dnetIdentifier);
|
86
|
|
87
|
if (resolvedIdentifier != null && !StringUtils.isBlank(resolvedIdentifier))
|
88
|
return resolvedIdentifier;
|
89
|
|
90
|
ResolvedObject resolvedObject = resolve(pid, pidType);
|
91
|
if (resolvedObject != null) {
|
92
|
storeManager.insertRecord(getName(), resolvedObject);
|
93
|
cache.put(new Element(dnetIdentifier, resolvedObject));
|
94
|
return dnetIdentifier;
|
95
|
}
|
96
|
|
97
|
return null;
|
98
|
}
|
99
|
|
100
|
public String getName() {
|
101
|
return this.getClass().getSimpleName();
|
102
|
}
|
103
|
|
104
|
public void setName(final String name) {
|
105
|
this.name = name;
|
106
|
}
|
107
|
|
108
|
protected abstract boolean canResolvePid(final String pidType);
|
109
|
|
110
|
protected abstract ResolvedObject resolve(final String pid, final String pidType);
|
111
|
|
112
|
public Cache getCache() {
|
113
|
return cache;
|
114
|
}
|
115
|
|
116
|
@Required
|
117
|
public void setCache(final Cache cache) {
|
118
|
this.cache = cache;
|
119
|
}
|
120
|
|
121
|
protected String requestURL(final String url) {
|
122
|
int timeout = 4;
|
123
|
RequestConfig config = RequestConfig.custom()
|
124
|
.setConnectTimeout(timeout * 1000)
|
125
|
.setConnectionRequestTimeout(timeout * 1000)
|
126
|
.setSocketTimeout(timeout * 1000).build();
|
127
|
final CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
128
|
try {
|
129
|
HttpGet httpGet = new HttpGet(url);
|
130
|
return httpclient.execute(httpGet, responseHandler);
|
131
|
} catch (Throwable e) {
|
132
|
log.debug("Error in getting url " + url, e);
|
133
|
return null;
|
134
|
} finally {
|
135
|
try {
|
136
|
httpclient.close();
|
137
|
} catch (IOException e) {
|
138
|
log.error("Error on closing httpclient", e);
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
|
143
|
|
144
|
@Override
|
145
|
public int getOrder() {
|
146
|
return this.order;
|
147
|
}
|
148
|
|
149
|
@Required
|
150
|
public AbstractPIDResolver setOrder(int order) {
|
151
|
this.order = order;
|
152
|
return this;
|
153
|
}
|
154
|
|
155
|
|
156
|
@Override
|
157
|
public int compareTo(PIDResolver o) {
|
158
|
return Integer.compare(getOrder(), o.getOrder());
|
159
|
}
|
160
|
|
161
|
|
162
|
public boolean isAvailableOffline() {
|
163
|
return availableOffline;
|
164
|
}
|
165
|
|
166
|
@Required
|
167
|
public AbstractPIDResolver setAvailableOffline(boolean availableOffline) {
|
168
|
this.availableOffline = availableOffline;
|
169
|
return this;
|
170
|
}
|
171
|
}
|