annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 102:9140017e8962

fix bug with empty username. add logging for JSON exceptions.
author casties
date Thu, 09 Feb 2017 20:46:15 +0100
parents 7268c3ca025b
children 7417f5915181
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
1 package de.mpiwg.itgroup.annotations.restlet;
47b53ae385d1 merging old code
casties
parents:
diff changeset
2
70
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
3 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
4 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
5 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
6 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
7 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
8 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
9 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
10 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
11 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
12 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
13 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
14 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
17 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
18 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
19 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
20 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
22 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
23 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
24
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
25 import java.io.IOException;
65
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
26 import java.io.UnsupportedEncodingException;
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
27 import java.net.URLDecoder;
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
28 import java.util.ArrayList;
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
29 import java.util.List;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
30
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
31 import org.json.JSONArray;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
32 import org.json.JSONException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
33 import org.json.JSONObject;
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
34 import org.restlet.data.Form;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
35 import org.restlet.data.Status;
47b53ae385d1 merging old code
casties
parents:
diff changeset
36 import org.restlet.ext.json.JsonRepresentation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
37 import org.restlet.representation.Representation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
38 import org.restlet.resource.Delete;
47b53ae385d1 merging old code
casties
parents:
diff changeset
39 import org.restlet.resource.Get;
47b53ae385d1 merging old code
casties
parents:
diff changeset
40 import org.restlet.resource.Post;
47b53ae385d1 merging old code
casties
parents:
diff changeset
41 import org.restlet.resource.Put;
47b53ae385d1 merging old code
casties
parents:
diff changeset
42
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
43 import de.mpiwg.itgroup.annotations.Annotation;
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
44 import de.mpiwg.itgroup.annotations.Person;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
45 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
46 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
47
47b53ae385d1 merging old code
casties
parents:
diff changeset
48 /**
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
49 * Implements the "annotations" uri of the Annotator API. see
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
50 * <https://github.com/okfn/annotator/wiki/Storage>
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
51 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
52 * @author dwinter, casties
47b53ae385d1 merging old code
casties
parents:
diff changeset
53 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
54 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
55 public class AnnotatorAnnotations extends AnnotatorResourceImpl {
47b53ae385d1 merging old code
casties
parents:
diff changeset
56
47b53ae385d1 merging old code
casties
parents:
diff changeset
57 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
58 * GET with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
59 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
60 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
61 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
62 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
63 @Get("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
64 public Representation doGetJSON(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
65 logger.fine("AnnotatorAnnotations doGetJSON!");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
66 // id from URI /annotations/{id}
65
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
67 String id = null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
68 String jsonId = (String) getRequest().getAttributes().get("id");
65
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
69 if (jsonId != null) {
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
70 // URL decode
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
71 try {
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
72 jsonId = URLDecoder.decode(jsonId, "UTF-8");
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
73 } catch (UnsupportedEncodingException e) {
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
74 // this shouldn't happen
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
75 }
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
76 id = decodeJsonId(jsonId);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
77 logger.fine("annotation-id=" + id);
65
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
78 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
79
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
80 // do authentication
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
81 Person authUser = getUserFromAuthToken(entity);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
82 logger.fine("request authenticated=" + authUser);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
83
37
34b9d044d0bf authorisation added
dwinter
parents: 34
diff changeset
84 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
85 // 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
86 Form form = getRequest().getResourceRef().getQueryAsForm();
101
7268c3ca025b make admin ui view of all annotations scale better.
casties
parents: 91
diff changeset
87 int limit = getInt(form.getFirstValue("limit", "1000"));
7268c3ca025b make admin ui view of all annotations scale better.
casties
parents: 91
diff changeset
88 int offset = getInt(form.getFirstValue("offset", "0"));
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 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
90 return getAllAnnotations(authUser, limit, offset, sortBy);
37
34b9d044d0bf authorisation added
dwinter
parents: 34
diff changeset
91 }
34b9d044d0bf authorisation added
dwinter
parents: 34
diff changeset
92
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
93 // send annotation with id
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
94 AnnotationStore store = getAnnotationStore();
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
95 Annotation annot = store.getAnnotationById(id);
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
96 if (annot != null) {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
97 if (!annot.isActionAllowed("read", authUser, store)) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
98 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
99 return null;
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
100 }
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
101 JSONObject result = createAnnotatorJson(annot, (authUser == null));
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
102 return new JsonRepresentation(result);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
103 } else {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
104 // not found
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
105 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
106 return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
107 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
108 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
109
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
110 private Representation getAllAnnotations(Person authUser, int limit, int offset, String sortBy) {
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
111 AnnotationStore store = getAnnotationStore();
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
112 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
65
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
113
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
114 // read all annotations
101
7268c3ca025b make admin ui view of all annotations scale better.
casties
parents: 91
diff changeset
115 List<Annotation> annotations = store.getAnnotations(null, null, 0, 0);
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
116 for (Annotation annotation : annotations) {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
117 // check permission
65
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
118 if (!annotation.isActionAllowed("read", authUser, store))
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
119 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
120 // add annotation to list
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
121 JSONObject jo = createAnnotatorJson(annotation, false);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
122 results.add(jo);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
123 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
124
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
125 // sort if necessary
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
126 if (sortBy != null) {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
127 JSONObjectComparator.sortAnnotations(results, sortBy);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
128 }
65
c0dd5314bada deal with special characters in urls.
casties
parents: 63
diff changeset
129
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
130 // 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
131 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
132 int cnt = 0;
101
7268c3ca025b make admin ui view of all annotations scale better.
casties
parents: 91
diff changeset
133 int max = limit + offset;
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
134 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
135 cnt += 1;
101
7268c3ca025b make admin ui view of all annotations scale better.
casties
parents: 91
diff changeset
136 if (cnt < offset) 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
137 rows.put(result);
101
7268c3ca025b make admin ui view of all annotations scale better.
casties
parents: 91
diff changeset
138 if (limit > 0 && cnt >= max) break;
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
139 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
140
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
141 // assemble result object
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
142 JSONObject result = new JSONObject();
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
143 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
144 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
145 result.put("total", rows.length());
31
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
146 } catch (JSONException e) {
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
147 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
148 return null;
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
149 }
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
150 return new JsonRepresentation(result);
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
151 }
9f653697437e annotationbrowser
dwinter
parents: 22
diff changeset
152
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
153 /**
20
715aa11d138b fixes in permission handling: admin and delete default to creator.
casties
parents: 16
diff changeset
154 * POST with JSON content-type. Creates a new Annotation.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
155 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
156 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
157 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
158 @Post("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
159 public Representation doPostJson(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
160 logger.fine("AnnotatorAnnotations doPostJSON!");
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
161
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
162 // do authentication TODO: who's allowed to create?
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
163 Person authUser = getUserFromAuthToken(entity);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
164 logger.fine("request authenticated=" + authUser);
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
165 if (authUser == null) {
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
166 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
167 return null;
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
168 }
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
169
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
170 Annotation annot = null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
171 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
172 JsonRepresentation jrep = new JsonRepresentation(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
173 JSONObject jo = jrep.getJsonObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
174 if (jo == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
175 setStatus(Status.SERVER_ERROR_INTERNAL);
47b53ae385d1 merging old code
casties
parents:
diff changeset
176 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
177 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
178 // make sure id is not set for POST
47b53ae385d1 merging old code
casties
parents:
diff changeset
179 jo.remove("id");
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
180 // create Annotation object from posted JSON
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
181 annot = createAnnotation(jo, entity);
102
9140017e8962 fix bug with empty username. add logging for JSON exceptions.
casties
parents: 101
diff changeset
182 } catch (IOException e) {
9140017e8962 fix bug with empty username. add logging for JSON exceptions.
casties
parents: 101
diff changeset
183 logger.warning(e.toString());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
184 setStatus(Status.SERVER_ERROR_INTERNAL);
47b53ae385d1 merging old code
casties
parents:
diff changeset
185 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
186 } catch (JSONException e) {
102
9140017e8962 fix bug with empty username. add logging for JSON exceptions.
casties
parents: 101
diff changeset
187 logger.warning(e.toString());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
188 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
189 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
190 }
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
191 if (annot == null) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
192 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
193 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
194 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
195 Annotation storedAnnot;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
196 // store Annotation
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
197 storedAnnot = getAnnotationStore().storeAnnotation(annot);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
198 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
199 * according to https://github.com/okfn/annotator/wiki/Storage we should
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
200 * return 303: see other. For now we return the annotation.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
201 */
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
202 JSONObject jo = createAnnotatorJson(storedAnnot, (authUser == null));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
203 JsonRepresentation retRep = new JsonRepresentation(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
204 return retRep;
47b53ae385d1 merging old code
casties
parents:
diff changeset
205 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
206
47b53ae385d1 merging old code
casties
parents:
diff changeset
207 /**
20
715aa11d138b fixes in permission handling: admin and delete default to creator.
casties
parents: 16
diff changeset
208 * PUT with JSON content-type. Modifies an Annotation.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
209 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
210 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
211 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
212 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
213 @Put("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
214 public Representation doPutJSON(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
215 logger.fine("AnnotatorAnnotations doPutJSON!");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
216 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
217 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
218 String id = decodeJsonId(jsonId);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
219 logger.fine("annotation-id=" + id);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
220
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
221 // do authentication
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
222 Person authUser = getUserFromAuthToken(entity);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
223 logger.fine("request authenticated=" + authUser);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
224
47b53ae385d1 merging old code
casties
parents:
diff changeset
225 Annotation annot = null;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
226 AnnotationStore store = getAnnotationStore();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
227 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
228 JsonRepresentation jrep = new JsonRepresentation(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
229 JSONObject jo = jrep.getJsonObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
230 if (jo == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
231 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
232 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
233 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
234 // get stored Annotation
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
235 Annotation storedAnnot = store.getAnnotationById(id);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
236 if (storedAnnot == null) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
237 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
47b53ae385d1 merging old code
casties
parents:
diff changeset
238 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
239 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
240 if (!storedAnnot.isActionAllowed("update", authUser, store)) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
241 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
242 return null;
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
243 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
244 // update from posted JSON
47b53ae385d1 merging old code
casties
parents:
diff changeset
245 annot = updateAnnotation(storedAnnot, jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
246 // store Annotation
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
247 storedAnnot = store.storeAnnotation(annot);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
248 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
249 * according to https://github.com/okfn/annotator/wiki/Storage we
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
250 * should return 303: see other. but the client doesn't like it
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
251 * setStatus(Status.REDIRECTION_SEE_OTHER); // go to same URL as
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
252 * this one Reference thisUrl = this.getReference();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
253 * this.getResponse().setLocationRef(thisUrl);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
254 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
255 // return new annotation
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
256 jo = createAnnotatorJson(storedAnnot, (authUser == null));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
257 JsonRepresentation retRep = new JsonRepresentation(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
258 return retRep;
47b53ae385d1 merging old code
casties
parents:
diff changeset
259 } catch (JSONException e) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
260 logger.severe("Error in doPutJSON: "+e);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
261 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
262 } catch (IOException e) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
263 logger.severe("Error in doPutJSON: "+e);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
264 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
265 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
266 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
267 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
268
47b53ae385d1 merging old code
casties
parents:
diff changeset
269 /**
20
715aa11d138b fixes in permission handling: admin and delete default to creator.
casties
parents: 16
diff changeset
270 * DELETE with JSON content-type. Deletes an Annotation.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
271 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
272 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
273 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
274 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
275 @Delete("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
276 public Representation doDeleteJSON(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
277 logger.fine("AnnotatorAnnotations doDeleteJSON!");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
278 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
279 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
280 String id = decodeJsonId(jsonId);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
281 logger.fine("annotation-id=" + id);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
282
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
283 // do authentication
88
b406507a953d upped version to 0.5.
casties
parents: 75
diff changeset
284 Person authUser = getUserFromAuthToken(entity);
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
285 logger.fine("request authenticated=" + authUser);
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
286 AnnotationStore store = getAnnotationStore();
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
287 Annotation annot = store.getAnnotationById(id);
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
288 if (annot != null) {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 41
diff changeset
289 if (!annot.isActionAllowed("delete", authUser, store)) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
290 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
291 return null;
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
292 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
293 }
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
294 // delete annotation
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 22
diff changeset
295 store.deleteAnnotationById(id);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
296 setStatus(Status.SUCCESS_NO_CONTENT);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
297 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
298 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
299
47b53ae385d1 merging old code
casties
parents:
diff changeset
300 }