comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 16:794077e6288c

CLOSED - # 252: Tags for Annotations https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/252
author casties
date Tue, 04 Sep 2012 20:02:59 +0200
parents 58357a4b86de
children aafa3884b2c4
comparison
equal deleted inserted replaced
15:58357a4b86de 16:794077e6288c
7 import java.security.InvalidKeyException; 7 import java.security.InvalidKeyException;
8 import java.security.SignatureException; 8 import java.security.SignatureException;
9 import java.text.SimpleDateFormat; 9 import java.text.SimpleDateFormat;
10 import java.util.ArrayList; 10 import java.util.ArrayList;
11 import java.util.Calendar; 11 import java.util.Calendar;
12 import java.util.HashSet;
12 import java.util.List; 13 import java.util.List;
14 import java.util.Set;
13 import java.util.regex.Matcher; 15 import java.util.regex.Matcher;
14 import java.util.regex.Pattern; 16 import java.util.regex.Pattern;
15 17
16 import javax.servlet.ServletContext; 18 import javax.servlet.ServletContext;
17 19
187 JSONObject jo = new JSONObject(); 189 JSONObject jo = new JSONObject();
188 try { 190 try {
189 jo.put("text", annot.getBodyText()); 191 jo.put("text", annot.getBodyText());
190 jo.put("uri", annot.getTargetBaseUri()); 192 jo.put("uri", annot.getTargetBaseUri());
191 193
194 /*
195 * user
196 */
192 if (makeUserObject) { 197 if (makeUserObject) {
193 // create user object 198 // create user object
194 JSONObject userObject = new JSONObject(); 199 JSONObject userObject = new JSONObject();
195 Actor creator = annot.getCreator(); 200 Actor creator = annot.getCreator();
196 // save creator as uri 201 // save creator as uri
211 } else { 216 } else {
212 // save user as string 217 // save user as string
213 jo.put("user", annot.getCreatorUri()); 218 jo.put("user", annot.getCreatorUri());
214 } 219 }
215 220
221 /*
222 * ranges
223 */
216 if (annot.getTargetFragment() != null) { 224 if (annot.getTargetFragment() != null) {
217 // we only look at the first xpointer 225 // we only look at the first xpointer
218 List<String> fragments = new ArrayList<String>(); 226 List<String> fragments = new ArrayList<String>();
219 fragments.add(annot.getTargetFragment()); 227 fragments.add(annot.getTargetFragment());
220 FragmentTypes xt = annot.getFragmentType(); 228 FragmentTypes xt = annot.getFragmentType();
223 } else if (xt == FragmentTypes.AREA) { 231 } else if (xt == FragmentTypes.AREA) {
224 jo.put("areas", transformToAreas(fragments)); 232 jo.put("areas", transformToAreas(fragments));
225 } 233 }
226 } 234 }
227 235
228 // permissions 236 /*
237 * permissions
238 */
229 JSONObject perms = new JSONObject(); 239 JSONObject perms = new JSONObject();
230 jo.put("permissions", perms); 240 jo.put("permissions", perms);
231 // admin 241 // admin
232 JSONArray adminPerms = new JSONArray(); 242 JSONArray adminPerms = new JSONArray();
233 perms.put("admin", adminPerms); 243 perms.put("admin", adminPerms);
264 Actor readPerm = annot.getReadPermission(); 274 Actor readPerm = annot.getReadPermission();
265 if (readPerm != null) { 275 if (readPerm != null) {
266 readPerms.put(readPerm.getIdString()); 276 readPerms.put(readPerm.getIdString());
267 } 277 }
268 278
279 /*
280 * tags
281 */
282 Set<String> tagset = annot.getTags();
283 if (tagset != null) {
284 JSONArray tags = new JSONArray();
285 jo.put("tags", tags);
286 for (String tag : tagset) {
287 tags.put(tag);
288 }
289 }
290
291 /*
292 * id
293 */
269 // encode Annotation URL (=id) in base64 294 // encode Annotation URL (=id) in base64
270 String annotUrl = annot.getUri(); 295 String annotUrl = annot.getUri();
271 String annotId = encodeJsonId(annotUrl); 296 String annotId = encodeJsonId(annotUrl);
272 jo.put("id", annotId); 297 jo.put("id", annotId);
273 return jo; 298 return jo;
410 * @throws JSONException 435 * @throws JSONException
411 * @throws UnsupportedEncodingException 436 * @throws UnsupportedEncodingException
412 */ 437 */
413 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException, 438 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException,
414 UnsupportedEncodingException { 439 UnsupportedEncodingException {
415 // target uri 440 /*
441 * target uri
442 */
416 if (jo.has("uri")) { 443 if (jo.has("uri")) {
417 annot.setTargetBaseUri(jo.getString("uri")); 444 annot.setTargetBaseUri(jo.getString("uri"));
418 } 445 }
419 // annotation text 446 /*
447 * annotation text
448 */
420 if (jo.has("text")) { 449 if (jo.has("text")) {
421 annot.setBodyText(jo.getString("text")); 450 annot.setBodyText(jo.getString("text"));
422 } 451 }
423 // check authentication 452 /*
453 * check authentication
454 */
424 String authUser = checkAuthToken(entity); 455 String authUser = checkAuthToken(entity);
425 if (authUser == null) { 456 if (authUser == null) {
426 /* 457 /*
427 * // try http auth User httpUser = getHttpAuthUser(entity); if 458 * // try http auth User httpUser = getHttpAuthUser(entity); if
428 * (httpUser == null) { 459 * (httpUser == null) {
431 return null; 462 return null;
432 /* 463 /*
433 * } authUser = httpUser.getIdentifier(); 464 * } authUser = httpUser.getIdentifier();
434 */ 465 */
435 } 466 }
436 // get or create creator object 467 /*
468 * get or create creator object
469 */
437 Actor creator = annot.getCreator(); 470 Actor creator = annot.getCreator();
438 if (creator == null) { 471 if (creator == null) {
439 creator = new Person(); 472 creator = new Person();
440 annot.setCreator(creator); 473 annot.setCreator(creator);
441 } 474 }
480 } 513 }
481 // TODO: should we overwrite the creator? 514 // TODO: should we overwrite the creator?
482 if (creator.getUri() == null) { 515 if (creator.getUri() == null) {
483 creator.setUri(userUri); 516 creator.setUri(userUri);
484 } 517 }
485 518 /*
519 * creation date
520 */
486 if (annot.getCreated() == null) { 521 if (annot.getCreated() == null) {
487 // set creation date 522 // set creation date
488 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 523 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
489 String ct = format.format(Calendar.getInstance().getTime()); 524 String ct = format.format(Calendar.getInstance().getTime());
490 annot.setCreated(ct); 525 annot.setCreated(ct);
491 } 526 }
492 527
493 // create xpointer from the first range/area 528 /*
529 * create xpointer from the first range/area
530 */
494 if (jo.has("ranges")) { 531 if (jo.has("ranges")) {
495 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0); 532 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
496 annot.setFragmentType(FragmentTypes.XPOINTER); 533 annot.setFragmentType(FragmentTypes.XPOINTER);
497 String fragment = parseRange(ranges); 534 String fragment = parseRange(ranges);
498 annot.setTargetFragment(fragment); 535 annot.setTargetFragment(fragment);
502 annot.setFragmentType(FragmentTypes.AREA); 539 annot.setFragmentType(FragmentTypes.AREA);
503 String fragment = parseArea(area); 540 String fragment = parseArea(area);
504 annot.setTargetFragment(fragment); 541 annot.setTargetFragment(fragment);
505 } 542 }
506 543
507 // permissions 544 /*
545 * permissions
546 */
508 if (jo.has("permissions")) { 547 if (jo.has("permissions")) {
509 JSONObject permissions = jo.getJSONObject("permissions"); 548 JSONObject permissions = jo.getJSONObject("permissions");
510 if (permissions.has("admin")) { 549 if (permissions.has("admin")) {
511 JSONArray perms = permissions.getJSONArray("admin"); 550 JSONArray perms = permissions.getJSONArray("admin");
512 Actor actor = getActorFromPermissions(perms); 551 Actor actor = getActorFromPermissions(perms);
527 Actor actor = getActorFromPermissions(perms); 566 Actor actor = getActorFromPermissions(perms);
528 annot.setReadPermission(actor); 567 annot.setReadPermission(actor);
529 } 568 }
530 } 569 }
531 570
571 /*
572 * tags
573 */
574 if (jo.has("tags")) {
575 HashSet<String> tagset = new HashSet<String>();
576 JSONArray tags = jo.getJSONArray("tags");
577 for (int i = 0; i < tags.length(); ++i) {
578 tagset.add(tags.getString(i));
579 }
580 annot.setTags(tagset);
581 }
582
583
532 return annot; 584 return annot;
533 } 585 }
534 586
535 @SuppressWarnings("unused") // i in for loop 587 @SuppressWarnings("unused") // i in for loop
536 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException { 588 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {