Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByResources.java @ 65:c0dd5314bada
deal with special characters in urls.
author | casties |
---|---|
date | Wed, 05 Dec 2012 15:36:43 +0100 |
parents | 64aa756c60cc |
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; |
20 | 22 |
21 /** | 23 /** |
22 * API for accessing tags in the Annotation store. | 24 * API for accessing tags in the Annotation store. |
23 * | 25 * |
24 * @author dwinter | 26 * @author dwinter |
25 * | 27 * |
26 */ | 28 */ |
27 public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl { | 29 public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl { |
28 protected String getAllowedMethodsForHeader() { | 30 protected String getAllowedMethodsForHeader() { |
29 return "OPTIONS,GET"; | 31 return "OPTIONS,GET"; |
30 } | 32 } |
31 | 33 |
32 @Get("json") | 34 @Get("json") |
33 public Representation doGetJSON(Representation entity) { | 35 public Representation doGetJSON(Representation entity) { |
34 logger.debug("AnnotatorAnnotatonsByResource doGetJSON!"); | 36 logger.debug("AnnotatorAnnotatonsByResource doGetJSON!"); |
35 setCorsHeaders(); | 37 setCorsHeaders(); |
36 | 38 |
37 // do authentication | 39 // do authentication |
38 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); | 40 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
39 logger.debug("request authenticated=" + authUser); | 41 logger.debug("request authenticated=" + authUser); |
40 | 42 |
41 | 43 String id = null; |
42 String jsonId = (String) getRequest().getAttributes().get("id"); | 44 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 | |
44 Form form = getRequest().getResourceRef().getQueryAsForm(); | 57 Form form = getRequest().getResourceRef().getQueryAsForm(); |
45 String sortBy=null; | 58 String sortBy = null; |
46 for (Parameter parameter : form) { | 59 for (Parameter parameter : form) { |
47 if (parameter.getName().equals("sortBy")){ | 60 if (parameter.getName().equals("sortBy")) { |
48 sortBy = parameter.getValue(); | 61 sortBy = parameter.getValue(); |
49 } | 62 } |
50 } | 63 } |
51 | 64 |
52 String id = decodeJsonId(jsonId); | |
53 //String id = jsonId; | |
54 logger.debug("ressource-id=" + id); | |
55 | |
56 AnnotationStore store = getAnnotationStore(); | 65 AnnotationStore store = getAnnotationStore(); |
57 //String tagUri=NS.MPIWG_TAGS_URL+id; | 66 // String tagUri=NS.MPIWG_TAGS_URL+id; |
58 List<Annotation> annotations = store.getAnnotationsByResource(id); | 67 List<Annotation> annotations = store.getAnnotationsByResource(id); |
59 | 68 |
60 //JSONArray results = new JSONArray(); | 69 // JSONArray results = new JSONArray(); |
61 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); | 70 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); |
62 | 71 |
63 for (Annotation annot : annotations) { | 72 for (Annotation annot : annotations) { |
64 //check permission | 73 // check permission |
65 if (!annot.isActionAllowed("read", authUser, store)) continue; | 74 if (!annot.isActionAllowed("read", authUser, store)) |
66 | 75 continue; |
67 JSONObject jo = createAnnotatorJson(annot,false); | 76 |
77 JSONObject jo = createAnnotatorJson(annot, false); | |
68 results.add(jo); | 78 results.add(jo); |
69 } | 79 } |
70 | 80 |
71 if (sortBy!=null){ | 81 if (sortBy != null) { |
72 JSONObjectComparator.sortAnnotations(results,sortBy); | 82 JSONObjectComparator.sortAnnotations(results, sortBy); |
73 } | 83 } |
74 | 84 |
75 JSONArray resultsJa = new JSONArray(); | 85 JSONArray resultsJa = new JSONArray(); |
76 for (JSONObject result:results){ | 86 for (JSONObject result : results) { |
77 resultsJa.put(result); | 87 resultsJa.put(result); |
78 } | 88 } |
79 | 89 |
80 // assemble result object | 90 // assemble result object |
81 JSONObject result = new JSONObject(); | 91 JSONObject result = new JSONObject(); |
82 try { | 92 try { |
83 result.put("rows", resultsJa); | 93 result.put("rows", resultsJa); |
84 result.put("total", resultsJa.length()); | 94 result.put("total", resultsJa.length()); |
85 } catch (JSONException e) { | 95 } catch (JSONException e) { |
86 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | 96 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
87 return null; | 97 return null; |
98 } | |
99 return new JsonRepresentation(result); | |
88 } | 100 } |
89 logger.debug("sending:"); | 101 |
90 logger.debug(result); | |
91 return new JsonRepresentation(result); | |
92 } | 102 } |
93 | |
94 | |
95 | |
96 } |