annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.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 acd44dfec9c8
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: 64
diff changeset
3 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
4 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
5 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
6 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
7 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
8 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
9 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
10 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
11 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
12 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
13 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
14 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
17 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
18 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
19 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
20 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
22 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
23 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 64
diff changeset
24
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
25 import java.io.UnsupportedEncodingException;
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
26 import java.text.SimpleDateFormat;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
27 import java.util.ArrayList;
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
28 import java.util.Calendar;
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
29 import java.util.HashSet;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
30 import java.util.List;
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
31 import java.util.Set;
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 73
diff changeset
32 import java.util.logging.Logger;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
33 import java.util.regex.Matcher;
47b53ae385d1 merging old code
casties
parents:
diff changeset
34 import java.util.regex.Pattern;
47b53ae385d1 merging old code
casties
parents:
diff changeset
35
47b53ae385d1 merging old code
casties
parents:
diff changeset
36 import net.oauth.jsontoken.Checker;
47b53ae385d1 merging old code
casties
parents:
diff changeset
37 import net.oauth.jsontoken.JsonToken;
47b53ae385d1 merging old code
casties
parents:
diff changeset
38 import net.oauth.jsontoken.JsonTokenParser;
47b53ae385d1 merging old code
casties
parents:
diff changeset
39 import net.oauth.jsontoken.SystemClock;
47b53ae385d1 merging old code
casties
parents:
diff changeset
40 import net.oauth.jsontoken.crypto.HmacSHA256Verifier;
47b53ae385d1 merging old code
casties
parents:
diff changeset
41 import net.oauth.jsontoken.crypto.Verifier;
47b53ae385d1 merging old code
casties
parents:
diff changeset
42
47b53ae385d1 merging old code
casties
parents:
diff changeset
43 import org.apache.commons.codec.binary.Base64;
47b53ae385d1 merging old code
casties
parents:
diff changeset
44 import org.json.JSONArray;
47b53ae385d1 merging old code
casties
parents:
diff changeset
45 import org.json.JSONException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
46 import org.json.JSONObject;
91
cf44d9e1a4a7 let CORS be handled by Restlet 2.3 CorsFilter.
casties
parents: 90
diff changeset
47 import org.restlet.data.Header;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
48 import org.restlet.data.Status;
47b53ae385d1 merging old code
casties
parents:
diff changeset
49 import org.restlet.representation.Representation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
50 import org.restlet.resource.ServerResource;
72
4c2cea836bc0 restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents: 70
diff changeset
51 import org.restlet.util.Series;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
52
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
53 import com.google.gson.JsonArray;
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
54 import com.google.gson.JsonElement;
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
55 import com.google.gson.JsonObject;
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
56
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
57 import de.mpiwg.itgroup.annotations.Actor;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
58 import de.mpiwg.itgroup.annotations.Annotation;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
59 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
60 import de.mpiwg.itgroup.annotations.Group;
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
61 import de.mpiwg.itgroup.annotations.Person;
48
0e00bf8e27fb targets and resources of Annotation object are objects now.
casties
parents: 40
diff changeset
62 import de.mpiwg.itgroup.annotations.Resource;
0e00bf8e27fb targets and resources of Annotation object are objects now.
casties
parents: 40
diff changeset
63 import de.mpiwg.itgroup.annotations.Target;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
64 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
65
47b53ae385d1 merging old code
casties
parents:
diff changeset
66 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
67 * Base class for Annotator resource classes.
47b53ae385d1 merging old code
casties
parents:
diff changeset
68 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
69 * @author dwinter, casties
47b53ae385d1 merging old code
casties
parents:
diff changeset
70 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
71 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
72 public abstract class AnnotatorResourceImpl extends ServerResource {
47b53ae385d1 merging old code
casties
parents:
diff changeset
73
89
247cbbb385de improved logging.
casties
parents: 88
diff changeset
74 protected static Logger logger = Logger.getLogger(AnnotatorResourceImpl.class.getCanonicalName());
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
75
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
76 private AnnotationStore store;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
77
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
78 protected AnnotationStore getAnnotationStore() {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
79 if (store == null) {
19
f0f55ab768c9 more work on HTML UI.
casties
parents: 18
diff changeset
80 store = ((BaseRestlet) getApplication()).getAnnotationStore();
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
81 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
82 return store;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
83 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
84
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
85 public String encodeJsonId(String id) {
64
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
86 if (id == null)
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
87 return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
88 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
89 return Base64.encodeBase64URLSafeString(id.getBytes("UTF-8"));
47b53ae385d1 merging old code
casties
parents:
diff changeset
90 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
91 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
92 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
93 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
94
47b53ae385d1 merging old code
casties
parents:
diff changeset
95 public String decodeJsonId(String id) {
64
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
96 if (id == null)
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
97 return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
98 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
99 return new String(Base64.decodeBase64(id), "UTF-8");
47b53ae385d1 merging old code
casties
parents:
diff changeset
100 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
101 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
102 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
103 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
104
47b53ae385d1 merging old code
casties
parents:
diff changeset
105 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
106 * returns if authentication information from headers is valid.
47b53ae385d1 merging old code
casties
parents:
diff changeset
107 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
108 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
109 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
110 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
111 public boolean isAuthenticated(Representation entity) {
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
112 return (getUserFromAuthToken(entity) != null);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
113 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
114
47b53ae385d1 merging old code
casties
parents:
diff changeset
115 /**
57
4efb21cf0ce0 new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents: 52
diff changeset
116 * Checks Annotator Auth plugin authentication information from headers.
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
117 * Returns userId if successful. Returns "anonymous" in non-authorization
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
118 * mode.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
119 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
120 * @param entity
86
e3f0613b2f2d renamed getFullname to make it configurable.
casties
parents: 85
diff changeset
121 * @return user-id
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
122 */
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
123 public Person getUserFromAuthToken(Representation entity) {
72
4c2cea836bc0 restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents: 70
diff changeset
124 @SuppressWarnings("unchecked")
4c2cea836bc0 restlet 2.1 works now. (it's the start() method, stupid!)
casties
parents: 70
diff changeset
125 Series<Header> requestHeaders = (Series<Header>) getRequest().getAttributes().get("org.restlet.http.headers");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
126 String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true);
57
4efb21cf0ce0 new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents: 52
diff changeset
127 if (authToken == null) {
4efb21cf0ce0 new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents: 52
diff changeset
128 if (!((BaseRestlet) getApplication()).isAuthorizationMode()) {
89
247cbbb385de improved logging.
casties
parents: 88
diff changeset
129 // no token, no-auth mode -> anonymous
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
130 return Person.getAnonymous();
57
4efb21cf0ce0 new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents: 52
diff changeset
131 }
89
247cbbb385de improved logging.
casties
parents: 88
diff changeset
132 // no token, auth mode -> null
57
4efb21cf0ce0 new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents: 52
diff changeset
133 return null;
4efb21cf0ce0 new non-authorized mode without tokens. enabled by default. configured with annotationmanager.authorization=false property.
casties
parents: 52
diff changeset
134 }
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
135 try {
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
136 // decode token first to get consumer key
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
137 JsonToken token = new JsonTokenParser(null, null).deserialize(authToken);
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
138 String consumerKey = token.getParamAsPrimitive("consumerKey").getAsString();
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
139 // get stored consumer secret for key
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
140 BaseRestlet restServer = (BaseRestlet) getApplication();
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
141 String consumerSecret = restServer.getConsumerSecret(consumerKey);
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
142 logger.fine("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
143 if (consumerSecret == null) {
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
144 logger.warning("Error: unknown consumer key: "+consumerKey);
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
145 return null;
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
146 }
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
147 // logger.fine(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm()));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
148 List<Verifier> verifiers = new ArrayList<Verifier>();
47b53ae385d1 merging old code
casties
parents:
diff changeset
149 // we only do HS256 yet
47b53ae385d1 merging old code
casties
parents:
diff changeset
150 verifiers.add(new HmacSHA256Verifier(consumerSecret.getBytes("UTF-8")));
47b53ae385d1 merging old code
casties
parents:
diff changeset
151 // verify token signature(should really be static...)
47b53ae385d1 merging old code
casties
parents:
diff changeset
152 new JsonTokenParser(new SystemClock(), null, (Checker[]) null).verify(token, verifiers);
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
153 // create Person
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
154 JsonObject payload = token.getPayloadAsJsonObject();
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
155 // userId is mandatory
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
156 String userId = payload.get("userId").getAsString();
89
247cbbb385de improved logging.
casties
parents: 88
diff changeset
157 Person user = new Person(userId);
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
158 // displayName is optional
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
159 if (payload.has("displayName")) {
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
160 user.name = payload.get("displayName").getAsString();
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
161 }
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
162 // memberOf groups is optional
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
163 if (payload.has("memberOf")) {
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
164 Set<String> groups = new HashSet<String>();
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
165 JsonArray jgroups = payload.get("memberOf").getAsJsonArray();
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
166 for (JsonElement jgroup : jgroups) {
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
167 groups.add(jgroup.getAsString());
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
168 }
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
169 user.groups = groups;
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
170 }
89
247cbbb385de improved logging.
casties
parents: 88
diff changeset
171 logger.fine("auth OK! user=" + user);
247cbbb385de improved logging.
casties
parents: 88
diff changeset
172 return user;
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
173 } catch (Exception e) {
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
174 logger.warning("Error checking auth token: "+e.toString());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
175 }
89
247cbbb385de improved logging.
casties
parents: 88
diff changeset
176 return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
177 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
178
47b53ae385d1 merging old code
casties
parents:
diff changeset
179 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
180 * creates Annotator-JSON from an Annotation object.
47b53ae385d1 merging old code
casties
parents:
diff changeset
181 *
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
182 * @param annot annotation object
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
183 * @param forAnonymous
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
184 * @return Annotator-JSON
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
185 */
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
186 public JSONObject createAnnotatorJson(Annotation annot, boolean forAnonymous) {
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
187 // return user as a JSON object (otherwise just as string)
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
188 boolean makeUserObject = true;
47b53ae385d1 merging old code
casties
parents:
diff changeset
189 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
190 try {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
191 jo.put("text", annot.getBodyText());
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
192 jo.put("uri", annot.getTargetBaseUri());
40
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
193 if (annot.getResourceUri() != null) {
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
194 jo.put("resource", annot.getResourceUri());
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
195 }
76
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
196 if (annot.getQuote() != null) {
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
197 jo.put("quote", annot.getQuote());
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
198 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
199
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
200 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
201 * user
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
202 */
64
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
203 Actor creator = annot.getCreator();
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
204 if (creator != null) {
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
205 if (makeUserObject) {
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
206 // create user object
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
207 JSONObject userObject = new JSONObject();
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
208 // save creator as uri
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
209 userObject.put("uri", creator.getUri());
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
210 // make short user id
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
211 String userId = creator.getIdString();
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
212 // set as id
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
213 userObject.put("id", userId);
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
214 // get full name
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
215 String userName = creator.getName();
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
216 if (userName == null) {
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
217 BaseRestlet restServer = (BaseRestlet) getApplication();
86
e3f0613b2f2d renamed getFullname to make it configurable.
casties
parents: 85
diff changeset
218 userName = restServer.getFullNameForId(userId);
64
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
219 }
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
220 userObject.put("name", userName);
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
221 // save user object
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
222 jo.put("user", userObject);
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
223 } else {
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
224 // save user as string
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
225 jo.put("user", annot.getCreatorUri());
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
226 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
227 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
228
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
229 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
230 * ranges
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
231 */
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
232 if (annot.getTargetFragment() != null) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
233 // we only look at the first xpointer
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
234 List<String> fragments = new ArrayList<String>();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
235 fragments.add(annot.getTargetFragment());
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
236 FragmentTypes xt = annot.getFragmentType();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
237 if (xt == FragmentTypes.XPOINTER) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
238 jo.put("ranges", transformToRanges(fragments));
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
239 } else if (xt == FragmentTypes.AREA) {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
240 jo.put("shapes", transformToShapes(fragments));
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
241 } else if (xt == FragmentTypes.WKT) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
242 jo.put("shapes", transformToShapes(fragments));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
243 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
244 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
245
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
246 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
247 * permissions
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
248 */
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
249 JSONObject perms = new JSONObject();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
250 jo.put("permissions", perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
251 // admin
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
252 JSONArray adminPerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
253 perms.put("admin", adminPerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
254 Actor adminPerm = annot.getAdminPermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
255 if (adminPerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
256 adminPerms.put(adminPerm.getIdString());
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
257 } else if (forAnonymous) {
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
258 // set something because its not allowed for anonymous
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
259 adminPerms.put("not-you");
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
260 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
261 // delete
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
262 JSONArray deletePerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
263 perms.put("delete", deletePerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
264 Actor deletePerm = annot.getDeletePermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
265 if (deletePerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
266 deletePerms.put(deletePerm.getIdString());
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
267 } else if (forAnonymous) {
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
268 // set something because its not allowed for anonymous
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
269 deletePerms.put("not-you");
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
270 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
271 // update
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
272 JSONArray updatePerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
273 perms.put("update", updatePerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
274 Actor updatePerm = annot.getUpdatePermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
275 if (updatePerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
276 updatePerms.put(updatePerm.getIdString());
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
277 } else if (forAnonymous) {
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
278 // set something because its not allowed for anonymous
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
279 updatePerms.put("not-you");
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
280 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
281 // read
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
282 JSONArray readPerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
283 perms.put("read", readPerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
284 Actor readPerm = annot.getReadPermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
285 if (readPerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
286 readPerms.put(readPerm.getIdString());
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
287 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
288
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
289 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
290 * tags
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
291 */
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
292 Set<String> tagset = annot.getTags();
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
293 if (tagset != null) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
294 JSONArray tags = new JSONArray();
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
295 jo.put("tags", tags);
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
296 for (String tag : tagset) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
297 tags.put(tag);
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
298 }
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
299 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
300
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
301 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
302 * id
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
303 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
304 // encode Annotation URL (=id) in base64
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
305 String annotUrl = annot.getUri();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
306 String annotId = encodeJsonId(annotUrl);
47b53ae385d1 merging old code
casties
parents:
diff changeset
307 jo.put("id", annotId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
308 return jo;
47b53ae385d1 merging old code
casties
parents:
diff changeset
309 } catch (JSONException e) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 73
diff changeset
310 logger.severe("Unable to create AnnotatorJSON! "+e);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
311 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
312 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
313 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
314
47b53ae385d1 merging old code
casties
parents:
diff changeset
315 private JSONArray transformToRanges(List<String> xpointers) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
316 JSONArray ja = new JSONArray();
47b53ae385d1 merging old code
casties
parents:
diff changeset
317 Pattern rg = Pattern
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
318 .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
319 Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
320 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
321 for (String xpointer : xpointers) {
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
322 // String decoded = URLDecoder.decode(xpointer, "utf-8");
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
323 String decoded = xpointer;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
324 Matcher m = rg.matcher(decoded);
47b53ae385d1 merging old code
casties
parents:
diff changeset
325 if (m.find()) {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
326 JSONObject jo = new JSONObject();
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
327 jo.put("start", m.group(1));
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
328 jo.put("startOffset", m.group(2));
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
329 jo.put("end", m.group(3));
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
330 jo.put("endOffset", m.group(4));
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
331 ja.put(jo);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
332 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
333 m = rg1.matcher(xpointer);
47b53ae385d1 merging old code
casties
parents:
diff changeset
334 if (m.find()) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
335 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
336 jo.put("start", m.group(1));
47b53ae385d1 merging old code
casties
parents:
diff changeset
337 jo.put("startOffset", m.group(2));
47b53ae385d1 merging old code
casties
parents:
diff changeset
338 ja.put(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
339 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
340 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
341 } catch (JSONException e) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 73
diff changeset
342 logger.severe("Unable to transform to ranges! "+e);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
343 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
344 return ja;
47b53ae385d1 merging old code
casties
parents:
diff changeset
345 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
346
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
347 private JSONArray transformToShapes(List<String> fragments) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
348 JSONArray ja = new JSONArray();
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
349 Pattern xywhPattern = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
350 Pattern wktPattern = Pattern.compile("wkt=(\\w+)\\(+([\\d\\.\\,\\ ]+)\\)+");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
351 try {
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
352 for (String fragment : fragments) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
353 Matcher xywhMatch = xywhPattern.matcher(fragment);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
354 Matcher wktMatch = wktPattern.matcher(fragment);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
355 if (xywhMatch.find()) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
356 // xywh rectangle fragment
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
357 String units = xywhMatch.group(1);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
358 float x = getFloat(xywhMatch.group(2));
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
359 float y = getFloat(xywhMatch.group(3));
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
360 float width = getFloat(xywhMatch.group(4));
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
361 float height = getFloat(xywhMatch.group(5));
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
362 JSONObject shape = new JSONObject();
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
363 JSONObject geom = new JSONObject();
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
364 geom.put("units", units);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
365 geom.put("x", x);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
366 geom.put("y", y);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
367 if (width == 0 || height == 0) {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
368 shape.put("type", "point");
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
369 shape.put("geometry", geom);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
370 } else {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
371 shape.put("type", "rectangle");
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
372 geom.put("width", width);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
373 geom.put("height", height);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
374 shape.put("geometry", geom);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
375 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
376 ja.put(shape);
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
377 } else if (wktMatch.find()) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
378 // wkt shape fragment
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
379 String type = wktMatch.group(1);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
380 String coordString = wktMatch.group(2);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
381 JSONObject shape = new JSONObject();
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
382 JSONObject geom = new JSONObject();
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
383 shape.put("type", type.toLowerCase());
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
384 // TODO: add units/crs to fragment?
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
385 geom.put("units", "fraction");
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
386 JSONArray coords = new JSONArray();
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
387 String[] coordPairs = coordString.split(", *");
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
388 for (String coordPairString : coordPairs) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
389 String[] coordPair = coordPairString.split(" +");
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
390 coords.put(new JSONArray(coordPair));
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
391 }
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
392 geom.put("coordinates", coords);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
393 shape.put("geometry", geom);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
394 ja.put(shape);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
395 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
396 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
397 } catch (JSONException e) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 73
diff changeset
398 logger.severe("Unable to transform to shapes! "+e);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
399 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
400 return ja;
47b53ae385d1 merging old code
casties
parents:
diff changeset
401 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
402
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
403 protected String parseShape(JSONObject shape) throws JSONException {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
404 String fragment = null;
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
405 String type = shape.getString("type");
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
406 JSONObject geom = shape.getJSONObject("geometry");
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
407 if (type.equalsIgnoreCase("point")) {
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
408 // point shape
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
409 String x = geom.getString("x");
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
410 String y = geom.getString("y");
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
411 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y);
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
412 } else if (type.equalsIgnoreCase("rectangle")) {
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
413 // rectangle shape
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
414 String x = geom.getString("x");
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
415 String y = geom.getString("y");
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
416 String width = geom.getString("width");
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
417 String height = geom.getString("height");
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
418 fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
419 } else if (type.equalsIgnoreCase("polygon")) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
420 // polygon shape
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
421 JSONArray coordArray = geom.getJSONArray("coordinates");
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
422 StringBuilder coords = new StringBuilder();
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
423 int numCoords = coordArray.length();
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
424 for (int i = 0; i < numCoords; ++i) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
425 JSONArray coordPair = coordArray.getJSONArray(i);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
426 coords.append(coordPair.getString(0));
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
427 coords.append(" ");
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
428 coords.append(coordPair.getString(1));
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
429 if (i < numCoords-1) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
430 coords.append(", ");
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
431 }
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
432 }
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
433 // TODO: add units/crs to wkt
85
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
434 // assume polygon with outer ring
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
435 fragment = String.format("wkt=POLYGON((%s))", coords);
85
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
436 } else if (type.equalsIgnoreCase("linestring")) {
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
437 // linestring (polyline) shape
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
438 JSONArray coordArray = geom.getJSONArray("coordinates");
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
439 StringBuilder coords = new StringBuilder();
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
440 int numCoords = coordArray.length();
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
441 for (int i = 0; i < numCoords; ++i) {
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
442 JSONArray coordPair = coordArray.getJSONArray(i);
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
443 coords.append(coordPair.getString(0));
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
444 coords.append(" ");
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
445 coords.append(coordPair.getString(1));
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
446 if (i < numCoords-1) {
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
447 coords.append(", ");
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
448 }
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
449 }
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
450 // TODO: add units/crs to wkt
ed51eadc82c5 add polyline annotation shape.
casties
parents: 84
diff changeset
451 fragment = String.format("wkt=LINESTRING(%s)", coords);
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
452 } else {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 73
diff changeset
453 logger.severe("Unable to parse this shape: " + shape);
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
454 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
455 return fragment;
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
456 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
457
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
458 protected String parseArea(JSONObject area) throws JSONException {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
459 String x = area.getString("x");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
460 String y = area.getString("y");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
461 String width = "0";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
462 String height = "0";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
463 if (area.has("width")) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
464 width = area.getString("width");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
465 height = area.getString("height");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
466 }
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
467 String fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
468 return fragment;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
469 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
470
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
471 protected String parseRange(JSONObject range) throws JSONException {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
472 String start = range.getString("start");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
473 String end = range.getString("end");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
474 String startOffset = range.getString("startOffset");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
475 String endOffset = range.getString("endOffset");
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
476 String fragment = String.format(
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
477 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start,
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
478 startOffset, end, endOffset);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
479 return fragment;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
480 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
481
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
482 /**
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
483 * Creates an Annotation object with data from JSON.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
484 *
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
485 * uses the specification from the annotator project: {@link https
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
486 * ://github.com/okfn/annotator/wiki/Annotation-format}
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
487 *
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
488 * The username will be transformed to an URI if not given already as URI,
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
489 * if not it will set to the MPIWG namespace defined in
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
490 * de.mpiwg.itgroup.annotationManager.Constants.NS
47b53ae385d1 merging old code
casties
parents:
diff changeset
491 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
492 * @param jo
47b53ae385d1 merging old code
casties
parents:
diff changeset
493 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
494 * @throws JSONException
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
495 * @throws UnsupportedEncodingException
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
496 */
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
497 public Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException, UnsupportedEncodingException {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
498 return updateAnnotation(new Annotation(), jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
499 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
500
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
501 /**
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
502 * Updates an Annotation object with data from JSON.
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
503 *
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
504 * uses the specification from the annotator project: {@link https
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
505 * ://github.com/okfn/annotator/wiki/Annotation-format}
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
506 *
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
507 * The username will be transformed to an URI if not given already as URI,
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
508 * if not it will set to the MPIWG namespace defined in
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
509 * de.mpiwg.itgroup.annotationManager.Constants.NS
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
510 *
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
511 * @param annot
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
512 * @param jo
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
513 * @return
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
514 * @throws JSONException
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
515 * @throws UnsupportedEncodingException
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
516 */
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
517 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException,
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
518 UnsupportedEncodingException {
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
519 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
520 * target uri
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
521 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
522 if (jo.has("uri")) {
48
0e00bf8e27fb targets and resources of Annotation object are objects now.
casties
parents: 40
diff changeset
523 annot.setTarget(new Target(jo.getString("uri")));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
524 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
525 /*
40
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
526 * resource uri
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
527 */
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
528 if (jo.has("resource")) {
48
0e00bf8e27fb targets and resources of Annotation object are objects now.
casties
parents: 40
diff changeset
529 annot.setResource(new Resource(jo.getString("resource")));
40
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
530 }
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
531 /*
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
532 * annotation text
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
533 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
534 if (jo.has("text")) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
535 annot.setBodyText(jo.getString("text"));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
536 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
537 /*
76
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
538 * annotation quote
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
539 */
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
540 if (jo.has("quote")) {
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
541 annot.setQuote(jo.getString("quote"));
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
542 }
4e2dc67997a0 save text quote from Annotator.
casties
parents: 75
diff changeset
543 /*
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
544 * check authentication
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
545 */
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
546 Person authUser = getUserFromAuthToken(entity);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
547 if (authUser == null) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
548 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
549 * // try http auth User httpUser = getHttpAuthUser(entity); if
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
550 * (httpUser == null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
551 */
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
552 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
553 return null;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
554 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
555 * } authUser = httpUser.getIdentifier();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
556 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
557 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
558 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
559 * get or create creator object
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
560 */
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
561 Actor creator = annot.getCreator();
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
562 if (creator == null) {
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
563 creator = new Person();
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
564 annot.setCreator(creator);
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
565 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
566 // username not required, if no username given authuser will be used
47b53ae385d1 merging old code
casties
parents:
diff changeset
567 String username = null;
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
568 String userUri = creator.getUri();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
569 if (jo.has("user")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
570 if (jo.get("user") instanceof String) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
571 // user is just a String
47b53ae385d1 merging old code
casties
parents:
diff changeset
572 username = jo.getString("user");
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
573 creator.setId(username);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
574 // TODO: what if username and authUser are different?
47b53ae385d1 merging old code
casties
parents:
diff changeset
575 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
576 // user is an object
47b53ae385d1 merging old code
casties
parents:
diff changeset
577 JSONObject user = jo.getJSONObject("user");
47b53ae385d1 merging old code
casties
parents:
diff changeset
578 if (user.has("id")) {
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
579 String id = user.getString("id");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
580 creator.setId(id);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
581 username = id;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
582 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
583 if (user.has("uri")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
584 userUri = user.getString("uri");
47b53ae385d1 merging old code
casties
parents:
diff changeset
585 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
586 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
587 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
588 if (username == null) {
88
b406507a953d upped version to 0.5.
casties
parents: 86
diff changeset
589 username = authUser.getName();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
590 }
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
591 // try to get full name
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
592 if (creator.getName() == null && username != null) {
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents: 16
diff changeset
593 BaseRestlet restServer = (BaseRestlet) getApplication();
86
e3f0613b2f2d renamed getFullname to make it configurable.
casties
parents: 85
diff changeset
594 String fullName = restServer.getFullNameForId(username);
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
595 creator.setName(fullName);
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
596 }
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
597 // userUri should be a URI, if not it will set to the MPIWG namespace
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
598 if (userUri == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
599 if (username.startsWith("http")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
600 userUri = username;
47b53ae385d1 merging old code
casties
parents:
diff changeset
601 } else {
58
f5c0e6df7e88 made uri prefixes in store configurable.
casties
parents: 57
diff changeset
602 userUri = BaseRestlet.PERSONS_URI_PREFIX + username;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
603 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
604 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
605 // TODO: should we overwrite the creator?
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
606 if (creator.getUri() == null) {
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
607 creator.setUri(userUri);
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
608 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
609 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
610 * creation date
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
611 */
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
612 if (annot.getCreated() == null) {
95
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
613 // new - set creation date
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
614 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
615 String ct = format.format(Calendar.getInstance().getTime());
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
616 annot.setCreated(ct);
95
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
617 } else {
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
618 /*
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
619 * update date
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
620 */
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
621 // not new - set last update date
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
622 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
623 String ct = format.format(Calendar.getInstance().getTime());
acd44dfec9c8 added last update field for annotations in database.
casties
parents: 91
diff changeset
624 annot.setUpdated(ct);
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
625 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
626
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
627 /*
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
628 * create fragment from the first range/area
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
629 */
52
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
630 try {
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
631 if (jo.has("ranges")) {
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
632 JSONArray ranges = jo.getJSONArray("ranges");
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
633 if (ranges.length() > 0) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
634 JSONObject range = ranges.getJSONObject(0);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
635 annot.setFragmentType(FragmentTypes.XPOINTER);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
636 String fragment = parseRange(range);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
637 annot.setTargetFragment(fragment);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
638 }
52
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
639 }
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
640 } catch (JSONException e) {
102
9140017e8962 fix bug with empty username. add logging for JSON exceptions.
casties
parents: 95
diff changeset
641 logger.warning(e.toString());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
642 }
52
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
643 try {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
644 if (jo.has("shapes")) {
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
645 JSONArray shapes = jo.getJSONArray("shapes");
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
646 if (shapes.length() > 0) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
647 JSONObject shape = shapes.getJSONObject(0);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
648 String fragment = parseShape(shape);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
649 annot.setTargetFragment(fragment);
84
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
650 if (fragment.startsWith("wkt=")) {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
651 annot.setFragmentType(FragmentTypes.WKT);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
652 } else {
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
653 annot.setFragmentType(FragmentTypes.AREA);
6bf38b5e30a8 also stores polygon shape image annotations.
casties
parents: 76
diff changeset
654 }
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
655 }
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
656 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
657 } catch (JSONException e) {
102
9140017e8962 fix bug with empty username. add logging for JSON exceptions.
casties
parents: 95
diff changeset
658 logger.warning(e.toString());
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
659 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
660 // deprecated areas type
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
661 try {
52
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
662 if (jo.has("areas")) {
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
663 JSONArray areas = jo.getJSONArray("areas");
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
664 if (areas.length() > 0) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
665 JSONObject area = areas.getJSONObject(0);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
666 annot.setFragmentType(FragmentTypes.AREA);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
667 String fragment = parseArea(area);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
668 annot.setTargetFragment(fragment);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
669 }
52
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
670 }
a52c597075dc more resilience to ranges and areas in JSON
casties
parents: 48
diff changeset
671 } catch (JSONException e) {
102
9140017e8962 fix bug with empty username. add logging for JSON exceptions.
casties
parents: 95
diff changeset
672 logger.warning(e.toString());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
673 }
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
674 // no fragment is an error
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
675 if (annot.getFragmentType() == null || annot.getTargetFragment() == null) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
676 throw new JSONException("Annotation has no valid target fragment!");
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
677 }
64
c48435e7f312 fix npe when an annotation has no creator.
casties
parents: 63
diff changeset
678
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
679 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
680 * permissions
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
681 */
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
682 if (jo.has("permissions")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
683 JSONObject permissions = jo.getJSONObject("permissions");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
684 if (permissions.has("admin")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
685 JSONArray perms = permissions.getJSONArray("admin");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
686 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
687 annot.setAdminPermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
688 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
689 if (permissions.has("delete")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
690 JSONArray perms = permissions.getJSONArray("delete");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
691 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
692 annot.setDeletePermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
693 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
694 if (permissions.has("update")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
695 JSONArray perms = permissions.getJSONArray("update");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
696 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
697 annot.setUpdatePermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
698 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
699 if (permissions.has("read")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
700 JSONArray perms = permissions.getJSONArray("read");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
701 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
702 annot.setReadPermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
703 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
704 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
705
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
706 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
707 * tags
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
708 */
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
709 if (jo.has("tags")) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
710 HashSet<String> tagset = new HashSet<String>();
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
711 JSONArray tags = jo.getJSONArray("tags");
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
712 for (int i = 0; i < tags.length(); ++i) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
713 tagset.add(tags.getString(i));
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
714 }
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
715 annot.setTags(tagset);
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
716 }
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
717
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
718 return annot;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
719 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
720
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
721 @SuppressWarnings("unused")
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
722 // i in for loop
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
723 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
724 Actor actor = null;
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
725 for (int i = 0; i < perms.length(); ++i) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
726 String perm = perms.getString(i);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
727 if (perm.toLowerCase().startsWith("group:")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
728 String groupId = perm.substring(6);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
729 actor = new Group(groupId);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
730 } else {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
731 actor = new Person(perm);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
732 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
733 // we just take the first one
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
734 break;
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
735 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
736 return actor;
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
737 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
738
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
739 public static float getFloat(String s) {
61
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
740 try {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
741 return Float.parseFloat(s);
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
742 } catch (NumberFormatException e) {
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
743 }
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
744 return 0f;
b8ef15c8c4a5 implemented new shape format for image annotations.
casties
parents: 58
diff changeset
745 }
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
746
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
747 public static int getInt(String s) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
748 try {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
749 return Integer.parseInt(s);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
750 } catch (NumberFormatException e) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
751 }
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
752 return 0;
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 61
diff changeset
753 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
754 }