Changeset 65:c0dd5314bada in AnnotationManagerN4J for src


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

deal with special characters in urls.

Location:
src/main/java/de/mpiwg/itgroup/annotations/restlet
Files:
9 edited

Legend:

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

    r63 r65  
    66
    77import java.io.IOException;
     8import java.io.UnsupportedEncodingException;
     9import java.net.URLDecoder;
    810import java.util.ArrayList;
    911import java.util.List;
     
    5052        setCorsHeaders();
    5153        // id from URI /annotations/{id}
     54        String id = null;
    5255        String jsonId = (String) getRequest().getAttributes().get("id");
    53         String id = decodeJsonId(jsonId);
    54         logger.debug("annotation-id=" + id);
     56        if (jsonId != null) {
     57            // URL decode
     58            try {
     59                jsonId = URLDecoder.decode(jsonId, "UTF-8");
     60            } catch (UnsupportedEncodingException e) {
     61                // this shouldn't happen
     62            }
     63            id = decodeJsonId(jsonId);
     64            logger.debug("annotation-id=" + id);
     65        }
    5566
    5667        // do authentication
     
    8798        AnnotationStore store = getAnnotationStore();
    8899        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    89        
     100
    90101        // read all annotations
    91102        List<Annotation> annotations = store.getAnnotations(null, null);
    92103        for (Annotation annotation : annotations) {
    93104            // check permission
    94             if (!annotation.isActionAllowed("read", authUser, store)) continue;
     105            if (!annotation.isActionAllowed("read", authUser, store))
     106                continue;
    95107            // add annotation to list
    96108            JSONObject jo = createAnnotatorJson(annotation, false);
     
    102114            JSONObjectComparator.sortAnnotations(results, sortBy);
    103115        }
    104        
     116
    105117        // put in JSON list
    106118        JSONArray rows = new JSONArray();
     
    108120        for (JSONObject result : results) {
    109121            cnt += 1;
    110             if (offset > 0 && cnt < offset) continue;
     122            if (offset > 0 && cnt < offset)
     123                continue;
    111124            rows.put(result);
    112             if (limit > 0 && cnt >= limit) break;
     125            if (limit > 0 && cnt >= limit)
     126                break;
    113127        }
    114128
  • 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 }
  • 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 }
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java

    r61 r65  
    44package de.mpiwg.itgroup.annotations.restlet;
    55
     6import java.io.UnsupportedEncodingException;
     7import java.net.URLDecoder;
    68import java.util.List;
    79
     
    4244
    4345        String jsonId = (String) getRequest().getAttributes().get("id");
     46        if (jsonId != null) {
     47            // URL decode
     48            try {
     49                jsonId = URLDecoder.decode(jsonId, "UTF-8");
     50            } catch (UnsupportedEncodingException e) {
     51                // this shouldn't happen
     52            }
     53        }
    4454        String uri = decodeJsonId(jsonId);
    4555
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java

    r58 r65  
    44package de.mpiwg.itgroup.annotations.restlet;
    55
     6import java.io.UnsupportedEncodingException;
     7import java.net.URLDecoder;
    68import java.util.List;
    79
     
    1820import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    1921
    20 
    2122/**
    2223 * API for accessing tags in the Annotation store.
    2324 *
    2425 * @author dwinter
    25  *
     26 * 
    2627 */
    2728public class AnnotatorTags extends AnnotatorResourceImpl {
     
    3132
    3233    /**
    33      * GET with JSON content-type.
     34     * GET with JSON content-type. 
    3435     * Parameters:
    35      *   user: short user name
     36     *   user: short user name 
    3637     *   uri: user uri
    37      *  
     38     *
    3839     * @param entity
    3940     * @return
     
    4344        logger.debug("AnnotatorGroups doGetJSON!");
    4445        setCorsHeaders();
    45        
     46
    4647        String jsonId = (String) getRequest().getAttributes().get("id");
    47         //String id = decodeJsonId(jsonId);
     48        // String id = decodeJsonId(jsonId);
    4849        String id = jsonId;
    4950        logger.debug("annotation-id=" + id);
    5051
    51         if (id==null){
    52         return getAllTags();
     52        if (id == null) {
     53            return getAllTags();
    5354        } else {
    54 
    55                 return getTag(id);
     55            // URL decode
     56            try {
     57                id = URLDecoder.decode(id, "UTF-8");
     58            } catch (UnsupportedEncodingException e) {
     59                // this shouldn't happen
     60            }
     61            return getTag(id);
    5662        }
    5763    }
    58    
    59     protected Representation getTag(String id){
    60           AnnotationStore store = getAnnotationStore();
    61           String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
    62           Node tagNode = store.getTagNodeByUri(tagUri);
    63           Tag tag = store.createTagFromNode(tagNode);
    64           JSONObject jo = new JSONObject();
    65           try {
    66               jo.put("id", tag.getId());
    67               jo.put("name", tag.getName());
    68               jo.put("uri", tag.getUri());
    69           } catch (JSONException e) {
    70           }
    71          
    72           return new JsonRepresentation(jo);
     64
     65    protected Representation getTag(String id) {
     66        AnnotationStore store = getAnnotationStore();
     67        String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
     68        Node tagNode = store.getTagNodeByUri(tagUri);
     69        Tag tag = store.createTagFromNode(tagNode);
     70        JSONObject jo = new JSONObject();
     71        try {
     72            jo.put("id", tag.getId());
     73            jo.put("name", tag.getName());
     74            jo.put("uri", tag.getUri());
     75        } catch (JSONException e) {
     76        }
     77
     78        return new JsonRepresentation(jo);
    7379    }
    74         protected Representation getAllTags() {
    75                 JSONArray results = new JSONArray();
     80
     81    protected Representation getAllTags() {
     82        JSONArray results = new JSONArray();
    7683        AnnotationStore store = getAnnotationStore();
    77        
    78        
    79             List<Tag> tags = store.getTags(null, null);
    80             for (Tag tag : tags) {
    81                 JSONObject jo = new JSONObject();
    82                 try {
    83                     jo.put("id", tag.getId());
    84                     jo.put("name", tag.getName());
    85                     jo.put("uri", tag.getUri());
    86                 } catch (JSONException e) {
    87                 }
    88                 results.put(jo);
     84
     85        List<Tag> tags = store.getTags(null, null);
     86        for (Tag tag : tags) {
     87            JSONObject jo = new JSONObject();
     88            try {
     89                jo.put("id", tag.getId());
     90                jo.put("name", tag.getName());
     91                jo.put("uri", tag.getUri());
     92            } catch (JSONException e) {
    8993            }
    90        
     94            results.put(jo);
     95        }
     96
    9197        // assemble result object
    9298        JSONObject result = new JSONObject();
     
    101107        logger.debug(result);
    102108        return new JsonRepresentation(result);
    103         }               
    104        
     109    }
    105110
    106    
    107111}
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java

    r50 r65  
    33 */
    44package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
     5
     6import java.io.UnsupportedEncodingException;
     7import java.net.URLDecoder;
    58
    69import org.apache.log4j.Logger;
     
    4952        // get annotation from store
    5053        if (requestId != null) {
     54            // URL decode
     55            try {
     56                requestId = URLDecoder.decode(requestId, "UTF-8");
     57            } catch (UnsupportedEncodingException e) {
     58                // this shouldn't happen
     59            }
    5160            // the ID in the path is encoded
    5261            String id = Annotation.decodeId(requestId);
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java

    r32 r65  
    33 */
    44package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
     5
     6import java.io.UnsupportedEncodingException;
     7import java.net.URLDecoder;
    58
    69import org.apache.log4j.Logger;
     
    4952        // get group from store
    5053        if (requestId != null) {
     54            // URL decode
     55            try {
     56                requestId = URLDecoder.decode(requestId, "UTF-8");
     57            } catch (UnsupportedEncodingException e) {
     58                // this shouldn't happen
     59            }
    5160            group = (Group) store.getActor(new Group(requestId));
    5261        }
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.java

    r32 r65  
    33 */
    44package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
     5
     6import java.io.UnsupportedEncodingException;
     7import java.net.URLDecoder;
    58
    69import org.apache.log4j.Logger;
     
    4245        // id from URI /annotations/persons/{id}
    4346        requestId = (String) getRequest().getAttributes().get("id");
    44         logger.debug("group-id=" + requestId);
     47        logger.debug("person-id=" + requestId);
    4548        // get store instance
    4649        if (store == null) {
    4750            store = ((BaseRestlet) getApplication()).getAnnotationStore();
    4851        }
    49         // get group from store
     52        // get person from store
    5053        if (requestId != null) {
     54            // URL decode
     55            try {
     56                requestId = URLDecoder.decode(requestId, "UTF-8");
     57            } catch (UnsupportedEncodingException e) {
     58                // this shouldn't happen
     59            }
    5160            person = (Person) store.getActor(new Person(requestId));
    5261        }
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java

    r50 r65  
    5757        String form = f.getFirstValue("form");
    5858        if (form != null && form.equals("new_person")) {
    59             // output new group form
     59            // output new person form
    6060            result = "<html><body>\n";
    6161            result += "<h1>New person</h1>\n";
     
    6969            result += "</form>\n</body>\n</html>";
    7070        } else {
    71             // list all groups
     71            // list all persons
    7272            result = "<html><body>\n<h1>Persons</h1>\n<table>";
    7373            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
     
    7575            for (Person person : persons) {
    7676                Reference url = this.getReference().clone();
    77                 url.addSegment(person.getId());
     77                url.addSegment(person.getIdString());
    7878                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
    7979                        person.getIdString(), person.getName(), person.getUri());
     
    8787
    8888    /**
    89      * POST creates a new Group.
     89     * POST creates a new person.
    9090     *
    9191     * @return
Note: See TracChangeset for help on using the changeset viewer.