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