forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 61
/
VIAFAuthority.java
95 lines (78 loc) · 3.03 KB
/
VIAFAuthority.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content.authority;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.dspace.content.Collection;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Implementation to retrieve object from viaf.org "autosuggest" webservice
*
* @see https://viaf.org/
*
* @author Riccardo Fazio (riccardo.fazio at 4science dot it)
*
*/
public class VIAFAuthority implements ChoiceAuthority {
Logger log = Logger.getLogger(VIAFAuthority.class);
String viafurl = "http://viaf.org/viaf/AutoSuggest";
@Override
public Choices getMatches(String field, String text, Collection collection, int start, int limit, String locale) {
List<BasicNameValuePair> args = new ArrayList<BasicNameValuePair>();
args.add(new BasicNameValuePair("query", text));
String sUrl = viafurl + "?" + URLEncodedUtils.format(args, "UTF8");
try {
URL url = new URL(sUrl);
InputStream is = url.openStream();
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
sb.append(inputLine);
}
in.close();
//VIAF responds a json with duplicate keys? must remove them as they are unused
String str= sb.toString().replaceAll("\"bav\":\"adv\\d+\",", "").replaceAll("\"dnb\":\"\\d+\",", "");
JSONObject ob = new JSONObject(str);
JSONArray results = ob.getJSONArray("result");
Choice[] choices = new Choice[results.length()];
for(int i=0;i< results.length();i++){
JSONObject result = results.getJSONObject(i);
String term = result.getString("term");
String label = result.getString("displayForm");
String authority = result.getString("recordID");
choices[i] = new Choice(authority, term, label);
}
return new Choices(choices, 0, choices.length, Choices.CF_ACCEPTED, false);
} catch (MalformedURLException e) {
log.error(e.getMessage(),e);
} catch (IOException e) {
log.error(e.getMessage(),e);
}
return null;
}
@Override
public Choices getBestMatch(String field, String text, Collection collection, String locale) {
return getMatches(field, text, collection, 0, 1, locale);
}
@Override
public String getLabel(String field, String key, String locale) {
return key;
}
}