Revision 794be65e
Added by Andreas Müller over 1 year ago
cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/bci/BciServiceWrapper.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2009 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.ext.bci; |
|
10 |
|
|
11 |
import java.io.BufferedReader; |
|
12 |
import java.io.IOException; |
|
13 |
import java.io.InputStream; |
|
14 |
import java.io.InputStreamReader; |
|
15 |
import java.net.HttpURLConnection; |
|
16 |
import java.net.MalformedURLException; |
|
17 |
import java.net.URISyntaxException; |
|
18 |
import java.net.URL; |
|
19 |
import java.util.ArrayList; |
|
20 |
import java.util.HashMap; |
|
21 |
import java.util.List; |
|
22 |
import java.util.Map; |
|
23 |
|
|
24 |
import org.apache.commons.lang.StringUtils; |
|
25 |
import org.apache.http.NameValuePair; |
|
26 |
import org.apache.http.message.BasicNameValuePair; |
|
27 |
import org.apache.logging.log4j.LogManager; |
|
28 |
import org.apache.logging.log4j.Logger; |
|
29 |
import org.springframework.stereotype.Component; |
|
30 |
|
|
31 |
import com.ibm.lsid.MalformedLSIDException; |
|
32 |
|
|
33 |
import eu.etaxonomy.cdm.api.application.ICdmRepository; |
|
34 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
35 |
import eu.etaxonomy.cdm.common.URI; |
|
36 |
import eu.etaxonomy.cdm.ext.common.SchemaAdapterBase; |
|
37 |
import eu.etaxonomy.cdm.ext.common.ServiceWrapperBase; |
|
38 |
import eu.etaxonomy.cdm.model.common.IdentifiableEntity; |
|
39 |
import eu.etaxonomy.cdm.model.common.LSID; |
|
40 |
import eu.etaxonomy.cdm.model.occurrence.Collection; |
|
41 |
import eu.etaxonomy.cdm.model.reference.OriginalSourceType; |
|
42 |
import eu.etaxonomy.cdm.model.reference.Reference; |
|
43 |
import eu.etaxonomy.cdm.model.reference.ReferenceFactory; |
|
44 |
|
|
45 |
/** |
|
46 |
* This service allows to query the Biodiversity collection index {@link http://www.biodiversitycollectionsindex.org} |
|
47 |
* @author a.mueller |
|
48 |
* @since Aug 16, 2010 |
|
49 |
*/ |
|
50 |
@Component |
|
51 |
public class BciServiceWrapper |
|
52 |
extends ServiceWrapperBase<Collection> |
|
53 |
implements IBciServiceWrapper{ |
|
54 |
|
|
55 |
private static final Logger logger = LogManager.getLogger(BciServiceWrapper.class); |
|
56 |
|
|
57 |
private enum ServiceType{ |
|
58 |
AUTHOR, |
|
59 |
NAME, |
|
60 |
PUBLICATION, |
|
61 |
} |
|
62 |
|
|
63 |
// private URL serviceUrl; |
|
64 |
|
|
65 |
// ******************************** CONSTRUCTOR ************************************** |
|
66 |
|
|
67 |
|
|
68 |
// /** |
|
69 |
// * Creates new instance of this factory and connects it to the given |
|
70 |
// * CDM Community Stores access point. |
|
71 |
// * |
|
72 |
// * Typically, there is no need to instantiate this class. |
|
73 |
// */ |
|
74 |
// protected IpniService(URL webserviceUrl){ |
|
75 |
// this.serviceUrl = webserviceUrl; |
|
76 |
// } |
|
77 |
|
|
78 |
// ****************************** METHODS ****************************************************/ |
|
79 |
|
|
80 |
@Override |
|
81 |
public List<Collection> getCollectionsByCode(String code, ICdmRepository appConfig){ |
|
82 |
|
|
83 |
SchemaAdapterBase<Collection> schemaAdapter = schemaAdapterMap.get("recordSchema"); |
|
84 |
if(schemaAdapter == null){ |
|
85 |
throw new RuntimeException("No SchemaAdapter found for " + "recordSchema"); |
|
86 |
} |
|
87 |
|
|
88 |
String SruOperation = "searchRetrieve"; |
|
89 |
|
|
90 |
List<NameValuePair> pairs = new ArrayList<>(); |
|
91 |
pairs.add(new BasicNameValuePair("code", SruOperation)); |
|
92 |
|
|
93 |
Map<String, String> requestHeaders = new HashMap<>(); |
|
94 |
requestHeaders.put("Accept-Charset", "UTF-8"); |
|
95 |
|
|
96 |
try { |
|
97 |
URI requestUri = createUri(null, pairs); |
|
98 |
InputStream stream = executeHttpGet(requestUri, requestHeaders); |
|
99 |
return schemaAdapter.getCmdEntities(stream); |
|
100 |
} catch (IOException e) { |
|
101 |
// thrown by doHttpGet |
|
102 |
logger.error(e); |
|
103 |
} catch (URISyntaxException e) { |
|
104 |
// thrown by createUri |
|
105 |
logger.error(e); |
|
106 |
} |
|
107 |
|
|
108 |
code = normalizeParameter(code); |
|
109 |
String request = code; |
|
110 |
|
|
111 |
@SuppressWarnings("unchecked") |
|
112 |
List<Collection> result = (List<Collection>)queryService(request, appConfig, getServiceUrl(IBciServiceWrapper.LOOKUP_CODE_REST), ServiceType.AUTHOR); |
|
113 |
return result; |
|
114 |
} |
|
115 |
|
|
116 |
private List<? extends IdentifiableEntity> queryService(String request, ICdmRepository appConfig, URL serviceUrl, ServiceType serviceType){ |
|
117 |
try { |
|
118 |
// create the request url |
|
119 |
URL newUrl = new URL(serviceUrl.getProtocol(), |
|
120 |
serviceUrl.getHost(), |
|
121 |
serviceUrl.getPort(), |
|
122 |
serviceUrl.getPath() |
|
123 |
+ "" + request); |
|
124 |
// open a connection |
|
125 |
HttpURLConnection connection = (HttpURLConnection) newUrl.openConnection(); |
|
126 |
// set the accept property to XML so we can use jdom to handle the content |
|
127 |
//connection.setRequestProperty("Accept", "text/xml"); |
|
128 |
|
|
129 |
logger.info("Firing request for URL: " + newUrl); |
|
130 |
|
|
131 |
int responseCode = connection.getResponseCode(); |
|
132 |
|
|
133 |
// get the content at the resource |
|
134 |
InputStream content = (InputStream) connection.getContent(); |
|
135 |
|
|
136 |
// build the result |
|
137 |
List<? extends IdentifiableEntity<?>> result; |
|
138 |
if (serviceType.equals(ServiceType.AUTHOR)){ |
|
139 |
result = buildCollectionList(content, appConfig); |
|
140 |
}else if (serviceType.equals(ServiceType.NAME)){ |
|
141 |
// |
|
142 |
result = null; |
|
143 |
}else{ |
|
144 |
// |
|
145 |
result = null; |
|
146 |
} |
|
147 |
if(responseCode == HttpURLConnection.HTTP_OK){ |
|
148 |
return result; |
|
149 |
}else if(responseCode == HttpURLConnection.HTTP_MULT_CHOICE){ |
|
150 |
return result; |
|
151 |
}else{ |
|
152 |
//TODO error handling |
|
153 |
logger.error("No Http_OK"); |
|
154 |
} |
|
155 |
|
|
156 |
} catch (IOException e) { |
|
157 |
logger.error("No content for request: " + request); |
|
158 |
} |
|
159 |
|
|
160 |
// error |
|
161 |
return null; |
|
162 |
} |
|
163 |
|
|
164 |
|
|
165 |
private List<Collection> buildCollectionList(InputStream content, ICdmRepository appConfig) throws IOException { |
|
166 |
List<Collection> result = new ArrayList<Collection>(); |
|
167 |
BufferedReader reader = new BufferedReader (new InputStreamReader(content)); |
|
168 |
|
|
169 |
String headerLine = reader.readLine(); |
|
170 |
|
|
171 |
String line = reader.readLine(); |
|
172 |
while (StringUtils.isNotBlank(line)){ |
|
173 |
Collection collection = getCollectionFromLine(line, appConfig); |
|
174 |
result.add(collection); |
|
175 |
line = reader.readLine(); |
|
176 |
} |
|
177 |
|
|
178 |
return result; |
|
179 |
} |
|
180 |
|
|
181 |
private Collection getCollectionFromLine(String line, ICdmRepository appConfig) { |
|
182 |
//urn:lsid:biocol.org:col:15727 http://biocol.org/urn:lsid:biocol.org:col:15727 University of Bergen Herbarium |
|
183 |
String[] splits = line.split("\t"); |
|
184 |
if (splits.length != 3){ |
|
185 |
logger.warn("Unknwon BCI line format: " + line); |
|
186 |
return null; |
|
187 |
} |
|
188 |
String lsidString = splits[0]; |
|
189 |
String urlString = splits[1]; |
|
190 |
String collectionName = splits[2]; |
|
191 |
|
|
192 |
Collection result = Collection.NewInstance(); |
|
193 |
|
|
194 |
//LSID |
|
195 |
LSID lsid = null; |
|
196 |
try { |
|
197 |
lsid = new LSID(lsidString); |
|
198 |
} catch (MalformedLSIDException e) { |
|
199 |
logger.warn("Malformed LSID " + lsidString, e); |
|
200 |
} |
|
201 |
|
|
202 |
result.setLsid(lsid); |
|
203 |
String id = getCollectionId(lsid); |
|
204 |
|
|
205 |
result.setName(collectionName); |
|
206 |
|
|
207 |
//id, citation |
|
208 |
Reference citation = getBciCitation(appConfig); |
|
209 |
result.addSource(OriginalSourceType.Lineage, id, null, citation, null); |
|
210 |
|
|
211 |
|
|
212 |
return result; |
|
213 |
} |
|
214 |
|
|
215 |
|
|
216 |
private String getCollectionId(LSID lsid) { |
|
217 |
String result = lsid == null? null : lsid.getObject(); |
|
218 |
return result; |
|
219 |
} |
|
220 |
|
|
221 |
|
|
222 |
private Reference getBciCitation(ICdmRepository appConfig) { |
|
223 |
Reference bciReference; |
|
224 |
if (appConfig != null){ |
|
225 |
bciReference = appConfig.getReferenceService().find(uuidBci); |
|
226 |
if (bciReference == null){ |
|
227 |
bciReference = getNewBciReference(); |
|
228 |
bciReference.setUuid(uuidBci); |
|
229 |
appConfig.getReferenceService().save(bciReference); |
|
230 |
} |
|
231 |
}else{ |
|
232 |
bciReference = getNewBciReference(); |
|
233 |
} |
|
234 |
return bciReference; |
|
235 |
} |
|
236 |
|
|
237 |
private Reference getNewBciReference() { |
|
238 |
Reference bciReference; |
|
239 |
bciReference = ReferenceFactory.newDatabase(); |
|
240 |
bciReference.setTitleCache("Biodiversity Collection Index (BCI))", true); |
|
241 |
return bciReference; |
|
242 |
} |
|
243 |
|
|
244 |
private String normalizeParameter(String parameter) { |
|
245 |
String result = CdmUtils.Nz(parameter).replace(" ", "+"); |
|
246 |
return result; |
|
247 |
} |
|
248 |
|
|
249 |
@Override |
|
250 |
public URL getServiceUrl(String url) { |
|
251 |
URL serviceUrl; |
|
252 |
try { |
|
253 |
serviceUrl = new URL(url); |
|
254 |
} catch (MalformedURLException e) { |
|
255 |
throw new RuntimeException("This should not happen", e); |
|
256 |
} |
|
257 |
return serviceUrl; |
|
258 |
} |
|
259 |
} |
|
1 |
/** |
|
2 |
* Copyright (C) 2009 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.ext.bci; |
|
10 |
|
|
11 |
import java.io.BufferedReader; |
|
12 |
import java.io.IOException; |
|
13 |
import java.io.InputStream; |
|
14 |
import java.io.InputStreamReader; |
|
15 |
import java.net.HttpURLConnection; |
|
16 |
import java.net.MalformedURLException; |
|
17 |
import java.net.URISyntaxException; |
|
18 |
import java.net.URL; |
|
19 |
import java.util.ArrayList; |
|
20 |
import java.util.HashMap; |
|
21 |
import java.util.List; |
|
22 |
import java.util.Map; |
|
23 |
|
|
24 |
import org.apache.commons.lang.StringUtils; |
|
25 |
import org.apache.http.NameValuePair; |
|
26 |
import org.apache.http.message.BasicNameValuePair; |
|
27 |
import org.apache.logging.log4j.LogManager; |
|
28 |
import org.apache.logging.log4j.Logger; |
|
29 |
import org.springframework.stereotype.Component; |
|
30 |
|
|
31 |
import com.ibm.lsid.MalformedLSIDException; |
|
32 |
|
|
33 |
import eu.etaxonomy.cdm.api.application.ICdmRepository; |
|
34 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
35 |
import eu.etaxonomy.cdm.common.URI; |
|
36 |
import eu.etaxonomy.cdm.ext.common.SchemaAdapterBase; |
|
37 |
import eu.etaxonomy.cdm.ext.common.ServiceWrapperBase; |
|
38 |
import eu.etaxonomy.cdm.model.common.IdentifiableEntity; |
|
39 |
import eu.etaxonomy.cdm.model.common.LSID; |
|
40 |
import eu.etaxonomy.cdm.model.occurrence.Collection; |
|
41 |
import eu.etaxonomy.cdm.model.reference.OriginalSourceType; |
|
42 |
import eu.etaxonomy.cdm.model.reference.Reference; |
|
43 |
import eu.etaxonomy.cdm.model.reference.ReferenceFactory; |
|
44 |
|
|
45 |
/** |
|
46 |
* This service allows to query the Biodiversity collection index {@link http://www.biodiversitycollectionsindex.org} |
|
47 |
* @author a.mueller |
|
48 |
* @since Aug 16, 2010 |
|
49 |
*/ |
|
50 |
@Component |
|
51 |
public class BciServiceWrapper |
|
52 |
extends ServiceWrapperBase<Collection> |
|
53 |
implements IBciServiceWrapper{ |
|
54 |
|
|
55 |
private static final Logger logger = LogManager.getLogger(); |
|
56 |
|
|
57 |
private enum ServiceType{ |
|
58 |
AUTHOR, |
|
59 |
NAME, |
|
60 |
PUBLICATION, |
|
61 |
} |
|
62 |
|
|
63 |
// private URL serviceUrl; |
|
64 |
|
|
65 |
// ******************************** CONSTRUCTOR ************************************** |
|
66 |
|
|
67 |
|
|
68 |
// /** |
|
69 |
// * Creates new instance of this factory and connects it to the given |
|
70 |
// * CDM Community Stores access point. |
|
71 |
// * |
|
72 |
// * Typically, there is no need to instantiate this class. |
|
73 |
// */ |
|
74 |
// protected IpniService(URL webserviceUrl){ |
|
75 |
// this.serviceUrl = webserviceUrl; |
|
76 |
// } |
|
77 |
|
|
78 |
// ****************************** METHODS ****************************************************/ |
|
79 |
|
|
80 |
@Override |
|
81 |
public List<Collection> getCollectionsByCode(String code, ICdmRepository appConfig){ |
|
82 |
|
|
83 |
SchemaAdapterBase<Collection> schemaAdapter = schemaAdapterMap.get("recordSchema"); |
|
84 |
if(schemaAdapter == null){ |
|
85 |
throw new RuntimeException("No SchemaAdapter found for " + "recordSchema"); |
|
86 |
} |
|
87 |
|
|
88 |
String SruOperation = "searchRetrieve"; |
|
89 |
|
|
90 |
List<NameValuePair> pairs = new ArrayList<>(); |
|
91 |
pairs.add(new BasicNameValuePair("code", SruOperation)); |
|
92 |
|
|
93 |
Map<String, String> requestHeaders = new HashMap<>(); |
|
94 |
requestHeaders.put("Accept-Charset", "UTF-8"); |
|
95 |
|
|
96 |
try { |
|
97 |
URI requestUri = createUri(null, pairs); |
|
98 |
InputStream stream = executeHttpGet(requestUri, requestHeaders); |
|
99 |
return schemaAdapter.getCmdEntities(stream); |
|
100 |
} catch (IOException e) { |
|
101 |
// thrown by doHttpGet |
|
102 |
logger.error(e); |
|
103 |
} catch (URISyntaxException e) { |
|
104 |
// thrown by createUri |
|
105 |
logger.error(e); |
|
106 |
} |
|
107 |
|
|
108 |
code = normalizeParameter(code); |
|
109 |
String request = code; |
|
110 |
|
|
111 |
@SuppressWarnings("unchecked") |
|
112 |
List<Collection> result = (List<Collection>)queryService(request, appConfig, getServiceUrl(IBciServiceWrapper.LOOKUP_CODE_REST), ServiceType.AUTHOR); |
|
113 |
return result; |
|
114 |
} |
|
115 |
|
|
116 |
private List<? extends IdentifiableEntity> queryService(String request, ICdmRepository appConfig, URL serviceUrl, ServiceType serviceType){ |
|
117 |
try { |
|
118 |
// create the request url |
|
119 |
URL newUrl = new URL(serviceUrl.getProtocol(), |
|
120 |
serviceUrl.getHost(), |
|
121 |
serviceUrl.getPort(), |
|
122 |
serviceUrl.getPath() |
|
123 |
+ "" + request); |
|
124 |
// open a connection |
|
125 |
HttpURLConnection connection = (HttpURLConnection) newUrl.openConnection(); |
|
126 |
// set the accept property to XML so we can use jdom to handle the content |
|
127 |
//connection.setRequestProperty("Accept", "text/xml"); |
|
128 |
|
|
129 |
logger.info("Firing request for URL: " + newUrl); |
|
130 |
|
|
131 |
int responseCode = connection.getResponseCode(); |
|
132 |
|
|
133 |
// get the content at the resource |
|
134 |
InputStream content = (InputStream) connection.getContent(); |
|
135 |
|
|
136 |
// build the result |
|
137 |
List<? extends IdentifiableEntity<?>> result; |
|
138 |
if (serviceType.equals(ServiceType.AUTHOR)){ |
|
139 |
result = buildCollectionList(content, appConfig); |
|
140 |
}else if (serviceType.equals(ServiceType.NAME)){ |
|
141 |
// |
|
142 |
result = null; |
|
143 |
}else{ |
|
144 |
// |
|
145 |
result = null; |
|
146 |
} |
|
147 |
if(responseCode == HttpURLConnection.HTTP_OK){ |
|
148 |
return result; |
|
149 |
}else if(responseCode == HttpURLConnection.HTTP_MULT_CHOICE){ |
|
150 |
return result; |
|
151 |
}else{ |
|
152 |
//TODO error handling |
|
153 |
logger.error("No Http_OK"); |
|
154 |
} |
|
155 |
|
|
156 |
} catch (IOException e) { |
|
157 |
logger.error("No content for request: " + request); |
|
158 |
} |
|
159 |
|
|
160 |
// error |
|
161 |
return null; |
|
162 |
} |
|
163 |
|
|
164 |
|
|
165 |
private List<Collection> buildCollectionList(InputStream content, ICdmRepository appConfig) throws IOException { |
|
166 |
List<Collection> result = new ArrayList<Collection>(); |
|
167 |
BufferedReader reader = new BufferedReader (new InputStreamReader(content)); |
|
168 |
|
|
169 |
String headerLine = reader.readLine(); |
|
170 |
|
|
171 |
String line = reader.readLine(); |
|
172 |
while (StringUtils.isNotBlank(line)){ |
|
173 |
Collection collection = getCollectionFromLine(line, appConfig); |
|
174 |
result.add(collection); |
|
175 |
line = reader.readLine(); |
|
176 |
} |
|
177 |
|
|
178 |
return result; |
|
179 |
} |
|
180 |
|
|
181 |
private Collection getCollectionFromLine(String line, ICdmRepository appConfig) { |
|
182 |
//urn:lsid:biocol.org:col:15727 http://biocol.org/urn:lsid:biocol.org:col:15727 University of Bergen Herbarium |
|
183 |
String[] splits = line.split("\t"); |
|
184 |
if (splits.length != 3){ |
|
185 |
logger.warn("Unknwon BCI line format: " + line); |
|
186 |
return null; |
|
187 |
} |
|
188 |
String lsidString = splits[0]; |
|
189 |
String urlString = splits[1]; |
|
190 |
String collectionName = splits[2]; |
|
191 |
|
|
192 |
Collection result = Collection.NewInstance(); |
|
193 |
|
|
194 |
//LSID |
|
195 |
LSID lsid = null; |
|
196 |
try { |
|
197 |
lsid = new LSID(lsidString); |
|
198 |
} catch (MalformedLSIDException e) { |
|
199 |
logger.warn("Malformed LSID " + lsidString, e); |
|
200 |
} |
|
201 |
|
|
202 |
result.setLsid(lsid); |
|
203 |
String id = getCollectionId(lsid); |
|
204 |
|
|
205 |
result.setName(collectionName); |
|
206 |
|
|
207 |
//id, citation |
|
208 |
Reference citation = getBciCitation(appConfig); |
|
209 |
result.addSource(OriginalSourceType.Lineage, id, null, citation, null); |
|
210 |
|
|
211 |
|
|
212 |
return result; |
|
213 |
} |
|
214 |
|
|
215 |
|
|
216 |
private String getCollectionId(LSID lsid) { |
|
217 |
String result = lsid == null? null : lsid.getObject(); |
|
218 |
return result; |
|
219 |
} |
|
220 |
|
|
221 |
|
|
222 |
private Reference getBciCitation(ICdmRepository appConfig) { |
|
223 |
Reference bciReference; |
|
224 |
if (appConfig != null){ |
|
225 |
bciReference = appConfig.getReferenceService().find(uuidBci); |
|
226 |
if (bciReference == null){ |
|
227 |
bciReference = getNewBciReference(); |
|
228 |
bciReference.setUuid(uuidBci); |
|
229 |
appConfig.getReferenceService().save(bciReference); |
|
230 |
} |
|
231 |
}else{ |
|
232 |
bciReference = getNewBciReference(); |
|
233 |
} |
|
234 |
return bciReference; |
|
235 |
} |
|
236 |
|
|
237 |
private Reference getNewBciReference() { |
|
238 |
Reference bciReference; |
|
239 |
bciReference = ReferenceFactory.newDatabase(); |
|
240 |
bciReference.setTitleCache("Biodiversity Collection Index (BCI))", true); |
|
241 |
return bciReference; |
|
242 |
} |
|
243 |
|
|
244 |
private String normalizeParameter(String parameter) { |
|
245 |
String result = CdmUtils.Nz(parameter).replace(" ", "+"); |
|
246 |
return result; |
|
247 |
} |
|
248 |
|
|
249 |
@Override |
|
250 |
public URL getServiceUrl(String url) { |
|
251 |
URL serviceUrl; |
|
252 |
try { |
|
253 |
serviceUrl = new URL(url); |
|
254 |
} catch (MalformedURLException e) { |
|
255 |
throw new RuntimeException("This should not happen", e); |
|
256 |
} |
|
257 |
return serviceUrl; |
|
258 |
} |
|
259 |
} |
Also available in: Unified diff
cleanup