annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 61:b8ef15c8c4a5

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