comparison src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java @ 2:6888ae3287b8

changed to namespace handling for users
author dwinter
date Thu, 24 Nov 2011 11:39:12 +0100
parents f2f41d0dedf5
children e5f0906c107c
comparison
equal deleted inserted replaced
1:f2f41d0dedf5 2:6888ae3287b8
26 import org.restlet.resource.Options; 26 import org.restlet.resource.Options;
27 import org.restlet.resource.Post; 27 import org.restlet.resource.Post;
28 import org.restlet.resource.ServerResource; 28 import org.restlet.resource.ServerResource;
29 import org.restlet.security.User; 29 import org.restlet.security.User;
30 30
31 import de.mpiwg.itgroup.annotationManager.Constants.NS;
31 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError; 32 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
32 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError; 33 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
33 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert; 34 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
34 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation; 35 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
35 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher; 36 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
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-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-Allow-Credentials", "false");
65 responseHeaders.add("Access-Control-Max-Age", "60"); 66 responseHeaders.add("Access-Control-Max-Age", "60");
66 } 67 }
67 68
69 @Get("html")
70 public Representation doGetHTML(Representation entity){
71
72 doOptions(entity);
73 Form form = getRequest().getResourceRef().getQueryAsForm();
74 String uri = form.getFirstValue("uri");
75 String user = form.getFirstValue("user");
76
77 String limit=form.getFirstValue("limit");
78 String offset=form.getFirstValue("offset");
79
80
81 //
82 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
83
84 String retString="<html><body><table>";
85 String lineFormat="<tr><td>%s</td>" +
86 "<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>";
87 try {
88
89 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
90
91 for (Convert.Annotation annot:annots){
92
93
94 RestServer restServer = (RestServer) getApplication();
95 String userName=restServer.getUserNameFromLdap(annot.creator);
96 List<String> xpointer = new ArrayList<String>();
97
98 if (annot.xpointers==null || annot.xpointers.size()==0)
99 retString+=String.format(lineFormat, userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri);
100 else {
101 for(String xpointerString:annot.xpointers){
102 retString+=String.format(lineFormat, userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri);
103
104 }
105 }
106
107 }
108 } catch (TripleStoreHandlerException e) {
109 // TODO Auto-generated catch block
110 e.printStackTrace();
111 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
112 return null;
113 } catch (TripleStoreSearchError e) {
114 // TODO Auto-generated catch block
115 e.printStackTrace();
116 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
117 return null;
118 }
119
120 retString+="</table></body></html>";
121
122 logger.debug("sending:");
123 logger.debug(retString);
124 return new StringRepresentation(retString,MediaType.TEXT_HTML);
125 }
126
127
68 @Get("json") 128 @Get("json")
69 public Representation doGetHTML(Representation entity){ 129 public Representation doGetJSON(Representation entity){
70 130
71 doOptions(entity); 131 doOptions(entity);
72 Form form = getRequest().getResourceRef().getQueryAsForm(); 132 Form form = getRequest().getResourceRef().getQueryAsForm();
73 String uri = form.getFirstValue("uri"); 133 String uri = form.getFirstValue("uri");
74 String user = form.getFirstValue("user"); 134 String user = form.getFirstValue("user");
93 153
94 JSONObject userObject= new JSONObject(); 154 JSONObject userObject= new JSONObject();
95 userObject.put("id",annot.creator); 155 userObject.put("id",annot.creator);
96 156
97 RestServer restServer = (RestServer) getApplication(); 157 RestServer restServer = (RestServer) getApplication();
98 String userName=restServer.getUserNameFromLdap(annot.creator); 158
159 String userID= annot.creator;
160 if (userID.startsWith(NS.MPIWG_PERSONS)){
161 userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
162 }
163 String userName=restServer.getUserNameFromLdap(userID);
99 userObject.put("name",userName); 164 userObject.put("name",userName);
100 165
101 jo.put("user",userObject); 166 jo.put("user",userObject);
102 167
103 List<String> xpointer = new ArrayList<String>(); 168 List<String> xpointer = new ArrayList<String>();
300 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error"); 365 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
301 return null; 366 return null;
302 } 367 }
303 } 368 }
304 369
370
371 /**
372 *
373 * @param entity should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
374 *
375 * username,password is optional, if not given BasicAuthentification is used.
376 *
377 *
378 *
379 * 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
380 *
381 * @return
382 */
305 protected Convert.Annotation handleForm(Representation entity) { 383 protected Convert.Annotation handleForm(Representation entity) {
306 Convert.Annotation annot; 384 Convert.Annotation annot;
307 Form form = new Form(entity); 385 Form form = new Form(entity);
308 String username = form.getValues("username"); 386 String username = form.getValues("username");
309 String password = form.getValues("password"); 387 String password = form.getValues("password");
336 username = userFromForm.getIdentifier(); 414 username = userFromForm.getIdentifier();
337 } else { 415 } else {
338 username = authUser.getIdentifier(); 416 username = authUser.getIdentifier();
339 } 417 }
340 418
419 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
420 if (!username.startsWith("http"))
421 username=NS.MPIWG_PERSONS+username;
422
341 annot = new Convert.Annotation(xpointer, username, null, text, 423 annot = new Convert.Annotation(xpointer, username, null, text,
342 type, url); 424 type, url);
343 return annot; 425 return annot;
344 } 426 }
345 427
368 * must contain xpointer, text,url,type and can contain a 450 * must contain xpointer, text,url,type and can contain a
369 * username, if not the username form the authentification will 451 * username, if not the username form the authentification will
370 * be used. 452 * be used.
371 * @param authUser 453 * @param authUser
372 * user object 454 * user object
455 * 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
456
373 * @return 457 * @return
374 * @throws JSONException 458 * @throws JSONException
375 */ 459 */
376 public Annotation handleAnnotea(JSONObject jo, Representation entity) 460 public Annotation handleAnnotea(JSONObject jo, Representation entity)
377 throws JSONException { 461 throws JSONException {
394 type = jo.getString("type"); 478 type = jo.getString("type");
395 479
396 if (username == null) 480 if (username == null)
397 username = authUser.getIdentifier(); 481 username = authUser.getIdentifier();
398 482
483 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
484 if (!username.startsWith("http"))
485 username=NS.MPIWG_PERSONS+username;
486
399 return new Convert.Annotation(xpointer, username, null, text, type, url); 487 return new Convert.Annotation(xpointer, username, null, text, type, url);
400 } 488 }
401 489
402 /** 490 /**
403 * uses the specification from the annotator project. 491 * uses the specification from the annotator project.
408 * id and 496 * id and
409 * password or 497 * password or
410 * basic 498 * basic
411 * authentification 499 * authentification
412 * is used. 500 * is used.
413 * 501 * 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
414 * @param jo 502 * @param jo
415 * @param authUser 503 * @param authUser
416 * @return 504 * @return
417 * @throws JSONException 505 * @throws JSONException
418 */ 506 */
474 return null; 562 return null;
475 } 563 }
476 } else { 564 } else {
477 xpointer = url; 565 xpointer = url;
478 } 566 }
567
568 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
569 if (!username.startsWith("http"))
570 username=NS.MPIWG_PERSONS+username;
571
479 return new Convert.Annotation(xpointer, username, null, text, null); 572 return new Convert.Annotation(xpointer, username, null, text, null);
480 } 573 }
481 574
482 } 575 }