source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java @ 61:b8ef15c8c4a5

Last change on this file since 61:b8ef15c8c4a5 was 61:b8ef15c8c4a5, checked in by casties, 11 years ago

implemented new shape format for image annotations.
minor cleanups.

File size: 2.8 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.Status;
13import org.restlet.ext.json.JsonRepresentation;
14import org.restlet.representation.Representation;
15import org.restlet.resource.Get;
16
17import de.mpiwg.itgroup.annotations.Resource;
18import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
19
20/**
21 * API for accessing resource objects in the Annotation store.
22 *
23 * @author dwinter
24 *
25 */
26public class AnnotatorResources extends AnnotatorResourceImpl {
27    protected String getAllowedMethodsForHeader() {
28        return "OPTIONS,GET";
29    }
30
31    /**
32     * GET with JSON content-type.
33     * Parameters: user: short user name uri: user uri
34     *
35     * @param entity
36     * @return
37     */
38    @Get("json")
39    public Representation doGetJSON(Representation entity) {
40        logger.debug("AnnotatorResources doGetJSON!");
41        setCorsHeaders();
42
43        String jsonId = (String) getRequest().getAttributes().get("id");
44        String uri = decodeJsonId(jsonId);
45
46        logger.debug("resources-id=" + uri);
47
48        if (uri == null) {
49            return getAllResources();
50        } else {
51            return getResource(uri);
52        }
53    }
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);
67    }
68
69    protected Representation getAllResources() {
70        JSONArray results = new JSONArray();
71        AnnotationStore store = getAnnotationStore();
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) {
80            }
81            results.put(jo);
82        }
83
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);
96    }
97
98}
Note: See TracBrowser for help on using the repository browser.