1 | /** |
---|
2 | * ReST API for accessing groups in the Annotation store. |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet; |
---|
5 | |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import org.json.JSONArray; |
---|
9 | import org.json.JSONException; |
---|
10 | import org.json.JSONObject; |
---|
11 | import org.neo4j.graphdb.Node; |
---|
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.Resource; |
---|
18 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
19 | |
---|
20 | /** |
---|
21 | * API for accessing resource objects in the Annotation store. |
---|
22 | * |
---|
23 | * @author dwinter |
---|
24 | * |
---|
25 | */ |
---|
26 | public class AnnotatorResources extends AnnotatorResourceImpl { |
---|
27 | protected String getAllowedMethodsForHeader() { |
---|
28 | return "OPTIONS,GET"; |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * GET with JSON content-type. |
---|
33 | * Parameters: user: short user name uri: user uri |
---|
34 | * |
---|
35 | * @param entity |
---|
36 | * @return |
---|
37 | */ |
---|
38 | @Get("json") |
---|
39 | public Representation doGetJSON(Representation entity) { |
---|
40 | logger.debug("AnnotatorResources doGetJSON!"); |
---|
41 | setCorsHeaders(); |
---|
42 | |
---|
43 | String jsonId = (String) getRequest().getAttributes().get("id"); |
---|
44 | String uri = decodeJsonId(jsonId); |
---|
45 | |
---|
46 | logger.debug("resources-id=" + uri); |
---|
47 | |
---|
48 | if (uri == null) { |
---|
49 | return getAllResources(); |
---|
50 | } else { |
---|
51 | return getResource(uri); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | protected Representation getResource(String uri) { |
---|
56 | AnnotationStore store = getAnnotationStore(); |
---|
57 | Node resNode = store.getResourceNodeByUri(uri); |
---|
58 | Resource resource = store.createResourceFromNode(resNode); |
---|
59 | JSONObject jo = new JSONObject(); |
---|
60 | try { |
---|
61 | jo.put("id", encodeJsonId(resource.getUri())); |
---|
62 | jo.put("uri", resource.getUri()); |
---|
63 | } catch (JSONException e) { |
---|
64 | } |
---|
65 | |
---|
66 | return new JsonRepresentation(jo); |
---|
67 | } |
---|
68 | |
---|
69 | protected Representation getAllResources() { |
---|
70 | JSONArray results = new JSONArray(); |
---|
71 | AnnotationStore store = getAnnotationStore(); |
---|
72 | |
---|
73 | List<Resource> resources = store.getResources(null, null); |
---|
74 | for (Resource resource : resources) { |
---|
75 | JSONObject jo = new JSONObject(); |
---|
76 | try { |
---|
77 | jo.put("id", encodeJsonId(resource.getUri())); |
---|
78 | jo.put("uri", resource.getUri()); |
---|
79 | } catch (JSONException e) { |
---|
80 | } |
---|
81 | results.put(jo); |
---|
82 | } |
---|
83 | |
---|
84 | // assemble result object |
---|
85 | JSONObject result = new JSONObject(); |
---|
86 | try { |
---|
87 | result.put("rows", results); |
---|
88 | result.put("total", results.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 | } |
---|