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

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