source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.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.Tag;
42import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
43
44/**
45 * API for accessing tags in the Annotation store.
46 *
47 * @author dwinter
48 *
49 */
50public class AnnotatorTags extends AnnotatorResourceImpl {
51    protected String getAllowedMethodsForHeader() {
52        return "OPTIONS,GET";
53    }
54
55    /**
56     * GET with JSON content-type.
57     * Parameters:
58     *   user: short user name
59     *   uri: user uri
60     *
61     * @param entity
62     * @return
63     */
64    @Get("json")
65    public Representation doGetJSON(Representation entity) {
66        logger.debug("AnnotatorGroups doGetJSON!");
67        setCorsHeaders();
68
69        String jsonId = (String) getRequest().getAttributes().get("id");
70        // String id = decodeJsonId(jsonId);
71        String id = jsonId;
72        logger.debug("annotation-id=" + id);
73
74        if (id == null) {
75            return getAllTags();
76        } else {
77            // URL decode
78            try {
79                id = URLDecoder.decode(id, "UTF-8");
80            } catch (UnsupportedEncodingException e) {
81                // this shouldn't happen
82            }
83            return getTag(id);
84        }
85    }
86
87    protected Representation getTag(String id) {
88        AnnotationStore store = getAnnotationStore();
89        String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
90        Node tagNode = store.getTagNodeByUri(tagUri);
91        Tag tag = store.createTagFromNode(tagNode);
92        JSONObject jo = new JSONObject();
93        try {
94            jo.put("id", tag.getId());
95            jo.put("name", tag.getName());
96            jo.put("uri", tag.getUri());
97        } catch (JSONException e) {
98        }
99
100        return new JsonRepresentation(jo);
101    }
102
103    protected Representation getAllTags() {
104        JSONArray results = new JSONArray();
105        AnnotationStore store = getAnnotationStore();
106
107        List<Tag> tags = store.getTags(null, null);
108        for (Tag tag : tags) {
109            JSONObject jo = new JSONObject();
110            try {
111                jo.put("id", tag.getId());
112                jo.put("name", tag.getName());
113                jo.put("uri", tag.getUri());
114            } catch (JSONException e) {
115            }
116            results.put(jo);
117        }
118
119        // assemble result object
120        JSONObject result = new JSONObject();
121        try {
122            result.put("rows", results);
123            result.put("total", results.length());
124        } catch (JSONException e) {
125            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
126            return null;
127        }
128        logger.debug("sending:");
129        logger.debug(result);
130        return new JsonRepresentation(result);
131    }
132
133}
Note: See TracBrowser for help on using the repository browser.