|
11
|
1 /**
|
|
|
2 * Implements the "annotations" uri of the Annotator API. see
|
|
|
3 * <https://github.com/okfn/annotator/wiki/Storage>
|
|
|
4 */
|
|
|
5 package de.mpiwg.itgroup.annotationManager.restlet;
|
|
|
6
|
|
|
7 import java.io.IOException;
|
|
17
|
8 import java.util.ArrayList;
|
|
11
|
9 import java.util.List;
|
|
|
10
|
|
|
11 import org.json.JSONArray;
|
|
|
12 import org.json.JSONException;
|
|
|
13 import org.json.JSONObject;
|
|
|
14 import org.restlet.Context;
|
|
|
15 import org.restlet.data.Form;
|
|
|
16 import org.restlet.data.MediaType;
|
|
|
17 import org.restlet.data.Status;
|
|
|
18 import org.restlet.ext.json.JsonRepresentation;
|
|
|
19 import org.restlet.representation.Representation;
|
|
|
20 import org.restlet.representation.StringRepresentation;
|
|
17
|
21 import org.restlet.resource.Delete;
|
|
11
|
22 import org.restlet.resource.Get;
|
|
|
23 import org.restlet.resource.Post;
|
|
17
|
24 import org.restlet.resource.Put;
|
|
11
|
25 import org.restlet.security.User;
|
|
|
26
|
|
|
27 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
|
|
28 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
|
|
29 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
|
|
17
|
30 import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation;
|
|
11
|
31 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
|
|
|
32 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
|
|
|
33 import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
|
|
|
34 import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
|
|
|
35 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
|
36
|
|
|
37 /**
|
|
|
38 * Implements the "annotations" uri of the Annotator API. see <https://github.com/okfn/annotator/wiki/Storage>
|
|
|
39 *
|
|
|
40 * @author dwinter, casties
|
|
|
41 *
|
|
|
42 */
|
|
|
43 public class AnnotatorAnnotations extends AnnotatorResourceImpl {
|
|
|
44
|
|
|
45 protected String getAllowedMethodsForHeader() {
|
|
|
46 return "OPTIONS,GET,POST,PUT,DELETE";
|
|
|
47 }
|
|
|
48
|
|
|
49 /**
|
|
|
50 * GET with JSON content-type.
|
|
|
51 *
|
|
|
52 * @param entity
|
|
|
53 * @return
|
|
|
54 */
|
|
|
55 @Get("json")
|
|
|
56 public Representation doGetJSON(Representation entity) {
|
|
13
|
57 logger.debug("AnnotatorAnnotations doGetJSON!");
|
|
15
|
58 setCorsHeaders();
|
|
|
59 // id from URI /annotations/{id}
|
|
17
|
60 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
|
61 String id = decodeJsonId(jsonId);
|
|
|
62 logger.debug("annotation-id=" + id);
|
|
|
63
|
|
|
64 // TODO: what to return without id - list of all annotations?
|
|
15
|
65
|
|
11
|
66 // TODO: what to do with authentication?
|
|
|
67 boolean authenticated = isAuthenticated(entity);
|
|
|
68 logger.debug("request authenticated=" + authenticated);
|
|
|
69
|
|
|
70 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
|
|
|
71
|
|
|
72 try {
|
|
17
|
73 List<Annotation> annots = searcher.searchById(id);
|
|
|
74 if (annots.size() == 1) {
|
|
|
75 // there should be only one
|
|
|
76 JSONObject result = createAnnotatorJson(annots.get(0));
|
|
|
77 logger.debug("sending:");
|
|
|
78 logger.debug(result);
|
|
|
79 return new JsonRepresentation(result);
|
|
|
80 } else {
|
|
|
81 JSONArray results;
|
|
|
82 results = new JSONArray();
|
|
|
83 for (Annotation annot : annots) {
|
|
|
84 JSONObject jo = createAnnotatorJson(annot);
|
|
|
85 if (jo != null) {
|
|
|
86 results.put(createAnnotatorJson(annot));
|
|
|
87 } else {
|
|
|
88 setStatus(Status.SERVER_ERROR_INTERNAL, "JSon Error");
|
|
|
89 return null;
|
|
|
90 }
|
|
11
|
91 }
|
|
17
|
92 // annotator read request returns a list of annotation objects
|
|
|
93 logger.debug("sending:");
|
|
|
94 logger.debug(results);
|
|
|
95 return new JsonRepresentation(results);
|
|
11
|
96 }
|
|
|
97 } catch (TripleStoreHandlerException e) {
|
|
|
98 e.printStackTrace();
|
|
|
99 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
|
|
|
100 return null;
|
|
|
101 } catch (TripleStoreSearchError e) {
|
|
|
102 e.printStackTrace();
|
|
|
103 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
|
|
|
104 return null;
|
|
|
105 }
|
|
|
106 }
|
|
|
107
|
|
|
108 /**
|
|
17
|
109 * POST with JSON content-type.
|
|
13
|
110 *
|
|
17
|
111 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des Dokumentes) text: text der
|
|
|
112 * annotation annoturl: url auf eine Annotation falls extern
|
|
11
|
113 *
|
|
|
114 * @return
|
|
|
115 */
|
|
|
116 @Post("json")
|
|
|
117 public Representation doPostJson(Representation entity) {
|
|
13
|
118 logger.debug("AnnotatorAnnotations doPostJSON!");
|
|
|
119 // set headers
|
|
|
120 setCorsHeaders();
|
|
|
121 Annotation annot = null;
|
|
11
|
122 try {
|
|
13
|
123 JsonRepresentation jrep = new JsonRepresentation(entity);
|
|
11
|
124 JSONObject jo = jrep.getJsonObject();
|
|
|
125 if (jo == null) {
|
|
|
126 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
|
127 return null;
|
|
|
128 }
|
|
17
|
129 // make sure id is not set for POST
|
|
|
130 jo.remove("id");
|
|
13
|
131 // get Annotation object from posted JSON
|
|
11
|
132 annot = createAnnotation(jo, entity);
|
|
|
133 } catch (IOException e1) {
|
|
|
134 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
|
135 return null;
|
|
|
136 } catch (JSONException e) {
|
|
|
137 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
138 return null;
|
|
|
139 }
|
|
13
|
140 if (annot == null || annot.xpointer == null || annot.creator == null) {
|
|
11
|
141 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
142 return null;
|
|
|
143 }
|
|
13
|
144 Annotation storedAnnot;
|
|
11
|
145 try {
|
|
13
|
146 // store Annotation
|
|
|
147 storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
|
|
11
|
148 } catch (TripleStoreStoreError e) {
|
|
|
149 e.printStackTrace();
|
|
|
150 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
|
|
|
151 return null;
|
|
|
152 }
|
|
13
|
153 // return stored annotation
|
|
|
154 JSONObject jo = createAnnotatorJson(storedAnnot);
|
|
11
|
155 JsonRepresentation retRep = new JsonRepresentation(jo);
|
|
|
156 return retRep;
|
|
|
157 }
|
|
|
158
|
|
|
159 /**
|
|
|
160 * POST with HTML content-type.
|
|
|
161 *
|
|
|
162 * @param entity
|
|
|
163 * @return
|
|
|
164 */
|
|
|
165 @Post("html")
|
|
|
166 public Representation doPostHtml(Representation entity) {
|
|
13
|
167 logger.debug("AnnotatorAnnotations doPostHtml!");
|
|
17
|
168 Annotation annot;
|
|
11
|
169 annot = handleForm(entity);
|
|
|
170 if (annot.xpointer == null || annot.creator == null) {
|
|
|
171 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
172
|
|
|
173 return null;
|
|
|
174 }
|
|
|
175
|
|
|
176 Annotation retValAnnot;
|
|
|
177 try {
|
|
|
178 retValAnnot = new Convert("file:///annotations").storeAnnotation(annot);
|
|
|
179 } catch (TripleStoreStoreError e) {
|
|
|
180 e.printStackTrace();
|
|
|
181 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
|
|
|
182 return null;
|
|
|
183 }
|
|
|
184 if (retValAnnot == null) {
|
|
|
185 return null;
|
|
|
186 }
|
|
17
|
187 String retVal = retValAnnot.getAnnotationUri();
|
|
11
|
188 if (retVal == null) {
|
|
|
189 return null;
|
|
|
190 }
|
|
|
191
|
|
|
192 String text = String.format("<html><body><a href=\"%s\">%s</a></body></html>", retVal.replace(">", "").replace("<", ""),
|
|
|
193 retVal.replace(">", ">").replace("<", "<"));
|
|
|
194 Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML);
|
|
|
195 return retRep;
|
|
|
196 }
|
|
|
197
|
|
|
198 /**
|
|
|
199 *
|
|
13
|
200 * @param entity
|
|
|
201 * should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
|
|
11
|
202 *
|
|
13
|
203 * username,password is optional, if not given BasicAuthentification is used.
|
|
11
|
204 *
|
|
13
|
205 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG
|
|
|
206 * namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
11
|
207 *
|
|
|
208 * @return
|
|
|
209 */
|
|
17
|
210 protected Annotation handleForm(Representation entity) {
|
|
|
211 Annotation annot;
|
|
11
|
212 Form form = new Form(entity);
|
|
|
213 String username = form.getValues("username");
|
|
|
214 String mode = form.getValues("mode");
|
|
|
215 String password = form.getValues("password");
|
|
|
216 String xpointer = form.getValues("xpointer");
|
|
|
217 String text = form.getValues("text");
|
|
|
218 String title = form.getValues("title");
|
|
|
219 String url = form.getValues("url");
|
|
|
220 String type = form.getValues("type");
|
|
|
221 RestServer restServer = (RestServer) getApplication();
|
|
|
222
|
|
|
223 // falls user and password nicht null sind:
|
|
|
224 User userFromForm = null;
|
|
|
225 if (username != null && password != null) {
|
|
|
226 if (restServer.authenticate(username, password, getRequest())) {
|
|
|
227 userFromForm = new User(username);
|
|
|
228 }
|
|
|
229 }
|
|
|
230 User authUser = null;
|
|
|
231
|
|
|
232 if (userFromForm == null) {
|
|
13
|
233 authUser = getHttpAuthUser(entity);
|
|
11
|
234 }
|
|
|
235
|
|
|
236 // weder BasicAuth noch FormAuth
|
|
|
237 if (authUser == null && userFromForm == null) {
|
|
|
238 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
|
239 return null;
|
|
|
240 }
|
|
|
241
|
|
|
242 if (userFromForm != null) {
|
|
|
243 username = userFromForm.getIdentifier();
|
|
|
244 } else {
|
|
|
245 username = authUser.getIdentifier();
|
|
|
246 }
|
|
|
247
|
|
13
|
248 // username should be a URI, if not it will set to the MPIWG namespace defined in
|
|
|
249 // de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
|
250 String usernameOrig = username;
|
|
11
|
251 if (!username.startsWith("http"))
|
|
13
|
252 username = NS.MPIWG_PERSONS + username;
|
|
|
253
|
|
|
254 if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource
|
|
|
255
|
|
11
|
256 Context context = getContext();
|
|
|
257 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
|
|
13
|
258
|
|
11
|
259 AnnotationHandler ah = new AnnotationHandler(drupalPath);
|
|
|
260 JSONObject newAnnot;
|
|
|
261 try {
|
|
|
262 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
|
|
|
263 } catch (UnknowUserException e1) {
|
|
|
264 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
|
265 e1.printStackTrace();
|
|
|
266 return null;
|
|
|
267 }
|
|
|
268 try {
|
|
17
|
269 annot = new Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
|
|
11
|
270 } catch (JSONException e) {
|
|
|
271 // TODO Auto-generated catch block
|
|
|
272 e.printStackTrace();
|
|
|
273 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
|
274 return null;
|
|
|
275 }
|
|
|
276 } else
|
|
17
|
277 annot = new Annotation(xpointer, username, null, text, type, url);
|
|
11
|
278 return annot;
|
|
|
279 }
|
|
|
280
|
|
17
|
281
|
|
|
282 /**
|
|
|
283 * PUT with JSON content-type.
|
|
|
284 *
|
|
|
285 * @param entity
|
|
|
286 * @return
|
|
|
287 */
|
|
|
288 @Put("json")
|
|
|
289 public Representation doPutJSON(Representation entity) {
|
|
|
290 logger.debug("AnnotatorAnnotations doPutJSON!");
|
|
|
291 setCorsHeaders();
|
|
|
292 // id from URI /annotations/{id}
|
|
|
293 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
|
294 String id = decodeJsonId(jsonId);
|
|
|
295 logger.debug("annotation-id=" + id);
|
|
|
296
|
|
|
297 // TODO: what to do with authentication? we should check the owner
|
|
|
298 boolean authenticated = isAuthenticated(entity);
|
|
|
299 logger.debug("request authenticated=" + authenticated);
|
|
|
300 if (!authenticated) {
|
|
|
301 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
|
|
|
302 return null;
|
|
|
303 }
|
|
|
304
|
|
|
305 Annotation annot = null;
|
|
|
306 try {
|
|
|
307 JsonRepresentation jrep = new JsonRepresentation(entity);
|
|
|
308 JSONObject jo = jrep.getJsonObject();
|
|
|
309 if (jo == null) {
|
|
|
310 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
311 return null;
|
|
|
312 }
|
|
|
313 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
|
|
|
314 // get stored Annotation
|
|
|
315 List<Annotation> annots = searcher.searchById(id);
|
|
|
316 if (annots.size() < 1) {
|
|
|
317 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
|
|
318 return null;
|
|
|
319 }
|
|
|
320 Annotation storedAnnot = annots.get(0);
|
|
|
321 // delete
|
|
|
322 searcher.deleteById(id);
|
|
|
323 // update from posted JSON
|
|
|
324 annot = updateAnnotation(storedAnnot, jo, entity);
|
|
|
325 // store Annotation
|
|
|
326 storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
|
|
|
327 /* according to https://github.com/okfn/annotator/wiki/Storage
|
|
|
328 * we should return 303: see other
|
|
|
329 * setStatus(Status.REDIRECTION_SEE_OTHER, "go to GET");
|
|
|
330 */
|
|
|
331 setStatus(Status.SUCCESS_NO_CONTENT);
|
|
|
332 } catch (TripleStoreHandlerException e) {
|
|
|
333 e.printStackTrace();
|
|
|
334 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
|
|
|
335 return null;
|
|
|
336 } catch (TripleStoreSearchError e) {
|
|
|
337 e.printStackTrace();
|
|
|
338 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
|
|
|
339 return null;
|
|
|
340 } catch (JSONException e) {
|
|
|
341 e.printStackTrace();
|
|
|
342 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
343 return null;
|
|
|
344 } catch (IOException e) {
|
|
|
345 e.printStackTrace();
|
|
|
346 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
|
|
|
347 return null;
|
|
|
348 }
|
|
|
349 return null;
|
|
|
350 }
|
|
|
351
|
|
|
352 /**
|
|
|
353 * DELETE with JSON content-type.
|
|
|
354 *
|
|
|
355 * @param entity
|
|
|
356 * @return
|
|
|
357 */
|
|
|
358 @Delete("json")
|
|
|
359 public Representation doDeleteJSON(Representation entity) {
|
|
|
360 logger.debug("AnnotatorAnnotations doDeleteJSON!");
|
|
|
361 setCorsHeaders();
|
|
|
362 // id from URI /annotations/{id}
|
|
|
363 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
|
364 String id = decodeJsonId(jsonId);
|
|
|
365 logger.debug("annotation-id=" + id);
|
|
|
366
|
|
|
367 // TODO: what to do with authentication? we should check the owner
|
|
|
368 boolean authenticated = isAuthenticated(entity);
|
|
|
369 logger.debug("request authenticated=" + authenticated);
|
|
|
370 if (!authenticated) {
|
|
|
371 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
|
|
|
372 return null;
|
|
|
373 }
|
|
|
374
|
|
|
375 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
|
|
|
376
|
|
|
377 try {
|
|
|
378 // delete annotation
|
|
|
379 searcher.deleteById(id);
|
|
|
380 setStatus(Status.SUCCESS_NO_CONTENT);
|
|
|
381 } catch (TripleStoreHandlerException e) {
|
|
|
382 e.printStackTrace();
|
|
|
383 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
|
|
|
384 } catch (TripleStoreSearchError e) {
|
|
|
385 e.printStackTrace();
|
|
|
386 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
|
|
|
387 }
|
|
|
388 return null;
|
|
|
389 }
|
|
|
390
|
|
|
391
|
|
11
|
392 }
|