source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java @ 65:c0dd5314bada

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

deal with special characters in urls.

File size: 3.1 KB
Line 
1/**
2 * ReST API for accessing groups in the Annotation store.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
5
6import java.io.UnsupportedEncodingException;
7import java.net.URLDecoder;
8import java.util.List;
9
10import org.json.JSONArray;
11import org.json.JSONException;
12import org.json.JSONObject;
13import org.neo4j.graphdb.Node;
14import org.restlet.data.Status;
15import org.restlet.ext.json.JsonRepresentation;
16import org.restlet.representation.Representation;
17import org.restlet.resource.Get;
18
19import de.mpiwg.itgroup.annotations.Resource;
20import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
21
22/**
23 * API for accessing resource objects in the Annotation store.
24 *
25 * @author dwinter
26 *
27 */
28public class AnnotatorResources extends AnnotatorResourceImpl {
29    protected String getAllowedMethodsForHeader() {
30        return "OPTIONS,GET";
31    }
32
33    /**
34     * GET with JSON content-type.
35     * Parameters: user: short user name uri: user uri
36     *
37     * @param entity
38     * @return
39     */
40    @Get("json")
41    public Representation doGetJSON(Representation entity) {
42        logger.debug("AnnotatorResources doGetJSON!");
43        setCorsHeaders();
44
45        String jsonId = (String) getRequest().getAttributes().get("id");
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        }
54        String uri = decodeJsonId(jsonId);
55
56        logger.debug("resources-id=" + uri);
57
58        if (uri == null) {
59            return getAllResources();
60        } else {
61            return getResource(uri);
62        }
63    }
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);
77    }
78
79    protected Representation getAllResources() {
80        JSONArray results = new JSONArray();
81        AnnotationStore store = getAnnotationStore();
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) {
90            }
91            results.put(jo);
92        }
93
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);
106    }
107
108}
Note: See TracBrowser for help on using the repository browser.