annotate src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java @ 15:6c7c4140630d

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