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
|
|
21 /**
|
|
22 * API for accessing tags in the Annotation store.
|
|
23 *
|
|
24 * @author dwinter
|
|
25 *
|
|
26 */
|
|
27 public class AnnotatorResources extends AnnotatorResourceImpl {
|
|
28 protected String getAllowedMethodsForHeader() {
|
|
29 return "OPTIONS,GET";
|
|
30 }
|
|
31
|
|
32 /**
|
|
33 * GET with JSON content-type.
|
|
34 * Parameters:
|
|
35 * user: short user name
|
|
36 * uri: user uri
|
|
37 *
|
|
38 * @param entity
|
|
39 * @return
|
|
40 */
|
|
41 @Get("json")
|
|
42 public Representation doGetJSON(Representation entity) {
|
|
43 logger.debug("AnnotatorGroups doGetJSON!");
|
|
44 setCorsHeaders();
|
|
45
|
|
46 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
47 String uri = decodeJsonId(jsonId);
|
|
48
|
|
49 logger.debug("resources-id=" + uri);
|
|
50
|
|
51 if (uri==null){
|
|
52 return getAllResources();
|
|
53 } else {
|
|
54
|
|
55 return getResource(uri);
|
|
56 }
|
|
57 }
|
|
58
|
|
59 protected Representation getResource(String uri){
|
|
60 AnnotationStore store = getAnnotationStore();
|
|
61 //String tagUri=NS.MPIWG_TAGS_URL+id;
|
|
62 Node tagNode = store.getResourceNodeByUri(uri);
|
|
63 Resource resource = store.createResourceFromNode(tagNode);
|
|
64 JSONObject jo = new JSONObject();
|
|
65 try {
|
|
66 jo.put("id", encodeJsonId(resource.getUri()));
|
|
67 jo.put("uri", resource.getUri());
|
|
68 } catch (JSONException e) {
|
|
69 }
|
|
70
|
|
71 return new JsonRepresentation(jo);
|
|
72 }
|
|
73 protected Representation getAllResources() {
|
|
74 JSONArray results = new JSONArray();
|
|
75 AnnotationStore store = getAnnotationStore();
|
|
76
|
|
77
|
|
78 List<Resource> resources = store.getResources(null, null);
|
|
79 for (Resource resource : resources) {
|
|
80 JSONObject jo = new JSONObject();
|
|
81 try {
|
|
82 jo.put("id", encodeJsonId(resource.getUri()));
|
|
83 jo.put("uri", resource.getUri());
|
|
84 } catch (JSONException e) {
|
|
85 }
|
|
86 results.put(jo);
|
|
87 }
|
|
88
|
|
89 // assemble result object
|
|
90 JSONObject result = new JSONObject();
|
|
91 try {
|
|
92 result.put("rows", results);
|
|
93 result.put("total", results.length());
|
|
94 } catch (JSONException e) {
|
|
95 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
|
|
96 return null;
|
|
97 }
|
|
98 logger.debug("sending:");
|
|
99 logger.debug(result);
|
|
100 return new JsonRepresentation(result);
|
|
101 }
|
|
102
|
|
103
|
|
104
|
|
105 }
|