annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 4:3599b29c393f

store seems to work now :-)
author casties
date Mon, 02 Jul 2012 22:39:46 +0200
parents 47b53ae385d1
children 629e15b345aa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
1 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
2 * Implements the "annotations" uri of the Annotator API. see
47b53ae385d1 merging old code
casties
parents:
diff changeset
3 * <https://github.com/okfn/annotator/wiki/Storage>
47b53ae385d1 merging old code
casties
parents:
diff changeset
4 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
5 package de.mpiwg.itgroup.annotations.restlet;
47b53ae385d1 merging old code
casties
parents:
diff changeset
6
47b53ae385d1 merging old code
casties
parents:
diff changeset
7 import java.io.IOException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
8
47b53ae385d1 merging old code
casties
parents:
diff changeset
9 import org.json.JSONArray;
47b53ae385d1 merging old code
casties
parents:
diff changeset
10 import org.json.JSONException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
11 import org.json.JSONObject;
47b53ae385d1 merging old code
casties
parents:
diff changeset
12 import org.restlet.data.Status;
47b53ae385d1 merging old code
casties
parents:
diff changeset
13 import org.restlet.ext.json.JsonRepresentation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
14 import org.restlet.representation.Representation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
15 import org.restlet.resource.Delete;
47b53ae385d1 merging old code
casties
parents:
diff changeset
16 import org.restlet.resource.Get;
47b53ae385d1 merging old code
casties
parents:
diff changeset
17 import org.restlet.resource.Post;
47b53ae385d1 merging old code
casties
parents:
diff changeset
18 import org.restlet.resource.Put;
47b53ae385d1 merging old code
casties
parents:
diff changeset
19
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
20 import de.mpiwg.itgroup.annotations.Annotation;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
21 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
22
47b53ae385d1 merging old code
casties
parents:
diff changeset
23 /**
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
24 * Implements the "annotations" uri of the Annotator API. see
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
25 * <https://github.com/okfn/annotator/wiki/Storage>
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
26 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
27 * @author dwinter, casties
47b53ae385d1 merging old code
casties
parents:
diff changeset
28 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
29 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
30 public class AnnotatorAnnotations extends AnnotatorResourceImpl {
47b53ae385d1 merging old code
casties
parents:
diff changeset
31
47b53ae385d1 merging old code
casties
parents:
diff changeset
32 protected String getAllowedMethodsForHeader() {
47b53ae385d1 merging old code
casties
parents:
diff changeset
33 return "OPTIONS,GET,POST,PUT,DELETE";
47b53ae385d1 merging old code
casties
parents:
diff changeset
34 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
35
47b53ae385d1 merging old code
casties
parents:
diff changeset
36 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
37 * GET with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
38 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
39 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
40 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
41 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
42 @Get("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
43 public Representation doGetJSON(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
44 logger.debug("AnnotatorAnnotations doGetJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
45 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
46 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
47 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
48 String id = decodeJsonId(jsonId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
49 logger.debug("annotation-id=" + id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
50
47b53ae385d1 merging old code
casties
parents:
diff changeset
51 // TODO: what to return without id - list of all annotations?
47b53ae385d1 merging old code
casties
parents:
diff changeset
52
47b53ae385d1 merging old code
casties
parents:
diff changeset
53 // TODO: what to do with authentication?
47b53ae385d1 merging old code
casties
parents:
diff changeset
54 boolean authenticated = isAuthenticated(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
55 logger.debug("request authenticated=" + authenticated);
47b53ae385d1 merging old code
casties
parents:
diff changeset
56
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
57 Annotation annots = getAnnotationStore().getAnnotationById(id);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
58 if (annots != null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
59 // there should be only one
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
60 JSONObject result = createAnnotatorJson(annots);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
61 logger.debug("sending:");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
62 logger.debug(result);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
63 return new JsonRepresentation(result);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
64 } else {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
65 JSONArray results = new JSONArray();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
66 // annotator read request returns a list of annotation objects
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
67 logger.debug("sending:");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
68 logger.debug(results);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
69 return new JsonRepresentation(results);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
70 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
71 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
72
47b53ae385d1 merging old code
casties
parents:
diff changeset
73 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
74 * POST with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
75 *
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
76 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
77 * (incl. der URL des Dokumentes) text: text der annotation annoturl: url
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
78 * auf eine Annotation falls extern
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
79 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
80 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
81 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
82 @Post("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
83 public Representation doPostJson(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
84 logger.debug("AnnotatorAnnotations doPostJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
85 // set headers
47b53ae385d1 merging old code
casties
parents:
diff changeset
86 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
87 Annotation annot = null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
88 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
89 JsonRepresentation jrep = new JsonRepresentation(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
90 JSONObject jo = jrep.getJsonObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
91 if (jo == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
92 setStatus(Status.SERVER_ERROR_INTERNAL);
47b53ae385d1 merging old code
casties
parents:
diff changeset
93 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
94 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
95 // make sure id is not set for POST
47b53ae385d1 merging old code
casties
parents:
diff changeset
96 jo.remove("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
97 // get Annotation object from posted JSON
47b53ae385d1 merging old code
casties
parents:
diff changeset
98 annot = createAnnotation(jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
99 } catch (IOException e1) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
100 setStatus(Status.SERVER_ERROR_INTERNAL);
47b53ae385d1 merging old code
casties
parents:
diff changeset
101 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
102 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
103 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
104 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
105 }
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
106 if (annot == null) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
107 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
108 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
109 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
110 Annotation storedAnnot;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
111 // store Annotation
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
112 storedAnnot = getAnnotationStore().storeAnnotation(annot);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
113 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
114 * according to https://github.com/okfn/annotator/wiki/Storage we should
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
115 * return 303: see other. For now we return the annotation.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
116 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
117 JSONObject jo = createAnnotatorJson(storedAnnot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
118 JsonRepresentation retRep = new JsonRepresentation(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
119 return retRep;
47b53ae385d1 merging old code
casties
parents:
diff changeset
120 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
121
47b53ae385d1 merging old code
casties
parents:
diff changeset
122 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
123 * PUT with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
124 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
125 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
126 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
127 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
128 @Put("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
129 public Representation doPutJSON(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
130 logger.debug("AnnotatorAnnotations doPutJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
131 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
132 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
133 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
134 String id = decodeJsonId(jsonId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
135 logger.debug("annotation-id=" + id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
136
47b53ae385d1 merging old code
casties
parents:
diff changeset
137 // TODO: what to do with authentication? we should check the owner
47b53ae385d1 merging old code
casties
parents:
diff changeset
138 boolean authenticated = isAuthenticated(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
139 logger.debug("request authenticated=" + authenticated);
47b53ae385d1 merging old code
casties
parents:
diff changeset
140 if (!authenticated) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
141 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
142 return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
143 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
144
47b53ae385d1 merging old code
casties
parents:
diff changeset
145 Annotation annot = null;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
146 AnnotationStore store = getAnnotationStore();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
147 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
148 JsonRepresentation jrep = new JsonRepresentation(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
149 JSONObject jo = jrep.getJsonObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
150 if (jo == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
151 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
152 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
153 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
154 // get stored Annotation
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
155 Annotation storedAnnot = store.getAnnotationById(id);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
156 if (storedAnnot == null) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
157 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
47b53ae385d1 merging old code
casties
parents:
diff changeset
158 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
159 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
160 // update from posted JSON
47b53ae385d1 merging old code
casties
parents:
diff changeset
161 annot = updateAnnotation(storedAnnot, jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
162 // store Annotation
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
163 storedAnnot = store.storeAnnotation(annot);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
164 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
165 * according to https://github.com/okfn/annotator/wiki/Storage we
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
166 * should return 303: see other. but the client doesn't like it
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
167 * setStatus(Status.REDIRECTION_SEE_OTHER); // go to same URL as
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
168 * this one Reference thisUrl = this.getReference();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
169 * this.getResponse().setLocationRef(thisUrl);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
170 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
171 // return new annotation
47b53ae385d1 merging old code
casties
parents:
diff changeset
172 jo = createAnnotatorJson(storedAnnot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
173 JsonRepresentation retRep = new JsonRepresentation(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
174 return retRep;
47b53ae385d1 merging old code
casties
parents:
diff changeset
175 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
176 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
177 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
178 } catch (IOException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
179 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
180 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
181 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
182 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
183 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
184
47b53ae385d1 merging old code
casties
parents:
diff changeset
185 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
186 * DELETE with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
187 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
188 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
189 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
190 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
191 @Delete("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
192 public Representation doDeleteJSON(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
193 logger.debug("AnnotatorAnnotations doDeleteJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
194 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
195 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
196 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
197 String id = decodeJsonId(jsonId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
198 logger.debug("annotation-id=" + id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
199
47b53ae385d1 merging old code
casties
parents:
diff changeset
200 // TODO: what to do with authentication? we should check the owner
47b53ae385d1 merging old code
casties
parents:
diff changeset
201 boolean authenticated = isAuthenticated(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
202 logger.debug("request authenticated=" + authenticated);
47b53ae385d1 merging old code
casties
parents:
diff changeset
203 if (!authenticated) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
204 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
205 return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
206 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
207
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
208 // delete annotation
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
209 getAnnotationStore().deleteById(id);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
210 setStatus(Status.SUCCESS_NO_CONTENT);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
211 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
212 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
213
47b53ae385d1 merging old code
casties
parents:
diff changeset
214 }