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