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/AnnotatorAnnotationsByResources.java

    r50 r65  
    11package de.mpiwg.itgroup.annotations.restlet;
    22
     3import java.io.UnsupportedEncodingException;
     4import java.net.URLDecoder;
    35import java.util.ArrayList;
    46import java.util.List;
     
    2325 *
    2426 * @author dwinter
    25  *
     27 * 
    2628 */
    2729public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl {
     
    2931        return "OPTIONS,GET";
    3032    }
    31    
     33
    3234    @Get("json")
    3335    public Representation doGetJSON(Representation entity) {
    3436        logger.debug("AnnotatorAnnotatonsByResource doGetJSON!");
    3537        setCorsHeaders();
    36        
     38
    3739        // do authentication
    3840        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    3941        logger.debug("request authenticated=" + authUser);
    4042
    41        
     43        String id = null;
    4244        String jsonId = (String) getRequest().getAttributes().get("id");
    43        
     45        if (jsonId != null) {
     46            // URL decode
     47            try {
     48                jsonId = URLDecoder.decode(jsonId, "UTF-8");
     49            } catch (UnsupportedEncodingException e) {
     50                // this shouldn't happen
     51            }
     52            id = decodeJsonId(jsonId);
     53            // String id = jsonId;
     54            logger.debug("ressource-id=" + id);
     55        }
     56
    4457        Form form = getRequest().getResourceRef().getQueryAsForm();
    45         String sortBy=null;
     58        String sortBy = null;
    4659        for (Parameter parameter : form) {
    47           if (parameter.getName().equals("sortBy")){
    48           sortBy = parameter.getValue();
    49           }
     60            if (parameter.getName().equals("sortBy")) {
     61                sortBy = parameter.getValue();
     62            }
    5063        }
    51        
    52         String id = decodeJsonId(jsonId);
    53         //String id = jsonId;
    54         logger.debug("ressource-id=" + id);
    55        
     64
    5665        AnnotationStore store = getAnnotationStore();
    57                 //String tagUri=NS.MPIWG_TAGS_URL+id;
     66        // String tagUri=NS.MPIWG_TAGS_URL+id;
    5867        List<Annotation> annotations = store.getAnnotationsByResource(id);
    59        
    60                 //JSONArray results = new JSONArray();
     68
     69        // JSONArray results = new JSONArray();
    6170        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);
     71
     72        for (Annotation annot : annotations) {
     73            // check permission
     74            if (!annot.isActionAllowed("read", authUser, store))
     75                continue;
     76
     77            JSONObject jo = createAnnotatorJson(annot, false);
    6878            results.add(jo);
    6979        }
    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;
     80
     81        if (sortBy != null) {
     82            JSONObjectComparator.sortAnnotations(results, sortBy);
     83        }
     84
     85        JSONArray resultsJa = new JSONArray();
     86        for (JSONObject result : results) {
     87            resultsJa.put(result);
     88        }
     89
     90        // assemble result object
     91        JSONObject result = new JSONObject();
     92        try {
     93            result.put("rows", resultsJa);
     94            result.put("total", resultsJa.length());
     95        } catch (JSONException e) {
     96            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
     97            return null;
     98        }
     99        return new JsonRepresentation(result);
    88100    }
    89     logger.debug("sending:");
    90     logger.debug(result);
    91     return new JsonRepresentation(result);
     101
    92102}
    93 
    94    
    95        
    96 }
Note: See TracChangeset for help on using the changeset viewer.