diff src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java @ 65:c0dd5314bada

deal with special characters in urls.
author casties
date Wed, 05 Dec 2012 15:36:43 +0100
parents f5c0e6df7e88
children 2b1e6df5e21a
line wrap: on
line diff
--- 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);
-	}        	
-        
+    }
 
-    
 }