comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 65:c0dd5314bada

deal with special characters in urls.
author casties
date Wed, 05 Dec 2012 15:36:43 +0100
parents 9f8c9611848a
children 2b1e6df5e21a
comparison
equal deleted inserted replaced
64:c48435e7f312 65:c0dd5314bada
3 * <https://github.com/okfn/annotator/wiki/Storage> 3 * <https://github.com/okfn/annotator/wiki/Storage>
4 */ 4 */
5 package de.mpiwg.itgroup.annotations.restlet; 5 package de.mpiwg.itgroup.annotations.restlet;
6 6
7 import java.io.IOException; 7 import java.io.IOException;
8 import java.io.UnsupportedEncodingException;
9 import java.net.URLDecoder;
8 import java.util.ArrayList; 10 import java.util.ArrayList;
9 import java.util.List; 11 import java.util.List;
10 12
11 import org.json.JSONArray; 13 import org.json.JSONArray;
12 import org.json.JSONException; 14 import org.json.JSONException;
47 @Get("json") 49 @Get("json")
48 public Representation doGetJSON(Representation entity) { 50 public Representation doGetJSON(Representation entity) {
49 logger.debug("AnnotatorAnnotations doGetJSON!"); 51 logger.debug("AnnotatorAnnotations doGetJSON!");
50 setCorsHeaders(); 52 setCorsHeaders();
51 // id from URI /annotations/{id} 53 // id from URI /annotations/{id}
54 String id = null;
52 String jsonId = (String) getRequest().getAttributes().get("id"); 55 String jsonId = (String) getRequest().getAttributes().get("id");
53 String id = decodeJsonId(jsonId); 56 if (jsonId != null) {
54 logger.debug("annotation-id=" + id); 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 }
55 66
56 // do authentication 67 // do authentication
57 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 68 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
58 logger.debug("request authenticated=" + authUser); 69 logger.debug("request authenticated=" + authUser);
59 70
84 } 95 }
85 96
86 private Representation getAllAnnotations(Person authUser, int limit, int offset, String sortBy) { 97 private Representation getAllAnnotations(Person authUser, int limit, int offset, String sortBy) {
87 AnnotationStore store = getAnnotationStore(); 98 AnnotationStore store = getAnnotationStore();
88 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); 99 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
89 100
90 // read all annotations 101 // read all annotations
91 List<Annotation> annotations = store.getAnnotations(null, null); 102 List<Annotation> annotations = store.getAnnotations(null, null);
92 for (Annotation annotation : annotations) { 103 for (Annotation annotation : annotations) {
93 // check permission 104 // check permission
94 if (!annotation.isActionAllowed("read", authUser, store)) continue; 105 if (!annotation.isActionAllowed("read", authUser, store))
106 continue;
95 // add annotation to list 107 // add annotation to list
96 JSONObject jo = createAnnotatorJson(annotation, false); 108 JSONObject jo = createAnnotatorJson(annotation, false);
97 results.add(jo); 109 results.add(jo);
98 } 110 }
99 111
100 // sort if necessary 112 // sort if necessary
101 if (sortBy != null) { 113 if (sortBy != null) {
102 JSONObjectComparator.sortAnnotations(results, sortBy); 114 JSONObjectComparator.sortAnnotations(results, sortBy);
103 } 115 }
104 116
105 // put in JSON list 117 // put in JSON list
106 JSONArray rows = new JSONArray(); 118 JSONArray rows = new JSONArray();
107 int cnt = 0; 119 int cnt = 0;
108 for (JSONObject result : results) { 120 for (JSONObject result : results) {
109 cnt += 1; 121 cnt += 1;
110 if (offset > 0 && cnt < offset) continue; 122 if (offset > 0 && cnt < offset)
123 continue;
111 rows.put(result); 124 rows.put(result);
112 if (limit > 0 && cnt >= limit) break; 125 if (limit > 0 && cnt >= limit)
126 break;
113 } 127 }
114 128
115 // assemble result object 129 // assemble result object
116 JSONObject result = new JSONObject(); 130 JSONObject result = new JSONObject();
117 try { 131 try {