# HG changeset patch # User casties # Date 1354718203 -3600 # Node ID c0dd5314bada1ceae1ab4c90271d3c76ee547319 # Parent c48435e7f3126cfd87d0da562a8fa4ccc8b8d46a deal with special characters in urls. diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java Wed Dec 05 15:36:43 2012 +0100 @@ -5,6 +5,8 @@ package de.mpiwg.itgroup.annotations.restlet; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; @@ -49,9 +51,18 @@ logger.debug("AnnotatorAnnotations doGetJSON!"); setCorsHeaders(); // id from URI /annotations/{id} + String id = null; String jsonId = (String) getRequest().getAttributes().get("id"); - String id = decodeJsonId(jsonId); - logger.debug("annotation-id=" + id); + if (jsonId != null) { + // URL decode + try { + jsonId = URLDecoder.decode(jsonId, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } + id = decodeJsonId(jsonId); + logger.debug("annotation-id=" + id); + } // do authentication Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); @@ -86,12 +97,13 @@ private Representation getAllAnnotations(Person authUser, int limit, int offset, String sortBy) { AnnotationStore store = getAnnotationStore(); ArrayList results = new ArrayList(); - + // read all annotations List annotations = store.getAnnotations(null, null); for (Annotation annotation : annotations) { // check permission - if (!annotation.isActionAllowed("read", authUser, store)) continue; + if (!annotation.isActionAllowed("read", authUser, store)) + continue; // add annotation to list JSONObject jo = createAnnotatorJson(annotation, false); results.add(jo); @@ -101,15 +113,17 @@ if (sortBy != null) { JSONObjectComparator.sortAnnotations(results, sortBy); } - + // put in JSON list JSONArray rows = new JSONArray(); int cnt = 0; for (JSONObject result : results) { cnt += 1; - if (offset > 0 && cnt < offset) continue; + if (offset > 0 && cnt < offset) + continue; rows.put(result); - if (limit > 0 && cnt >= limit) break; + if (limit > 0 && cnt >= limit) + break; } // assemble result object diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java Wed Dec 05 15:36:43 2012 +0100 @@ -1,5 +1,7 @@ package de.mpiwg.itgroup.annotations.restlet; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; @@ -22,75 +24,79 @@ * API for accessing tags in the Annotation store. * * @author dwinter - * + * */ public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl { protected String getAllowedMethodsForHeader() { return "OPTIONS,GET"; } - + @Get("json") public Representation doGetJSON(Representation entity) { logger.debug("AnnotatorAnnotatonsByResource doGetJSON!"); setCorsHeaders(); - + // do authentication Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); logger.debug("request authenticated=" + authUser); - + String id = null; String jsonId = (String) getRequest().getAttributes().get("id"); - - Form form = getRequest().getResourceRef().getQueryAsForm(); - String sortBy=null; - for (Parameter parameter : form) { - if (parameter.getName().equals("sortBy")){ - sortBy = parameter.getValue(); - } + if (jsonId != null) { + // URL decode + try { + jsonId = URLDecoder.decode(jsonId, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } + id = decodeJsonId(jsonId); + // String id = jsonId; + logger.debug("ressource-id=" + id); } - - String id = decodeJsonId(jsonId); - //String id = jsonId; - logger.debug("ressource-id=" + id); - + + Form form = getRequest().getResourceRef().getQueryAsForm(); + String sortBy = null; + for (Parameter parameter : form) { + if (parameter.getName().equals("sortBy")) { + sortBy = parameter.getValue(); + } + } + AnnotationStore store = getAnnotationStore(); - //String tagUri=NS.MPIWG_TAGS_URL+id; + // String tagUri=NS.MPIWG_TAGS_URL+id; List annotations = store.getAnnotationsByResource(id); - - //JSONArray results = new JSONArray(); + + // JSONArray results = new JSONArray(); ArrayList results = new ArrayList(); - - for (Annotation annot : annotations) { - //check permission - if (!annot.isActionAllowed("read", authUser, store)) continue; - - JSONObject jo = createAnnotatorJson(annot,false); + + for (Annotation annot : annotations) { + // check permission + if (!annot.isActionAllowed("read", authUser, store)) + continue; + + JSONObject jo = createAnnotatorJson(annot, false); results.add(jo); } - - if (sortBy!=null){ - JSONObjectComparator.sortAnnotations(results,sortBy); - } - - JSONArray resultsJa = new JSONArray(); - for (JSONObject result:results){ - resultsJa.put(result); - } - - // assemble result object - JSONObject result = new JSONObject(); - try { - result.put("rows", resultsJa); - result.put("total", resultsJa.length()); - } catch (JSONException e) { - setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); - return null; + + if (sortBy != null) { + JSONObjectComparator.sortAnnotations(results, sortBy); + } + + JSONArray resultsJa = new JSONArray(); + for (JSONObject result : results) { + resultsJa.put(result); + } + + // assemble result object + JSONObject result = new JSONObject(); + try { + result.put("rows", resultsJa); + result.put("total", resultsJa.length()); + } catch (JSONException e) { + setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); + return null; + } + return new JsonRepresentation(result); } - logger.debug("sending:"); - logger.debug(result); - return new JsonRepresentation(result); -} - - } \ No newline at end of file diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java Wed Dec 05 15:36:43 2012 +0100 @@ -1,5 +1,7 @@ package de.mpiwg.itgroup.annotations.restlet; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; @@ -7,7 +9,6 @@ import org.json.JSONException; import org.json.JSONObject; import org.restlet.data.Form; -import org.restlet.data.Parameter; import org.restlet.data.Status; import org.restlet.ext.json.JsonRepresentation; import org.restlet.representation.Representation; @@ -22,75 +23,69 @@ * API for accessing tags in the Annotation store. * * @author dwinter - * + * */ public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl { protected String getAllowedMethodsForHeader() { return "OPTIONS,GET"; } - + @Get("json") public Representation doGetJSON(Representation entity) { logger.debug("AnnotatorAnnotatonsBytag doGetJSON!"); setCorsHeaders(); - + // do authentication Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); logger.debug("request authenticated=" + authUser); - - String jsonId = (String) getRequest().getAttributes().get("id"); - + String id = (String) getRequest().getAttributes().get("id"); + // URL decode + try { + id = URLDecoder.decode(id, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } + logger.debug("annotation-id=" + id); + Form form = getRequest().getResourceRef().getQueryAsForm(); - String sortBy=null; - for (Parameter parameter : form) { - if (parameter.getName().equals("sortBy")){ - sortBy = parameter.getValue(); - } - } - - //String id = decodeJsonId(jsonId); - String id = jsonId; - logger.debug("annotation-id=" + id); - + String sortBy = form.getFirstValue("sortBy"); + AnnotationStore store = getAnnotationStore(); String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; List annotations = store.getAnnotationsByTag(tagUri); - - //JSONArray results = new JSONArray(); + + // JSONArray results = new JSONArray(); ArrayList results = new ArrayList(); - - for (Annotation annot : annotations) { - //check permission - if (!annot.isActionAllowed("read", authUser, store)) continue; - - JSONObject jo = createAnnotatorJson(annot,false); + + for (Annotation annot : annotations) { + // check permission + if (!annot.isActionAllowed("read", authUser, store)) + continue; + + JSONObject jo = createAnnotatorJson(annot, false); results.add(jo); } - - if (sortBy!=null){ - JSONObjectComparator.sortAnnotations(results,sortBy); - } - - JSONArray resultsJa = new JSONArray(); - for (JSONObject result:results){ - resultsJa.put(result); - } - - // assemble result object - JSONObject result = new JSONObject(); - try { - result.put("rows", resultsJa); - result.put("total", resultsJa.length()); - } catch (JSONException e) { - setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); - return null; + + if (sortBy != null) { + JSONObjectComparator.sortAnnotations(results, sortBy); + } + + JSONArray resultsJa = new JSONArray(); + for (JSONObject result : results) { + resultsJa.put(result); + } + + // assemble result object + JSONObject result = new JSONObject(); + try { + result.put("rows", resultsJa); + result.put("total", resultsJa.length()); + } catch (JSONException e) { + setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); + return null; + } + return new JsonRepresentation(result); } - logger.debug("sending:"); - logger.debug(result); - return new JsonRepresentation(result); -} - - } \ No newline at end of file diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java Wed Dec 05 15:36:43 2012 +0100 @@ -3,6 +3,8 @@ */ package de.mpiwg.itgroup.annotations.restlet; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.List; import org.json.JSONArray; @@ -41,6 +43,14 @@ setCorsHeaders(); String jsonId = (String) getRequest().getAttributes().get("id"); + if (jsonId != null) { + // URL decode + try { + jsonId = URLDecoder.decode(jsonId, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } + } String uri = decodeJsonId(jsonId); logger.debug("resources-id=" + uri); diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java Wed Dec 05 15:36:43 2012 +0100 @@ -3,6 +3,8 @@ */ package de.mpiwg.itgroup.annotations.restlet; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.util.List; import org.json.JSONArray; @@ -17,12 +19,11 @@ import de.mpiwg.itgroup.annotations.Tag; import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; - /** * API for accessing tags in the Annotation store. * * @author dwinter - * + * */ public class AnnotatorTags extends AnnotatorResourceImpl { protected String getAllowedMethodsForHeader() { @@ -30,11 +31,11 @@ } /** - * GET with JSON content-type. + * GET with JSON content-type. * Parameters: - * user: short user name + * user: short user name * uri: user uri - * + * * @param entity * @return */ @@ -42,52 +43,57 @@ public Representation doGetJSON(Representation entity) { logger.debug("AnnotatorGroups doGetJSON!"); setCorsHeaders(); - + String jsonId = (String) getRequest().getAttributes().get("id"); - //String id = decodeJsonId(jsonId); + // String id = decodeJsonId(jsonId); String id = jsonId; logger.debug("annotation-id=" + id); - if (id==null){ - return getAllTags(); + if (id == null) { + return getAllTags(); } else { - - return getTag(id); + // URL decode + try { + id = URLDecoder.decode(id, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } + return getTag(id); } } - - protected Representation getTag(String id){ - AnnotationStore store = getAnnotationStore(); - String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; - Node tagNode = store.getTagNodeByUri(tagUri); - Tag tag = store.createTagFromNode(tagNode); - JSONObject jo = new JSONObject(); - try { - jo.put("id", tag.getId()); - jo.put("name", tag.getName()); - jo.put("uri", tag.getUri()); - } catch (JSONException e) { - } - - return new JsonRepresentation(jo); + + protected Representation getTag(String id) { + AnnotationStore store = getAnnotationStore(); + String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; + Node tagNode = store.getTagNodeByUri(tagUri); + Tag tag = store.createTagFromNode(tagNode); + JSONObject jo = new JSONObject(); + try { + jo.put("id", tag.getId()); + jo.put("name", tag.getName()); + jo.put("uri", tag.getUri()); + } catch (JSONException e) { + } + + return new JsonRepresentation(jo); } - protected Representation getAllTags() { - JSONArray results = new JSONArray(); + + protected Representation getAllTags() { + JSONArray results = new JSONArray(); AnnotationStore store = getAnnotationStore(); - - - List tags = store.getTags(null, null); - for (Tag tag : tags) { - JSONObject jo = new JSONObject(); - try { - jo.put("id", tag.getId()); - jo.put("name", tag.getName()); - jo.put("uri", tag.getUri()); - } catch (JSONException e) { - } - results.put(jo); + + List tags = store.getTags(null, null); + for (Tag tag : tags) { + JSONObject jo = new JSONObject(); + try { + jo.put("id", tag.getId()); + jo.put("name", tag.getName()); + jo.put("uri", tag.getUri()); + } catch (JSONException e) { } - + results.put(jo); + } + // assemble result object JSONObject result = new JSONObject(); try { @@ -100,8 +106,6 @@ logger.debug("sending:"); logger.debug(result); return new JsonRepresentation(result); - } - + } - } diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java Wed Dec 05 15:36:43 2012 +0100 @@ -3,6 +3,9 @@ */ package de.mpiwg.itgroup.annotations.restlet.annotations_ui; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + import org.apache.log4j.Logger; import org.restlet.data.Form; import org.restlet.data.MediaType; @@ -48,6 +51,12 @@ } // get annotation from store if (requestId != null) { + // URL decode + try { + requestId = URLDecoder.decode(requestId, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } // the ID in the path is encoded String id = Annotation.decodeId(requestId); annotation = store.getAnnotationById(id); diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java Wed Dec 05 15:36:43 2012 +0100 @@ -3,6 +3,9 @@ */ package de.mpiwg.itgroup.annotations.restlet.annotations_ui; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + import org.apache.log4j.Logger; import org.restlet.data.Form; import org.restlet.data.MediaType; @@ -48,6 +51,12 @@ } // get group from store if (requestId != null) { + // URL decode + try { + requestId = URLDecoder.decode(requestId, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } group = (Group) store.getActor(new Group(requestId)); } } diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.java Wed Dec 05 15:36:43 2012 +0100 @@ -3,6 +3,9 @@ */ package de.mpiwg.itgroup.annotations.restlet.annotations_ui; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + import org.apache.log4j.Logger; import org.restlet.data.Form; import org.restlet.data.MediaType; @@ -41,13 +44,19 @@ super.doInit(); // id from URI /annotations/persons/{id} requestId = (String) getRequest().getAttributes().get("id"); - logger.debug("group-id=" + requestId); + logger.debug("person-id=" + requestId); // get store instance if (store == null) { store = ((BaseRestlet) getApplication()).getAnnotationStore(); } - // get group from store + // get person from store if (requestId != null) { + // URL decode + try { + requestId = URLDecoder.decode(requestId, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // this shouldn't happen + } person = (Person) store.getActor(new Person(requestId)); } } diff -r c48435e7f312 -r c0dd5314bada src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java Mon Dec 03 18:42:20 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java Wed Dec 05 15:36:43 2012 +0100 @@ -56,7 +56,7 @@ Form f = this.getQuery(); String form = f.getFirstValue("form"); if (form != null && form.equals("new_person")) { - // output new group form + // output new person form result = "\n"; result += "

New person

\n"; result += String.format("

All persons

", this.getReference()); @@ -68,13 +68,13 @@ result += "

\n"; result += "\n\n"; } else { - // list all groups + // list all persons result = "\n

Persons

\n"; result += ""; List persons = store.getPersons("uri", "*"); for (Person person : persons) { Reference url = this.getReference().clone(); - url.addSegment(person.getId()); + url.addSegment(person.getIdString()); result += String.format("\n", url, person.getIdString(), person.getName(), person.getUri()); } @@ -86,7 +86,7 @@ } /** - * POST creates a new Group. + * POST creates a new person. * * @return */
idnameuri
%s%s%s