44
|
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;
|
44
|
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.Resource;
|
|
42 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
43
|
|
44 /**
|
61
|
45 * API for accessing resource objects in the Annotation store.
|
44
|
46 *
|
|
47 * @author dwinter
|
61
|
48 *
|
44
|
49 */
|
|
50 public class AnnotatorResources extends AnnotatorResourceImpl {
|
|
51 protected String getAllowedMethodsForHeader() {
|
|
52 return "OPTIONS,GET";
|
|
53 }
|
|
54
|
|
55 /**
|
61
|
56 * GET with JSON content-type.
|
|
57 * Parameters: user: short user name uri: user uri
|
|
58 *
|
44
|
59 * @param entity
|
|
60 * @return
|
|
61 */
|
|
62 @Get("json")
|
|
63 public Representation doGetJSON(Representation entity) {
|
61
|
64 logger.debug("AnnotatorResources doGetJSON!");
|
44
|
65 setCorsHeaders();
|
61
|
66
|
44
|
67 String jsonId = (String) getRequest().getAttributes().get("id");
|
65
|
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 }
|
44
|
76 String uri = decodeJsonId(jsonId);
|
61
|
77
|
44
|
78 logger.debug("resources-id=" + uri);
|
|
79
|
61
|
80 if (uri == null) {
|
|
81 return getAllResources();
|
44
|
82 } else {
|
61
|
83 return getResource(uri);
|
44
|
84 }
|
|
85 }
|
61
|
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);
|
44
|
99 }
|
61
|
100
|
|
101 protected Representation getAllResources() {
|
|
102 JSONArray results = new JSONArray();
|
44
|
103 AnnotationStore store = getAnnotationStore();
|
61
|
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) {
|
44
|
112 }
|
61
|
113 results.put(jo);
|
|
114 }
|
|
115
|
44
|
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);
|
61
|
128 }
|
44
|
129
|
|
130 }
|