|
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 }
|
|
18
|
153 /* according to https://github.com/okfn/annotator/wiki/Storage
|
|
|
154 * we should return 303: see other.
|
|
|
155 * For now we return the annotation.
|
|
|
156 */
|
|
13
|
157 JSONObject jo = createAnnotatorJson(storedAnnot);
|
|
11
|
158 JsonRepresentation retRep = new JsonRepresentation(jo);
|
|
|
159 return retRep;
|
|
|
160 }
|
|
|
161
|
|
|
162 /**
|
|
|
163 * POST with HTML content-type.
|
|
|
164 *
|
|
|
165 * @param entity
|
|
|
166 * @return
|
|
|
167 */
|
|
|
168 @Post("html")
|
|
|
169 public Representation doPostHtml(Representation entity) {
|
|
13
|
170 logger.debug("AnnotatorAnnotations doPostHtml!");
|
|
17
|
171 Annotation annot;
|
|
11
|
172 annot = handleForm(entity);
|
|
|
173 if (annot.xpointer == null || annot.creator == null) {
|
|
|
174 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
175
|
|
|
176 return null;
|
|
|
177 }
|
|
|
178
|
|
|
179 Annotation retValAnnot;
|
|
|
180 try {
|
|
|
181 retValAnnot = new Convert("file:///annotations").storeAnnotation(annot);
|
|
|
182 } catch (TripleStoreStoreError e) {
|
|
|
183 e.printStackTrace();
|
|
|
184 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
|
|
|
185 return null;
|
|
|
186 }
|
|
|
187 if (retValAnnot == null) {
|
|
|
188 return null;
|
|
|
189 }
|
|
17
|
190 String retVal = retValAnnot.getAnnotationUri();
|
|
11
|
191 if (retVal == null) {
|
|
|
192 return null;
|
|
|
193 }
|
|
|
194
|
|
|
195 String text = String.format("<html><body><a href=\"%s\">%s</a></body></html>", retVal.replace(">", "").replace("<", ""),
|
|
|
196 retVal.replace(">", ">").replace("<", "<"));
|
|
|
197 Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML);
|
|
|
198 return retRep;
|
|
|
199 }
|
|
|
200
|
|
|
201 /**
|
|
|
202 *
|
|
13
|
203 * @param entity
|
|
|
204 * should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
|
|
11
|
205 *
|
|
13
|
206 * username,password is optional, if not given BasicAuthentification is used.
|
|
11
|
207 *
|
|
13
|
208 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG
|
|
|
209 * namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
11
|
210 *
|
|
|
211 * @return
|
|
|
212 */
|
|
17
|
213 protected Annotation handleForm(Representation entity) {
|
|
|
214 Annotation annot;
|
|
11
|
215 Form form = new Form(entity);
|
|
|
216 String username = form.getValues("username");
|
|
|
217 String mode = form.getValues("mode");
|
|
|
218 String password = form.getValues("password");
|
|
|
219 String xpointer = form.getValues("xpointer");
|
|
|
220 String text = form.getValues("text");
|
|
|
221 String title = form.getValues("title");
|
|
|
222 String url = form.getValues("url");
|
|
|
223 String type = form.getValues("type");
|
|
|
224 RestServer restServer = (RestServer) getApplication();
|
|
|
225
|
|
|
226 // falls user and password nicht null sind:
|
|
|
227 User userFromForm = null;
|
|
|
228 if (username != null && password != null) {
|
|
|
229 if (restServer.authenticate(username, password, getRequest())) {
|
|
|
230 userFromForm = new User(username);
|
|
|
231 }
|
|
|
232 }
|
|
|
233 User authUser = null;
|
|
|
234
|
|
|
235 if (userFromForm == null) {
|
|
13
|
236 authUser = getHttpAuthUser(entity);
|
|
11
|
237 }
|
|
|
238
|
|
|
239 // weder BasicAuth noch FormAuth
|
|
|
240 if (authUser == null && userFromForm == null) {
|
|
|
241 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
|
242 return null;
|
|
|
243 }
|
|
|
244
|
|
|
245 if (userFromForm != null) {
|
|
|
246 username = userFromForm.getIdentifier();
|
|
|
247 } else {
|
|
|
248 username = authUser.getIdentifier();
|
|
|
249 }
|
|
|
250
|
|
13
|
251 // username should be a URI, if not it will set to the MPIWG namespace defined in
|
|
|
252 // de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
|
253 String usernameOrig = username;
|
|
11
|
254 if (!username.startsWith("http"))
|
|
13
|
255 username = NS.MPIWG_PERSONS + username;
|
|
|
256
|
|
|
257 if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource
|
|
|
258
|
|
11
|
259 Context context = getContext();
|
|
|
260 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
|
|
13
|
261
|
|
11
|
262 AnnotationHandler ah = new AnnotationHandler(drupalPath);
|
|
|
263 JSONObject newAnnot;
|
|
|
264 try {
|
|
|
265 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
|
|
|
266 } catch (UnknowUserException e1) {
|
|
|
267 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
|
268 e1.printStackTrace();
|
|
|
269 return null;
|
|
|
270 }
|
|
|
271 try {
|
|
17
|
272 annot = new Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
|
|
11
|
273 } catch (JSONException e) {
|
|
|
274 // TODO Auto-generated catch block
|
|
|
275 e.printStackTrace();
|
|
|
276 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
|
277 return null;
|
|
|
278 }
|
|
|
279 } else
|
|
17
|
280 annot = new Annotation(xpointer, username, null, text, type, url);
|
|
11
|
281 return annot;
|
|
|
282 }
|
|
|
283
|
|
17
|
284
|
|
|
285 /**
|
|
|
286 * PUT with JSON content-type.
|
|
|
287 *
|
|
|
288 * @param entity
|
|
|
289 * @return
|
|
|
290 */
|
|
|
291 @Put("json")
|
|
|
292 public Representation doPutJSON(Representation entity) {
|
|
|
293 logger.debug("AnnotatorAnnotations doPutJSON!");
|
|
|
294 setCorsHeaders();
|
|
|
295 // id from URI /annotations/{id}
|
|
|
296 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
|
297 String id = decodeJsonId(jsonId);
|
|
|
298 logger.debug("annotation-id=" + id);
|
|
|
299
|
|
|
300 // TODO: what to do with authentication? we should check the owner
|
|
|
301 boolean authenticated = isAuthenticated(entity);
|
|
|
302 logger.debug("request authenticated=" + authenticated);
|
|
|
303 if (!authenticated) {
|
|
|
304 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
|
|
|
305 return null;
|
|
|
306 }
|
|
|
307
|
|
|
308 Annotation annot = null;
|
|
|
309 try {
|
|
|
310 JsonRepresentation jrep = new JsonRepresentation(entity);
|
|
|
311 JSONObject jo = jrep.getJsonObject();
|
|
|
312 if (jo == null) {
|
|
|
313 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
314 return null;
|
|
|
315 }
|
|
|
316 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
|
|
|
317 // get stored Annotation
|
|
|
318 List<Annotation> annots = searcher.searchById(id);
|
|
|
319 if (annots.size() < 1) {
|
|
|
320 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
|
|
321 return null;
|
|
|
322 }
|
|
|
323 Annotation storedAnnot = annots.get(0);
|
|
|
324 // delete
|
|
|
325 searcher.deleteById(id);
|
|
|
326 // update from posted JSON
|
|
|
327 annot = updateAnnotation(storedAnnot, jo, entity);
|
|
|
328 // store Annotation
|
|
|
329 storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
|
|
|
330 /* according to https://github.com/okfn/annotator/wiki/Storage
|
|
18
|
331 * we should return 303: see other.
|
|
|
332 * For now we return the annotation.
|
|
17
|
333 */
|
|
18
|
334 jo = createAnnotatorJson(storedAnnot);
|
|
|
335 JsonRepresentation retRep = new JsonRepresentation(jo);
|
|
|
336 return retRep;
|
|
17
|
337 } catch (TripleStoreHandlerException e) {
|
|
|
338 e.printStackTrace();
|
|
|
339 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
|
|
|
340 } catch (TripleStoreSearchError e) {
|
|
|
341 e.printStackTrace();
|
|
|
342 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
|
|
|
343 } catch (JSONException e) {
|
|
|
344 e.printStackTrace();
|
|
|
345 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
|
346 } catch (IOException e) {
|
|
|
347 e.printStackTrace();
|
|
|
348 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
|
|
|
349 }
|
|
|
350 return null;
|
|
|
351 }
|
|
|
352
|
|
|
353 /**
|
|
|
354 * DELETE with JSON content-type.
|
|
|
355 *
|
|
|
356 * @param entity
|
|
|
357 * @return
|
|
|
358 */
|
|
|
359 @Delete("json")
|
|
|
360 public Representation doDeleteJSON(Representation entity) {
|
|
|
361 logger.debug("AnnotatorAnnotations doDeleteJSON!");
|
|
|
362 setCorsHeaders();
|
|
|
363 // id from URI /annotations/{id}
|
|
|
364 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
|
365 String id = decodeJsonId(jsonId);
|
|
|
366 logger.debug("annotation-id=" + id);
|
|
|
367
|
|
|
368 // TODO: what to do with authentication? we should check the owner
|
|
|
369 boolean authenticated = isAuthenticated(entity);
|
|
|
370 logger.debug("request authenticated=" + authenticated);
|
|
|
371 if (!authenticated) {
|
|
|
372 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
|
|
|
373 return null;
|
|
|
374 }
|
|
|
375
|
|
|
376 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
|
|
|
377
|
|
|
378 try {
|
|
|
379 // delete annotation
|
|
|
380 searcher.deleteById(id);
|
|
|
381 setStatus(Status.SUCCESS_NO_CONTENT);
|
|
|
382 } catch (TripleStoreHandlerException e) {
|
|
|
383 e.printStackTrace();
|
|
|
384 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
|
|
|
385 } catch (TripleStoreSearchError e) {
|
|
|
386 e.printStackTrace();
|
|
|
387 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
|
|
|
388 }
|
|
|
389 return null;
|
|
|
390 }
|
|
|
391
|
|
|
392
|
|
11
|
393 }
|