0
|
1 //TODO: handle XML-Post des Annoteaprotocolls http://www.w3.org/2001/Annotea/User/Protocol.html
|
|
2
|
|
3 package de.mpiwg.itgroup.annotationManager.restlet;
|
|
4
|
|
5 import java.io.IOException;
|
|
6 import java.io.UnsupportedEncodingException;
|
|
7 import java.net.URLDecoder;
|
|
8 import java.net.URLEncoder;
|
|
9 import java.util.ArrayList;
|
|
10 import java.util.List;
|
|
11 import java.util.regex.Matcher;
|
|
12 import java.util.regex.Pattern;
|
|
13
|
|
14 import org.apache.log4j.Logger;
|
|
15 import org.json.JSONArray;
|
|
16 import org.json.JSONException;
|
|
17 import org.json.JSONObject;
|
|
18 import org.restlet.data.ClientInfo;
|
|
19 import org.restlet.data.Form;
|
|
20 import org.restlet.data.MediaType;
|
|
21 import org.restlet.data.Status;
|
|
22 import org.restlet.ext.json.JsonRepresentation;
|
|
23 import org.restlet.representation.Representation;
|
|
24 import org.restlet.representation.StringRepresentation;
|
|
25 import org.restlet.resource.Get;
|
|
26 import org.restlet.resource.Options;
|
|
27 import org.restlet.resource.Post;
|
|
28 import org.restlet.resource.ServerResource;
|
|
29 import org.restlet.security.User;
|
|
30
|
|
31 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
|
32 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
|
|
33 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
|
|
34 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
|
|
35 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
|
|
36 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
|
|
37
|
|
38 public class AddAndSearchAnnotations extends ServerResource {
|
|
39
|
|
40 private Logger logger = Logger.getRootLogger();
|
|
41
|
|
42 /**
|
|
43 *
|
|
44 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt
|
|
45 * (incl. der URL des Dokumentes) text: text der annotation annoturl: url
|
|
46 * auf eine Annotation falls extern
|
|
47 *
|
|
48 * @return
|
|
49 */
|
|
50
|
|
51
|
|
52 @Options
|
|
53 public void doOptions(Representation entity) {
|
|
54 Form responseHeaders = (Form) getResponse().getAttributes().get(
|
|
55 "org.restlet.http.headers");
|
|
56 if (responseHeaders == null) {
|
|
57 responseHeaders = new Form();
|
|
58 getResponse().getAttributes().put("org.restlet.http.headers",
|
|
59 responseHeaders);
|
|
60 }
|
|
61 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
62 responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
|
|
63 responseHeaders.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Annotator-Account-Id, X-Annotator-User-Id, X-Annotator-Auth-Token-Valid-Until, X-Annotator-Auth-Token");
|
|
64 responseHeaders.add("Access-Control-Allow-Credentials", "false");
|
|
65 responseHeaders.add("Access-Control-Max-Age", "60");
|
|
66 }
|
|
67
|
|
68 @Get("json")
|
|
69 public Representation doGetHTML(Representation entity){
|
|
70
|
|
71 doOptions(entity);
|
|
72 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
73 String uri = form.getFirstValue("uri");
|
|
74 String user = form.getFirstValue("user");
|
|
75
|
|
76 String limit=form.getFirstValue("limit");
|
|
77 String offset=form.getFirstValue("offset");
|
|
78
|
|
79
|
|
80 //
|
|
81 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
|
|
82
|
|
83 JSONArray ja;
|
|
84 try {
|
|
85
|
|
86 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
|
|
87
|
|
88 ja = new JSONArray();
|
|
89 for (Convert.Annotation annot:annots){
|
|
90 JSONObject jo = new JSONObject();
|
|
91 jo.put("text", annot.text);
|
|
92 jo.put("uri",annot.url);
|
|
93
|
|
94 JSONObject userObject= new JSONObject();
|
|
95 userObject.put("id",annot.creator);
|
|
96
|
|
97 RestServer restServer = (RestServer) getApplication();
|
|
98 String userName=restServer.getUserNameFromLdap(annot.creator);
|
|
99 userObject.put("name",userName);
|
|
100
|
|
101 jo.put("user",userObject);
|
|
102
|
|
103 List<String> xpointer = new ArrayList<String>();
|
|
104
|
|
105 if (annot.xpointers==null || annot.xpointers.size()==0)
|
|
106 xpointer.add(annot.xpointer);
|
|
107 else {
|
|
108 for(String xpointerString:annot.xpointers){
|
|
109 xpointer.add(xpointerString);
|
|
110 }
|
|
111 }
|
|
112 jo.put("ranges", transformToRanges(xpointer));
|
|
113 ja.put(jo);
|
|
114 }
|
|
115 } catch (TripleStoreHandlerException e) {
|
|
116 // TODO Auto-generated catch block
|
|
117 e.printStackTrace();
|
|
118 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
|
|
119 return null;
|
|
120 } catch (TripleStoreSearchError e) {
|
|
121 // TODO Auto-generated catch block
|
|
122 e.printStackTrace();
|
|
123 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
|
|
124 return null;
|
|
125 } catch (JSONException e) {
|
|
126 // TODO Auto-generated catch block
|
|
127 e.printStackTrace();
|
|
128 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
|
|
129 return null;
|
|
130 }
|
|
131
|
|
132 JSONObject retObject = new JSONObject();
|
|
133 try {
|
|
134 retObject.put("rows",ja);
|
|
135 retObject.put("total",ja.length());
|
|
136 } catch (JSONException e) {
|
|
137 // TODO Auto-generated catch block
|
|
138 e.printStackTrace();
|
|
139 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
|
|
140 return null;
|
|
141 }
|
|
142
|
|
143 logger.debug("sending:");
|
|
144 logger.debug(retObject);
|
|
145 return new JsonRepresentation(retObject);
|
|
146 }
|
|
147
|
|
148 private JSONArray transformToRanges(List<String> xpointers) {
|
|
149
|
|
150 JSONArray ja = new JSONArray();
|
|
151
|
|
152 Pattern rg = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
|
|
153 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
|
|
154
|
|
155
|
|
156
|
|
157 try {
|
|
158 for(String xpointer:xpointers){
|
|
159 String decoded =URLDecoder.decode(xpointer,"utf-8");
|
|
160 Matcher m=rg.matcher(decoded);
|
1
|
161
|
|
162 if (m.find()){
|
0
|
163 {
|
|
164 JSONObject jo = new JSONObject();
|
|
165 jo.put("start", m.group(1));
|
|
166 jo.put("startOffset", m.group(2));
|
|
167 jo.put("end", m.group(3));
|
|
168 jo.put("endOffset", m.group(4));
|
|
169 ja.put(jo);
|
1
|
170 }
|
0
|
171 }
|
|
172 m=rg1.matcher(xpointer);
|
1
|
173 if (m.find()){
|
0
|
174 JSONObject jo = new JSONObject();
|
|
175 jo.put("start", m.group(1));
|
|
176 jo.put("startOffset", m.group(2));
|
|
177
|
|
178 ja.put(jo);
|
|
179 }
|
|
180
|
|
181
|
|
182 }
|
|
183 } catch (JSONException e) {
|
|
184 // TODO Auto-generated catch block
|
|
185 e.printStackTrace();
|
|
186 } catch (UnsupportedEncodingException e) {
|
|
187 // TODO Auto-generated catch block
|
|
188 e.printStackTrace();
|
|
189 }
|
|
190
|
|
191
|
|
192 return ja;
|
|
193
|
|
194
|
|
195
|
|
196
|
|
197
|
|
198 }
|
|
199
|
|
200 @Post("json")
|
|
201 public Representation doPostJson(Representation entity) {
|
|
202
|
|
203 String retVal = doPost(entity);
|
|
204 JSONObject jo;
|
|
205 try {
|
|
206 jo = new JSONObject("{\"annotUrl\":\"" + retVal + "\"}");
|
|
207 } catch (JSONException e) {
|
|
208 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
209 return null;
|
|
210 }
|
|
211 JsonRepresentation retRep = new JsonRepresentation(jo);
|
|
212 return retRep;
|
|
213 }
|
|
214
|
|
215 @Post("html")
|
|
216 public Representation doPostHtml(Representation entity) {
|
|
217 String retVal = doPost(entity);
|
|
218 if (retVal == null) {
|
|
219 return null;
|
|
220 }
|
|
221 String text = String.format(
|
|
222 "<html><body><a href=\"%s\">%s</a></body></html>", retVal
|
|
223 .replace(">", "").replace("<", ""),
|
|
224 retVal.replace(">", ">").replace("<", "<"));
|
|
225 Representation retRep = new StringRepresentation(text,
|
|
226 MediaType.TEXT_HTML);
|
|
227 return retRep;
|
|
228 }
|
|
229
|
|
230 public String doPost(Representation entity) {
|
|
231 Convert.Annotation annot;
|
|
232 // versuche basic authentifizierung und hole den Benutzer von dort.
|
|
233
|
|
234 // User authUser;= handleBasicAuthentification(entity);
|
|
235
|
|
236 if (entity.getMediaType().equals(MediaType.APPLICATION_JSON)) {
|
|
237
|
|
238 JsonRepresentation jrep;
|
|
239 try {
|
|
240 jrep = new JsonRepresentation(entity);
|
|
241 } catch (IOException e1) {
|
|
242 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
243 return null;
|
|
244 }
|
|
245
|
|
246 // try {
|
|
247 // logger.debug(jrep.getText());
|
|
248 // } catch (IOException e1) {
|
|
249 // // TODO Auto-generated catch block
|
|
250 // e1.printStackTrace();
|
|
251 // }
|
|
252 //
|
|
253
|
|
254 try {
|
|
255 JSONObject jo = jrep.getJsonObject();
|
|
256 String mode = jo.getString("mode"); // hole modus
|
|
257 if (mode==null || mode.equals(""))
|
|
258 mode="annotea"; // default mode (annotea) TODO make this configurable
|
|
259
|
|
260 if (mode.equals("annotator") ) { // annotator format
|
|
261 annot = handleAnnotatorSchema(jo, entity);
|
|
262 logger.debug("storing annotator object");
|
|
263 logger.debug(jo);
|
|
264 } else if (mode.equals("annotea")){
|
|
265 annot = handleAnnotea(jo, entity);
|
|
266 } else {
|
|
267 setStatus(Status.CLIENT_ERROR_BAD_REQUEST,"mode "+mode+"not supported!");
|
|
268 return null;
|
|
269 }
|
|
270
|
|
271 } catch (JSONException e) {
|
|
272 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
273 return null;
|
|
274 }
|
|
275
|
|
276 } else if (entity.getMediaType().equals(MediaType.APPLICATION_WWW_FORM)) {
|
|
277 annot = handleForm(entity);
|
|
278
|
|
279 } else {
|
|
280 setStatus(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
|
|
281
|
|
282 return null;
|
|
283 }
|
|
284
|
|
285 if (annot==null){
|
|
286 return null;
|
|
287 }
|
|
288 if (annot.xpointer == null || annot.creator == null) {
|
|
289 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
290
|
|
291 return null;
|
|
292 }
|
|
293
|
|
294
|
|
295
|
|
296 try {
|
|
297 return new Convert("file:///annotations").storeAnnotation(annot);
|
|
298 } catch (TripleStoreStoreError e) {
|
|
299 e.printStackTrace();
|
|
300 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
|
|
301 return null;
|
|
302 }
|
|
303 }
|
|
304
|
|
305 protected Convert.Annotation handleForm(Representation entity) {
|
|
306 Convert.Annotation annot;
|
|
307 Form form = new Form(entity);
|
|
308 String username = form.getValues("username");
|
|
309 String password = form.getValues("password");
|
|
310 String xpointer = form.getValues("xpointer");
|
|
311 String text = form.getValues("text");
|
|
312 String url = form.getValues("url");
|
|
313 String type = form.getValues("type");
|
|
314 RestServer restServer = (RestServer) getApplication();
|
|
315
|
|
316 // falls user and password nicht null sind:
|
|
317 User userFromForm = null;
|
|
318 if (username != null && password != null) {
|
|
319 if (restServer.authenticate(username, password, getRequest())) {
|
|
320 userFromForm = new User(username);
|
|
321 }
|
|
322 }
|
|
323 User authUser = null;
|
|
324
|
|
325 if (userFromForm == null) {
|
|
326 authUser = handleBasicAuthentification(entity);
|
|
327 }
|
|
328
|
|
329 // weder BasicAuth noch FormAuth
|
|
330 if (authUser == null && userFromForm == null) {
|
|
331 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
332 return null;
|
|
333 }
|
|
334
|
|
335 if (userFromForm != null) {
|
|
336 username = userFromForm.getIdentifier();
|
|
337 } else {
|
|
338 username = authUser.getIdentifier();
|
|
339 }
|
|
340
|
|
341 annot = new Convert.Annotation(xpointer, username, null, text,
|
|
342 type, url);
|
|
343 return annot;
|
|
344 }
|
|
345
|
|
346 @Post
|
|
347 public Representation doPostHtml2(Representation entity) {
|
|
348 return doPostHtml(entity);
|
|
349 }
|
|
350
|
|
351 private User handleBasicAuthentification(Representation entity) {
|
|
352 RestServer restServer = (RestServer) getApplication();
|
|
353 if (!restServer.authenticate(getRequest(), getResponse())) {
|
|
354 // Not authenticated
|
|
355 return null;
|
|
356 }
|
|
357
|
|
358 ClientInfo ci = getRequest().getClientInfo();
|
|
359 logger.debug(ci);
|
|
360 return getRequest().getClientInfo().getUser();
|
|
361
|
|
362 }
|
|
363
|
|
364 /**
|
|
365 * using a minimal annotation format based on the annotea specification
|
|
366 *
|
|
367 * @param jo
|
|
368 * must contain xpointer, text,url,type and can contain a
|
|
369 * username, if not the username form the authentification will
|
|
370 * be used.
|
|
371 * @param authUser
|
|
372 * user object
|
|
373 * @return
|
|
374 * @throws JSONException
|
|
375 */
|
|
376 public Annotation handleAnnotea(JSONObject jo, Representation entity)
|
|
377 throws JSONException {
|
|
378
|
|
379 User authUser = handleBasicAuthentification(entity);
|
|
380 String username = jo.getString("username"); // not required, if no
|
|
381 // username given authuser
|
|
382 // will be used.
|
|
383 String xpointer = jo.getString("xpointer");
|
|
384 String text = null;
|
|
385 if (jo.has("text"))
|
|
386 text = jo.getString("text");
|
|
387
|
|
388 String url = null;
|
|
389 if (jo.has("url"))
|
|
390 url = jo.getString("url");
|
|
391
|
|
392 String type = null;
|
|
393 if (jo.has("type"))
|
|
394 type = jo.getString("type");
|
|
395
|
|
396 if (username == null)
|
|
397 username = authUser.getIdentifier();
|
|
398
|
|
399 return new Convert.Annotation(xpointer, username, null, text, type, url);
|
|
400 }
|
|
401
|
|
402 /**
|
|
403 * uses the specification from the annotator project.
|
|
404 *
|
|
405 * @see{https://github.com/okfn/annotator/wiki/Annotation-format The user
|
|
406 * object must
|
|
407 * contain an
|
|
408 * id and
|
|
409 * password or
|
|
410 * basic
|
|
411 * authentification
|
|
412 * is used.
|
|
413 *
|
|
414 * @param jo
|
|
415 * @param authUser
|
|
416 * @return
|
|
417 * @throws JSONException
|
|
418 */
|
|
419 public Convert.Annotation handleAnnotatorSchema(JSONObject jo,
|
|
420 Representation entity) throws JSONException {
|
|
421 Convert.Annotation annot;
|
|
422 String url = jo.getString("uri");
|
|
423 String text = jo.getString("text");
|
|
424
|
|
425 String username = null;
|
|
426 if (jo.has("user")) { // not required, if no username given authuser
|
|
427 // will be used otherwise username and password
|
|
428 // has to be submitted
|
|
429 JSONObject user = jo.getJSONObject("user");
|
|
430 if (user.has("id")) {
|
|
431 username = user.getString("id");
|
|
432 if(!user.has("password")){
|
|
433 User authUser = handleBasicAuthentification(entity);
|
|
434 if (authUser==null){
|
|
435 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
436 return null;
|
|
437 }
|
|
438 username = authUser.getIdentifier();
|
|
439 } else {
|
|
440 String password = user.getString("password");
|
|
441 if (!((RestServer) getApplication()).authenticate(username,
|
|
442 password, getRequest())) {
|
|
443 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
444 return null;
|
|
445 }
|
|
446 }
|
|
447 }
|
|
448
|
|
449 } else {
|
|
450 User authUser = handleBasicAuthentification(entity);
|
|
451 if (authUser == null) {
|
|
452 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
453 return null;
|
|
454 }
|
|
455 username = authUser.getIdentifier();
|
|
456 }
|
|
457
|
|
458 String xpointer;
|
|
459 if (jo.has("ranges")) {
|
|
460 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
|
|
461 String start = ranges.getString("start");
|
|
462 String end = ranges.getString("end");
|
|
463 String startOffset = ranges.getString("startOffset");
|
|
464 String endOffset = ranges.getString("endOffset");
|
|
465
|
|
466 try {
|
|
467 xpointer = url+"#"+
|
|
468 URLEncoder.encode(String.format(
|
|
469 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))",
|
|
470 start, startOffset, end, endOffset),"utf-8");
|
|
471 } catch (UnsupportedEncodingException e) {
|
|
472 e.printStackTrace();
|
|
473 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
474 return null;
|
|
475 }
|
|
476 } else {
|
|
477 xpointer = url;
|
|
478 }
|
|
479 return new Convert.Annotation(xpointer, username, null, text, null);
|
|
480 }
|
|
481
|
|
482 }
|