1
|
package edu.ucsb.nceas.metacat.annotation;
|
2
|
|
3
|
import java.io.InputStream;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
|
9
|
import net.minidev.json.JSONArray;
|
10
|
import net.minidev.json.JSONObject;
|
11
|
import net.minidev.json.JSONValue;
|
12
|
|
13
|
import org.apache.commons.httpclient.HttpClient;
|
14
|
import org.apache.commons.httpclient.HttpMethod;
|
15
|
import org.apache.commons.httpclient.methods.GetMethod;
|
16
|
import org.apache.commons.io.IOUtils;
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
20
|
|
21
|
public class AnnotatorService {
|
22
|
|
23
|
private static Logger logMetacat = Logger.getLogger(AnnotatorService.class);
|
24
|
|
25
|
/**
|
26
|
* Look up annotations from annotator service
|
27
|
* @see "http://docs.annotatorjs.org/en/latest/storage.html"
|
28
|
* @param pid the identifier to fetch annotations about
|
29
|
* @return
|
30
|
*/
|
31
|
public static Map<String, List<Object>> lookUpAnnotations(String pid) {
|
32
|
|
33
|
|
34
|
String annotatorUrl = null;
|
35
|
String consumerKey = null;
|
36
|
try {
|
37
|
|
38
|
annotatorUrl = PropertyService.getProperty("annotator.store.url");
|
39
|
consumerKey = PropertyService.getProperty("annotator.consumerKey");
|
40
|
|
41
|
// skip if not configured to query the annotator-store
|
42
|
if (annotatorUrl == null || annotatorUrl.length() == 0) {
|
43
|
return null;
|
44
|
}
|
45
|
|
46
|
// TODO: query for matching PID only - wasting time iterating over full list
|
47
|
//String urlParameters = "pid=" + URLEncoder.encode(pid, "UTF-8");
|
48
|
String urlParameters = "consumer=" + consumerKey;
|
49
|
|
50
|
String url = annotatorUrl + "?" + urlParameters;
|
51
|
HttpClient client = new HttpClient();
|
52
|
HttpMethod method = new GetMethod(url);
|
53
|
method.addRequestHeader("Accept", "application/json");
|
54
|
client.executeMethod(method);
|
55
|
InputStream is = method.getResponseBodyAsStream();
|
56
|
|
57
|
String results = IOUtils.toString(is, "UTF-8");
|
58
|
logMetacat.debug("RESULTS: " + results);
|
59
|
JSONObject jo = (JSONObject) JSONValue.parse(results);
|
60
|
|
61
|
JSONArray rows = (JSONArray) jo.get("rows");
|
62
|
int count = rows.size();
|
63
|
Map<String, List<Object>> annotations = new HashMap<String, List<Object>>();
|
64
|
List<Object> values = null;
|
65
|
|
66
|
// default the tags to a catch-all dynamic field as we develop
|
67
|
String solrKey = "annotation_sm";
|
68
|
|
69
|
for (int i = 0; i < count; i++){
|
70
|
JSONObject row = (JSONObject) rows.get(i);
|
71
|
|
72
|
// skip this row if it is not about this pid
|
73
|
// FIXME: Bug in annotator-store prevents effective search by pid
|
74
|
String pidValue = row.get("pid").toString();
|
75
|
if (!pidValue.equals(pid)) {
|
76
|
continue;
|
77
|
}
|
78
|
|
79
|
// if the annotation told us the target index field, then use it
|
80
|
Object field = row.get("field");
|
81
|
if (field != null) {
|
82
|
solrKey = field.toString();
|
83
|
}
|
84
|
|
85
|
values = annotations.get(solrKey);
|
86
|
if (values == null) {
|
87
|
values = new ArrayList<Object>();
|
88
|
}
|
89
|
String key = "tags";
|
90
|
Object obj = row.get(key);
|
91
|
if (obj instanceof JSONArray) {
|
92
|
JSONArray tags = (JSONArray) row.get(key);
|
93
|
values.addAll(tags);
|
94
|
} else {
|
95
|
String value = row.get(key).toString();
|
96
|
values.add(value);
|
97
|
}
|
98
|
annotations.put(solrKey, values);
|
99
|
|
100
|
}
|
101
|
// just populate this one field for example
|
102
|
return annotations;
|
103
|
|
104
|
} catch (Exception e) {
|
105
|
logMetacat.error("Could not lookup annotation using: " + annotatorUrl, e);
|
106
|
}
|
107
|
|
108
|
return null;
|
109
|
}
|
110
|
|
111
|
|
112
|
}
|