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

Last change on this file since 44:5e9d90461929 was 44:5e9d90461929, checked in by dwinter, 12 years ago

rest interface for resources

File size: 3.2 KB
Line 
1/**
2 * ReST API for accessing groups in the Annotation store.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
5
6import java.util.List;
7
8import org.json.JSONArray;
9import org.json.JSONException;
10import org.json.JSONObject;
11import org.neo4j.graphdb.Node;
12import org.restlet.data.Form;
13import org.restlet.data.Status;
14import org.restlet.ext.json.JsonRepresentation;
15import org.restlet.representation.Representation;
16import org.restlet.resource.Get;
17
18import de.mpiwg.itgroup.annotations.Actor;
19import de.mpiwg.itgroup.annotations.Group;
20import de.mpiwg.itgroup.annotations.NS;
21import de.mpiwg.itgroup.annotations.Resource;
22import de.mpiwg.itgroup.annotations.Tag;
23import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
24import 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 */
33public 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}
Note: See TracBrowser for help on using the repository browser.