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
|
|
52 /**
|
61
|
53 * GET with JSON content-type.
|
|
54 * Parameters: user: short user name uri: user uri
|
|
55 *
|
44
|
56 * @param entity
|
|
57 * @return
|
|
58 */
|
|
59 @Get("json")
|
|
60 public Representation doGetJSON(Representation entity) {
|
75
|
61 logger.fine("AnnotatorResources doGetJSON!");
|
61
|
62
|
44
|
63 String jsonId = (String) getRequest().getAttributes().get("id");
|
65
|
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 }
|
44
|
72 String uri = decodeJsonId(jsonId);
|
61
|
73
|
75
|
74 logger.fine("resources-id=" + uri);
|
44
|
75
|
61
|
76 if (uri == null) {
|
|
77 return getAllResources();
|
44
|
78 } else {
|
61
|
79 return getResource(uri);
|
44
|
80 }
|
|
81 }
|
61
|
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);
|
44
|
95 }
|
61
|
96
|
|
97 protected Representation getAllResources() {
|
|
98 JSONArray results = new JSONArray();
|
44
|
99 AnnotationStore store = getAnnotationStore();
|
61
|
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) {
|
44
|
108 }
|
61
|
109 results.put(jo);
|
|
110 }
|
|
111
|
44
|
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 }
|
75
|
121 logger.fine("sending:");
|
|
122 logger.fine(result.toString());
|
44
|
123 return new JsonRepresentation(result);
|
61
|
124 }
|
44
|
125
|
|
126 }
|