Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java @ 65:c0dd5314bada
deal with special characters in urls.
author | casties |
---|---|
date | Wed, 05 Dec 2012 15:36:43 +0100 |
parents | f5c0e6df7e88 |
children | 2b1e6df5e21a |
comparison
equal
deleted
inserted
replaced
64:c48435e7f312 | 65:c0dd5314bada |
---|---|
1 package de.mpiwg.itgroup.annotations.restlet; | 1 package de.mpiwg.itgroup.annotations.restlet; |
2 | 2 |
3 import java.io.UnsupportedEncodingException; | |
4 import java.net.URLDecoder; | |
3 import java.util.ArrayList; | 5 import java.util.ArrayList; |
4 import java.util.List; | 6 import java.util.List; |
5 | 7 |
6 import org.json.JSONArray; | 8 import org.json.JSONArray; |
7 import org.json.JSONException; | 9 import org.json.JSONException; |
8 import org.json.JSONObject; | 10 import org.json.JSONObject; |
9 import org.restlet.data.Form; | 11 import org.restlet.data.Form; |
10 import org.restlet.data.Parameter; | |
11 import org.restlet.data.Status; | 12 import org.restlet.data.Status; |
12 import org.restlet.ext.json.JsonRepresentation; | 13 import org.restlet.ext.json.JsonRepresentation; |
13 import org.restlet.representation.Representation; | 14 import org.restlet.representation.Representation; |
14 import org.restlet.resource.Get; | 15 import org.restlet.resource.Get; |
15 | 16 |
20 | 21 |
21 /** | 22 /** |
22 * API for accessing tags in the Annotation store. | 23 * API for accessing tags in the Annotation store. |
23 * | 24 * |
24 * @author dwinter | 25 * @author dwinter |
25 * | 26 * |
26 */ | 27 */ |
27 public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl { | 28 public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl { |
28 protected String getAllowedMethodsForHeader() { | 29 protected String getAllowedMethodsForHeader() { |
29 return "OPTIONS,GET"; | 30 return "OPTIONS,GET"; |
30 } | 31 } |
31 | 32 |
32 @Get("json") | 33 @Get("json") |
33 public Representation doGetJSON(Representation entity) { | 34 public Representation doGetJSON(Representation entity) { |
34 logger.debug("AnnotatorAnnotatonsBytag doGetJSON!"); | 35 logger.debug("AnnotatorAnnotatonsBytag doGetJSON!"); |
35 setCorsHeaders(); | 36 setCorsHeaders(); |
36 | 37 |
37 // do authentication | 38 // do authentication |
38 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); | 39 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
39 logger.debug("request authenticated=" + authUser); | 40 logger.debug("request authenticated=" + authUser); |
40 | 41 |
41 | 42 String id = (String) getRequest().getAttributes().get("id"); |
42 String jsonId = (String) getRequest().getAttributes().get("id"); | 43 // URL decode |
43 | 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 | |
44 Form form = getRequest().getResourceRef().getQueryAsForm(); | 51 Form form = getRequest().getResourceRef().getQueryAsForm(); |
45 String sortBy=null; | 52 String sortBy = form.getFirstValue("sortBy"); |
46 for (Parameter parameter : form) { | 53 |
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 | |
56 AnnotationStore store = getAnnotationStore(); | 54 AnnotationStore store = getAnnotationStore(); |
57 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; | 55 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; |
58 List<Annotation> annotations = store.getAnnotationsByTag(tagUri); | 56 List<Annotation> annotations = store.getAnnotationsByTag(tagUri); |
59 | 57 |
60 //JSONArray results = new JSONArray(); | 58 // JSONArray results = new JSONArray(); |
61 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); | 59 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); |
62 | 60 |
63 for (Annotation annot : annotations) { | 61 for (Annotation annot : annotations) { |
64 //check permission | 62 // check permission |
65 if (!annot.isActionAllowed("read", authUser, store)) continue; | 63 if (!annot.isActionAllowed("read", authUser, store)) |
66 | 64 continue; |
67 JSONObject jo = createAnnotatorJson(annot,false); | 65 |
66 JSONObject jo = createAnnotatorJson(annot, false); | |
68 results.add(jo); | 67 results.add(jo); |
69 } | 68 } |
70 | 69 |
71 if (sortBy!=null){ | 70 if (sortBy != null) { |
72 JSONObjectComparator.sortAnnotations(results,sortBy); | 71 JSONObjectComparator.sortAnnotations(results, sortBy); |
73 } | 72 } |
74 | 73 |
75 JSONArray resultsJa = new JSONArray(); | 74 JSONArray resultsJa = new JSONArray(); |
76 for (JSONObject result:results){ | 75 for (JSONObject result : results) { |
77 resultsJa.put(result); | 76 resultsJa.put(result); |
78 } | 77 } |
79 | 78 |
80 // assemble result object | 79 // assemble result object |
81 JSONObject result = new JSONObject(); | 80 JSONObject result = new JSONObject(); |
82 try { | 81 try { |
83 result.put("rows", resultsJa); | 82 result.put("rows", resultsJa); |
84 result.put("total", resultsJa.length()); | 83 result.put("total", resultsJa.length()); |
85 } catch (JSONException e) { | 84 } catch (JSONException e) { |
86 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | 85 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
87 return null; | 86 return null; |
87 } | |
88 return new JsonRepresentation(result); | |
88 } | 89 } |
89 logger.debug("sending:"); | 90 |
90 logger.debug(result); | |
91 return new JsonRepresentation(result); | |
92 } | 91 } |
93 | |
94 | |
95 | |
96 } |