44
|
1 /**
|
|
2 * ReST API for accessing groups in the Annotation store.
|
|
3 */
|
|
4 package de.mpiwg.itgroup.annotations.restlet;
|
|
5
|
65
|
6 import java.io.UnsupportedEncodingException;
|
|
7 import java.net.URLDecoder;
|
44
|
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 /**
|
61
|
23 * API for accessing resource objects in the Annotation store.
|
44
|
24 *
|
|
25 * @author dwinter
|
61
|
26 *
|
44
|
27 */
|
|
28 public class AnnotatorResources extends AnnotatorResourceImpl {
|
|
29 protected String getAllowedMethodsForHeader() {
|
|
30 return "OPTIONS,GET";
|
|
31 }
|
|
32
|
|
33 /**
|
61
|
34 * GET with JSON content-type.
|
|
35 * Parameters: user: short user name uri: user uri
|
|
36 *
|
44
|
37 * @param entity
|
|
38 * @return
|
|
39 */
|
|
40 @Get("json")
|
|
41 public Representation doGetJSON(Representation entity) {
|
61
|
42 logger.debug("AnnotatorResources doGetJSON!");
|
44
|
43 setCorsHeaders();
|
61
|
44
|
44
|
45 String jsonId = (String) getRequest().getAttributes().get("id");
|
65
|
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 }
|
44
|
54 String uri = decodeJsonId(jsonId);
|
61
|
55
|
44
|
56 logger.debug("resources-id=" + uri);
|
|
57
|
61
|
58 if (uri == null) {
|
|
59 return getAllResources();
|
44
|
60 } else {
|
61
|
61 return getResource(uri);
|
44
|
62 }
|
|
63 }
|
61
|
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);
|
44
|
77 }
|
61
|
78
|
|
79 protected Representation getAllResources() {
|
|
80 JSONArray results = new JSONArray();
|
44
|
81 AnnotationStore store = getAnnotationStore();
|
61
|
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) {
|
44
|
90 }
|
61
|
91 results.put(jo);
|
|
92 }
|
|
93
|
44
|
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);
|
61
|
106 }
|
44
|
107
|
|
108 }
|