1 | /** |
---|
2 | * ReST API for accessing groups in the Annotation store. |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet; |
---|
5 | |
---|
6 | import java.io.UnsupportedEncodingException; |
---|
7 | import java.net.URLDecoder; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | import org.json.JSONArray; |
---|
11 | import org.json.JSONException; |
---|
12 | import org.json.JSONObject; |
---|
13 | import org.neo4j.graphdb.Node; |
---|
14 | import org.restlet.data.Status; |
---|
15 | import org.restlet.ext.json.JsonRepresentation; |
---|
16 | import org.restlet.representation.Representation; |
---|
17 | import org.restlet.resource.Get; |
---|
18 | |
---|
19 | import de.mpiwg.itgroup.annotations.Resource; |
---|
20 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
21 | |
---|
22 | /** |
---|
23 | * API for accessing resource objects in the Annotation store. |
---|
24 | * |
---|
25 | * @author dwinter |
---|
26 | * |
---|
27 | */ |
---|
28 | public class AnnotatorResources extends AnnotatorResourceImpl { |
---|
29 | protected String getAllowedMethodsForHeader() { |
---|
30 | return "OPTIONS,GET"; |
---|
31 | } |
---|
32 | |
---|
33 | /** |
---|
34 | * GET with JSON content-type. |
---|
35 | * Parameters: user: short user name uri: user uri |
---|
36 | * |
---|
37 | * @param entity |
---|
38 | * @return |
---|
39 | */ |
---|
40 | @Get("json") |
---|
41 | public Representation doGetJSON(Representation entity) { |
---|
42 | logger.debug("AnnotatorResources doGetJSON!"); |
---|
43 | setCorsHeaders(); |
---|
44 | |
---|
45 | String jsonId = (String) getRequest().getAttributes().get("id"); |
---|
46 | if (jsonId != null) { |
---|
47 | // URL decode |
---|
48 | try { |
---|
49 | jsonId = URLDecoder.decode(jsonId, "UTF-8"); |
---|
50 | } catch (UnsupportedEncodingException e) { |
---|
51 | // this shouldn't happen |
---|
52 | } |
---|
53 | } |
---|
54 | String uri = decodeJsonId(jsonId); |
---|
55 | |
---|
56 | logger.debug("resources-id=" + uri); |
---|
57 | |
---|
58 | if (uri == null) { |
---|
59 | return getAllResources(); |
---|
60 | } else { |
---|
61 | return getResource(uri); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | protected Representation getResource(String uri) { |
---|
66 | AnnotationStore store = getAnnotationStore(); |
---|
67 | Node resNode = store.getResourceNodeByUri(uri); |
---|
68 | Resource resource = store.createResourceFromNode(resNode); |
---|
69 | JSONObject jo = new JSONObject(); |
---|
70 | try { |
---|
71 | jo.put("id", encodeJsonId(resource.getUri())); |
---|
72 | jo.put("uri", resource.getUri()); |
---|
73 | } catch (JSONException e) { |
---|
74 | } |
---|
75 | |
---|
76 | return new JsonRepresentation(jo); |
---|
77 | } |
---|
78 | |
---|
79 | protected Representation getAllResources() { |
---|
80 | JSONArray results = new JSONArray(); |
---|
81 | AnnotationStore store = getAnnotationStore(); |
---|
82 | |
---|
83 | List<Resource> resources = store.getResources(null, null); |
---|
84 | for (Resource resource : resources) { |
---|
85 | JSONObject jo = new JSONObject(); |
---|
86 | try { |
---|
87 | jo.put("id", encodeJsonId(resource.getUri())); |
---|
88 | jo.put("uri", resource.getUri()); |
---|
89 | } catch (JSONException e) { |
---|
90 | } |
---|
91 | results.put(jo); |
---|
92 | } |
---|
93 | |
---|
94 | // assemble result object |
---|
95 | JSONObject result = new JSONObject(); |
---|
96 | try { |
---|
97 | result.put("rows", results); |
---|
98 | result.put("total", results.length()); |
---|
99 | } catch (JSONException e) { |
---|
100 | setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
---|
101 | return null; |
---|
102 | } |
---|
103 | logger.debug("sending:"); |
---|
104 | logger.debug(result); |
---|
105 | return new JsonRepresentation(result); |
---|
106 | } |
---|
107 | |
---|
108 | } |
---|