Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 10:90911b2da322
more work on permissions...
author | casties |
---|---|
date | Thu, 12 Jul 2012 17:01:32 +0200 |
parents | b2bfc3bc9ba8 |
children | 629e15b345aa |
comparison
equal
deleted
inserted
replaced
9:b2bfc3bc9ba8 | 10:90911b2da322 |
---|---|
34 import org.restlet.resource.ServerResource; | 34 import org.restlet.resource.ServerResource; |
35 | 35 |
36 import de.mpiwg.itgroup.annotations.Actor; | 36 import de.mpiwg.itgroup.annotations.Actor; |
37 import de.mpiwg.itgroup.annotations.Annotation; | 37 import de.mpiwg.itgroup.annotations.Annotation; |
38 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes; | 38 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes; |
39 import de.mpiwg.itgroup.annotations.Group; | |
40 import de.mpiwg.itgroup.annotations.Person; | |
39 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | 41 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
40 import de.mpiwg.itgroup.annotations.old.NS; | 42 import de.mpiwg.itgroup.annotations.old.NS; |
41 | 43 |
42 /** | 44 /** |
43 * Base class for Annotator resource classes. | 45 * Base class for Annotator resource classes. |
186 jo.put("uri", annot.getTargetBaseUri()); | 188 jo.put("uri", annot.getTargetBaseUri()); |
187 | 189 |
188 if (makeUserObject) { | 190 if (makeUserObject) { |
189 // create user object | 191 // create user object |
190 JSONObject userObject = new JSONObject(); | 192 JSONObject userObject = new JSONObject(); |
193 Actor creator = annot.getCreator(); | |
191 // save creator as uri | 194 // save creator as uri |
192 userObject.put("uri", annot.getCreatorUri()); | 195 userObject.put("uri", creator.getUri()); |
193 // make short user id | 196 // make short user id |
194 String userId = annot.getCreatorUri(); | 197 String userId = creator.getIdString(); |
195 // remove namespace from user uri to get id | |
196 if (userId != null && userId.startsWith(NS.MPIWG_PERSONS_URL)) { | |
197 userId = userId.replace(NS.MPIWG_PERSONS_URL, ""); | |
198 } | |
199 // set as id | 198 // set as id |
200 userObject.put("id", userId); | 199 userObject.put("id", userId); |
201 // get full name | 200 // get full name |
202 String userName = annot.getCreatorName(); | 201 String userName = creator.getName(); |
203 if (userName == null) { | 202 if (userName == null) { |
204 RestServer restServer = (RestServer) getApplication(); | 203 RestServer restServer = (RestServer) getApplication(); |
205 userName = restServer.getFullNameFromLdap(userId); | 204 userName = restServer.getFullNameFromLdap(userId); |
206 } | 205 } |
207 userObject.put("name", userName); | 206 userObject.put("name", userName); |
221 jo.put("ranges", transformToRanges(fragments)); | 220 jo.put("ranges", transformToRanges(fragments)); |
222 } else if (xt == FragmentTypes.AREA) { | 221 } else if (xt == FragmentTypes.AREA) { |
223 jo.put("areas", transformToAreas(fragments)); | 222 jo.put("areas", transformToAreas(fragments)); |
224 } | 223 } |
225 } | 224 } |
225 | |
226 // permissions | |
227 JSONObject perms = new JSONObject(); | |
228 jo.put("permissions", perms); | |
229 // admin | |
230 JSONArray adminPerms = new JSONArray(); | |
231 perms.put("admin", adminPerms); | |
232 Actor adminPerm = annot.getAdminPermission(); | |
233 if (adminPerm != null) { | |
234 adminPerms.put(adminPerm.getIdString()); | |
235 } | |
236 // delete | |
237 JSONArray deletePerms = new JSONArray(); | |
238 perms.put("delete", deletePerms); | |
239 Actor deletePerm = annot.getDeletePermission(); | |
240 if (deletePerm != null) { | |
241 deletePerms.put(deletePerm.getIdString()); | |
242 } | |
243 // update | |
244 JSONArray updatePerms = new JSONArray(); | |
245 perms.put("update", updatePerms); | |
246 Actor updatePerm = annot.getUpdatePermission(); | |
247 if (updatePerm != null) { | |
248 updatePerms.put(updatePerm.getIdString()); | |
249 } | |
250 // read | |
251 JSONArray readPerms = new JSONArray(); | |
252 perms.put("read", readPerms); | |
253 Actor readPerm = annot.getReadPermission(); | |
254 if (readPerm != null) { | |
255 readPerms.put(readPerm.getIdString()); | |
256 } | |
257 | |
226 // encode Annotation URL (=id) in base64 | 258 // encode Annotation URL (=id) in base64 |
227 String annotUrl = annot.getUri(); | 259 String annotUrl = annot.getUri(); |
228 String annotId = encodeJsonId(annotUrl); | 260 String annotId = encodeJsonId(annotUrl); |
229 jo.put("id", annotId); | 261 jo.put("id", annotId); |
230 return jo; | 262 return jo; |
243 .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)"); | 275 .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)"); |
244 Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)"); | 276 Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)"); |
245 | 277 |
246 try { | 278 try { |
247 for (String xpointer : xpointers) { | 279 for (String xpointer : xpointers) { |
248 //String decoded = URLDecoder.decode(xpointer, "utf-8"); | 280 // String decoded = URLDecoder.decode(xpointer, "utf-8"); |
249 String decoded = xpointer; | 281 String decoded = xpointer; |
250 Matcher m = rg.matcher(decoded); | 282 Matcher m = rg.matcher(decoded); |
251 | 283 |
252 if (m.find()) { | 284 if (m.find()) { |
253 { | 285 { |
281 | 313 |
282 Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); | 314 Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); |
283 | 315 |
284 try { | 316 try { |
285 for (String xpointer : xpointers) { | 317 for (String xpointer : xpointers) { |
286 //String decoded = URLDecoder.decode(xpointer, "utf-8"); | 318 // String decoded = URLDecoder.decode(xpointer, "utf-8"); |
287 String decoded = xpointer; | 319 String decoded = xpointer; |
288 Matcher m = rg.matcher(decoded); | 320 Matcher m = rg.matcher(decoded); |
289 | 321 |
290 if (m.find()) { | 322 if (m.find()) { |
291 { | 323 { |
292 JSONObject jo = new JSONObject(); | 324 JSONObject jo = new JSONObject(); |
325 @SuppressWarnings("unused") | |
293 String unit = m.group(1); | 326 String unit = m.group(1); |
294 jo.put("x", m.group(2)); | 327 jo.put("x", m.group(2)); |
295 jo.put("y", m.group(3)); | 328 jo.put("y", m.group(3)); |
296 jo.put("width", m.group(4)); | 329 jo.put("width", m.group(4)); |
297 jo.put("height", m.group(5)); | 330 jo.put("height", m.group(5)); |
390 */ | 423 */ |
391 } | 424 } |
392 // get or create creator object | 425 // get or create creator object |
393 Actor creator = annot.getCreator(); | 426 Actor creator = annot.getCreator(); |
394 if (creator == null) { | 427 if (creator == null) { |
395 creator = new Actor(false, null, null); | 428 creator = new Person(); |
396 annot.setCreator(creator); | 429 annot.setCreator(creator); |
397 } | 430 } |
398 // username not required, if no username given authuser will be used | 431 // username not required, if no username given authuser will be used |
399 String username = null; | 432 String username = null; |
400 String userUri = annot.getCreatorUri(); | 433 String userUri = creator.getUri(); |
401 if (jo.has("user")) { | 434 if (jo.has("user")) { |
402 if (jo.get("user") instanceof String) { | 435 if (jo.get("user") instanceof String) { |
403 // user is just a String | 436 // user is just a String |
404 username = jo.getString("user"); | 437 username = jo.getString("user"); |
438 creator.setId(username); | |
405 // TODO: what if username and authUser are different? | 439 // TODO: what if username and authUser are different? |
406 } else { | 440 } else { |
407 // user is an object | 441 // user is an object |
408 JSONObject user = jo.getJSONObject("user"); | 442 JSONObject user = jo.getJSONObject("user"); |
409 if (user.has("id")) { | 443 if (user.has("id")) { |
410 username = user.getString("id"); | 444 String id = user.getString("id"); |
445 creator.setId(id); | |
446 username = id; | |
411 } | 447 } |
412 if (user.has("uri")) { | 448 if (user.has("uri")) { |
413 userUri = user.getString("uri"); | 449 userUri = user.getString("uri"); |
414 } | 450 } |
415 } | 451 } |
433 } | 469 } |
434 // TODO: should we overwrite the creator? | 470 // TODO: should we overwrite the creator? |
435 if (creator.getUri() == null) { | 471 if (creator.getUri() == null) { |
436 creator.setUri(userUri); | 472 creator.setUri(userUri); |
437 } | 473 } |
438 | 474 |
439 if (annot.getCreated() == null) { | 475 if (annot.getCreated() == null) { |
440 // set creation date | 476 // set creation date |
441 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | 477 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
442 String ct = format.format(Calendar.getInstance().getTime()); | 478 String ct = format.format(Calendar.getInstance().getTime()); |
443 annot.setCreated(ct); | 479 annot.setCreated(ct); |
454 JSONObject area = jo.getJSONArray("areas").getJSONObject(0); | 490 JSONObject area = jo.getJSONArray("areas").getJSONObject(0); |
455 annot.setFragmentType(FragmentTypes.AREA); | 491 annot.setFragmentType(FragmentTypes.AREA); |
456 String fragment = parseArea(area); | 492 String fragment = parseArea(area); |
457 annot.setTargetFragment(fragment); | 493 annot.setTargetFragment(fragment); |
458 } | 494 } |
495 | |
496 // permissions | |
497 if (jo.has("permissions")) { | |
498 JSONObject permissions = jo.getJSONObject("permissions"); | |
499 if (permissions.has("admin")) { | |
500 JSONArray perms = permissions.getJSONArray("admin"); | |
501 Actor actor = getActorFromPermissions(perms); | |
502 annot.setAdminPermission(actor); | |
503 } | |
504 if (permissions.has("delete")) { | |
505 JSONArray perms = permissions.getJSONArray("delete"); | |
506 Actor actor = getActorFromPermissions(perms); | |
507 annot.setDeletePermission(actor); | |
508 } | |
509 if (permissions.has("update")) { | |
510 JSONArray perms = permissions.getJSONArray("update"); | |
511 Actor actor = getActorFromPermissions(perms); | |
512 annot.setUpdatePermission(actor); | |
513 } | |
514 if (permissions.has("read")) { | |
515 JSONArray perms = permissions.getJSONArray("read"); | |
516 Actor actor = getActorFromPermissions(perms); | |
517 annot.setReadPermission(actor); | |
518 } | |
519 } | |
520 | |
459 return annot; | 521 return annot; |
460 } | 522 } |
461 | 523 |
524 @SuppressWarnings("unused") | |
525 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException { | |
526 Actor actor = null; | |
527 for (int i = 0; i < perms.length(); ++i) { | |
528 String perm = perms.getString(i); | |
529 if (perm.toLowerCase().startsWith("group:")) { | |
530 String groupId = perm.substring(6); | |
531 actor = new Group(groupId); | |
532 } else { | |
533 actor = new Person(perm); | |
534 } | |
535 // we just take the first one | |
536 break; | |
537 } | |
538 return actor; | |
539 } | |
540 | |
462 } | 541 } |