annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 40:03e0f7574224

saving and loading resource targets should work now (no searching yet)
author casties
date Wed, 26 Sep 2012 14:56:42 +0200
parents b1fb0d117877
children 0e00bf8e27fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
1 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
2 * Base class for Annotator resource classes.
47b53ae385d1 merging old code
casties
parents:
diff changeset
3 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
4 package de.mpiwg.itgroup.annotations.restlet;
47b53ae385d1 merging old code
casties
parents:
diff changeset
5
47b53ae385d1 merging old code
casties
parents:
diff changeset
6 import java.io.UnsupportedEncodingException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
7 import java.security.InvalidKeyException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
8 import java.security.SignatureException;
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
9 import java.text.SimpleDateFormat;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
10 import java.util.ArrayList;
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
11 import java.util.Calendar;
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
12 import java.util.HashSet;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
13 import java.util.List;
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
14 import java.util.Set;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
15 import java.util.regex.Matcher;
47b53ae385d1 merging old code
casties
parents:
diff changeset
16 import java.util.regex.Pattern;
47b53ae385d1 merging old code
casties
parents:
diff changeset
17
47b53ae385d1 merging old code
casties
parents:
diff changeset
18 import net.oauth.jsontoken.Checker;
47b53ae385d1 merging old code
casties
parents:
diff changeset
19 import net.oauth.jsontoken.JsonToken;
47b53ae385d1 merging old code
casties
parents:
diff changeset
20 import net.oauth.jsontoken.JsonTokenParser;
47b53ae385d1 merging old code
casties
parents:
diff changeset
21 import net.oauth.jsontoken.SystemClock;
47b53ae385d1 merging old code
casties
parents:
diff changeset
22 import net.oauth.jsontoken.crypto.HmacSHA256Verifier;
47b53ae385d1 merging old code
casties
parents:
diff changeset
23 import net.oauth.jsontoken.crypto.Verifier;
47b53ae385d1 merging old code
casties
parents:
diff changeset
24
47b53ae385d1 merging old code
casties
parents:
diff changeset
25 import org.apache.commons.codec.binary.Base64;
47b53ae385d1 merging old code
casties
parents:
diff changeset
26 import org.apache.log4j.Logger;
47b53ae385d1 merging old code
casties
parents:
diff changeset
27 import org.json.JSONArray;
47b53ae385d1 merging old code
casties
parents:
diff changeset
28 import org.json.JSONException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
29 import org.json.JSONObject;
47b53ae385d1 merging old code
casties
parents:
diff changeset
30 import org.restlet.data.Form;
47b53ae385d1 merging old code
casties
parents:
diff changeset
31 import org.restlet.data.Status;
47b53ae385d1 merging old code
casties
parents:
diff changeset
32 import org.restlet.representation.Representation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
33 import org.restlet.resource.Options;
47b53ae385d1 merging old code
casties
parents:
diff changeset
34 import org.restlet.resource.ServerResource;
47b53ae385d1 merging old code
casties
parents:
diff changeset
35
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
36 import de.mpiwg.itgroup.annotations.Actor;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
37 import de.mpiwg.itgroup.annotations.Annotation;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
38 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
39 import de.mpiwg.itgroup.annotations.Group;
19
f0f55ab768c9 more work on HTML UI.
casties
parents: 18
diff changeset
40 import de.mpiwg.itgroup.annotations.NS;
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
41 import de.mpiwg.itgroup.annotations.Person;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
42 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
43
47b53ae385d1 merging old code
casties
parents:
diff changeset
44 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
45 * Base class for Annotator resource classes.
47b53ae385d1 merging old code
casties
parents:
diff changeset
46 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
47 * @author dwinter, casties
47b53ae385d1 merging old code
casties
parents:
diff changeset
48 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
49 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
50 public abstract class AnnotatorResourceImpl extends ServerResource {
47b53ae385d1 merging old code
casties
parents:
diff changeset
51
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
52 protected static Logger logger = Logger.getLogger(AnnotatorResourceImpl.class);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
53
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
54 private AnnotationStore store;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
55
47b53ae385d1 merging old code
casties
parents:
diff changeset
56 protected String getAllowedMethodsForHeader() {
47b53ae385d1 merging old code
casties
parents:
diff changeset
57 return "OPTIONS,GET,POST";
47b53ae385d1 merging old code
casties
parents:
diff changeset
58 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
59
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
60 protected AnnotationStore getAnnotationStore() {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
61 if (store == null) {
19
f0f55ab768c9 more work on HTML UI.
casties
parents: 18
diff changeset
62 store = ((BaseRestlet) getApplication()).getAnnotationStore();
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
63 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
64 return store;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
65 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
66
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
67 public String encodeJsonId(String id) {
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 19
diff changeset
68 if (id == null) return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
69 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
70 return Base64.encodeBase64URLSafeString(id.getBytes("UTF-8"));
47b53ae385d1 merging old code
casties
parents:
diff changeset
71 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
72 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
73 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
74 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
75
47b53ae385d1 merging old code
casties
parents:
diff changeset
76 public String decodeJsonId(String id) {
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 19
diff changeset
77 if (id == null) return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
78 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
79 return new String(Base64.decodeBase64(id), "UTF-8");
47b53ae385d1 merging old code
casties
parents:
diff changeset
80 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
81 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
82 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
83 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
84
47b53ae385d1 merging old code
casties
parents:
diff changeset
85 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
86 * Handle options request to allow CORS for AJAX.
47b53ae385d1 merging old code
casties
parents:
diff changeset
87 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
88 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
89 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
90 @Options
47b53ae385d1 merging old code
casties
parents:
diff changeset
91 public void doOptions(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
92 logger.debug("AnnotatorResourceImpl doOptions!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
93 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
94 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
95
47b53ae385d1 merging old code
casties
parents:
diff changeset
96 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
97 * set headers to allow CORS for AJAX.
47b53ae385d1 merging old code
casties
parents:
diff changeset
98 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
99 protected void setCorsHeaders() {
47b53ae385d1 merging old code
casties
parents:
diff changeset
100 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
47b53ae385d1 merging old code
casties
parents:
diff changeset
101 if (responseHeaders == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
102 responseHeaders = new Form();
47b53ae385d1 merging old code
casties
parents:
diff changeset
103 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
47b53ae385d1 merging old code
casties
parents:
diff changeset
104 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
105 responseHeaders.add("Access-Control-Allow-Methods", getAllowedMethodsForHeader());
47b53ae385d1 merging old code
casties
parents:
diff changeset
106 // echo back Origin and Request-Headers
47b53ae385d1 merging old code
casties
parents:
diff changeset
107 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
47b53ae385d1 merging old code
casties
parents:
diff changeset
108 String origin = requestHeaders.getFirstValue("Origin", true);
47b53ae385d1 merging old code
casties
parents:
diff changeset
109 if (origin == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
110 responseHeaders.add("Access-Control-Allow-Origin", "*");
47b53ae385d1 merging old code
casties
parents:
diff changeset
111 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
112 responseHeaders.add("Access-Control-Allow-Origin", origin);
47b53ae385d1 merging old code
casties
parents:
diff changeset
113 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
114 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
47b53ae385d1 merging old code
casties
parents:
diff changeset
115 if (allowHeaders != null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
116 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
47b53ae385d1 merging old code
casties
parents:
diff changeset
117 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
118 responseHeaders.add("Access-Control-Allow-Credentials", "true");
47b53ae385d1 merging old code
casties
parents:
diff changeset
119 responseHeaders.add("Access-Control-Max-Age", "60");
47b53ae385d1 merging old code
casties
parents:
diff changeset
120 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
121
47b53ae385d1 merging old code
casties
parents:
diff changeset
122 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
123 * returns if authentication information from headers is valid.
47b53ae385d1 merging old code
casties
parents:
diff changeset
124 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
125 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
126 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
127 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
128 public boolean isAuthenticated(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
129 return (checkAuthToken(entity) != null);
47b53ae385d1 merging old code
casties
parents:
diff changeset
130 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
131
47b53ae385d1 merging old code
casties
parents:
diff changeset
132 /**
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
133 * checks Annotator Auth plugin authentication information from headers.
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
134 * returns userId if successful.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
135 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
136 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
137 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
138 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
139 public String checkAuthToken(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
140 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
47b53ae385d1 merging old code
casties
parents:
diff changeset
141 String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true);
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
142 if (authToken == null) return null;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
143 // decode token first to get consumer key
47b53ae385d1 merging old code
casties
parents:
diff changeset
144 JsonToken token = new JsonTokenParser(null, null).deserialize(authToken);
47b53ae385d1 merging old code
casties
parents:
diff changeset
145 String userId = token.getParamAsPrimitive("userId").getAsString();
47b53ae385d1 merging old code
casties
parents:
diff changeset
146 String consumerKey = token.getParamAsPrimitive("consumerKey").getAsString();
47b53ae385d1 merging old code
casties
parents:
diff changeset
147 // get stored consumer secret for key
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents: 16
diff changeset
148 BaseRestlet restServer = (BaseRestlet) getApplication();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
149 String consumerSecret = restServer.getConsumerSecret(consumerKey);
47b53ae385d1 merging old code
casties
parents:
diff changeset
150 logger.debug("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
47b53ae385d1 merging old code
casties
parents:
diff changeset
151 if (consumerSecret == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
152 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
153 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
154 // logger.debug(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm()));
47b53ae385d1 merging old code
casties
parents:
diff changeset
155 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
156 List<Verifier> verifiers = new ArrayList<Verifier>();
47b53ae385d1 merging old code
casties
parents:
diff changeset
157 // we only do HS256 yet
47b53ae385d1 merging old code
casties
parents:
diff changeset
158 verifiers.add(new HmacSHA256Verifier(consumerSecret.getBytes("UTF-8")));
47b53ae385d1 merging old code
casties
parents:
diff changeset
159 // verify token signature(should really be static...)
47b53ae385d1 merging old code
casties
parents:
diff changeset
160 new JsonTokenParser(new SystemClock(), null, (Checker[]) null).verify(token, verifiers);
47b53ae385d1 merging old code
casties
parents:
diff changeset
161 } catch (SignatureException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
162 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
163 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
164 } catch (InvalidKeyException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
165 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
166 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
167 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
168 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
169 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
170 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
171 // must be ok then
47b53ae385d1 merging old code
casties
parents:
diff changeset
172 logger.debug("auth OK! user=" + userId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
173 return userId;
47b53ae385d1 merging old code
casties
parents:
diff changeset
174 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
175
47b53ae385d1 merging old code
casties
parents:
diff changeset
176 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
177 * creates Annotator-JSON from an Annotation object.
47b53ae385d1 merging old code
casties
parents:
diff changeset
178 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
179 * @param annot
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
180 * @param forAnonymous TODO
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
181 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
182 */
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
183 public JSONObject createAnnotatorJson(Annotation annot, boolean forAnonymous) {
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
184 // return user as a JSON object (otherwise just as string)
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
185 boolean makeUserObject = true;
47b53ae385d1 merging old code
casties
parents:
diff changeset
186 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
187 try {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
188 jo.put("text", annot.getBodyText());
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
189 jo.put("uri", annot.getTargetBaseUri());
40
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
190 if (annot.getResourceUri() != null) {
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
191 jo.put("resource", annot.getResourceUri());
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
192 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
193
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
194 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
195 * user
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
196 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
197 if (makeUserObject) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
198 // create user object
47b53ae385d1 merging old code
casties
parents:
diff changeset
199 JSONObject userObject = new JSONObject();
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
200 Actor creator = annot.getCreator();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
201 // save creator as uri
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
202 userObject.put("uri", creator.getUri());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
203 // make short user id
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
204 String userId = creator.getIdString();
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
205 // set as id
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
206 userObject.put("id", userId);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
207 // get full name
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
208 String userName = creator.getName();
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
209 if (userName == null) {
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents: 16
diff changeset
210 BaseRestlet restServer = (BaseRestlet) getApplication();
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
211 userName = restServer.getFullNameFromLdap(userId);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
212 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
213 userObject.put("name", userName);
47b53ae385d1 merging old code
casties
parents:
diff changeset
214 // save user object
47b53ae385d1 merging old code
casties
parents:
diff changeset
215 jo.put("user", userObject);
47b53ae385d1 merging old code
casties
parents:
diff changeset
216 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
217 // save user as string
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
218 jo.put("user", annot.getCreatorUri());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
219 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
220
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
221 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
222 * ranges
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
223 */
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
224 if (annot.getTargetFragment() != null) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
225 // we only look at the first xpointer
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
226 List<String> fragments = new ArrayList<String>();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
227 fragments.add(annot.getTargetFragment());
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
228 FragmentTypes xt = annot.getFragmentType();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
229 if (xt == FragmentTypes.XPOINTER) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
230 jo.put("ranges", transformToRanges(fragments));
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
231 } else if (xt == FragmentTypes.AREA) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
232 jo.put("areas", transformToAreas(fragments));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
233 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
234 }
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
235
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
236 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
237 * permissions
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
238 */
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
239 JSONObject perms = new JSONObject();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
240 jo.put("permissions", perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
241 // admin
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
242 JSONArray adminPerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
243 perms.put("admin", adminPerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
244 Actor adminPerm = annot.getAdminPermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
245 if (adminPerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
246 adminPerms.put(adminPerm.getIdString());
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
247 } else if (forAnonymous) {
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
248 // set something because its not allowed for anonymous
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 10
diff changeset
249 adminPerms.put("not-you");
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
250 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
251 // delete
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
252 JSONArray deletePerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
253 perms.put("delete", deletePerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
254 Actor deletePerm = annot.getDeletePermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
255 if (deletePerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
256 deletePerms.put(deletePerm.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 deletePerms.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 // update
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
262 JSONArray updatePerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
263 perms.put("update", updatePerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
264 Actor updatePerm = annot.getUpdatePermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
265 if (updatePerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
266 updatePerms.put(updatePerm.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 updatePerms.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 // read
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
272 JSONArray readPerms = new JSONArray();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
273 perms.put("read", readPerms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
274 Actor readPerm = annot.getReadPermission();
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
275 if (readPerm != null) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
276 readPerms.put(readPerm.getIdString());
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
277 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
278
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
279 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
280 * tags
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
281 */
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
282 Set<String> tagset = annot.getTags();
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
283 if (tagset != null) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
284 JSONArray tags = new JSONArray();
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
285 jo.put("tags", tags);
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
286 for (String tag : tagset) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
287 tags.put(tag);
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
288 }
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
289 }
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
290
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
291 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
292 * id
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
293 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
294 // encode Annotation URL (=id) in base64
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
295 String annotUrl = annot.getUri();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
296 String annotId = encodeJsonId(annotUrl);
47b53ae385d1 merging old code
casties
parents:
diff changeset
297 jo.put("id", annotId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
298 return jo;
47b53ae385d1 merging old code
casties
parents:
diff changeset
299 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
300 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
301 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
302 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
303 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
304 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
305
47b53ae385d1 merging old code
casties
parents:
diff changeset
306 private JSONArray transformToRanges(List<String> xpointers) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
307
47b53ae385d1 merging old code
casties
parents:
diff changeset
308 JSONArray ja = new JSONArray();
47b53ae385d1 merging old code
casties
parents:
diff changeset
309
47b53ae385d1 merging old code
casties
parents:
diff changeset
310 Pattern rg = Pattern
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
311 .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
312 Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
313
47b53ae385d1 merging old code
casties
parents:
diff changeset
314 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
315 for (String xpointer : xpointers) {
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
316 // String decoded = URLDecoder.decode(xpointer, "utf-8");
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
317 String decoded = xpointer;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
318 Matcher m = rg.matcher(decoded);
47b53ae385d1 merging old code
casties
parents:
diff changeset
319
47b53ae385d1 merging old code
casties
parents:
diff changeset
320 if (m.find()) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
321 {
47b53ae385d1 merging old code
casties
parents:
diff changeset
322 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
323 jo.put("start", m.group(1));
47b53ae385d1 merging old code
casties
parents:
diff changeset
324 jo.put("startOffset", m.group(2));
47b53ae385d1 merging old code
casties
parents:
diff changeset
325 jo.put("end", m.group(3));
47b53ae385d1 merging old code
casties
parents:
diff changeset
326 jo.put("endOffset", m.group(4));
47b53ae385d1 merging old code
casties
parents:
diff changeset
327 ja.put(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
328 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
329 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
330 m = rg1.matcher(xpointer);
47b53ae385d1 merging old code
casties
parents:
diff changeset
331 if (m.find()) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
332 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
333 jo.put("start", m.group(1));
47b53ae385d1 merging old code
casties
parents:
diff changeset
334 jo.put("startOffset", m.group(2));
47b53ae385d1 merging old code
casties
parents:
diff changeset
335
47b53ae385d1 merging old code
casties
parents:
diff changeset
336 ja.put(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
337 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
338 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
339 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
340 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
341 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
342 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
343 return ja;
47b53ae385d1 merging old code
casties
parents:
diff changeset
344 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
345
47b53ae385d1 merging old code
casties
parents:
diff changeset
346 private JSONArray transformToAreas(List<String> xpointers) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
347
47b53ae385d1 merging old code
casties
parents:
diff changeset
348 JSONArray ja = new JSONArray();
47b53ae385d1 merging old code
casties
parents:
diff changeset
349
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
350 Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
351
47b53ae385d1 merging old code
casties
parents:
diff changeset
352 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
353 for (String xpointer : xpointers) {
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
354 // String decoded = URLDecoder.decode(xpointer, "utf-8");
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
355 String decoded = xpointer;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
356 Matcher m = rg.matcher(decoded);
47b53ae385d1 merging old code
casties
parents:
diff changeset
357
47b53ae385d1 merging old code
casties
parents:
diff changeset
358 if (m.find()) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
359 {
47b53ae385d1 merging old code
casties
parents:
diff changeset
360 JSONObject jo = new JSONObject();
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
361 @SuppressWarnings("unused")
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
362 String unit = m.group(1);
47b53ae385d1 merging old code
casties
parents:
diff changeset
363 jo.put("x", m.group(2));
47b53ae385d1 merging old code
casties
parents:
diff changeset
364 jo.put("y", m.group(3));
47b53ae385d1 merging old code
casties
parents:
diff changeset
365 jo.put("width", m.group(4));
47b53ae385d1 merging old code
casties
parents:
diff changeset
366 jo.put("height", m.group(5));
47b53ae385d1 merging old code
casties
parents:
diff changeset
367 ja.put(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
368 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
369 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
370 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
371 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
372 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
373 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
374 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
375 return ja;
47b53ae385d1 merging old code
casties
parents:
diff changeset
376 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
377
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
378 protected String parseArea(JSONObject area) throws JSONException {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
379 String x = area.getString("x");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
380 String y = area.getString("y");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
381 String width = "0";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
382 String height = "0";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
383 if (area.has("width")) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
384 width = area.getString("width");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
385 height = area.getString("height");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
386 }
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
387 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
388 return fragment;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
389 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
390
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
391 protected String parseRange(JSONObject range) throws JSONException {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
392 String start = range.getString("start");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
393 String end = range.getString("end");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
394 String startOffset = range.getString("startOffset");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
395 String endOffset = range.getString("endOffset");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
396
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
397 String fragment = String.format(
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
398 "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
399 startOffset, end, endOffset);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
400 return fragment;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
401 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
402
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
403 /**
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
404 * Creates an Annotation object with data from JSON.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
405 *
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
406 * uses the specification from the annotator project: {@link https
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
407 * ://github.com/okfn/annotator/wiki/Annotation-format}
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
408 *
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
409 * 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
410 * if not it will set to the MPIWG namespace defined in
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
411 * de.mpiwg.itgroup.annotationManager.Constants.NS
47b53ae385d1 merging old code
casties
parents:
diff changeset
412 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
413 * @param jo
47b53ae385d1 merging old code
casties
parents:
diff changeset
414 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
415 * @throws JSONException
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
416 * @throws UnsupportedEncodingException
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
417 */
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
418 public Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException, UnsupportedEncodingException {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
419 return updateAnnotation(new Annotation(), jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
420 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
421
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
422 /**
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
423 * Updates an Annotation object with data from JSON.
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
424 *
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
425 * uses the specification from the annotator project: {@link https
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
426 * ://github.com/okfn/annotator/wiki/Annotation-format}
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
427 *
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
428 * 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
429 * if not it will set to the MPIWG namespace defined in
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
430 * de.mpiwg.itgroup.annotationManager.Constants.NS
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
431 *
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
432 * @param annot
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
433 * @param jo
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
434 * @return
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
435 * @throws JSONException
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
436 * @throws UnsupportedEncodingException
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
437 */
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
438 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException,
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
439 UnsupportedEncodingException {
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
440 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
441 * target uri
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
442 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
443 if (jo.has("uri")) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
444 annot.setTargetBaseUri(jo.getString("uri"));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
445 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
446 /*
40
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
447 * resource uri
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
448 */
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
449 if (jo.has("resource")) {
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
450 annot.setResourceUri(jo.getString("resource"));
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
451 }
03e0f7574224 saving and loading resource targets should work now (no searching yet)
casties
parents: 22
diff changeset
452 /*
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
453 * annotation text
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
454 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
455 if (jo.has("text")) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
456 annot.setBodyText(jo.getString("text"));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
457 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
458 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
459 * check authentication
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
460 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
461 String authUser = checkAuthToken(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
462 if (authUser == null) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
463 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
464 * // try http auth User httpUser = getHttpAuthUser(entity); if
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
465 * (httpUser == null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
466 */
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
467 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
468 return null;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
469 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
470 * } authUser = httpUser.getIdentifier();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
471 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
472 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
473 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
474 * get or create creator object
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
475 */
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
476 Actor creator = annot.getCreator();
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
477 if (creator == null) {
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
478 creator = new Person();
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
479 annot.setCreator(creator);
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
480 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
481 // username not required, if no username given authuser will be used
47b53ae385d1 merging old code
casties
parents:
diff changeset
482 String username = null;
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
483 String userUri = creator.getUri();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
484 if (jo.has("user")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
485 if (jo.get("user") instanceof String) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
486 // user is just a String
47b53ae385d1 merging old code
casties
parents:
diff changeset
487 username = jo.getString("user");
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
488 creator.setId(username);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
489 // TODO: what if username and authUser are different?
47b53ae385d1 merging old code
casties
parents:
diff changeset
490 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
491 // user is an object
47b53ae385d1 merging old code
casties
parents:
diff changeset
492 JSONObject user = jo.getJSONObject("user");
47b53ae385d1 merging old code
casties
parents:
diff changeset
493 if (user.has("id")) {
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
494 String id = user.getString("id");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
495 creator.setId(id);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
496 username = id;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
497 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
498 if (user.has("uri")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
499 userUri = user.getString("uri");
47b53ae385d1 merging old code
casties
parents:
diff changeset
500 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
501 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
502 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
503 if (username == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
504 username = authUser;
47b53ae385d1 merging old code
casties
parents:
diff changeset
505 }
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
506 // try to get full name
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
507 if (creator.getName() == null && username != null) {
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents: 16
diff changeset
508 BaseRestlet restServer = (BaseRestlet) getApplication();
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
509 String fullName = restServer.getFullNameFromLdap(username);
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
510 creator.setName(fullName);
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
511 }
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
512 // userUri should be a URI, if not it will set to the MPIWG namespace
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
513 if (userUri == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
514 if (username.startsWith("http")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
515 userUri = username;
47b53ae385d1 merging old code
casties
parents:
diff changeset
516 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
517 userUri = NS.MPIWG_PERSONS_URL + username;
47b53ae385d1 merging old code
casties
parents:
diff changeset
518 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
519 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
520 // TODO: should we overwrite the creator?
9
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
521 if (creator.getUri() == null) {
b2bfc3bc9ba8 new internal actor class for creator.
casties
parents: 5
diff changeset
522 creator.setUri(userUri);
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
523 }
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
524 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
525 * creation date
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
526 */
5
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
527 if (annot.getCreated() == null) {
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
528 // set creation date
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
529 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
530 String ct = format.format(Calendar.getInstance().getTime());
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
531 annot.setCreated(ct);
bbf0cc5bee29 version 0.2 really works now
casties
parents: 4
diff changeset
532 }
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
533
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
534 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
535 * create xpointer from the first range/area
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
536 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
537 if (jo.has("ranges")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
538 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
539 annot.setFragmentType(FragmentTypes.XPOINTER);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
540 String fragment = parseRange(ranges);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
541 annot.setTargetFragment(fragment);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
542 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
543 if (jo.has("areas")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
544 JSONObject area = jo.getJSONArray("areas").getJSONObject(0);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
545 annot.setFragmentType(FragmentTypes.AREA);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
546 String fragment = parseArea(area);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
547 annot.setTargetFragment(fragment);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
548 }
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
549
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
550 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
551 * permissions
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
552 */
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
553 if (jo.has("permissions")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
554 JSONObject permissions = jo.getJSONObject("permissions");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
555 if (permissions.has("admin")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
556 JSONArray perms = permissions.getJSONArray("admin");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
557 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
558 annot.setAdminPermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
559 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
560 if (permissions.has("delete")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
561 JSONArray perms = permissions.getJSONArray("delete");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
562 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
563 annot.setDeletePermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
564 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
565 if (permissions.has("update")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
566 JSONArray perms = permissions.getJSONArray("update");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
567 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
568 annot.setUpdatePermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
569 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
570 if (permissions.has("read")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
571 JSONArray perms = permissions.getJSONArray("read");
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
572 Actor actor = getActorFromPermissions(perms);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
573 annot.setReadPermission(actor);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
574 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
575 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
576
16
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
577 /*
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
578 * tags
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
579 */
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
580 if (jo.has("tags")) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
581 HashSet<String> tagset = new HashSet<String>();
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
582 JSONArray tags = jo.getJSONArray("tags");
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
583 for (int i = 0; i < tags.length(); ++i) {
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
584 tagset.add(tags.getString(i));
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
585 }
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
586 annot.setTags(tagset);
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
587 }
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
588
794077e6288c CLOSED - # 252: Tags for Annotations
casties
parents: 15
diff changeset
589
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
590 return annot;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
591 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
592
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
593 @SuppressWarnings("unused") // i in for loop
10
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
594 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
595 Actor actor = null;
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
596 for (int i = 0; i < perms.length(); ++i) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
597 String perm = perms.getString(i);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
598 if (perm.toLowerCase().startsWith("group:")) {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
599 String groupId = perm.substring(6);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
600 actor = new Group(groupId);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
601 } else {
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
602 actor = new Person(perm);
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
603 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
604 // we just take the first one
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
605 break;
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
606 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
607 return actor;
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
608 }
90911b2da322 more work on permissions...
casties
parents: 9
diff changeset
609
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
610 }