diff src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.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/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