source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java @ 91:cf44d9e1a4a7

Last change on this file since 91:cf44d9e1a4a7 was 91:cf44d9e1a4a7, checked in by casties, 9 years ago

let CORS be handled by Restlet 2.3 CorsFilter?.

File size: 3.8 KB
Line 
1/**
2 * ReST API for accessing groups in the Annotation store.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
5
6/*
7 * #%L
8 * AnnotationManager
9 * %%
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
11 * %%
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Lesser Public License for more details.
21 *
22 * You should have received a copy of the GNU General Lesser Public
23 * License along with this program.  If not, see
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
25 * #L%
26 */
27
28import java.io.UnsupportedEncodingException;
29import java.net.URLDecoder;
30import java.util.List;
31
32import org.json.JSONArray;
33import org.json.JSONException;
34import org.json.JSONObject;
35import org.neo4j.graphdb.Node;
36import org.restlet.data.Status;
37import org.restlet.ext.json.JsonRepresentation;
38import org.restlet.representation.Representation;
39import org.restlet.resource.Get;
40
41import de.mpiwg.itgroup.annotations.Resource;
42import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
43
44/**
45 * API for accessing resource objects in the Annotation store.
46 *
47 * @author dwinter
48 *
49 */
50public class AnnotatorResources extends AnnotatorResourceImpl {
51
52    /**
53     * GET with JSON content-type.
54     * Parameters: user: short user name uri: user uri
55     *
56     * @param entity
57     * @return
58     */
59    @Get("json")
60    public Representation doGetJSON(Representation entity) {
61        logger.fine("AnnotatorResources doGetJSON!");
62
63        String jsonId = (String) getRequest().getAttributes().get("id");
64        if (jsonId != null) {
65            // URL decode
66            try {
67                jsonId = URLDecoder.decode(jsonId, "UTF-8");
68            } catch (UnsupportedEncodingException e) {
69                // this shouldn't happen
70            }
71        }
72        String uri = decodeJsonId(jsonId);
73
74        logger.fine("resources-id=" + uri);
75
76        if (uri == null) {
77            return getAllResources();
78        } else {
79            return getResource(uri);
80        }
81    }
82
83    protected Representation getResource(String uri) {
84        AnnotationStore store = getAnnotationStore();
85        Node resNode = store.getResourceNodeByUri(uri);
86        Resource resource = store.createResourceFromNode(resNode);
87        JSONObject jo = new JSONObject();
88        try {
89            jo.put("id", encodeJsonId(resource.getUri()));
90            jo.put("uri", resource.getUri());
91        } catch (JSONException e) {
92        }
93
94        return new JsonRepresentation(jo);
95    }
96
97    protected Representation getAllResources() {
98        JSONArray results = new JSONArray();
99        AnnotationStore store = getAnnotationStore();
100
101        List<Resource> resources = store.getResources(null, null);
102        for (Resource resource : resources) {
103            JSONObject jo = new JSONObject();
104            try {
105                jo.put("id", encodeJsonId(resource.getUri()));
106                jo.put("uri", resource.getUri());
107            } catch (JSONException e) {
108            }
109            results.put(jo);
110        }
111
112        // assemble result object
113        JSONObject result = new JSONObject();
114        try {
115            result.put("rows", results);
116            result.put("total", results.length());
117        } catch (JSONException e) {
118            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
119            return null;
120        }
121        logger.fine("sending:");
122        logger.fine(result.toString());
123        return new JsonRepresentation(result);
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.