Project

General

Profile

1
package eu.dnetlib.resolver;
2

    
3
import java.io.IOException;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import eu.dnetlib.resolver.model.ResolvedObject;
8
import net.sf.ehcache.Cache;
9
import net.sf.ehcache.Element;
10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.apache.http.HttpEntity;
14
import org.apache.http.client.ResponseHandler;
15
import org.apache.http.client.methods.HttpGet;
16
import org.apache.http.impl.client.CloseableHttpClient;
17
import org.apache.http.impl.client.HttpClients;
18
import org.apache.http.util.EntityUtils;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Required;
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

    
29
	public static Map<String, String> resolvedTypes = new HashMap<String, String>() {
30
		{
31
			put("pdb", "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s");
32
			put("ncbi-n", "http://www.ncbi.nlm.nih.gov/gquery/?term=%s");
33
			put("pmid", "http://www.ncbi.nlm.nih.gov/pubmed/%s");
34
			put("pmcid", "http://www.ncbi.nlm.nih.gov/pmc/articles/%s");
35
			put("pubmedid", "http://www.ncbi.nlm.nih.gov/pubmed/%s");
36
			put("doi", "http://dx.doi.org/%s");
37
			put("genbank", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
38
			put("nuccore", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
39
			put("swiss-prot", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
40
			put("arrayexpress", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
41
			put("biomodels", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
42
			put("bmrb", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
43
			put("ena", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
44
			put("genbank", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
45
			put("geo", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
46
			put("ensembl", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
47
			put("mgi", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
48
			put("bind", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
49
			put("pride", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
50
			put("ddbj", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
51
			put("bioproject", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
52
			put("embl", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
53
			put("sra", "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank");
54
		}
55

    
56
	};
57

    
58
	private Cache cache;
59
	private String name;
60
	@Autowired
61
	private ResolverStore storeManager;
62
	private ResponseHandler<String> responseHandler = response -> {
63
		int status = response.getStatusLine().getStatusCode();
64
		if (status >= 200 && status < 300) {
65
			HttpEntity entity = response.getEntity();
66
			return entity != null ? EntityUtils.toString(entity) : null;
67
		} else {
68
			log.debug("Unexpected response status: " + status);
69
			return null;
70
		}
71
	};
72

    
73
	public ResolvedObject retrievePID(final String pid, final String pidType) {
74

    
75
		if (!canResolvePid(pidType)) return null;
76
		final Element item = cache.get(storeManager.generateDNetIdentifier(pid, pidType));
77

    
78
		if (item != null) return (ResolvedObject) item.getObjectValue();
79

    
80
		ResolvedObject resolvedObject = storeManager.getRecord(pid, pidType);
81

    
82
		if (resolvedObject != null) {
83
			cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
84
			return resolvedObject;
85
		}
86
		resolvedObject = resolve(pid, pidType);
87
		if (resolvedObject != null) {
88

    
89
			storeManager.insertRecord(getName(), resolvedObject);
90
			cache.put(new Element(storeManager.generateDNetIdentifier(pid, pidType), resolvedObject));
91
			return resolvedObject;
92
		}
93
		return null;
94
	}
95

    
96
	public String retrieveDnetID(final String pid, final String pidType) {
97

    
98
		if (!canResolvePid(pidType)) return null;
99
		final String dnetIdentifier = storeManager.generateDNetIdentifier(pid, pidType);
100
		final Element item = cache.get(dnetIdentifier);
101

    
102
		if (item != null) return dnetIdentifier;
103

    
104
		String resolvedIdentifier = storeManager.getRecordIdentifier(pid, pidType);
105

    
106
		if (resolvedIdentifier != null && !StringUtils.isBlank(resolvedIdentifier))
107
			return resolvedIdentifier;
108

    
109
		ResolvedObject resolvedObject = resolve(pid, pidType);
110
		if (resolvedObject != null) {
111
			storeManager.insertRecord(getName(), resolvedObject);
112
			cache.put(new Element(dnetIdentifier, resolvedObject));
113
			return dnetIdentifier;
114
		}
115

    
116
		return null;
117
	}
118

    
119
	public String getName() {
120
		return name;
121
	}
122

    
123
	public void setName(final String name) {
124
		this.name = name;
125
	}
126

    
127
	protected abstract boolean canResolvePid(final String pidType);
128

    
129
	protected abstract ResolvedObject resolve(final String pid, final String pidType);
130

    
131
	public Cache getCache() {
132
		return cache;
133
	}
134

    
135
	@Required
136
	public void setCache(final Cache cache) {
137
		this.cache = cache;
138
	}
139

    
140
	protected String requestURL(final String url) {
141
		final CloseableHttpClient httpclient = HttpClients.createDefault();
142
		try {
143

    
144
			HttpGet httpGet = new HttpGet(url);
145
			return httpclient.execute(httpGet, responseHandler);
146
		} catch (Throwable e) {
147
			log.debug("Error in getting url " + url, e);
148
			return null;
149
		} finally {
150
			try {
151
				httpclient.close();
152
			} catch (IOException e) {
153
				log.error("Error on closing httpclient", e);
154
			}
155
		}
156
	}
157
}
(2-2/14)