changeset 65:c0dd5314bada

deal with special characters in urls.
author casties
date Wed, 05 Dec 2012 15:36:43 +0100
parents c48435e7f312
children 5b568de5ee0d
files src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java
diffstat 9 files changed, 212 insertions(+), 156 deletions(-) [+]
line wrap: on
line diff
--- 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<JSONObject> results = new ArrayList<JSONObject>();
-        
+
         // read all annotations
         List<Annotation> 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
--- 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<Annotation> annotations = store.getAnnotationsByResource(id);
-        
-		//JSONArray results = new JSONArray();
+
+        // JSONArray results = new JSONArray();
         ArrayList<JSONObject> results = new ArrayList<JSONObject>();
-        
-		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
--- 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<Annotation> annotations = store.getAnnotationsByTag(tagUri);
-        
-		//JSONArray results = new JSONArray();
+
+        // JSONArray results = new JSONArray();
         ArrayList<JSONObject> results = new ArrayList<JSONObject>();
-        
-		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
--- 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);
--- 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<Tag> 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<Tag> 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);
-	}        	
-        
+    }
 
-    
 }
--- 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);
--- 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));
         }
     }
--- 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));
         }
     }
--- 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 = "<html><body>\n";
             result += "<h1>New person</h1>\n";
             result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference());
@@ -68,13 +68,13 @@
             result += "<p><input type=\"submit\"/></p>\n";
             result += "</form>\n</body>\n</html>";
         } else {
-            // list all groups
+            // list all persons
             result = "<html><body>\n<h1>Persons</h1>\n<table>";
             result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
             List<Person> persons = store.getPersons("uri", "*");
             for (Person person : persons) {
                 Reference url = this.getReference().clone();
-                url.addSegment(person.getId());
+                url.addSegment(person.getIdString());
                 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
                         person.getIdString(), person.getName(), person.getUri());
             }
@@ -86,7 +86,7 @@
     }
 
     /**
-     * POST creates a new Group.
+     * POST creates a new person.
      * 
      * @return
      */