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
|
|
36
|
try {
|
37
|
|
38
|
annotatorUrl = PropertyService.getProperty("annotator.store.url");
|
39
|
|
40
|
// skip if not configured to query the annotator-store
|
41
|
if (annotatorUrl == null || annotatorUrl.length() == 0) {
|
42
|
return null;
|
43
|
}
|
44
|
|
45
|
//String urlParameters = "pid=" + URLEncoder.encode(pid, "UTF-8");
|
46
|
String urlParameters = "user=leinfelder";
|
47
|
|
48
|
String url = annotatorUrl + "?" + urlParameters;
|
49
|
HttpClient client = new HttpClient();
|
50
|
HttpMethod method = new GetMethod(url);
|
51
|
method.addRequestHeader("Accept", "application/json");
|
52
|
client.executeMethod(method);
|
53
|
InputStream is = method.getResponseBodyAsStream();
|
54
|
|
55
|
String results = IOUtils.toString(is, "UTF-8");
|
56
|
logMetacat.debug("RESULTS: " + results);
|
57
|
JSONObject jo = (JSONObject) JSONValue.parse(results);
|
58
|
|
59
|
JSONArray rows = (JSONArray) jo.get("rows");
|
60
|
int count = rows.size();
|
61
|
Map<String, List<Object>> annotations = new HashMap<String, List<Object>>();
|
62
|
List<Object> values = null;
|
63
|
|
64
|
// default the tags to a catch-all dynamic field as we develop
|
65
|
String solrKey = "annotation_sm";
|
66
|
|
67
|
for (int i = 0; i < count; i++){
|
68
|
JSONObject row = (JSONObject) rows.get(i);
|
69
|
|
70
|
// skip this row if it is not about this pid
|
71
|
// Bug in annotator-store prevents effective search by pid
|
72
|
String pidValue = row.get("pid").toString();
|
73
|
if (!pidValue.equals(pid)) {
|
74
|
continue;
|
75
|
}
|
76
|
|
77
|
// if the annotation told us the target index field, then use it
|
78
|
Object field = row.get("field");
|
79
|
if (field != null) {
|
80
|
solrKey = field.toString();
|
81
|
}
|
82
|
|
83
|
values = annotations.get(solrKey);
|
84
|
if (values == null) {
|
85
|
values = new ArrayList<Object>();
|
86
|
}
|
87
|
String key = "tags";
|
88
|
Object obj = row.get(key);
|
89
|
if (obj instanceof JSONArray) {
|
90
|
JSONArray tags = (JSONArray) row.get(key);
|
91
|
values.addAll(tags);
|
92
|
} else {
|
93
|
String value = row.get(key).toString();
|
94
|
values.add(value);
|
95
|
}
|
96
|
annotations.put(solrKey, values);
|
97
|
|
98
|
}
|
99
|
// just populate this one field for example
|
100
|
return annotations;
|
101
|
|
102
|
} catch (Exception e) {
|
103
|
logMetacat.error("Could not lookup annotation using: " + annotatorUrl, e);
|
104
|
}
|
105
|
|
106
|
return null;
|
107
|
}
|
108
|
|
109
|
|
110
|
}
|