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
|
2
|
31 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
0
|
32 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
|
33 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
|
|
34 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
|
|
35 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
|
|
36 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
|
|
37 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
|
|
38
|
|
39 public class AddAndSearchAnnotations 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 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
55 responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
|
|
56 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");
|
|
57 responseHeaders.add("Access-Control-Allow-Credentials", "false");
|
|
58 responseHeaders.add("Access-Control-Max-Age", "60");
|
|
59 }
|
|
60
|
2
|
61 @Get("html")
|
|
62 public Representation doGetHTML(Representation entity){
|
|
63
|
|
64 doOptions(entity);
|
|
65 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
66 String uri = form.getFirstValue("uri");
|
|
67 String user = form.getFirstValue("user");
|
|
68
|
|
69 String limit=form.getFirstValue("limit");
|
|
70 String offset=form.getFirstValue("offset");
|
|
71
|
|
72
|
|
73 //
|
|
74 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
|
|
75
|
|
76 String retString="<html><body><table>";
|
3
|
77 String lineFormat="<tr><td><a href=\"%s\">%s</a></td>" +
|
2
|
78 "<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>";
|
|
79 try {
|
|
80
|
|
81 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
|
|
82
|
|
83 for (Convert.Annotation annot:annots){
|
|
84
|
|
85
|
|
86 RestServer restServer = (RestServer) getApplication();
|
|
87 String userName=restServer.getUserNameFromLdap(annot.creator);
|
|
88 List<String> xpointer = new ArrayList<String>();
|
|
89
|
|
90 if (annot.xpointers==null || annot.xpointers.size()==0)
|
3
|
91 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri);
|
2
|
92 else {
|
|
93 for(String xpointerString:annot.xpointers){
|
3
|
94 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri);
|
2
|
95 }
|
|
96 }
|
|
97
|
|
98 }
|
|
99 } catch (TripleStoreHandlerException e) {
|
|
100 // TODO Auto-generated catch block
|
|
101 e.printStackTrace();
|
|
102 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
|
|
103 return null;
|
|
104 } catch (TripleStoreSearchError e) {
|
|
105 // TODO Auto-generated catch block
|
|
106 e.printStackTrace();
|
|
107 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
|
|
108 return null;
|
|
109 }
|
|
110
|
|
111 retString+="</table></body></html>";
|
|
112
|
|
113 logger.debug("sending:");
|
|
114 logger.debug(retString);
|
|
115 return new StringRepresentation(retString,MediaType.TEXT_HTML);
|
|
116 }
|
|
117
|
3
|
118 /**
|
|
119 * Erzeugt aus einer Annotation, das fŸr den Annotator notwendige JSON-Format
|
|
120 * @param annot
|
|
121 * @return
|
|
122 */
|
|
123 public JSONObject annot2AnnotatorJSON(Convert.Annotation annot){
|
|
124 JSONObject jo = new JSONObject();
|
|
125 try {
|
|
126 jo.put("text", annot.text);
|
|
127 jo.put("uri",annot.url);
|
|
128
|
|
129 JSONObject userObject= new JSONObject();
|
|
130 userObject.put("id",annot.creator);
|
|
131
|
|
132 RestServer restServer = (RestServer) getApplication();
|
|
133
|
|
134 String userID= annot.creator;
|
|
135 if (userID.startsWith(NS.MPIWG_PERSONS)){
|
|
136 userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
|
|
137 }
|
|
138 String userName=restServer.getUserNameFromLdap(userID);
|
|
139 userObject.put("name",userName);
|
|
140
|
|
141 jo.put("user",userObject);
|
|
142
|
|
143 List<String> xpointer = new ArrayList<String>();
|
|
144
|
|
145 if (annot.xpointers==null || annot.xpointers.size()==0)
|
|
146 xpointer.add(annot.xpointer);
|
|
147 else {
|
|
148 for(String xpointerString:annot.xpointers){
|
|
149 xpointer.add(xpointerString);
|
|
150 }
|
|
151 }
|
|
152 jo.put("ranges", transformToRanges(xpointer));
|
|
153 jo.put("id", annot.annotationUri);
|
|
154 return jo;
|
|
155 } catch (JSONException e) {
|
|
156 // TODO Auto-generated catch block
|
|
157 e.printStackTrace();
|
|
158 return null;
|
|
159 }
|
|
160 }
|
2
|
161
|
0
|
162 @Get("json")
|
2
|
163 public Representation doGetJSON(Representation entity){
|
0
|
164
|
|
165 doOptions(entity);
|
|
166 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
167 String uri = form.getFirstValue("uri");
|
|
168 String user = form.getFirstValue("user");
|
|
169
|
|
170 String limit=form.getFirstValue("limit");
|
|
171 String offset=form.getFirstValue("offset");
|
|
172
|
|
173
|
|
174 //
|
|
175 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
|
|
176
|
|
177 JSONArray ja;
|
|
178 try {
|
|
179
|
|
180 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
|
|
181
|
|
182 ja = new JSONArray();
|
|
183 for (Convert.Annotation annot:annots){
|
3
|
184 // JSONObject jo = new JSONObject();
|
|
185 // jo.put("text", annot.text);
|
|
186 // jo.put("uri",annot.url);
|
|
187 //
|
|
188 // JSONObject userObject= new JSONObject();
|
|
189 // userObject.put("id",annot.creator);
|
|
190 //
|
|
191 // RestServer restServer = (RestServer) getApplication();
|
|
192 //
|
|
193 // String userID= annot.creator;
|
|
194 // if (userID.startsWith(NS.MPIWG_PERSONS)){
|
|
195 // userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
|
|
196 // }
|
|
197 // String userName=restServer.getUserNameFromLdap(userID);
|
|
198 // userObject.put("name",userName);
|
|
199 //
|
|
200 // jo.put("user",userObject);
|
|
201 //
|
|
202 // List<String> xpointer = new ArrayList<String>();
|
|
203 //
|
|
204 // if (annot.xpointers==null || annot.xpointers.size()==0)
|
|
205 // xpointer.add(annot.xpointer);
|
|
206 // else {
|
|
207 // for(String xpointerString:annot.xpointers){
|
|
208 // xpointer.add(xpointerString);
|
|
209 // }
|
|
210 // }
|
|
211 // jo.put("ranges", transformToRanges(xpointer));
|
|
212 JSONObject jo = annot2AnnotatorJSON(annot);
|
|
213 if (jo!=null){
|
|
214 ja.put(annot2AnnotatorJSON(annot));
|
|
215 } else {
|
|
216 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
|
|
217 return null;
|
2
|
218 }
|
0
|
219 }
|
|
220 } catch (TripleStoreHandlerException e) {
|
|
221 // TODO Auto-generated catch block
|
|
222 e.printStackTrace();
|
|
223 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
|
|
224 return null;
|
|
225 } catch (TripleStoreSearchError e) {
|
|
226 // TODO Auto-generated catch block
|
|
227 e.printStackTrace();
|
|
228 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
|
|
229 return null;
|
3
|
230 }
|
0
|
231
|
|
232 JSONObject retObject = new JSONObject();
|
|
233 try {
|
|
234 retObject.put("rows",ja);
|
|
235 retObject.put("total",ja.length());
|
|
236 } catch (JSONException e) {
|
|
237 // TODO Auto-generated catch block
|
|
238 e.printStackTrace();
|
|
239 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
|
|
240 return null;
|
|
241 }
|
|
242
|
|
243 logger.debug("sending:");
|
|
244 logger.debug(retObject);
|
|
245 return new JsonRepresentation(retObject);
|
|
246 }
|
|
247
|
|
248 private JSONArray transformToRanges(List<String> xpointers) {
|
|
249
|
|
250 JSONArray ja = new JSONArray();
|
|
251
|
|
252 Pattern rg = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
|
|
253 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
|
|
254
|
|
255
|
|
256
|
|
257 try {
|
|
258 for(String xpointer:xpointers){
|
|
259 String decoded =URLDecoder.decode(xpointer,"utf-8");
|
|
260 Matcher m=rg.matcher(decoded);
|
1
|
261
|
|
262 if (m.find()){
|
0
|
263 {
|
|
264 JSONObject jo = new JSONObject();
|
|
265 jo.put("start", m.group(1));
|
|
266 jo.put("startOffset", m.group(2));
|
|
267 jo.put("end", m.group(3));
|
|
268 jo.put("endOffset", m.group(4));
|
|
269 ja.put(jo);
|
1
|
270 }
|
0
|
271 }
|
|
272 m=rg1.matcher(xpointer);
|
1
|
273 if (m.find()){
|
0
|
274 JSONObject jo = new JSONObject();
|
|
275 jo.put("start", m.group(1));
|
|
276 jo.put("startOffset", m.group(2));
|
|
277
|
|
278 ja.put(jo);
|
|
279 }
|
|
280
|
|
281
|
|
282 }
|
|
283 } catch (JSONException e) {
|
|
284 // TODO Auto-generated catch block
|
|
285 e.printStackTrace();
|
|
286 } catch (UnsupportedEncodingException e) {
|
|
287 // TODO Auto-generated catch block
|
|
288 e.printStackTrace();
|
|
289 }
|
|
290
|
|
291
|
|
292 return ja;
|
|
293
|
|
294
|
|
295
|
|
296
|
|
297
|
|
298 }
|
|
299
|
3
|
300
|
|
301 /**
|
|
302 *
|
|
303 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt
|
|
304 * (incl. der URL des Dokumentes) text: text der annotation annoturl: url
|
|
305 * auf eine Annotation falls extern
|
|
306 *
|
|
307 * @return
|
|
308 */
|
|
309
|
0
|
310 @Post("json")
|
|
311 public Representation doPostJson(Representation entity) {
|
|
312
|
3
|
313 Annotation retVal = doPost(entity);
|
|
314
|
|
315
|
|
316 // JSONObject jo;
|
|
317 // try {
|
|
318 // jo = new JSONObject("{\"annotUrl\":\"" + retVal + "\"}");
|
|
319 // } catch (JSONException e) {
|
|
320 // setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
321 // return null;
|
|
322 // }
|
|
323
|
|
324 if (retVal==null)
|
0
|
325 return null;
|
3
|
326 JSONObject jo = annot2AnnotatorJSON(retVal);
|
0
|
327 JsonRepresentation retRep = new JsonRepresentation(jo);
|
|
328 return retRep;
|
|
329 }
|
|
330
|
|
331 @Post("html")
|
|
332 public Representation doPostHtml(Representation entity) {
|
3
|
333 Annotation retValAnnot = doPost(entity);
|
|
334 String retVal=retValAnnot.annotationUri;
|
0
|
335 if (retVal == null) {
|
|
336 return null;
|
|
337 }
|
|
338 String text = String.format(
|
|
339 "<html><body><a href=\"%s\">%s</a></body></html>", retVal
|
|
340 .replace(">", "").replace("<", ""),
|
|
341 retVal.replace(">", ">").replace("<", "<"));
|
|
342 Representation retRep = new StringRepresentation(text,
|
|
343 MediaType.TEXT_HTML);
|
|
344 return retRep;
|
|
345 }
|
|
346
|
3
|
347 public Convert.Annotation doPost(Representation entity) {
|
|
348
|
|
349 doOptions(entity);
|
0
|
350 Convert.Annotation annot;
|
|
351 // versuche basic authentifizierung und hole den Benutzer von dort.
|
|
352
|
|
353 // User authUser;= handleBasicAuthentification(entity);
|
|
354
|
|
355 if (entity.getMediaType().equals(MediaType.APPLICATION_JSON)) {
|
|
356
|
|
357 JsonRepresentation jrep;
|
|
358 try {
|
|
359 jrep = new JsonRepresentation(entity);
|
|
360 } catch (IOException e1) {
|
|
361 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
362 return null;
|
|
363 }
|
|
364
|
|
365 // try {
|
|
366 // logger.debug(jrep.getText());
|
|
367 // } catch (IOException e1) {
|
|
368 // // TODO Auto-generated catch block
|
|
369 // e1.printStackTrace();
|
|
370 // }
|
|
371 //
|
|
372
|
|
373 try {
|
|
374 JSONObject jo = jrep.getJsonObject();
|
|
375 String mode = jo.getString("mode"); // hole modus
|
|
376 if (mode==null || mode.equals(""))
|
|
377 mode="annotea"; // default mode (annotea) TODO make this configurable
|
|
378
|
|
379 if (mode.equals("annotator") ) { // annotator format
|
|
380 annot = handleAnnotatorSchema(jo, entity);
|
|
381 logger.debug("storing annotator object");
|
|
382 logger.debug(jo);
|
|
383 } else if (mode.equals("annotea")){
|
|
384 annot = handleAnnotea(jo, entity);
|
|
385 } else {
|
|
386 setStatus(Status.CLIENT_ERROR_BAD_REQUEST,"mode "+mode+"not supported!");
|
|
387 return null;
|
|
388 }
|
|
389
|
|
390 } catch (JSONException e) {
|
|
391 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
392 return null;
|
|
393 }
|
|
394
|
|
395 } else if (entity.getMediaType().equals(MediaType.APPLICATION_WWW_FORM)) {
|
|
396 annot = handleForm(entity);
|
|
397
|
|
398 } else {
|
|
399 setStatus(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE);
|
|
400
|
|
401 return null;
|
|
402 }
|
|
403
|
|
404 if (annot==null){
|
|
405 return null;
|
|
406 }
|
|
407 if (annot.xpointer == null || annot.creator == null) {
|
|
408 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
409
|
|
410 return null;
|
|
411 }
|
|
412
|
|
413
|
|
414
|
|
415 try {
|
|
416 return new Convert("file:///annotations").storeAnnotation(annot);
|
|
417 } catch (TripleStoreStoreError e) {
|
|
418 e.printStackTrace();
|
|
419 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
|
|
420 return null;
|
|
421 }
|
|
422 }
|
|
423
|
2
|
424
|
|
425 /**
|
|
426 *
|
|
427 * @param entity should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
|
|
428 *
|
|
429 * username,password is optional, if not given BasicAuthentification is used.
|
|
430 *
|
|
431 *
|
|
432 *
|
|
433 * 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
|
|
434 *
|
|
435 * @return
|
|
436 */
|
0
|
437 protected Convert.Annotation handleForm(Representation entity) {
|
|
438 Convert.Annotation annot;
|
|
439 Form form = new Form(entity);
|
|
440 String username = form.getValues("username");
|
|
441 String password = form.getValues("password");
|
|
442 String xpointer = form.getValues("xpointer");
|
|
443 String text = form.getValues("text");
|
|
444 String url = form.getValues("url");
|
|
445 String type = form.getValues("type");
|
|
446 RestServer restServer = (RestServer) getApplication();
|
|
447
|
|
448 // falls user and password nicht null sind:
|
|
449 User userFromForm = null;
|
|
450 if (username != null && password != null) {
|
|
451 if (restServer.authenticate(username, password, getRequest())) {
|
|
452 userFromForm = new User(username);
|
|
453 }
|
|
454 }
|
|
455 User authUser = null;
|
|
456
|
|
457 if (userFromForm == null) {
|
|
458 authUser = handleBasicAuthentification(entity);
|
|
459 }
|
|
460
|
|
461 // weder BasicAuth noch FormAuth
|
|
462 if (authUser == null && userFromForm == null) {
|
|
463 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
464 return null;
|
|
465 }
|
|
466
|
|
467 if (userFromForm != null) {
|
|
468 username = userFromForm.getIdentifier();
|
|
469 } else {
|
|
470 username = authUser.getIdentifier();
|
|
471 }
|
|
472
|
2
|
473 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
474 if (!username.startsWith("http"))
|
|
475 username=NS.MPIWG_PERSONS+username;
|
|
476
|
0
|
477 annot = new Convert.Annotation(xpointer, username, null, text,
|
|
478 type, url);
|
|
479 return annot;
|
|
480 }
|
|
481
|
|
482 @Post
|
|
483 public Representation doPostHtml2(Representation entity) {
|
|
484 return doPostHtml(entity);
|
|
485 }
|
|
486
|
|
487 private User handleBasicAuthentification(Representation entity) {
|
|
488 RestServer restServer = (RestServer) getApplication();
|
|
489 if (!restServer.authenticate(getRequest(), getResponse())) {
|
|
490 // Not authenticated
|
|
491 return null;
|
|
492 }
|
|
493
|
|
494 ClientInfo ci = getRequest().getClientInfo();
|
|
495 logger.debug(ci);
|
|
496 return getRequest().getClientInfo().getUser();
|
|
497
|
|
498 }
|
|
499
|
|
500 /**
|
|
501 * using a minimal annotation format based on the annotea specification
|
|
502 *
|
|
503 * @param jo
|
|
504 * must contain xpointer, text,url,type and can contain a
|
|
505 * username, if not the username form the authentification will
|
|
506 * be used.
|
|
507 * @param authUser
|
|
508 * user object
|
2
|
509 * 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
|
|
510
|
0
|
511 * @return
|
|
512 * @throws JSONException
|
|
513 */
|
|
514 public Annotation handleAnnotea(JSONObject jo, Representation entity)
|
|
515 throws JSONException {
|
|
516
|
|
517 User authUser = handleBasicAuthentification(entity);
|
|
518 String username = jo.getString("username"); // not required, if no
|
|
519 // username given authuser
|
|
520 // will be used.
|
|
521 String xpointer = jo.getString("xpointer");
|
|
522 String text = null;
|
|
523 if (jo.has("text"))
|
|
524 text = jo.getString("text");
|
|
525
|
|
526 String url = null;
|
|
527 if (jo.has("url"))
|
|
528 url = jo.getString("url");
|
|
529
|
|
530 String type = null;
|
|
531 if (jo.has("type"))
|
|
532 type = jo.getString("type");
|
|
533
|
|
534 if (username == null)
|
|
535 username = authUser.getIdentifier();
|
|
536
|
2
|
537 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
538 if (!username.startsWith("http"))
|
|
539 username=NS.MPIWG_PERSONS+username;
|
|
540
|
0
|
541 return new Convert.Annotation(xpointer, username, null, text, type, url);
|
|
542 }
|
|
543
|
|
544 /**
|
|
545 * uses the specification from the annotator project.
|
|
546 *
|
3
|
547 * @see{https://github.com/okfn/annotator/wiki/Annotation-format} The user
|
0
|
548 * object must
|
|
549 * contain an
|
|
550 * id and
|
|
551 * password or
|
|
552 * basic
|
|
553 * authentification
|
|
554 * is used.
|
2
|
555 * 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
|
0
|
556 * @param jo
|
|
557 * @param authUser
|
|
558 * @return
|
|
559 * @throws JSONException
|
|
560 */
|
|
561 public Convert.Annotation handleAnnotatorSchema(JSONObject jo,
|
|
562 Representation entity) throws JSONException {
|
|
563 Convert.Annotation annot;
|
|
564 String url = jo.getString("uri");
|
|
565 String text = jo.getString("text");
|
|
566
|
|
567 String username = null;
|
|
568 if (jo.has("user")) { // not required, if no username given authuser
|
|
569 // will be used otherwise username and password
|
|
570 // has to be submitted
|
|
571 JSONObject user = jo.getJSONObject("user");
|
|
572 if (user.has("id")) {
|
|
573 username = user.getString("id");
|
|
574 if(!user.has("password")){
|
|
575 User authUser = handleBasicAuthentification(entity);
|
|
576 if (authUser==null){
|
|
577 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
578 return null;
|
|
579 }
|
|
580 username = authUser.getIdentifier();
|
|
581 } else {
|
|
582 String password = user.getString("password");
|
|
583 if (!((RestServer) getApplication()).authenticate(username,
|
|
584 password, getRequest())) {
|
|
585 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
586 return null;
|
|
587 }
|
|
588 }
|
|
589 }
|
|
590
|
|
591 } else {
|
|
592 User authUser = handleBasicAuthentification(entity);
|
|
593 if (authUser == null) {
|
|
594 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
595 return null;
|
|
596 }
|
|
597 username = authUser.getIdentifier();
|
|
598 }
|
|
599
|
|
600 String xpointer;
|
|
601 if (jo.has("ranges")) {
|
|
602 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
|
|
603 String start = ranges.getString("start");
|
|
604 String end = ranges.getString("end");
|
|
605 String startOffset = ranges.getString("startOffset");
|
|
606 String endOffset = ranges.getString("endOffset");
|
|
607
|
|
608 try {
|
|
609 xpointer = url+"#"+
|
|
610 URLEncoder.encode(String.format(
|
|
611 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))",
|
|
612 start, startOffset, end, endOffset),"utf-8");
|
|
613 } catch (UnsupportedEncodingException e) {
|
|
614 e.printStackTrace();
|
|
615 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
616 return null;
|
|
617 }
|
|
618 } else {
|
|
619 xpointer = url;
|
|
620 }
|
2
|
621
|
|
622 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
623 if (!username.startsWith("http"))
|
|
624 username=NS.MPIWG_PERSONS+username;
|
|
625
|
0
|
626 return new Convert.Annotation(xpointer, username, null, text, null);
|
|
627 }
|
|
628
|
|
629 }
|