Project

General

Profile

1
package edu.ucsb.nceas.metacat.annotation;
2

    
3
import java.io.InputStream;
4
import java.io.StringReader;
5
import java.net.URL;
6
import java.net.URLEncoder;
7

    
8
import org.apache.commons.io.IOUtils;
9
import org.apache.log4j.Logger;
10
import org.w3c.dom.Node;
11

    
12
import edu.ucsb.nceas.utilities.XMLUtilities;
13

    
14
public class OrcidService {
15
	
16
	private static Logger logMetacat = Logger.getLogger(OrcidService.class);
17
	
18
    private static final String REST_URL = "http://pub.sandbox.orcid.org/v1.1/search/orcid-bio";
19

    
20
    /**
21
	 * Look up possible ORCID from orcid service.
22
	 * @see "http://support.orcid.org/knowledgebase/articles/132354-searching-with-the-public-api"
23
	 * @param surName
24
	 * @param givenNames
25
	 * @param otherNames
26
	 * @return
27
	 */
28
	public static String lookupOrcid(String surName, String[] givenNames, String[] otherNames) {
29
		
30
		try {
31
			
32
			String urlParameters = "";
33
			if (surName != null) {
34
				urlParameters += "+family-name:\"" + surName + "\"";
35
			}
36
			if (otherNames != null) {
37
				for (String otherName: otherNames) {
38
					urlParameters += "+other-names:\"" + otherName + "\""; 
39
				}
40
			}
41
			if (givenNames != null) {
42
				for (String givenName: givenNames) {
43
					urlParameters += "+given-names:\"" + givenName + "\""; 
44
				}
45
			}
46
			
47
			urlParameters = URLEncoder.encode(urlParameters, "UTF-8");
48
			
49
			String url = REST_URL + "?q=" + urlParameters + "&rows=1";
50
			URL restURL = new URL(url);
51
			InputStream is = restURL.openStream();
52
			String results = IOUtils.toString(is);
53
			logMetacat.debug("RESULTS: " + results);
54
			Node doc = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new StringReader(results));
55
			Node orcidUriNodeList = XMLUtilities.getNodeWithXPath(doc, "//*[local-name()=\"uri\"]");
56
			if (orcidUriNodeList != null) {
57
				String orcidUri = orcidUriNodeList.getFirstChild().getNodeValue();
58
				logMetacat.info("Found ORCID URI: " + orcidUri);
59
				return orcidUri;
60
			}
61
		} catch (Exception e) {
62
			logMetacat.error("Could not lookup ORCID for surName=" + surName, e);
63
		}
64
		
65
		return null;
66
	}
67
}
(3-3/3)