|
8
|
1 /**
|
|
|
2 * Base class for Annotator resource classes.
|
|
|
3 */
|
|
|
4 package de.mpiwg.itgroup.annotationManager.restlet;
|
|
|
5
|
|
|
6 import java.io.UnsupportedEncodingException;
|
|
|
7 import java.net.URLDecoder;
|
|
11
|
8 import java.net.URLEncoder;
|
|
10
|
9 import java.security.MessageDigest;
|
|
|
10 import java.security.NoSuchAlgorithmException;
|
|
8
|
11 import java.util.ArrayList;
|
|
|
12 import java.util.List;
|
|
|
13 import java.util.regex.Matcher;
|
|
|
14 import java.util.regex.Pattern;
|
|
|
15
|
|
14
|
16 import javax.xml.bind.DatatypeConverter;
|
|
|
17
|
|
10
|
18 import org.apache.log4j.Logger;
|
|
|
19 import org.joda.time.DateTime;
|
|
|
20 import org.joda.time.format.DateTimeFormatter;
|
|
|
21 import org.joda.time.format.ISODateTimeFormat;
|
|
8
|
22 import org.json.JSONArray;
|
|
|
23 import org.json.JSONException;
|
|
|
24 import org.json.JSONObject;
|
|
13
|
25 import org.restlet.data.ClientInfo;
|
|
8
|
26 import org.restlet.data.Form;
|
|
11
|
27 import org.restlet.data.Status;
|
|
8
|
28 import org.restlet.representation.Representation;
|
|
|
29 import org.restlet.resource.Options;
|
|
|
30 import org.restlet.resource.ServerResource;
|
|
11
|
31 import org.restlet.security.User;
|
|
8
|
32
|
|
|
33 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
|
17
|
34 import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation;
|
|
8
|
35
|
|
|
36 /**
|
|
|
37 * Base class for Annotator resource classes.
|
|
|
38 *
|
|
|
39 * @author dwinter, casties
|
|
|
40 *
|
|
|
41 */
|
|
|
42 public abstract class AnnotatorResourceImpl extends ServerResource {
|
|
|
43
|
|
10
|
44 protected Logger logger = Logger.getRootLogger();
|
|
|
45
|
|
8
|
46 protected String getAllowedMethodsForHeader() {
|
|
|
47 return "OPTIONS,GET,POST";
|
|
|
48 }
|
|
|
49
|
|
|
50 /**
|
|
13
|
51 * returns a hex String of a SHA256 digest of text.
|
|
|
52 *
|
|
|
53 * @param text
|
|
|
54 * @return
|
|
|
55 */
|
|
|
56 public String getSha256Digest(String text) {
|
|
|
57 String digest = null;
|
|
|
58 try {
|
|
|
59 MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
|
60 md.update(text.getBytes("UTF-8"));
|
|
|
61 byte[] dg = md.digest();
|
|
14
|
62 digest = DatatypeConverter.printHexBinary(dg);
|
|
13
|
63 } catch (NoSuchAlgorithmException e) {
|
|
|
64 e.printStackTrace();
|
|
|
65 } catch (UnsupportedEncodingException e) {
|
|
|
66 e.printStackTrace();
|
|
|
67 }
|
|
|
68 return digest;
|
|
|
69 }
|
|
17
|
70
|
|
|
71 public String encodeJsonId(String id) {
|
|
|
72 try {
|
|
|
73 return DatatypeConverter.printBase64Binary(id.getBytes("UTF-8"));
|
|
|
74 } catch (UnsupportedEncodingException e) {
|
|
|
75 return null;
|
|
|
76 }
|
|
|
77 }
|
|
|
78
|
|
|
79 public String decodeJsonId(String id) {
|
|
|
80 try {
|
|
|
81 return new String(DatatypeConverter.parseBase64Binary(id), "UTF-8");
|
|
|
82 } catch (UnsupportedEncodingException e) {
|
|
|
83 return null;
|
|
|
84 }
|
|
|
85 }
|
|
13
|
86
|
|
|
87 /**
|
|
8
|
88 * Handle options request to allow CORS for AJAX.
|
|
|
89 *
|
|
|
90 * @param entity
|
|
|
91 */
|
|
|
92 @Options
|
|
|
93 public void doOptions(Representation entity) {
|
|
13
|
94 logger.debug("AnnotatorResourceImpl doOptions!");
|
|
|
95 setCorsHeaders();
|
|
|
96 }
|
|
|
97
|
|
|
98 /**
|
|
|
99 * set headers to allow CORS for AJAX.
|
|
|
100 */
|
|
|
101 protected void setCorsHeaders() {
|
|
10
|
102 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
|
|
8
|
103 if (responseHeaders == null) {
|
|
|
104 responseHeaders = new Form();
|
|
10
|
105 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
|
|
8
|
106 }
|
|
10
|
107 responseHeaders.add("Access-Control-Allow-Methods", getAllowedMethodsForHeader());
|
|
8
|
108 // echo back Origin and Request-Headers
|
|
10
|
109 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
|
|
8
|
110 String origin = requestHeaders.getFirstValue("Origin", true);
|
|
|
111 if (origin == null) {
|
|
|
112 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
|
113 } else {
|
|
|
114 responseHeaders.add("Access-Control-Allow-Origin", origin);
|
|
|
115 }
|
|
10
|
116 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
|
|
8
|
117 if (allowHeaders != null) {
|
|
|
118 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
|
|
|
119 }
|
|
|
120 responseHeaders.add("Access-Control-Allow-Credentials", "true");
|
|
|
121 responseHeaders.add("Access-Control-Max-Age", "60");
|
|
|
122 }
|
|
|
123
|
|
|
124 /**
|
|
10
|
125 * returns if authentication information from headers is valid.
|
|
|
126 *
|
|
|
127 * @param entity
|
|
|
128 * @return
|
|
|
129 */
|
|
|
130 public boolean isAuthenticated(Representation entity) {
|
|
13
|
131 return (checkAuthToken(entity) != null);
|
|
|
132 }
|
|
|
133
|
|
|
134 /**
|
|
|
135 * checks Annotator Auth plugin authentication information from headers. returns userId if successful.
|
|
|
136 *
|
|
|
137 * @param entity
|
|
|
138 * @return
|
|
|
139 */
|
|
|
140 public String checkAuthToken(Representation entity) {
|
|
10
|
141 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
|
|
|
142 String consumerKey = requestHeaders.getFirstValue("x-annotator-consumer-key", true);
|
|
|
143 if (consumerKey == null) {
|
|
13
|
144 return null;
|
|
10
|
145 }
|
|
13
|
146 // get stored consumer secret for key
|
|
10
|
147 RestServer restServer = (RestServer) getApplication();
|
|
|
148 String consumerSecret = restServer.getConsumerSecret(consumerKey);
|
|
|
149 logger.debug("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
|
|
|
150 if (consumerSecret == null) {
|
|
13
|
151 return null;
|
|
10
|
152 }
|
|
|
153 String userId = requestHeaders.getFirstValue("x-annotator-user-id", true);
|
|
|
154 String issueTime = requestHeaders.getFirstValue("x-annotator-auth-token-issue-time", true);
|
|
|
155 if (userId == null || issueTime == null) {
|
|
13
|
156 return null;
|
|
10
|
157 }
|
|
|
158 // compute hashed token based on the values we know
|
|
|
159 // computed_token = hashlib.sha256(consumer.secret + user_id + issue_time).hexdigest()
|
|
13
|
160 String computedToken = getSha256Digest(consumerSecret + userId + issueTime);
|
|
10
|
161 // compare to the token we got
|
|
|
162 String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true);
|
|
15
|
163 logger.debug(String.format("got: authToken=%s consumerSecret=%s userId=%s issueTime=%s computedToken=%s",
|
|
|
164 authToken, consumerSecret, userId, issueTime, computedToken));
|
|
|
165 if (!computedToken.equalsIgnoreCase(authToken)) {
|
|
|
166 logger.warn("authToken differ!");
|
|
13
|
167 return null;
|
|
10
|
168 }
|
|
|
169 // check token lifetime
|
|
|
170 // validity = iso8601.parse_date(issue_time)
|
|
|
171 // expiry = validity + datetime.timedelta(seconds=consumer.ttl)
|
|
|
172 int tokenTtl = 86400;
|
|
|
173 DateTime tokenValidity = null;
|
|
|
174 DateTime tokenExpiry = null;
|
|
|
175 try {
|
|
|
176 DateTimeFormatter parser = ISODateTimeFormat.dateTime();
|
|
|
177 tokenValidity = parser.parseDateTime(issueTime);
|
|
|
178 String tokenTtlString = requestHeaders.getFirstValue("x-annotator-auth-token-ttl", true);
|
|
|
179 tokenTtl = Integer.parseInt(tokenTtlString);
|
|
|
180 tokenExpiry = tokenValidity.plusSeconds(tokenTtl);
|
|
|
181 } catch (NumberFormatException e) {
|
|
|
182 e.printStackTrace();
|
|
|
183 }
|
|
15
|
184 if (tokenValidity == null || tokenValidity.isAfterNow() || tokenExpiry == null || tokenExpiry.isBeforeNow()) {
|
|
|
185 logger.warn(String.format("authToken invalid! tokenValidity=%s tokenExpiry=%s now=%s", tokenValidity, tokenExpiry, DateTime.now()));
|
|
|
186 // we dont care about validity right now
|
|
|
187 //return null;
|
|
10
|
188 }
|
|
|
189 // must be ok then
|
|
15
|
190 logger.debug("auth OK! user="+userId);
|
|
13
|
191 return userId;
|
|
10
|
192 }
|
|
|
193
|
|
|
194 /**
|
|
11
|
195 * creates Annotator-JSON from an Annotation object.
|
|
8
|
196 *
|
|
|
197 * @param annot
|
|
|
198 * @return
|
|
|
199 */
|
|
17
|
200 public JSONObject createAnnotatorJson(Annotation annot) {
|
|
13
|
201 boolean makeUserObject = true;
|
|
8
|
202 JSONObject jo = new JSONObject();
|
|
|
203 try {
|
|
|
204 jo.put("text", annot.text);
|
|
|
205 jo.put("uri", annot.url);
|
|
|
206
|
|
13
|
207 if (makeUserObject) {
|
|
|
208 // create user object
|
|
|
209 JSONObject userObject = new JSONObject();
|
|
|
210 // save creator as uri
|
|
|
211 userObject.put("uri", annot.creator);
|
|
|
212 // make short user id
|
|
|
213 String userID = annot.creator;
|
|
|
214 if (userID.startsWith(NS.MPIWG_PERSONS)) {
|
|
|
215 userID = userID.replace(NS.MPIWG_PERSONS, ""); // entferne NAMESPACE
|
|
|
216 }
|
|
|
217 // save as id
|
|
|
218 userObject.put("id", userID);
|
|
|
219 // get full name
|
|
|
220 RestServer restServer = (RestServer) getApplication();
|
|
|
221 String userName = restServer.getUserNameFromLdap(userID);
|
|
|
222 userObject.put("name", userName);
|
|
|
223 // save user object
|
|
|
224 jo.put("user", userObject);
|
|
|
225 } else {
|
|
|
226 // save user as string
|
|
|
227 jo.put("user", annot.creator);
|
|
8
|
228 }
|
|
|
229
|
|
13
|
230 List<String> xpointers = new ArrayList<String>();
|
|
8
|
231 if (annot.xpointers == null || annot.xpointers.size() == 0)
|
|
13
|
232 xpointers.add(annot.xpointer);
|
|
8
|
233 else {
|
|
|
234 for (String xpointerString : annot.xpointers) {
|
|
13
|
235 xpointers.add(xpointerString);
|
|
8
|
236 }
|
|
|
237 }
|
|
13
|
238 jo.put("ranges", transformToRanges(xpointers));
|
|
17
|
239 // encode Annotation URL (=id) in base64
|
|
|
240 String annotUrl = annot.getAnnotationUri();
|
|
|
241 String annotId = encodeJsonId(annotUrl);
|
|
|
242 jo.put("id", annotId);
|
|
8
|
243 return jo;
|
|
|
244 } catch (JSONException e) {
|
|
|
245 // TODO Auto-generated catch block
|
|
|
246 e.printStackTrace();
|
|
|
247 }
|
|
17
|
248 return null;
|
|
8
|
249 }
|
|
|
250
|
|
|
251 private JSONArray transformToRanges(List<String> xpointers) {
|
|
|
252
|
|
|
253 JSONArray ja = new JSONArray();
|
|
|
254
|
|
|
255 Pattern rg = Pattern
|
|
|
256 .compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
|
|
10
|
257 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
|
|
8
|
258
|
|
|
259 try {
|
|
|
260 for (String xpointer : xpointers) {
|
|
|
261 String decoded = URLDecoder.decode(xpointer, "utf-8");
|
|
|
262 Matcher m = rg.matcher(decoded);
|
|
|
263
|
|
|
264 if (m.find()) {
|
|
|
265 {
|
|
|
266 JSONObject jo = new JSONObject();
|
|
|
267 jo.put("start", m.group(1));
|
|
|
268 jo.put("startOffset", m.group(2));
|
|
|
269 jo.put("end", m.group(3));
|
|
|
270 jo.put("endOffset", m.group(4));
|
|
|
271 ja.put(jo);
|
|
|
272 }
|
|
|
273 }
|
|
|
274 m = rg1.matcher(xpointer);
|
|
|
275 if (m.find()) {
|
|
|
276 JSONObject jo = new JSONObject();
|
|
|
277 jo.put("start", m.group(1));
|
|
|
278 jo.put("startOffset", m.group(2));
|
|
|
279
|
|
|
280 ja.put(jo);
|
|
|
281 }
|
|
|
282 }
|
|
|
283 } catch (JSONException e) {
|
|
|
284 // TODO Auto-generated catch block
|
|
|
285 e.printStackTrace();
|
|
|
286 } catch (UnsupportedEncodingException e) {
|
|
|
287 // TODO Auto-generated catch block
|
|
|
288 e.printStackTrace();
|
|
|
289 }
|
|
|
290
|
|
|
291 return ja;
|
|
|
292 }
|
|
|
293
|
|
11
|
294 /**
|
|
13
|
295 * creates an Annotation object with data from JSON.
|
|
|
296 *
|
|
|
297 * uses the specification from the annotator project: {@link https://github.com/okfn/annotator/wiki/Annotation-format}
|
|
11
|
298 *
|
|
13
|
299 * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in
|
|
|
300 * de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
11
|
301 *
|
|
|
302 * @param jo
|
|
|
303 * @return
|
|
|
304 * @throws JSONException
|
|
|
305 */
|
|
17
|
306 public Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException {
|
|
|
307 return updateAnnotation(new Annotation(), jo, entity);
|
|
|
308 }
|
|
|
309
|
|
|
310 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException {
|
|
|
311 // annotated uri
|
|
|
312 String url = annot.url;
|
|
|
313 if (jo.has("uri")) {
|
|
|
314 url = jo.getString("uri");
|
|
|
315 }
|
|
|
316 // annotation text
|
|
|
317 String text = annot.text;
|
|
|
318 if (jo.has("text")) {
|
|
|
319 text = jo.getString("text");
|
|
|
320 }
|
|
13
|
321 // check authentication
|
|
|
322 String authUser = checkAuthToken(entity);
|
|
|
323 if (authUser == null) {
|
|
|
324 // try http auth
|
|
|
325 User httpUser = getHttpAuthUser(entity);
|
|
|
326 if (httpUser == null) {
|
|
11
|
327 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
|
328 return null;
|
|
|
329 }
|
|
13
|
330 authUser = httpUser.getIdentifier();
|
|
11
|
331 }
|
|
13
|
332 // username not required, if no username given authuser will be used
|
|
|
333 String username = null;
|
|
17
|
334 String userUri = annot.creator;
|
|
13
|
335 if (jo.has("user")) {
|
|
|
336 if (jo.get("user") instanceof String) {
|
|
|
337 // user is just a String
|
|
|
338 username = jo.getString("user");
|
|
|
339 // TODO: what if username and authUser are different?
|
|
|
340 } else {
|
|
|
341 // user is an object
|
|
|
342 JSONObject user = jo.getJSONObject("user");
|
|
|
343 if (user.has("id")) {
|
|
|
344 username = user.getString("id");
|
|
|
345 }
|
|
|
346 if (user.has("uri")) {
|
|
|
347 userUri = user.getString("uri");
|
|
|
348 }
|
|
|
349 }
|
|
|
350 }
|
|
|
351 if (username == null) {
|
|
|
352 username = authUser;
|
|
|
353 }
|
|
17
|
354 // username should be a URI, if not it will set to the MPIWG namespace defined in
|
|
|
355 // de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
|
356 if (userUri == null) {
|
|
|
357 if (username.startsWith("http")) {
|
|
|
358 userUri = username;
|
|
|
359 } else {
|
|
|
360 userUri = NS.MPIWG_PERSONS + username;
|
|
|
361 }
|
|
|
362 }
|
|
|
363 // TODO: should we overwrite the creator?
|
|
13
|
364
|
|
|
365 // create xpointer
|
|
17
|
366 String xpointer = annot.xpointer;
|
|
11
|
367 if (jo.has("ranges")) {
|
|
|
368 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
|
|
|
369 String start = ranges.getString("start");
|
|
|
370 String end = ranges.getString("end");
|
|
|
371 String startOffset = ranges.getString("startOffset");
|
|
|
372 String endOffset = ranges.getString("endOffset");
|
|
13
|
373
|
|
11
|
374 try {
|
|
|
375 xpointer = url
|
|
|
376 + "#"
|
|
|
377 + URLEncoder.encode(String.format(
|
|
|
378 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))",
|
|
|
379 start, startOffset, end, endOffset), "utf-8");
|
|
|
380 } catch (UnsupportedEncodingException e) {
|
|
|
381 e.printStackTrace();
|
|
|
382 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
|
383 return null;
|
|
|
384 }
|
|
|
385 }
|
|
17
|
386 return new Annotation(xpointer, userUri, annot.time, text, annot.type);
|
|
13
|
387 }
|
|
|
388
|
|
|
389 /**
|
|
|
390 * returns the logged in User.
|
|
|
391 *
|
|
|
392 * @param entity
|
|
|
393 * @return
|
|
|
394 */
|
|
|
395 protected User getHttpAuthUser(Representation entity) {
|
|
|
396 RestServer restServer = (RestServer) getApplication();
|
|
|
397 if (!restServer.authenticate(getRequest(), getResponse())) {
|
|
|
398 // Not authenticated
|
|
|
399 return null;
|
|
|
400 }
|
|
|
401
|
|
|
402 ClientInfo ci = getRequest().getClientInfo();
|
|
|
403 logger.debug(ci);
|
|
|
404 return getRequest().getClientInfo().getUser();
|
|
|
405
|
|
11
|
406 }
|
|
|
407
|
|
8
|
408 }
|