44
|
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 /**
|
61
|
21 * API for accessing resource objects in the Annotation store.
|
44
|
22 *
|
|
23 * @author dwinter
|
61
|
24 *
|
44
|
25 */
|
|
26 public class AnnotatorResources extends AnnotatorResourceImpl {
|
|
27 protected String getAllowedMethodsForHeader() {
|
|
28 return "OPTIONS,GET";
|
|
29 }
|
|
30
|
|
31 /**
|
61
|
32 * GET with JSON content-type.
|
|
33 * Parameters: user: short user name uri: user uri
|
|
34 *
|
44
|
35 * @param entity
|
|
36 * @return
|
|
37 */
|
|
38 @Get("json")
|
|
39 public Representation doGetJSON(Representation entity) {
|
61
|
40 logger.debug("AnnotatorResources doGetJSON!");
|
44
|
41 setCorsHeaders();
|
61
|
42
|
44
|
43 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
44 String uri = decodeJsonId(jsonId);
|
61
|
45
|
44
|
46 logger.debug("resources-id=" + uri);
|
|
47
|
61
|
48 if (uri == null) {
|
|
49 return getAllResources();
|
44
|
50 } else {
|
61
|
51 return getResource(uri);
|
44
|
52 }
|
|
53 }
|
61
|
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);
|
44
|
67 }
|
61
|
68
|
|
69 protected Representation getAllResources() {
|
|
70 JSONArray results = new JSONArray();
|
44
|
71 AnnotationStore store = getAnnotationStore();
|
61
|
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) {
|
44
|
80 }
|
61
|
81 results.put(jo);
|
|
82 }
|
|
83
|
44
|
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);
|
61
|
96 }
|
44
|
97
|
|
98 }
|