1
|
package edu.ucsb.nceas.metacat.annotation;
|
2
|
|
3
|
import java.io.InputStream;
|
4
|
import java.io.InputStreamReader;
|
5
|
import java.io.StringReader;
|
6
|
import java.net.URL;
|
7
|
import java.net.URLEncoder;
|
8
|
import java.util.ArrayList;
|
9
|
import java.util.List;
|
10
|
|
11
|
|
12
|
import org.apache.commons.httpclient.HttpClient;
|
13
|
import org.apache.commons.httpclient.HttpMethod;
|
14
|
import org.apache.commons.httpclient.methods.GetMethod;
|
15
|
import org.apache.commons.io.IOUtils;
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
|
18
|
import org.w3c.dom.Node;
|
19
|
import org.w3c.dom.NodeList;
|
20
|
|
21
|
import edu.ucsb.nceas.metacat.dataone.MNodeService;
|
22
|
import edu.ucsb.nceas.utilities.XMLUtilities;
|
23
|
|
24
|
public class OrcidService {
|
25
|
|
26
|
private static Logger logMetacat = Logger.getLogger(OrcidService.class);
|
27
|
|
28
|
//private static final String REST_URL = "http://pub.sandbox.orcid.org/v1.1/search/orcid-bio";
|
29
|
private static final String REST_URL = "http://pub.orcid.org/v1.1/search/orcid-bio";
|
30
|
|
31
|
|
32
|
/**
|
33
|
* Look up possible ORCID from orcid service.
|
34
|
* @see "http://support.orcid.org/knowledgebase/articles/132354-searching-with-the-public-api"
|
35
|
* @param surName
|
36
|
* @param givenNames
|
37
|
* @param otherNames
|
38
|
* @return
|
39
|
*/
|
40
|
public static String lookupOrcid(String text, String surName, List<String> givenNames, List<String> otherNames) {
|
41
|
|
42
|
String url = null;
|
43
|
|
44
|
try {
|
45
|
|
46
|
String urlParameters = "";
|
47
|
|
48
|
if (text != null) {
|
49
|
urlParameters += "\"" + text + "\"";
|
50
|
} else {
|
51
|
if (surName != null) {
|
52
|
// surName = surName.replaceAll(" ", "%20");
|
53
|
urlParameters += "+family-name:\"" + surName + "\"";
|
54
|
}
|
55
|
if (otherNames != null) {
|
56
|
for (String otherName: otherNames) {
|
57
|
// otherName = otherName.replaceAll(" ", "%20");
|
58
|
urlParameters += "+other-names:\"" + otherName + "\"";
|
59
|
}
|
60
|
}
|
61
|
if (givenNames != null) {
|
62
|
for (String givenName: givenNames) {
|
63
|
// givenName = givenName.replaceAll(" ", "%20");
|
64
|
urlParameters += "+given-names:\"" + givenName + "\"";
|
65
|
}
|
66
|
}
|
67
|
}
|
68
|
|
69
|
urlParameters = URLEncoder.encode(urlParameters, "UTF-8");
|
70
|
|
71
|
url = REST_URL + "?q=" + urlParameters + "&rows=1";
|
72
|
URL restURL = new URL(url);
|
73
|
HttpClient client = new HttpClient();
|
74
|
HttpMethod method = new GetMethod(url);
|
75
|
method.addRequestHeader("Accept", "application/orcid+xml");
|
76
|
client.executeMethod(method);
|
77
|
InputStream is = method.getResponseBodyAsStream();
|
78
|
//InputStream is = restURL.openStream();
|
79
|
|
80
|
String results = IOUtils.toString(is, "UTF-8");
|
81
|
logMetacat.debug("RESULTS: " + results);
|
82
|
Node doc = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new StringReader(results));
|
83
|
Node orcidUriNodeList = XMLUtilities.getNodeWithXPath(doc, "//*[local-name()=\"orcid-identifier\"]/*[local-name()=\"uri\"]");
|
84
|
if (orcidUriNodeList != null) {
|
85
|
String orcidUri = orcidUriNodeList.getFirstChild().getNodeValue();
|
86
|
logMetacat.info("Found ORCID URI: " + orcidUri);
|
87
|
return orcidUri;
|
88
|
}
|
89
|
} catch (Exception e) {
|
90
|
logMetacat.error("Could not lookup ORCID using: " + url, e);
|
91
|
}
|
92
|
|
93
|
return null;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Look up indexed creators
|
98
|
*/
|
99
|
public static List<String> lookupCreators(boolean includeObsolete) {
|
100
|
// Search for the creators if we can find them
|
101
|
List<String> retList = null;
|
102
|
try {
|
103
|
String query = "q=-obsoletedBy:[* TO *]&rows=0&facet=true&facet.limit=-1&facet.field=origin";
|
104
|
if (includeObsolete) {
|
105
|
query = "q=*:*&rows=0&facet=true&facet.limit=-1&facet.field=origin";
|
106
|
}
|
107
|
|
108
|
MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
|
109
|
InputStream results = MNodeService.getInstance(request ).query(null, "solr", query);
|
110
|
Node rootNode = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new InputStreamReader(results, "UTF-8"));
|
111
|
//String resultString = XMLUtilities.getDOMTreeAsString(rootNode);
|
112
|
NodeList nodeList = XMLUtilities.getNodeListWithXPath(rootNode, "//lst[@name=\"origin\"]/int/@name");
|
113
|
if (nodeList != null && nodeList.getLength() > 0) {
|
114
|
retList = new ArrayList<String>();
|
115
|
for (int i = 0; i < nodeList.getLength(); i++) {
|
116
|
String found = nodeList.item(i).getFirstChild().getNodeValue();
|
117
|
retList.add(found);
|
118
|
}
|
119
|
}
|
120
|
} catch (Exception e) {
|
121
|
logMetacat.error("Error checking for creators[s]: " + e.getMessage(), e);
|
122
|
}
|
123
|
|
124
|
return retList;
|
125
|
}
|
126
|
}
|