comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.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 /** 1 /**
2 * ReST API for accessing groups in the Annotation store. 2 * ReST API for accessing groups in the Annotation store.
3 */ 3 */
4 package de.mpiwg.itgroup.annotations.restlet; 4 package de.mpiwg.itgroup.annotations.restlet;
5 5
6 import java.io.UnsupportedEncodingException;
7 import java.net.URLDecoder;
6 import java.util.List; 8 import java.util.List;
7 9
8 import org.json.JSONArray; 10 import org.json.JSONArray;
9 import org.json.JSONException; 11 import org.json.JSONException;
10 import org.json.JSONObject; 12 import org.json.JSONObject;
15 import org.restlet.resource.Get; 17 import org.restlet.resource.Get;
16 18
17 import de.mpiwg.itgroup.annotations.Tag; 19 import de.mpiwg.itgroup.annotations.Tag;
18 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
19 21
20
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 AnnotatorTags extends AnnotatorResourceImpl { 28 public class AnnotatorTags extends AnnotatorResourceImpl {
28 protected String getAllowedMethodsForHeader() { 29 protected String getAllowedMethodsForHeader() {
29 return "OPTIONS,GET"; 30 return "OPTIONS,GET";
30 } 31 }
31 32
32 /** 33 /**
33 * GET with JSON content-type. 34 * GET with JSON content-type.
34 * Parameters: 35 * Parameters:
35 * user: short user name 36 * user: short user name
36 * uri: user uri 37 * uri: user uri
37 * 38 *
38 * @param entity 39 * @param entity
39 * @return 40 * @return
40 */ 41 */
41 @Get("json") 42 @Get("json")
42 public Representation doGetJSON(Representation entity) { 43 public Representation doGetJSON(Representation entity) {
43 logger.debug("AnnotatorGroups doGetJSON!"); 44 logger.debug("AnnotatorGroups doGetJSON!");
44 setCorsHeaders(); 45 setCorsHeaders();
45 46
46 String jsonId = (String) getRequest().getAttributes().get("id"); 47 String jsonId = (String) getRequest().getAttributes().get("id");
47 //String id = decodeJsonId(jsonId); 48 // String id = decodeJsonId(jsonId);
48 String id = jsonId; 49 String id = jsonId;
49 logger.debug("annotation-id=" + id); 50 logger.debug("annotation-id=" + id);
50 51
51 if (id==null){ 52 if (id == null) {
52 return getAllTags(); 53 return getAllTags();
53 } else { 54 } else {
54 55 // URL decode
55 return getTag(id); 56 try {
57 id = URLDecoder.decode(id, "UTF-8");
58 } catch (UnsupportedEncodingException e) {
59 // this shouldn't happen
60 }
61 return getTag(id);
56 } 62 }
57 } 63 }
58 64
59 protected Representation getTag(String id){ 65 protected Representation getTag(String id) {
60 AnnotationStore store = getAnnotationStore(); 66 AnnotationStore store = getAnnotationStore();
61 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; 67 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
62 Node tagNode = store.getTagNodeByUri(tagUri); 68 Node tagNode = store.getTagNodeByUri(tagUri);
63 Tag tag = store.createTagFromNode(tagNode); 69 Tag tag = store.createTagFromNode(tagNode);
64 JSONObject jo = new JSONObject(); 70 JSONObject jo = new JSONObject();
65 try { 71 try {
66 jo.put("id", tag.getId()); 72 jo.put("id", tag.getId());
67 jo.put("name", tag.getName()); 73 jo.put("name", tag.getName());
68 jo.put("uri", tag.getUri()); 74 jo.put("uri", tag.getUri());
69 } catch (JSONException e) { 75 } catch (JSONException e) {
70 } 76 }
71 77
72 return new JsonRepresentation(jo); 78 return new JsonRepresentation(jo);
73 } 79 }
74 protected Representation getAllTags() { 80
75 JSONArray results = new JSONArray(); 81 protected Representation getAllTags() {
82 JSONArray results = new JSONArray();
76 AnnotationStore store = getAnnotationStore(); 83 AnnotationStore store = getAnnotationStore();
77 84
78 85 List<Tag> tags = store.getTags(null, null);
79 List<Tag> tags = store.getTags(null, null); 86 for (Tag tag : tags) {
80 for (Tag tag : tags) { 87 JSONObject jo = new JSONObject();
81 JSONObject jo = new JSONObject(); 88 try {
82 try { 89 jo.put("id", tag.getId());
83 jo.put("id", tag.getId()); 90 jo.put("name", tag.getName());
84 jo.put("name", tag.getName()); 91 jo.put("uri", tag.getUri());
85 jo.put("uri", tag.getUri()); 92 } catch (JSONException e) {
86 } catch (JSONException e) {
87 }
88 results.put(jo);
89 } 93 }
90 94 results.put(jo);
95 }
96
91 // assemble result object 97 // assemble result object
92 JSONObject result = new JSONObject(); 98 JSONObject result = new JSONObject();
93 try { 99 try {
94 result.put("rows", results); 100 result.put("rows", results);
95 result.put("total", results.length()); 101 result.put("total", results.length());
98 return null; 104 return null;
99 } 105 }
100 logger.debug("sending:"); 106 logger.debug("sending:");
101 logger.debug(result); 107 logger.debug(result);
102 return new JsonRepresentation(result); 108 return new JsonRepresentation(result);
103 } 109 }
104
105 110
106
107 } 111 }