comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java @ 44:5e9d90461929

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