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

added lgpl_v3 license information.
author casties
date Thu, 06 Mar 2014 15:09:04 +0100
parents c0dd5314bada
children 25eb2e1df106
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
1 /**
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
2 * ReST API for accessing groups in the Annotation store.
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
3 */
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
4 package de.mpiwg.itgroup.annotations.restlet;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
5
70
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
6 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
7 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
8 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
9 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
11 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
12 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
13 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
14 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
15 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
16 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
17 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
20 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
21 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
22 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
23 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
25 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
26 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
27
65
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
28 import java.io.UnsupportedEncodingException;
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
29 import java.net.URLDecoder;
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
30 import java.util.List;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
31
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
32 import org.json.JSONArray;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
33 import org.json.JSONException;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
34 import org.json.JSONObject;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
35 import org.neo4j.graphdb.Node;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
36 import org.restlet.data.Status;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
37 import org.restlet.ext.json.JsonRepresentation;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
38 import org.restlet.representation.Representation;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
39 import org.restlet.resource.Get;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
40
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
41 import de.mpiwg.itgroup.annotations.Resource;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
42 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
43
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
44 /**
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
45 * API for accessing resource objects in the Annotation store.
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
46 *
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
47 * @author dwinter
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
48 *
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
49 */
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
50 public class AnnotatorResources extends AnnotatorResourceImpl {
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
51 protected String getAllowedMethodsForHeader() {
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
52 return "OPTIONS,GET";
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
53 }
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
54
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
55 /**
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
56 * GET with JSON content-type.
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
57 * Parameters: user: short user name uri: user uri
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
58 *
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
59 * @param entity
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
60 * @return
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
61 */
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
62 @Get("json")
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
63 public Representation doGetJSON(Representation entity) {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
64 logger.debug("AnnotatorResources doGetJSON!");
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
65 setCorsHeaders();
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
66
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
67 String jsonId = (String) getRequest().getAttributes().get("id");
65
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
68 if (jsonId != null) {
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
69 // URL decode
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
70 try {
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
71 jsonId = URLDecoder.decode(jsonId, "UTF-8");
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
72 } catch (UnsupportedEncodingException e) {
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
73 // this shouldn't happen
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
74 }
c0dd5314bada deal with special characters in urls.
casties
parents: 61
diff changeset
75 }
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
76 String uri = decodeJsonId(jsonId);
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
77
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
78 logger.debug("resources-id=" + uri);
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
79
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
80 if (uri == null) {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
81 return getAllResources();
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
82 } else {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
83 return getResource(uri);
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
84 }
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
85 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
86
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
87 protected Representation getResource(String uri) {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
88 AnnotationStore store = getAnnotationStore();
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
89 Node resNode = store.getResourceNodeByUri(uri);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
90 Resource resource = store.createResourceFromNode(resNode);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
91 JSONObject jo = new JSONObject();
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
92 try {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
93 jo.put("id", encodeJsonId(resource.getUri()));
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
94 jo.put("uri", resource.getUri());
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
95 } catch (JSONException e) {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
96 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
97
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
98 return new JsonRepresentation(jo);
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
99 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
100
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
101 protected Representation getAllResources() {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
102 JSONArray results = new JSONArray();
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
103 AnnotationStore store = getAnnotationStore();
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
104
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
105 List<Resource> resources = store.getResources(null, null);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
106 for (Resource resource : resources) {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
107 JSONObject jo = new JSONObject();
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
108 try {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
109 jo.put("id", encodeJsonId(resource.getUri()));
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
110 jo.put("uri", resource.getUri());
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
111 } catch (JSONException e) {
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
112 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
113 results.put(jo);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
114 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
115
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
116 // assemble result object
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
117 JSONObject result = new JSONObject();
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
118 try {
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
119 result.put("rows", results);
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
120 result.put("total", results.length());
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
121 } catch (JSONException e) {
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
122 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
123 return null;
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
124 }
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
125 logger.debug("sending:");
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
126 logger.debug(result);
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
127 return new JsonRepresentation(result);
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 50
diff changeset
128 }
44
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
129
5e9d90461929 rest interface for resources
dwinter
parents:
diff changeset
130 }