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.metacat.replication.ReplicationService;
13
import edu.ucsb.nceas.utilities.XMLUtilities;
14

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

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