7
|
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.UnsupportedEncodingException;
|
|
6 import java.net.URLDecoder;
|
|
7 import java.net.URLEncoder;
|
|
8 import java.util.ArrayList;
|
|
9 import java.util.List;
|
|
10 import java.util.regex.Matcher;
|
|
11 import java.util.regex.Pattern;
|
|
12
|
|
13 import org.apache.log4j.Logger;
|
|
14 import org.json.JSONArray;
|
|
15 import org.json.JSONException;
|
|
16 import org.json.JSONObject;
|
|
17 import org.restlet.Context;
|
|
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.ServerResource;
|
|
28 import org.restlet.security.User;
|
|
29
|
|
30 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
|
31 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
17
|
32 import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation;
|
7
|
33 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
|
|
34 import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
|
|
35 import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
|
|
36 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
37
|
|
38
|
|
39 public class SearchAnnotations extends ServerResource {
|
|
40
|
|
41 private Logger logger = Logger.getRootLogger();
|
|
42
|
|
43
|
|
44
|
|
45 @Options
|
|
46 public void doOptions(Representation entity) {
|
|
47 Form responseHeaders = (Form) getResponse().getAttributes().get(
|
|
48 "org.restlet.http.headers");
|
|
49 if (responseHeaders == null) {
|
|
50 responseHeaders = new Form();
|
|
51 getResponse().getAttributes().put("org.restlet.http.headers",
|
|
52 responseHeaders);
|
|
53 }
|
|
54 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
|
|
55 String origin = requestHeaders.getFirstValue("Origin", true);
|
|
56 if (origin == null) {
|
|
57 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
58 } else {
|
|
59 responseHeaders.add("Access-Control-Allow-Origin", origin);
|
|
60 }
|
|
61 responseHeaders.add("Access-Control-Allow-Methods", "OPTIONS,GET");
|
|
62 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
|
|
63 if (allowHeaders != null) {
|
|
64 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
|
|
65 }
|
|
66 //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");
|
|
67 responseHeaders.add("Access-Control-Allow-Credentials", "true");
|
|
68 responseHeaders.add("Access-Control-Max-Age", "60");
|
|
69 }
|
|
70
|
|
71 @Get("html")
|
|
72 public Representation doGetHTML(Representation entity){
|
|
73
|
|
74 doOptions(entity);
|
|
75 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
76 String uri = form.getFirstValue("uri");
|
|
77 String user = form.getFirstValue("user");
|
|
78
|
|
79 String limit=form.getFirstValue("limit");
|
|
80 String offset=form.getFirstValue("offset");
|
|
81
|
|
82 try {
|
|
83 if (uri!=null){
|
|
84 uri = URLDecoder.decode(uri, "utf-8");
|
|
85 }
|
|
86 } catch (UnsupportedEncodingException e1) {
|
|
87 e1.printStackTrace();
|
|
88 setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
|
|
89 return null;
|
|
90 }
|
|
91
|
|
92 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
|
|
93
|
|
94 String retString="<html><body><table>";
|
|
95 String lineFormat="<tr><td><a href=\"%s\">%s</a></td>" +
|
|
96 "<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>";
|
|
97 try {
|
|
98
|
17
|
99 List<Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
|
7
|
100
|
17
|
101 for (Annotation annot:annots){
|
7
|
102
|
|
103
|
|
104 RestServer restServer = (RestServer) getApplication();
|
|
105 String userName=restServer.getUserNameFromLdap(annot.creator);
|
|
106 List<String> xpointer = new ArrayList<String>();
|
|
107
|
|
108 if (annot.xpointers==null || annot.xpointers.size()==0)
|
17
|
109 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.getAnnotationUri(),annot.getAnnotationUri());
|
7
|
110 else {
|
|
111 for(String xpointerString:annot.xpointers){
|
17
|
112 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.getAnnotationUri(),annot.getAnnotationUri());
|
7
|
113 }
|
|
114 }
|
|
115
|
|
116 }
|
|
117 } catch (TripleStoreHandlerException e) {
|
|
118 // TODO Auto-generated catch block
|
|
119 e.printStackTrace();
|
|
120 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
|
|
121 return null;
|
|
122 } catch (TripleStoreSearchError e) {
|
|
123 // TODO Auto-generated catch block
|
|
124 e.printStackTrace();
|
|
125 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
|
|
126 return null;
|
|
127 }
|
|
128
|
|
129 retString+="</table></body></html>";
|
|
130
|
|
131 logger.debug("sending:");
|
|
132 logger.debug(retString);
|
|
133 return new StringRepresentation(retString,MediaType.TEXT_HTML);
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Erzeugt aus einer Annotation, das f�r den Annotator notwendige JSON-Format
|
|
138 * @param annot
|
|
139 * @return
|
|
140 */
|
17
|
141 public JSONObject annot2AnnotatorJSON(Annotation annot){
|
7
|
142 JSONObject jo = new JSONObject();
|
|
143 try {
|
|
144 jo.put("text", annot.text);
|
|
145 jo.put("uri",annot.url);
|
|
146
|
|
147 JSONObject userObject= new JSONObject();
|
|
148 userObject.put("id",annot.creator);
|
|
149
|
|
150 RestServer restServer = (RestServer) getApplication();
|
|
151
|
|
152 String userID= annot.creator;
|
26
|
153 if (userID.startsWith(NS.MPIWG_PERSONS_URL)){
|
|
154 userID=userID.replace(NS.MPIWG_PERSONS_URL, ""); //entferne NAMESPACE
|
7
|
155 }
|
|
156 String userName=restServer.getUserNameFromLdap(userID);
|
|
157 userObject.put("name",userName);
|
|
158
|
|
159 jo.put("user",userObject);
|
|
160
|
|
161 List<String> xpointer = new ArrayList<String>();
|
|
162
|
|
163 if (annot.xpointers==null || annot.xpointers.size()==0)
|
|
164 xpointer.add(annot.xpointer);
|
|
165 else {
|
|
166 for(String xpointerString:annot.xpointers){
|
|
167 xpointer.add(xpointerString);
|
|
168 }
|
|
169 }
|
|
170 jo.put("ranges", transformToRanges(xpointer));
|
17
|
171 jo.put("id", annot.getAnnotationUri());
|
7
|
172 return jo;
|
|
173 } catch (JSONException e) {
|
|
174 // TODO Auto-generated catch block
|
|
175 e.printStackTrace();
|
|
176 return null;
|
|
177 }
|
|
178 }
|
|
179
|
|
180 @Get("json")
|
|
181 public Representation doGetJSON(Representation entity){
|
|
182
|
|
183 doOptions(entity);
|
|
184 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
185 String uri = form.getFirstValue("uri");
|
|
186 String user = form.getFirstValue("user");
|
|
187
|
|
188 String limit=form.getFirstValue("limit");
|
|
189 String offset=form.getFirstValue("offset");
|
|
190
|
|
191
|
|
192 //
|
|
193 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
|
|
194
|
|
195 JSONArray ja;
|
|
196 try {
|
|
197
|
17
|
198 List<Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
|
7
|
199
|
|
200 ja = new JSONArray();
|
17
|
201 for (Annotation annot:annots){
|
7
|
202 // JSONObject jo = new JSONObject();
|
|
203 // jo.put("text", annot.text);
|
|
204 // jo.put("uri",annot.url);
|
|
205 //
|
|
206 // JSONObject userObject= new JSONObject();
|
|
207 // userObject.put("id",annot.creator);
|
|
208 //
|
|
209 // RestServer restServer = (RestServer) getApplication();
|
|
210 //
|
|
211 // String userID= annot.creator;
|
|
212 // if (userID.startsWith(NS.MPIWG_PERSONS)){
|
|
213 // userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
|
|
214 // }
|
|
215 // String userName=restServer.getUserNameFromLdap(userID);
|
|
216 // userObject.put("name",userName);
|
|
217 //
|
|
218 // jo.put("user",userObject);
|
|
219 //
|
|
220 // List<String> xpointer = new ArrayList<String>();
|
|
221 //
|
|
222 // if (annot.xpointers==null || annot.xpointers.size()==0)
|
|
223 // xpointer.add(annot.xpointer);
|
|
224 // else {
|
|
225 // for(String xpointerString:annot.xpointers){
|
|
226 // xpointer.add(xpointerString);
|
|
227 // }
|
|
228 // }
|
|
229 // jo.put("ranges", transformToRanges(xpointer));
|
|
230 JSONObject jo = annot2AnnotatorJSON(annot);
|
|
231 if (jo!=null){
|
|
232 ja.put(annot2AnnotatorJSON(annot));
|
|
233 } else {
|
|
234 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
|
|
235 return null;
|
|
236 }
|
|
237 }
|
|
238 } catch (TripleStoreHandlerException e) {
|
|
239 // TODO Auto-generated catch block
|
|
240 e.printStackTrace();
|
|
241 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
|
|
242 return null;
|
|
243 } catch (TripleStoreSearchError e) {
|
|
244 // TODO Auto-generated catch block
|
|
245 e.printStackTrace();
|
|
246 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
|
|
247 return null;
|
|
248 }
|
|
249
|
|
250 JSONObject result = new JSONObject();
|
|
251 try {
|
|
252 result.put("rows",ja);
|
|
253 result.put("total",ja.length());
|
|
254 } catch (JSONException e) {
|
|
255 // TODO Auto-generated catch block
|
|
256 e.printStackTrace();
|
|
257 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
|
|
258 return null;
|
|
259 }
|
|
260
|
|
261 logger.debug("sending:");
|
|
262 logger.debug(result);
|
|
263 return new JsonRepresentation(result);
|
|
264 }
|
|
265
|
|
266 private JSONArray transformToRanges(List<String> xpointers) {
|
|
267
|
|
268 JSONArray ja = new JSONArray();
|
|
269
|
|
270 Pattern rg = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
|
|
271 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
|
|
272
|
|
273
|
|
274
|
|
275 try {
|
|
276 for(String xpointer:xpointers){
|
|
277 String decoded =URLDecoder.decode(xpointer,"utf-8");
|
|
278 Matcher m=rg.matcher(decoded);
|
|
279
|
|
280 if (m.find()){
|
|
281 {
|
|
282 JSONObject jo = new JSONObject();
|
|
283 jo.put("start", m.group(1));
|
|
284 jo.put("startOffset", m.group(2));
|
|
285 jo.put("end", m.group(3));
|
|
286 jo.put("endOffset", m.group(4));
|
|
287 ja.put(jo);
|
|
288 }
|
|
289 }
|
|
290 m=rg1.matcher(xpointer);
|
|
291 if (m.find()){
|
|
292 JSONObject jo = new JSONObject();
|
|
293 jo.put("start", m.group(1));
|
|
294 jo.put("startOffset", m.group(2));
|
|
295
|
|
296 ja.put(jo);
|
|
297 }
|
|
298
|
|
299
|
|
300 }
|
|
301 } catch (JSONException e) {
|
|
302 // TODO Auto-generated catch block
|
|
303 e.printStackTrace();
|
|
304 } catch (UnsupportedEncodingException e) {
|
|
305 // TODO Auto-generated catch block
|
|
306 e.printStackTrace();
|
|
307 }
|
|
308
|
|
309
|
|
310 return ja;
|
|
311 }
|
|
312
|
|
313
|
|
314
|
|
315 /**
|
|
316 *
|
|
317 * @param entity should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
|
|
318 *
|
|
319 * username,password is optional, if not given BasicAuthentification is used.
|
|
320 *
|
|
321 *
|
|
322 *
|
|
323 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
324 *
|
|
325 * @return
|
|
326 */
|
17
|
327 protected Annotation handleForm(Representation entity) {
|
|
328 Annotation annot;
|
7
|
329 Form form = new Form(entity);
|
|
330 String username = form.getValues("username");
|
|
331 String mode = form.getValues("mode");
|
|
332 String password = form.getValues("password");
|
|
333 String xpointer = form.getValues("xpointer");
|
|
334 String text = form.getValues("text");
|
|
335 String title = form.getValues("title");
|
|
336 String url = form.getValues("url");
|
|
337 String type = form.getValues("type");
|
|
338 RestServer restServer = (RestServer) getApplication();
|
|
339
|
|
340 // falls user and password nicht null sind:
|
|
341 User userFromForm = null;
|
|
342 if (username != null && password != null) {
|
|
343 if (restServer.authenticate(username, password, getRequest())) {
|
|
344 userFromForm = new User(username);
|
|
345 }
|
|
346 }
|
|
347 User authUser = null;
|
|
348
|
|
349 if (userFromForm == null) {
|
|
350 authUser = handleBasicAuthentification(entity);
|
|
351 }
|
|
352
|
|
353 // weder BasicAuth noch FormAuth
|
|
354 if (authUser == null && userFromForm == null) {
|
|
355 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
356 return null;
|
|
357 }
|
|
358
|
|
359 if (userFromForm != null) {
|
|
360 username = userFromForm.getIdentifier();
|
|
361 } else {
|
|
362 username = authUser.getIdentifier();
|
|
363 }
|
|
364
|
|
365 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
366 String usernameOrig=username;
|
|
367 if (!username.startsWith("http"))
|
26
|
368 username=NS.MPIWG_PERSONS_URL+username;
|
7
|
369
|
|
370 if (mode.equals("complexAnnotation")){// Annotation mit text in externer ressource
|
|
371
|
|
372 Context context = getContext();
|
|
373 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
|
|
374
|
|
375
|
|
376 AnnotationHandler ah = new AnnotationHandler(drupalPath);
|
|
377 JSONObject newAnnot;
|
|
378 try {
|
|
379 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
|
|
380 } catch (UnknowUserException e1) {
|
|
381 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
382 e1.printStackTrace();
|
|
383 return null;
|
|
384 }
|
|
385 try {
|
17
|
386 annot= new Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
|
7
|
387 } catch (JSONException e) {
|
|
388 // TODO Auto-generated catch block
|
|
389 e.printStackTrace();
|
|
390 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
391 return null;
|
|
392 }
|
|
393 } else
|
17
|
394 annot = new Annotation(xpointer, username, null, text,
|
7
|
395 type, url);
|
|
396 return annot;
|
|
397 }
|
|
398
|
|
399 private User handleBasicAuthentification(Representation entity) {
|
|
400 RestServer restServer = (RestServer) getApplication();
|
|
401 if (!restServer.authenticate(getRequest(), getResponse())) {
|
|
402 // Not authenticated
|
|
403 return null;
|
|
404 }
|
|
405
|
|
406 ClientInfo ci = getRequest().getClientInfo();
|
|
407 logger.debug(ci);
|
|
408 return getRequest().getClientInfo().getUser();
|
|
409
|
|
410 }
|
|
411
|
|
412 /**
|
|
413 * using a minimal annotation format based on the annotea specification
|
|
414 *
|
|
415 * @param jo
|
|
416 * must contain xpointer, text,url,type and can contain a
|
|
417 * username, if not the username form the authentification will
|
|
418 * be used.
|
|
419 * @param authUser
|
|
420 * user object
|
|
421 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
422
|
|
423 * @return
|
|
424 * @throws JSONException
|
|
425 */
|
|
426 public Annotation handleAnnotea(JSONObject jo, Representation entity)
|
|
427 throws JSONException {
|
|
428
|
|
429 User authUser = handleBasicAuthentification(entity);
|
|
430 String username = jo.getString("username"); // not required, if no
|
|
431 // username given authuser
|
|
432 // will be used.
|
|
433 String xpointer = jo.getString("xpointer");
|
|
434 String text = null;
|
|
435 if (jo.has("text"))
|
|
436 text = jo.getString("text");
|
|
437
|
|
438 String url = null;
|
|
439 if (jo.has("url"))
|
|
440 url = jo.getString("url");
|
|
441
|
|
442 String type = null;
|
|
443 if (jo.has("type"))
|
|
444 type = jo.getString("type");
|
|
445
|
|
446 if (username == null)
|
|
447 username = authUser.getIdentifier();
|
|
448
|
|
449 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
450 if (!username.startsWith("http"))
|
26
|
451 username=NS.MPIWG_PERSONS_URL+username;
|
7
|
452
|
17
|
453 return new Annotation(xpointer, username, null, text, type, url);
|
7
|
454 }
|
|
455
|
|
456 /**
|
|
457 * uses the specification from the annotator project.
|
|
458 *
|
|
459 * @see{https://github.com/okfn/annotator/wiki/Annotation-format} The user
|
|
460 * object must
|
|
461 * contain an
|
|
462 * id and
|
|
463 * password or
|
|
464 * basic
|
|
465 * authentification
|
|
466 * is used.
|
|
467 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
468 * @param jo
|
|
469 * @param authUser
|
|
470 * @return
|
|
471 * @throws JSONException
|
|
472 */
|
17
|
473 public Annotation handleAnnotatorSchema(JSONObject jo,
|
7
|
474 Representation entity) throws JSONException {
|
17
|
475 Annotation annot;
|
7
|
476 String url = jo.getString("uri");
|
|
477 String text = jo.getString("text");
|
|
478
|
|
479 String username = null;
|
|
480 if (jo.has("user")) { // not required, if no username given authuser
|
|
481 // will be used otherwise username and password
|
|
482 // has to be submitted
|
|
483 JSONObject user = jo.getJSONObject("user");
|
|
484 if (user.has("id")) {
|
|
485 username = user.getString("id");
|
|
486 if(!user.has("password")){
|
|
487 User authUser = handleBasicAuthentification(entity);
|
|
488 if (authUser==null){
|
|
489 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
490 return null;
|
|
491 }
|
|
492 username = authUser.getIdentifier();
|
|
493 } else {
|
|
494 String password = user.getString("password");
|
|
495 if (!((RestServer) getApplication()).authenticate(username,
|
|
496 password, getRequest())) {
|
|
497 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
498 return null;
|
|
499 }
|
|
500 }
|
|
501 }
|
|
502
|
|
503 } else {
|
|
504 User authUser = handleBasicAuthentification(entity);
|
|
505 if (authUser == null) {
|
|
506 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
507 return null;
|
|
508 }
|
|
509 username = authUser.getIdentifier();
|
|
510 }
|
|
511
|
|
512 String xpointer;
|
|
513 if (jo.has("ranges")) {
|
|
514 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
|
|
515 String start = ranges.getString("start");
|
|
516 String end = ranges.getString("end");
|
|
517 String startOffset = ranges.getString("startOffset");
|
|
518 String endOffset = ranges.getString("endOffset");
|
|
519
|
|
520 try {
|
|
521 xpointer = url+"#"+
|
|
522 URLEncoder.encode(String.format(
|
|
523 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))",
|
|
524 start, startOffset, end, endOffset),"utf-8");
|
|
525 } catch (UnsupportedEncodingException e) {
|
|
526 e.printStackTrace();
|
|
527 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
528 return null;
|
|
529 }
|
|
530 } else {
|
|
531 xpointer = url;
|
|
532 }
|
|
533
|
|
534 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
535 if (!username.startsWith("http"))
|
26
|
536 username=NS.MPIWG_PERSONS_URL+username;
|
7
|
537
|
17
|
538 return new Annotation(xpointer, username, null, text, null);
|
7
|
539 }
|
|
540
|
|
541 }
|