annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 63:9f8c9611848a

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