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