Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 37:34b9d044d0bf
authorisation added
js / css aufgeraeumt
author | dwinter |
---|---|
date | Wed, 26 Sep 2012 14:48:41 +0200 |
parents | 8427930c5f88 |
children | 5d4260344db5 |
rev | line source |
---|---|
3 | 1 /** |
2 * Implements the "annotations" uri of the Annotator API. see | |
3 * <https://github.com/okfn/annotator/wiki/Storage> | |
4 */ | |
5 package de.mpiwg.itgroup.annotations.restlet; | |
6 | |
7 import java.io.IOException; | |
31 | 8 import java.util.ArrayList; |
9 import java.util.List; | |
3 | 10 |
31 | 11 import org.json.JSONArray; |
3 | 12 import org.json.JSONException; |
13 import org.json.JSONObject; | |
31 | 14 import org.restlet.data.Form; |
15 import org.restlet.data.Parameter; | |
3 | 16 import org.restlet.data.Status; |
17 import org.restlet.ext.json.JsonRepresentation; | |
18 import org.restlet.representation.Representation; | |
19 import org.restlet.resource.Delete; | |
20 import org.restlet.resource.Get; | |
21 import org.restlet.resource.Post; | |
22 import org.restlet.resource.Put; | |
23 | |
4 | 24 import de.mpiwg.itgroup.annotations.Annotation; |
15 | 25 import de.mpiwg.itgroup.annotations.Person; |
31 | 26 import de.mpiwg.itgroup.annotations.Tag; |
4 | 27 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
31 | 28 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; |
3 | 29 |
30 /** | |
4 | 31 * Implements the "annotations" uri of the Annotator API. see |
32 * <https://github.com/okfn/annotator/wiki/Storage> | |
3 | 33 * |
34 * @author dwinter, casties | |
35 * | |
36 */ | |
37 public class AnnotatorAnnotations extends AnnotatorResourceImpl { | |
38 | |
39 protected String getAllowedMethodsForHeader() { | |
40 return "OPTIONS,GET,POST,PUT,DELETE"; | |
41 } | |
42 | |
43 /** | |
44 * GET with JSON content-type. | |
45 * | |
46 * @param entity | |
47 * @return | |
48 */ | |
49 @Get("json") | |
50 public Representation doGetJSON(Representation entity) { | |
51 logger.debug("AnnotatorAnnotations doGetJSON!"); | |
52 setCorsHeaders(); | |
53 // id from URI /annotations/{id} | |
54 String jsonId = (String) getRequest().getAttributes().get("id"); | |
55 String id = decodeJsonId(jsonId); | |
56 logger.debug("annotation-id=" + id); | |
57 | |
37 | 58 |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
59 // do authentication |
15 | 60 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
61 logger.debug("request authenticated=" + authUser); |
3 | 62 |
37 | 63 if (id == null) { |
64 | |
65 return getAllAnnotations(authUser); | |
66 } | |
67 | |
68 | |
16 | 69 AnnotationStore store = getAnnotationStore(); |
70 Annotation annot = store.getAnnotationById(id); | |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
71 if (annot != null) { |
16 | 72 if (! annot.isActionAllowed("read", authUser, store)) { |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
73 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
74 return null; |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
75 } |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
76 JSONObject result = createAnnotatorJson(annot, (authUser == null)); |
4 | 77 logger.debug("sending:"); |
78 logger.debug(result); | |
79 return new JsonRepresentation(result); | |
80 } else { | |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
81 // not found |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
82 setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
83 return null; |
3 | 84 } |
85 } | |
86 | |
37 | 87 private Representation getAllAnnotations(Person authUser) { |
31 | 88 |
89 Form form = getRequest().getResourceRef().getQueryAsForm(); | |
90 String sortBy=null; | |
91 for (Parameter parameter : form) { | |
92 if (parameter.getName().equals("sortBy")){ | |
93 sortBy = parameter.getValue(); | |
94 } | |
95 } | |
96 | |
97 AnnotationStore store = getAnnotationStore(); | |
98 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); | |
99 | |
100 List<Annotation> annotations = store.getAnnotations(null, null); | |
101 for (Annotation annotation : annotations) { | |
37 | 102 //check permission |
103 if (!annotation.isActionAllowed("read", authUser, store)) continue; | |
104 | |
31 | 105 JSONObject jo = createAnnotatorJson(annotation,false); |
106 results.add(jo); | |
107 | |
108 } | |
109 | |
110 if (sortBy!=null){ | |
111 JSONObjectComparator.sortAnnotations(results,sortBy); | |
112 } | |
113 | |
114 JSONArray resultsJa = new JSONArray(); | |
115 for (JSONObject result:results){ | |
116 resultsJa.put(result); | |
117 } | |
118 | |
119 // assemble result object | |
120 JSONObject result = new JSONObject(); | |
121 try { | |
122 result.put("rows", resultsJa); | |
123 result.put("total", resultsJa.length()); | |
124 } catch (JSONException e) { | |
125 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
126 return null; | |
127 } | |
128 logger.debug("sending:"); | |
129 logger.debug(result); | |
130 return new JsonRepresentation(result); | |
131 } | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 /** | |
20
715aa11d138b
fixes in permission handling: admin and delete default to creator.
casties
parents:
16
diff
changeset
|
138 * POST with JSON content-type. Creates a new Annotation. |
3 | 139 * |
140 * @return | |
141 */ | |
142 @Post("json") | |
143 public Representation doPostJson(Representation entity) { | |
144 logger.debug("AnnotatorAnnotations doPostJSON!"); | |
145 // set headers | |
146 setCorsHeaders(); | |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
147 |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
148 // do authentication TODO: who's allowed to create? |
15 | 149 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
150 logger.debug("request authenticated=" + authUser); |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
151 if (authUser == null) { |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
152 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
153 return null; |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
154 } |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
155 |
3 | 156 Annotation annot = null; |
157 try { | |
158 JsonRepresentation jrep = new JsonRepresentation(entity); | |
159 JSONObject jo = jrep.getJsonObject(); | |
160 if (jo == null) { | |
161 setStatus(Status.SERVER_ERROR_INTERNAL); | |
162 return null; | |
163 } | |
164 // make sure id is not set for POST | |
165 jo.remove("id"); | |
166 // get Annotation object from posted JSON | |
167 annot = createAnnotation(jo, entity); | |
168 } catch (IOException e1) { | |
169 setStatus(Status.SERVER_ERROR_INTERNAL); | |
170 return null; | |
171 } catch (JSONException e) { | |
172 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
173 return null; | |
174 } | |
4 | 175 if (annot == null) { |
3 | 176 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
177 return null; | |
178 } | |
179 Annotation storedAnnot; | |
4 | 180 // store Annotation |
181 storedAnnot = getAnnotationStore().storeAnnotation(annot); | |
182 /* | |
183 * according to https://github.com/okfn/annotator/wiki/Storage we should | |
184 * return 303: see other. For now we return the annotation. | |
3 | 185 */ |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
186 JSONObject jo = createAnnotatorJson(storedAnnot, (authUser == null)); |
3 | 187 JsonRepresentation retRep = new JsonRepresentation(jo); |
188 return retRep; | |
189 } | |
190 | |
191 /** | |
20
715aa11d138b
fixes in permission handling: admin and delete default to creator.
casties
parents:
16
diff
changeset
|
192 * PUT with JSON content-type. Modifies an Annotation. |
3 | 193 * |
194 * @param entity | |
195 * @return | |
196 */ | |
197 @Put("json") | |
198 public Representation doPutJSON(Representation entity) { | |
199 logger.debug("AnnotatorAnnotations doPutJSON!"); | |
200 setCorsHeaders(); | |
201 // id from URI /annotations/{id} | |
202 String jsonId = (String) getRequest().getAttributes().get("id"); | |
203 String id = decodeJsonId(jsonId); | |
204 logger.debug("annotation-id=" + id); | |
205 | |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
206 // do authentication |
15 | 207 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
208 logger.debug("request authenticated=" + authUser); |
3 | 209 |
210 Annotation annot = null; | |
4 | 211 AnnotationStore store = getAnnotationStore(); |
3 | 212 try { |
213 JsonRepresentation jrep = new JsonRepresentation(entity); | |
214 JSONObject jo = jrep.getJsonObject(); | |
215 if (jo == null) { | |
216 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
217 return null; | |
218 } | |
219 // get stored Annotation | |
4 | 220 Annotation storedAnnot = store.getAnnotationById(id); |
221 if (storedAnnot == null) { | |
3 | 222 setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
223 return null; | |
224 } | |
16 | 225 if (! storedAnnot.isActionAllowed("update", authUser, store)) { |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
226 setStatus(Status.CLIENT_ERROR_FORBIDDEN); |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
227 return null; |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
228 } |
3 | 229 // update from posted JSON |
230 annot = updateAnnotation(storedAnnot, jo, entity); | |
231 // store Annotation | |
4 | 232 storedAnnot = store.storeAnnotation(annot); |
233 /* | |
234 * according to https://github.com/okfn/annotator/wiki/Storage we | |
235 * should return 303: see other. but the client doesn't like it | |
236 * setStatus(Status.REDIRECTION_SEE_OTHER); // go to same URL as | |
237 * this one Reference thisUrl = this.getReference(); | |
238 * this.getResponse().setLocationRef(thisUrl); | |
239 */ | |
3 | 240 // return new annotation |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
241 jo = createAnnotatorJson(storedAnnot, (authUser == null)); |
3 | 242 JsonRepresentation retRep = new JsonRepresentation(jo); |
243 return retRep; | |
244 } catch (JSONException e) { | |
245 e.printStackTrace(); | |
246 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
247 } catch (IOException e) { | |
248 e.printStackTrace(); | |
249 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error"); | |
250 } | |
251 return null; | |
252 } | |
253 | |
254 /** | |
20
715aa11d138b
fixes in permission handling: admin and delete default to creator.
casties
parents:
16
diff
changeset
|
255 * DELETE with JSON content-type. Deletes an Annotation. |
3 | 256 * |
257 * @param entity | |
258 * @return | |
259 */ | |
260 @Delete("json") | |
261 public Representation doDeleteJSON(Representation entity) { | |
262 logger.debug("AnnotatorAnnotations doDeleteJSON!"); | |
263 setCorsHeaders(); | |
264 // id from URI /annotations/{id} | |
265 String jsonId = (String) getRequest().getAttributes().get("id"); | |
266 String id = decodeJsonId(jsonId); | |
267 logger.debug("annotation-id=" + id); | |
268 | |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
269 // do authentication |
15 | 270 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
271 logger.debug("request authenticated=" + authUser); |
16 | 272 AnnotationStore store = getAnnotationStore(); |
273 Annotation annot = store.getAnnotationById(id); | |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
274 if (annot != null) { |
16 | 275 if (! annot.isActionAllowed("delete", authUser, store)) { |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
276 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
277 return null; |
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
278 } |
3 | 279 } |
14
629e15b345aa
permissions mostly work. need more server-side checking.
casties
parents:
4
diff
changeset
|
280 |
4 | 281 // delete annotation |
32
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
22
diff
changeset
|
282 store.deleteAnnotationById(id); |
4 | 283 setStatus(Status.SUCCESS_NO_CONTENT); |
3 | 284 return null; |
285 } | |
286 | |
287 } |