Changeset 61:b8ef15c8c4a5 in AnnotationManagerN4J
- Timestamp:
- Nov 22, 2012, 4:38:53 PM (12 years ago)
- Branch:
- default
- Location:
- src/main/java/de/mpiwg/itgroup/annotations/restlet
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r41 r61 55 55 logger.debug("annotation-id=" + id); 56 56 57 58 57 // do authentication 59 58 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); … … 61 60 62 61 if (id == null) { 63 64 62 return getAllAnnotations(authUser); 65 63 } 66 64 67 68 65 AnnotationStore store = getAnnotationStore(); 69 66 Annotation annot = store.getAnnotationById(id); 70 67 if (annot != null) { 71 if (! 68 if (!annot.isActionAllowed("read", authUser, store)) { 72 69 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 73 70 return null; … … 85 82 86 83 private Representation getAllAnnotations(Person authUser) { 87 88 89 String sortBy=null;90 91 if (parameter.getName().equals("sortBy")){92 sortBy =parameter.getValue();93 94 95 84 85 Form form = getRequest().getResourceRef().getQueryAsForm(); 86 String sortBy = null; 87 for (Parameter parameter : form) { 88 if (parameter.getName().equals("sortBy")) { 89 sortBy = parameter.getValue(); 90 } 91 } 92 96 93 AnnotationStore store = getAnnotationStore(); 97 94 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); 98 99 95 96 List<Annotation> annotations = store.getAnnotations(null, null); 100 97 for (Annotation annotation : annotations) { 101 //check permission 102 if (!annotation.isActionAllowed("read", authUser, store)) continue; 103 104 JSONObject jo = createAnnotatorJson(annotation,false); 105 results.add(jo); 106 107 } 108 109 if (sortBy!=null){ 110 JSONObjectComparator.sortAnnotations(results,sortBy); 111 } 112 113 JSONArray resultsJa = new JSONArray(); 114 for (JSONObject result:results){ 115 resultsJa.put(result); 116 } 117 98 // check permission 99 if (!annotation.isActionAllowed("read", authUser, store)) continue; 100 101 JSONObject jo = createAnnotatorJson(annotation, false); 102 results.add(jo); 103 } 104 105 if (sortBy != null) { 106 JSONObjectComparator.sortAnnotations(results, sortBy); 107 } 108 109 JSONArray resultsJa = new JSONArray(); 110 for (JSONObject result : results) { 111 resultsJa.put(result); 112 } 113 118 114 // assemble result object 119 115 JSONObject result = new JSONObject(); … … 130 126 } 131 127 132 133 134 135 136 /** 128 /** 137 129 * POST with JSON content-type. Creates a new Annotation. 138 130 * … … 144 136 // set headers 145 137 setCorsHeaders(); 146 147 // do authentication TODO: who's allowed to create? 138 139 // do authentication TODO: who's allowed to create? 148 140 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 149 141 logger.debug("request authenticated=" + authUser); … … 222 214 return null; 223 215 } 224 if (! 216 if (!storedAnnot.isActionAllowed("update", authUser, store)) { 225 217 setStatus(Status.CLIENT_ERROR_FORBIDDEN); 226 218 return null; … … 272 264 Annotation annot = store.getAnnotationById(id); 273 265 if (annot != null) { 274 if (! 266 if (!annot.isActionAllowed("delete", authUser, store)) { 275 267 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 276 268 return null; 277 269 } 278 270 } 279 271 280 272 // delete annotation 281 273 store.deleteAnnotationById(id); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r58 r61 133 133 /** 134 134 * Checks Annotator Auth plugin authentication information from headers. 135 * Returns userId if successful. Returns "anonymous" in non-authorization mode. 135 * Returns userId if successful. Returns "anonymous" in non-authorization 136 * mode. 136 137 * 137 138 * @param entity … … 184 185 * 185 186 * @param annot 186 * @param forAnonymous TODO 187 * @param forAnonymous 188 * TODO 187 189 * @return 188 190 */ … … 236 238 jo.put("ranges", transformToRanges(fragments)); 237 239 } else if (xt == FragmentTypes.AREA) { 238 jo.put(" areas", transformToAreas(fragments));239 } 240 } 241 240 jo.put("shapes", transformToShapes(fragments)); 241 } 242 } 243 242 244 /* 243 245 * permissions … … 282 284 readPerms.put(readPerm.getIdString()); 283 285 } 284 286 285 287 /* 286 288 * tags 287 289 */ 288 Set<String> tagset = annot.getTags(); 290 Set<String> tagset = annot.getTags(); 289 291 if (tagset != null) { 290 292 JSONArray tags = new JSONArray(); … … 294 296 } 295 297 } 296 298 297 299 /* 298 300 * id … … 304 306 return jo; 305 307 } catch (JSONException e) { 306 // TODO Auto-generated catch block 307 e.printStackTrace(); 308 logger.error("Unable to create AnnotatorJSON!", e); 308 309 } 309 310 return null; … … 311 312 312 313 private JSONArray transformToRanges(List<String> xpointers) { 313 314 314 JSONArray ja = new JSONArray(); 315 316 315 Pattern rg = Pattern 317 316 .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)"); 318 317 Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)"); 319 320 318 try { 321 319 for (String xpointer : xpointers) { … … 323 321 String decoded = xpointer; 324 322 Matcher m = rg.matcher(decoded); 325 326 323 if (m.find()) { 327 { 328 JSONObject jo = new JSONObject(); 329 jo.put("start", m.group(1)); 330 jo.put("startOffset", m.group(2)); 331 jo.put("end", m.group(3)); 332 jo.put("endOffset", m.group(4)); 333 ja.put(jo); 334 } 324 JSONObject jo = new JSONObject(); 325 jo.put("start", m.group(1)); 326 jo.put("startOffset", m.group(2)); 327 jo.put("end", m.group(3)); 328 jo.put("endOffset", m.group(4)); 329 ja.put(jo); 335 330 } 336 331 m = rg1.matcher(xpointer); … … 339 334 jo.put("start", m.group(1)); 340 335 jo.put("startOffset", m.group(2)); 341 342 336 ja.put(jo); 343 337 } 344 338 } 345 339 } catch (JSONException e) { 346 // TODO Auto-generated catch block 347 e.printStackTrace(); 340 logger.error("Unable to transform to ranges!", e); 348 341 } 349 342 return ja; 350 343 } 351 344 352 private JSONArray transformToAreas(List<String> xpointers) { 353 345 private JSONArray transformToShapes(List<String> xpointers) { 354 346 JSONArray ja = new JSONArray(); 355 356 Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); 357 347 Pattern rg = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); 358 348 try { 359 349 for (String xpointer : xpointers) { 360 // String decoded = URLDecoder.decode(xpointer, "utf-8");361 350 String decoded = xpointer; 362 351 Matcher m = rg.matcher(decoded); 363 364 352 if (m.find()) { 365 { 366 JSONObject jo = new JSONObject(); 367 @SuppressWarnings("unused") 368 String unit = m.group(1); 369 jo.put("x", m.group(2)); 370 jo.put("y", m.group(3)); 371 jo.put("width", m.group(4)); 372 jo.put("height", m.group(5)); 373 ja.put(jo); 353 String units = m.group(1); 354 float x = getFloat(m.group(2)); 355 float y = getFloat(m.group(3)); 356 float width = getFloat(m.group(4)); 357 float height = getFloat(m.group(5)); 358 JSONObject shape = new JSONObject(); 359 JSONObject geom = new JSONObject(); 360 geom.put("units", units); 361 geom.put("x", x); 362 geom.put("y", y); 363 if (width == 0 || height == 0) { 364 shape.put("type", "point"); 365 shape.put("geometry", geom); 366 } else { 367 shape.put("type", "rectangle"); 368 geom.put("width", width); 369 geom.put("height", height); 370 shape.put("geometry", geom); 374 371 } 372 ja.put(shape); 375 373 } 376 374 } 377 375 } catch (JSONException e) { 378 // TODO Auto-generated catch block 379 e.printStackTrace(); 376 logger.error("Unable to transform to shapes!", e); 380 377 } 381 378 return ja; 379 } 380 381 protected String parseShape(JSONObject shape) throws JSONException { 382 String fragment = null; 383 String type = shape.getString("type"); 384 JSONObject geom = shape.getJSONObject("geometry"); 385 if (type.equalsIgnoreCase("point")) { 386 String x = geom.getString("x"); 387 String y = geom.getString("y"); 388 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y); 389 } else if (type.equalsIgnoreCase("point")) { 390 String x = geom.getString("x"); 391 String y = geom.getString("y"); 392 String width = shape.getString("width"); 393 String height = shape.getString("height"); 394 fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); 395 } else { 396 logger.error("Unable to parse this shape: " + shape); 397 } 398 return fragment; 382 399 } 383 400 … … 539 556 540 557 /* 541 * create xpointerfrom the first range/area558 * create fragment from the first range/area 542 559 */ 543 560 try { … … 551 568 // nothing to do 552 569 } 570 try { 571 if (jo.has("shapes")) { 572 JSONObject shapes = jo.getJSONArray("shapes").getJSONObject(0); 573 annot.setFragmentType(FragmentTypes.AREA); 574 String fragment = parseShape(shapes); 575 annot.setTargetFragment(fragment); 576 } 577 } catch (JSONException e) { 578 // nothing to do 579 } 580 // deprecated areas type 553 581 try { 554 582 if (jo.has("areas")) { … … 601 629 } 602 630 603 604 631 return annot; 605 632 } 606 633 607 @SuppressWarnings("unused") // i in for loop 634 @SuppressWarnings("unused") 635 // i in for loop 608 636 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException { 609 637 Actor actor = null; … … 622 650 } 623 651 652 public float getFloat(String s) { 653 try { 654 return Float.parseFloat(s); 655 } catch (NumberFormatException e) { 656 } 657 return 0f; 658 } 624 659 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java
r50 r61 18 18 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 19 19 20 21 20 /** 22 * API for accessing tags in the Annotation store.21 * API for accessing resource objects in the Annotation store. 23 22 * 24 23 * @author dwinter 25 * 24 * 26 25 */ 27 26 public class AnnotatorResources extends AnnotatorResourceImpl { … … 31 30 32 31 /** 33 * GET with JSON content-type. 34 * Parameters: 35 * user: short user name 36 * uri: user uri 37 * 32 * GET with JSON content-type. 33 * Parameters: user: short user name uri: user uri 34 * 38 35 * @param entity 39 36 * @return … … 41 38 @Get("json") 42 39 public Representation doGetJSON(Representation entity) { 43 logger.debug("Annotator Groups doGetJSON!");40 logger.debug("AnnotatorResources doGetJSON!"); 44 41 setCorsHeaders(); 45 42 46 43 String jsonId = (String) getRequest().getAttributes().get("id"); 47 44 String uri = decodeJsonId(jsonId); 48 45 49 46 logger.debug("resources-id=" + uri); 50 47 51 if (uri ==null){52 return getAllResources();48 if (uri == null) { 49 return getAllResources(); 53 50 } else { 54 55 return getResource(uri); 51 return getResource(uri); 56 52 } 57 53 } 58 59 protected Representation getResource(String uri){ 60 AnnotationStore store = getAnnotationStore(); 61 //String tagUri=NS.MPIWG_TAGS_URL+id; 62 Node tagNode = store.getResourceNodeByUri(uri); 63 Resource resource = store.createResourceFromNode(tagNode); 64 JSONObject jo = new JSONObject(); 65 try { 66 jo.put("id", encodeJsonId(resource.getUri())); 67 jo.put("uri", resource.getUri()); 68 } catch (JSONException e) { 69 } 70 71 return new JsonRepresentation(jo); 54 55 protected Representation getResource(String uri) { 56 AnnotationStore store = getAnnotationStore(); 57 Node resNode = store.getResourceNodeByUri(uri); 58 Resource resource = store.createResourceFromNode(resNode); 59 JSONObject jo = new JSONObject(); 60 try { 61 jo.put("id", encodeJsonId(resource.getUri())); 62 jo.put("uri", resource.getUri()); 63 } catch (JSONException e) { 64 } 65 66 return new JsonRepresentation(jo); 72 67 } 73 protected Representation getAllResources() { 74 JSONArray results = new JSONArray(); 68 69 protected Representation getAllResources() { 70 JSONArray results = new JSONArray(); 75 71 AnnotationStore store = getAnnotationStore(); 76 77 78 List<Resource> resources = store.getResources(null, null); 79 for (Resource resource : resources) { 80 JSONObject jo = new JSONObject(); 81 try { 82 jo.put("id", encodeJsonId(resource.getUri())); 83 jo.put("uri", resource.getUri()); 84 } catch (JSONException e) { 85 } 86 results.put(jo); 72 73 List<Resource> resources = store.getResources(null, null); 74 for (Resource resource : resources) { 75 JSONObject jo = new JSONObject(); 76 try { 77 jo.put("id", encodeJsonId(resource.getUri())); 78 jo.put("uri", resource.getUri()); 79 } catch (JSONException e) { 87 80 } 88 81 results.put(jo); 82 } 83 89 84 // assemble result object 90 85 JSONObject result = new JSONObject(); … … 99 94 logger.debug(result); 100 95 return new JsonRepresentation(result); 101 } 102 96 } 103 97 104 105 98 }
Note: See TracChangeset
for help on using the changeset viewer.