28
|
1 package de.mpiwg.itgroup.annotations.restlet;
|
|
2
|
65
|
3 import java.io.UnsupportedEncodingException;
|
|
4 import java.net.URLDecoder;
|
28
|
5 import java.util.ArrayList;
|
|
6 import java.util.List;
|
|
7
|
|
8 import org.json.JSONArray;
|
|
9 import org.json.JSONException;
|
|
10 import org.json.JSONObject;
|
|
11 import org.restlet.data.Form;
|
|
12 import org.restlet.data.Status;
|
|
13 import org.restlet.ext.json.JsonRepresentation;
|
|
14 import org.restlet.representation.Representation;
|
|
15 import org.restlet.resource.Get;
|
|
16
|
|
17 import de.mpiwg.itgroup.annotations.Annotation;
|
37
|
18 import de.mpiwg.itgroup.annotations.Person;
|
28
|
19 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
20 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
|
|
21
|
|
22 /**
|
|
23 * API for accessing tags in the Annotation store.
|
|
24 *
|
|
25 * @author dwinter
|
65
|
26 *
|
28
|
27 */
|
|
28 public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl {
|
|
29 protected String getAllowedMethodsForHeader() {
|
|
30 return "OPTIONS,GET";
|
|
31 }
|
65
|
32
|
28
|
33 @Get("json")
|
|
34 public Representation doGetJSON(Representation entity) {
|
|
35 logger.debug("AnnotatorAnnotatonsBytag doGetJSON!");
|
|
36 setCorsHeaders();
|
65
|
37
|
37
|
38 // do authentication
|
|
39 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
|
|
40 logger.debug("request authenticated=" + authUser);
|
|
41
|
65
|
42 String id = (String) getRequest().getAttributes().get("id");
|
|
43 // URL decode
|
|
44 try {
|
|
45 id = URLDecoder.decode(id, "UTF-8");
|
|
46 } catch (UnsupportedEncodingException e) {
|
|
47 // this shouldn't happen
|
|
48 }
|
|
49 logger.debug("annotation-id=" + id);
|
|
50
|
28
|
51 Form form = getRequest().getResourceRef().getQueryAsForm();
|
65
|
52 String sortBy = form.getFirstValue("sortBy");
|
|
53
|
28
|
54 AnnotationStore store = getAnnotationStore();
|
58
|
55 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
|
28
|
56 List<Annotation> annotations = store.getAnnotationsByTag(tagUri);
|
65
|
57
|
|
58 // JSONArray results = new JSONArray();
|
28
|
59 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
|
65
|
60
|
|
61 for (Annotation annot : annotations) {
|
|
62 // check permission
|
|
63 if (!annot.isActionAllowed("read", authUser, store))
|
|
64 continue;
|
|
65
|
|
66 JSONObject jo = createAnnotatorJson(annot, false);
|
28
|
67 results.add(jo);
|
|
68 }
|
65
|
69
|
|
70 if (sortBy != null) {
|
|
71 JSONObjectComparator.sortAnnotations(results, sortBy);
|
|
72 }
|
|
73
|
|
74 JSONArray resultsJa = new JSONArray();
|
|
75 for (JSONObject result : results) {
|
|
76 resultsJa.put(result);
|
|
77 }
|
|
78
|
|
79 // assemble result object
|
|
80 JSONObject result = new JSONObject();
|
|
81 try {
|
|
82 result.put("rows", resultsJa);
|
|
83 result.put("total", resultsJa.length());
|
|
84 } catch (JSONException e) {
|
|
85 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
|
|
86 return null;
|
|
87 }
|
|
88 return new JsonRepresentation(result);
|
28
|
89 }
|
|
90
|
|
91 } |