source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java @ 70:2b1e6df5e21a

Last change on this file since 70:2b1e6df5e21a was 70:2b1e6df5e21a, checked in by casties, 10 years ago

added lgpl_v3 license information.

File size: 3.9 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    protected String getAllowedMethodsForHeader() {
52        return "OPTIONS,GET";
53    }
54
55    /**
56     * GET with JSON content-type.
57     * Parameters: user: short user name uri: user uri
58     *
59     * @param entity
60     * @return
61     */
62    @Get("json")
63    public Representation doGetJSON(Representation entity) {
64        logger.debug("AnnotatorResources doGetJSON!");
65        setCorsHeaders();
66
67        String jsonId = (String) getRequest().getAttributes().get("id");
68        if (jsonId != null) {
69            // URL decode
70            try {
71                jsonId = URLDecoder.decode(jsonId, "UTF-8");
72            } catch (UnsupportedEncodingException e) {
73                // this shouldn't happen
74            }
75        }
76        String uri = decodeJsonId(jsonId);
77
78        logger.debug("resources-id=" + uri);
79
80        if (uri == null) {
81            return getAllResources();
82        } else {
83            return getResource(uri);
84        }
85    }
86
87    protected Representation getResource(String uri) {
88        AnnotationStore store = getAnnotationStore();
89        Node resNode = store.getResourceNodeByUri(uri);
90        Resource resource = store.createResourceFromNode(resNode);
91        JSONObject jo = new JSONObject();
92        try {
93            jo.put("id", encodeJsonId(resource.getUri()));
94            jo.put("uri", resource.getUri());
95        } catch (JSONException e) {
96        }
97
98        return new JsonRepresentation(jo);
99    }
100
101    protected Representation getAllResources() {
102        JSONArray results = new JSONArray();
103        AnnotationStore store = getAnnotationStore();
104
105        List<Resource> resources = store.getResources(null, null);
106        for (Resource resource : resources) {
107            JSONObject jo = new JSONObject();
108            try {
109                jo.put("id", encodeJsonId(resource.getUri()));
110                jo.put("uri", resource.getUri());
111            } catch (JSONException e) {
112            }
113            results.put(jo);
114        }
115
116        // assemble result object
117        JSONObject result = new JSONObject();
118        try {
119            result.put("rows", results);
120            result.put("total", results.length());
121        } catch (JSONException e) {
122            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
123            return null;
124        }
125        logger.debug("sending:");
126        logger.debug(result);
127        return new JsonRepresentation(result);
128    }
129
130}
Note: See TracBrowser for help on using the repository browser.