comparison src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java @ 17:b0ef5c860464

updating and deleting annotations works now! more cleanup.
author casties
date Thu, 22 Mar 2012 21:37:16 +0100
parents 6c7c4140630d
children 6629e8422760
comparison
equal deleted inserted replaced
16:667d98fd28bd 17:b0ef5c860464
29 import org.restlet.resource.Options; 29 import org.restlet.resource.Options;
30 import org.restlet.resource.ServerResource; 30 import org.restlet.resource.ServerResource;
31 import org.restlet.security.User; 31 import org.restlet.security.User;
32 32
33 import de.mpiwg.itgroup.annotationManager.Constants.NS; 33 import de.mpiwg.itgroup.annotationManager.Constants.NS;
34 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert; 34 import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation;
35 35
36 /** 36 /**
37 * Base class for Annotator resource classes. 37 * Base class for Annotator resource classes.
38 * 38 *
39 * @author dwinter, casties 39 * @author dwinter, casties
64 e.printStackTrace(); 64 e.printStackTrace();
65 } catch (UnsupportedEncodingException e) { 65 } catch (UnsupportedEncodingException e) {
66 e.printStackTrace(); 66 e.printStackTrace();
67 } 67 }
68 return digest; 68 return digest;
69 }
70
71 public String encodeJsonId(String id) {
72 try {
73 return DatatypeConverter.printBase64Binary(id.getBytes("UTF-8"));
74 } catch (UnsupportedEncodingException e) {
75 return null;
76 }
77 }
78
79 public String decodeJsonId(String id) {
80 try {
81 return new String(DatatypeConverter.parseBase64Binary(id), "UTF-8");
82 } catch (UnsupportedEncodingException e) {
83 return null;
84 }
69 } 85 }
70 86
71 /** 87 /**
72 * Handle options request to allow CORS for AJAX. 88 * Handle options request to allow CORS for AJAX.
73 * 89 *
179 * creates Annotator-JSON from an Annotation object. 195 * creates Annotator-JSON from an Annotation object.
180 * 196 *
181 * @param annot 197 * @param annot
182 * @return 198 * @return
183 */ 199 */
184 public JSONObject createAnnotatorJson(Convert.Annotation annot) { 200 public JSONObject createAnnotatorJson(Annotation annot) {
185 boolean makeUserObject = true; 201 boolean makeUserObject = true;
186 JSONObject jo = new JSONObject(); 202 JSONObject jo = new JSONObject();
187 try { 203 try {
188 jo.put("text", annot.text); 204 jo.put("text", annot.text);
189 jo.put("uri", annot.url); 205 jo.put("uri", annot.url);
218 for (String xpointerString : annot.xpointers) { 234 for (String xpointerString : annot.xpointers) {
219 xpointers.add(xpointerString); 235 xpointers.add(xpointerString);
220 } 236 }
221 } 237 }
222 jo.put("ranges", transformToRanges(xpointers)); 238 jo.put("ranges", transformToRanges(xpointers));
223 jo.put("id", annot.annotationUri); 239 // encode Annotation URL (=id) in base64
240 String annotUrl = annot.getAnnotationUri();
241 String annotId = encodeJsonId(annotUrl);
242 jo.put("id", annotId);
224 return jo; 243 return jo;
225 } catch (JSONException e) { 244 } catch (JSONException e) {
226 // TODO Auto-generated catch block 245 // TODO Auto-generated catch block
227 e.printStackTrace(); 246 e.printStackTrace();
228 return null; 247 }
229 } 248 return null;
230 } 249 }
231 250
232 private JSONArray transformToRanges(List<String> xpointers) { 251 private JSONArray transformToRanges(List<String> xpointers) {
233 252
234 JSONArray ja = new JSONArray(); 253 JSONArray ja = new JSONArray();
282 * 301 *
283 * @param jo 302 * @param jo
284 * @return 303 * @return
285 * @throws JSONException 304 * @throws JSONException
286 */ 305 */
287 public Convert.Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException { 306 public Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException {
288 String url = jo.getString("uri"); 307 return updateAnnotation(new Annotation(), jo, entity);
289 String text = jo.getString("text"); 308 }
290 309
310 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException {
311 // annotated uri
312 String url = annot.url;
313 if (jo.has("uri")) {
314 url = jo.getString("uri");
315 }
316 // annotation text
317 String text = annot.text;
318 if (jo.has("text")) {
319 text = jo.getString("text");
320 }
291 // check authentication 321 // check authentication
292 String authUser = checkAuthToken(entity); 322 String authUser = checkAuthToken(entity);
293 if (authUser == null) { 323 if (authUser == null) {
294 // try http auth 324 // try http auth
295 User httpUser = getHttpAuthUser(entity); 325 User httpUser = getHttpAuthUser(entity);
299 } 329 }
300 authUser = httpUser.getIdentifier(); 330 authUser = httpUser.getIdentifier();
301 } 331 }
302 // username not required, if no username given authuser will be used 332 // username not required, if no username given authuser will be used
303 String username = null; 333 String username = null;
304 String userUri = null; 334 String userUri = annot.creator;
305 if (jo.has("user")) { 335 if (jo.has("user")) {
306 if (jo.get("user") instanceof String) { 336 if (jo.get("user") instanceof String) {
307 // user is just a String 337 // user is just a String
308 username = jo.getString("user"); 338 username = jo.getString("user");
309 // TODO: what if username and authUser are different? 339 // TODO: what if username and authUser are different?
319 } 349 }
320 } 350 }
321 if (username == null) { 351 if (username == null) {
322 username = authUser; 352 username = authUser;
323 } 353 }
354 // username should be a URI, if not it will set to the MPIWG namespace defined in
355 // de.mpiwg.itgroup.annotationManager.Constants.NS
356 if (userUri == null) {
357 if (username.startsWith("http")) {
358 userUri = username;
359 } else {
360 userUri = NS.MPIWG_PERSONS + username;
361 }
362 }
363 // TODO: should we overwrite the creator?
324 364
325 // create xpointer 365 // create xpointer
326 String xpointer; 366 String xpointer = annot.xpointer;
327 if (jo.has("ranges")) { 367 if (jo.has("ranges")) {
328 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0); 368 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
329 String start = ranges.getString("start"); 369 String start = ranges.getString("start");
330 String end = ranges.getString("end"); 370 String end = ranges.getString("end");
331 String startOffset = ranges.getString("startOffset"); 371 String startOffset = ranges.getString("startOffset");
340 } catch (UnsupportedEncodingException e) { 380 } catch (UnsupportedEncodingException e) {
341 e.printStackTrace(); 381 e.printStackTrace();
342 setStatus(Status.SERVER_ERROR_INTERNAL); 382 setStatus(Status.SERVER_ERROR_INTERNAL);
343 return null; 383 return null;
344 } 384 }
345 } else { 385 }
346 xpointer = url; 386 return new Annotation(xpointer, userUri, annot.time, text, annot.type);
347 }
348
349 // username should be a URI, if not it will set to the MPIWG namespace defined in
350 // de.mpiwg.itgroup.annotationManager.Constants.NS
351 if (userUri == null) {
352 if (username.startsWith("http")) {
353 userUri = username;
354 } else {
355 userUri = NS.MPIWG_PERSONS + username;
356 }
357 }
358 return new Convert.Annotation(xpointer, userUri, null, text, null);
359 } 387 }
360 388
361 /** 389 /**
362 * returns the logged in User. 390 * returns the logged in User.
363 * 391 *