28
|
1 /**
|
|
2 * ReST API for accessing groups in the Annotation store.
|
|
3 */
|
|
4 package de.mpiwg.itgroup.annotations.restlet;
|
|
5
|
70
|
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
|
65
|
28 import java.io.UnsupportedEncodingException;
|
|
29 import java.net.URLDecoder;
|
28
|
30 import java.util.List;
|
|
31
|
|
32 import org.json.JSONArray;
|
|
33 import org.json.JSONException;
|
|
34 import org.json.JSONObject;
|
|
35 import org.neo4j.graphdb.Node;
|
|
36 import org.restlet.data.Status;
|
|
37 import org.restlet.ext.json.JsonRepresentation;
|
|
38 import org.restlet.representation.Representation;
|
|
39 import org.restlet.resource.Get;
|
|
40
|
|
41 import de.mpiwg.itgroup.annotations.Tag;
|
|
42 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
43
|
|
44 /**
|
|
45 * API for accessing tags in the Annotation store.
|
|
46 *
|
|
47 * @author dwinter
|
65
|
48 *
|
28
|
49 */
|
|
50 public class AnnotatorTags extends AnnotatorResourceImpl {
|
|
51 protected String getAllowedMethodsForHeader() {
|
|
52 return "OPTIONS,GET";
|
|
53 }
|
|
54
|
|
55 /**
|
65
|
56 * GET with JSON content-type.
|
28
|
57 * Parameters:
|
65
|
58 * user: short user name
|
28
|
59 * uri: user uri
|
65
|
60 *
|
28
|
61 * @param entity
|
|
62 * @return
|
|
63 */
|
|
64 @Get("json")
|
|
65 public Representation doGetJSON(Representation entity) {
|
|
66 logger.debug("AnnotatorGroups doGetJSON!");
|
|
67 setCorsHeaders();
|
65
|
68
|
28
|
69 String jsonId = (String) getRequest().getAttributes().get("id");
|
65
|
70 // String id = decodeJsonId(jsonId);
|
28
|
71 String id = jsonId;
|
|
72 logger.debug("annotation-id=" + id);
|
|
73
|
65
|
74 if (id == null) {
|
|
75 return getAllTags();
|
28
|
76 } else {
|
65
|
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);
|
28
|
84 }
|
|
85 }
|
65
|
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);
|
28
|
101 }
|
65
|
102
|
|
103 protected Representation getAllTags() {
|
|
104 JSONArray results = new JSONArray();
|
28
|
105 AnnotationStore store = getAnnotationStore();
|
65
|
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) {
|
28
|
115 }
|
65
|
116 results.put(jo);
|
|
117 }
|
|
118
|
28
|
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);
|
65
|
131 }
|
28
|
132
|
|
133 }
|