Ignore:
Timestamp:
Dec 5, 2012, 2:36:43 PM (11 years ago)
Author:
casties
Branch:
default
Message:

deal with special characters in urls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java

    r58 r65  
    11package de.mpiwg.itgroup.annotations.restlet;
    22
     3import java.io.UnsupportedEncodingException;
     4import java.net.URLDecoder;
    35import java.util.ArrayList;
    46import java.util.List;
     
    810import org.json.JSONObject;
    911import org.restlet.data.Form;
    10 import org.restlet.data.Parameter;
    1112import org.restlet.data.Status;
    1213import org.restlet.ext.json.JsonRepresentation;
     
    2324 *
    2425 * @author dwinter
    25  *
     26 * 
    2627 */
    2728public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl {
     
    2930        return "OPTIONS,GET";
    3031    }
    31    
     32
    3233    @Get("json")
    3334    public Representation doGetJSON(Representation entity) {
    3435        logger.debug("AnnotatorAnnotatonsBytag doGetJSON!");
    3536        setCorsHeaders();
    36        
     37
    3738        // do authentication
    3839        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    3940        logger.debug("request authenticated=" + authUser);
    4041
    41        
    42         String jsonId = (String) getRequest().getAttributes().get("id");
    43        
     42        String id = (String) getRequest().getAttributes().get("id");
     43        // URL decode
     44        try {
     45            id = URLDecoder.decode(id, "UTF-8");
     46        } catch (UnsupportedEncodingException e) {
     47            // this shouldn't happen
     48        }
     49        logger.debug("annotation-id=" + id);
     50
    4451        Form form = getRequest().getResourceRef().getQueryAsForm();
    45         String sortBy=null;
    46         for (Parameter parameter : form) {
    47           if (parameter.getName().equals("sortBy")){
    48           sortBy =  parameter.getValue();
    49           }
    50         }
    51        
    52         //String id = decodeJsonId(jsonId);
    53         String id = jsonId;
    54         logger.debug("annotation-id=" + id);
    55        
     52        String sortBy = form.getFirstValue("sortBy");
     53
    5654        AnnotationStore store = getAnnotationStore();
    5755        String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
    5856        List<Annotation> annotations = store.getAnnotationsByTag(tagUri);
    59        
    60                 //JSONArray results = new JSONArray();
     57
     58        // JSONArray results = new JSONArray();
    6159        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    62        
    63                 for (Annotation annot : annotations) {
    64                         //check permission
    65                         if (!annot.isActionAllowed("read", authUser, store)) continue;
    66            
    67                         JSONObject jo = createAnnotatorJson(annot,false);
     60
     61        for (Annotation annot : annotations) {
     62            // check permission
     63            if (!annot.isActionAllowed("read", authUser, store))
     64                continue;
     65
     66            JSONObject jo = createAnnotatorJson(annot, false);
    6867            results.add(jo);
    6968        }
    70    
    71         if (sortBy!=null){
    72                 JSONObjectComparator.sortAnnotations(results,sortBy);
    73         }
    74                
    75         JSONArray resultsJa = new JSONArray();
    76         for (JSONObject result:results){
    77                 resultsJa.put(result);
    78         }
    79        
    80     // assemble result object
    81     JSONObject result = new JSONObject();
    82     try {
    83         result.put("rows", resultsJa);
    84         result.put("total", resultsJa.length());
    85     } catch (JSONException e) {
    86         setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
    87         return null;
     69
     70        if (sortBy != null) {
     71            JSONObjectComparator.sortAnnotations(results, sortBy);
     72        }
     73
     74        JSONArray resultsJa = new JSONArray();
     75        for (JSONObject result : results) {
     76            resultsJa.put(result);
     77        }
     78
     79        // assemble result object
     80        JSONObject result = new JSONObject();
     81        try {
     82            result.put("rows", resultsJa);
     83            result.put("total", resultsJa.length());
     84        } catch (JSONException e) {
     85            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
     86            return null;
     87        }
     88        return new JsonRepresentation(result);
    8889    }
    89     logger.debug("sending:");
    90     logger.debug(result);
    91     return new JsonRepresentation(result);
     90
    9291}
    93 
    94    
    95        
    96 }
Note: See TracChangeset for help on using the changeset viewer.