annotate src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java @ 12:2e5d526079de

small fix
author casties
date Wed, 21 Mar 2012 11:09:20 +0100
parents 2f8c72ae4c43
children 9393c9c9b916
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
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
16 import org.apache.log4j.Logger;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
17 import org.joda.time.DateTime;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
18 import org.joda.time.format.DateTimeFormatter;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
19 import org.joda.time.format.ISODateTimeFormat;
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
20 import org.json.JSONArray;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
21 import org.json.JSONException;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
22 import org.json.JSONObject;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
23 import org.restlet.data.Form;
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
24 import org.restlet.data.Status;
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
25 import org.restlet.representation.Representation;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
26 import org.restlet.resource.Options;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
27 import org.restlet.resource.ServerResource;
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
28 import org.restlet.security.User;
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
29
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
30 import de.mpiwg.itgroup.annotationManager.Constants.NS;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
31 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
32
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
33 /**
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
34 * Base class for Annotator resource classes.
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
35 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
36 * @author dwinter, casties
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
37 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
38 */
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
39 public abstract class AnnotatorResourceImpl extends ServerResource {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
40
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
41 protected Logger logger = Logger.getRootLogger();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
42
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
43 protected String getAllowedMethodsForHeader() {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
44 return "OPTIONS,GET,POST";
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
45 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
46
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
47 /**
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
48 * Handle options request to allow CORS for AJAX.
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
49 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
50 * @param entity
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
51 */
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
52 @Options
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
53 public void doOptions(Representation entity) {
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
54 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
55 if (responseHeaders == null) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
56 responseHeaders = new Form();
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
57 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
58 }
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
59 responseHeaders.add("Access-Control-Allow-Methods", getAllowedMethodsForHeader());
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
60 // echo back Origin and Request-Headers
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
61 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
62 String origin = requestHeaders.getFirstValue("Origin", true);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
63 if (origin == null) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
64 responseHeaders.add("Access-Control-Allow-Origin", "*");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
65 } else {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
66 responseHeaders.add("Access-Control-Allow-Origin", origin);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
67 }
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
68 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
69 if (allowHeaders != null) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
70 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
71 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
72 responseHeaders.add("Access-Control-Allow-Credentials", "true");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
73 responseHeaders.add("Access-Control-Max-Age", "60");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
74 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
75
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
76 /**
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
77 * returns if authentication information from headers is valid.
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
78 *
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
79 * @param entity
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
80 * @return
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
81 */
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
82 public boolean isAuthenticated(Representation entity) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
83 // get authToken
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
84 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
85 String consumerKey = requestHeaders.getFirstValue("x-annotator-consumer-key", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
86 if (consumerKey == null) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
87 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
88 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
89 RestServer restServer = (RestServer) getApplication();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
90 String consumerSecret = restServer.getConsumerSecret(consumerKey);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
91 logger.debug("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
92 if (consumerSecret == null) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
93 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
94 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
95 String userId = requestHeaders.getFirstValue("x-annotator-user-id", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
96 String issueTime = requestHeaders.getFirstValue("x-annotator-auth-token-issue-time", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
97 if (userId == null || issueTime == null) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
98 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
99 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
100 // compute hashed token based on the values we know
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
101 // computed_token = hashlib.sha256(consumer.secret + user_id + issue_time).hexdigest()
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
102 String computedToken;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
103 try {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
104 MessageDigest md = MessageDigest.getInstance("SHA-256");
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
105 String computedString = consumerSecret + userId + issueTime;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
106 md.update(computedString.getBytes("UTF-8"));
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
107 byte[] dg = md.digest();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
108 StringBuffer sb = new StringBuffer();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
109 for (byte b : dg) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
110 sb.append(String.format("%02x", b));
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
111 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
112 computedToken = sb.toString();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
113 } catch (NoSuchAlgorithmException e) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
114 e.printStackTrace();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
115 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
116 } catch (UnsupportedEncodingException e) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
117 e.printStackTrace();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
118 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
119 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
120 // compare to the token we got
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
121 String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
122 logger.debug(String.format("got: authToken=%s consumerSecret=%s userId=%s issueTime=%s", authToken, consumerSecret, userId,
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
123 issueTime));
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
124 if (!computedToken.equals(authToken)) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
125 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
126 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
127 // check token lifetime
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
128 // validity = iso8601.parse_date(issue_time)
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
129 // expiry = validity + datetime.timedelta(seconds=consumer.ttl)
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
130 int tokenTtl = 86400;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
131 DateTime tokenValidity = null;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
132 DateTime tokenExpiry = null;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
133 try {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
134 DateTimeFormatter parser = ISODateTimeFormat.dateTime();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
135 tokenValidity = parser.parseDateTime(issueTime);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
136 String tokenTtlString = requestHeaders.getFirstValue("x-annotator-auth-token-ttl", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
137 tokenTtl = Integer.parseInt(tokenTtlString);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
138 tokenExpiry = tokenValidity.plusSeconds(tokenTtl);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
139 } catch (NumberFormatException e) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
140 e.printStackTrace();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
141 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
142 if (tokenValidity == null || tokenValidity.isAfterNow() || tokenExpiry.isBeforeNow()) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
143 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
144 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
145 // must be ok then
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
146 return true;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
147 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
148
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
149 /**
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
150 * creates Annotator-JSON from an Annotation object.
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
151 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
152 * @param annot
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
153 * @return
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
154 */
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
155 public JSONObject annot2AnnotatorJSON(Convert.Annotation annot) {
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
156 JSONObject jo = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
157 try {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
158 jo.put("text", annot.text);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
159 jo.put("uri", annot.url);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
160
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
161 JSONObject userObject = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
162 userObject.put("id", annot.creator);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
163
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
164 RestServer restServer = (RestServer) getApplication();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
165
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
166 String userID = annot.creator;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
167 if (userID.startsWith(NS.MPIWG_PERSONS)) {
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
168 userID = userID.replace(NS.MPIWG_PERSONS, ""); // entferne NAMESPACE
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
169 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
170 String userName = restServer.getUserNameFromLdap(userID);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
171 userObject.put("name", userName);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
172
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
173 jo.put("user", userObject);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
174
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
175 List<String> xpointer = new ArrayList<String>();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
176
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
177 if (annot.xpointers == null || annot.xpointers.size() == 0)
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
178 xpointer.add(annot.xpointer);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
179 else {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
180 for (String xpointerString : annot.xpointers) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
181 xpointer.add(xpointerString);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
182 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
183 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
184 jo.put("ranges", transformToRanges(xpointer));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
185 jo.put("id", annot.annotationUri);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
186 return jo;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
187 } catch (JSONException e) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
188 // TODO Auto-generated catch block
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
189 e.printStackTrace();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
190 return null;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
191 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
192 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
193
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
194 private JSONArray transformToRanges(List<String> xpointers) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
195
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
196 JSONArray ja = new JSONArray();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
197
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
198 Pattern rg = Pattern
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
199 .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
200 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
201
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
202 try {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
203 for (String xpointer : xpointers) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
204 String decoded = URLDecoder.decode(xpointer, "utf-8");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
205 Matcher m = rg.matcher(decoded);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
206
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
207 if (m.find()) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
208 {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
209 JSONObject jo = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
210 jo.put("start", m.group(1));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
211 jo.put("startOffset", m.group(2));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
212 jo.put("end", m.group(3));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
213 jo.put("endOffset", m.group(4));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
214 ja.put(jo);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
215 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
216 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
217 m = rg1.matcher(xpointer);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
218 if (m.find()) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
219 JSONObject jo = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
220 jo.put("start", m.group(1));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
221 jo.put("startOffset", m.group(2));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
222
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
223 ja.put(jo);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
224 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
225 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
226 } catch (JSONException e) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
227 // TODO Auto-generated catch block
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
228 e.printStackTrace();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
229 } catch (UnsupportedEncodingException e) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
230 // TODO Auto-generated catch block
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
231 e.printStackTrace();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
232 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
233
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
234 return ja;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
235 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
236
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
237 /**
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
238 * creates an Annotation object with data from JSON.
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
239 * uses the specification from the annotator project.
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
240 *
12
2e5d526079de small fix
casties
parents: 11
diff changeset
241 * @see{https://github.com/okfn/annotator/wiki/Annotation-format}
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
242 *
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
243 * The username will be transformed to an URI if not given already
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
244 * as 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
245 * de.mpiwg.itgroup.annotationManager.Constants.NS
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
246 * @param jo
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
247 * @return
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
248 * @throws JSONException
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
249 */
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
250 public Convert.Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
251 Convert.Annotation annot;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
252 String url = jo.getString("uri");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
253 String text = jo.getString("text");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
254
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
255 String username = null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
256 if (jo.has("user")) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
257 // not required, if no username given authuser
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
258 // will be used otherwise username and password
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
259 // has to be submitted
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
260 JSONObject user = jo.getJSONObject("user");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
261 if (user.has("id")) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
262 username = user.getString("id");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
263 if (!user.has("password")) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
264 User authUser = handleBasicAuthentification(entity);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
265 if (authUser == null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
266 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
267 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
268 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
269 username = authUser.getIdentifier();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
270 } else {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
271 String password = user.getString("password");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
272 if (!((RestServer) getApplication()).authenticate(username, password, getRequest())) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
273 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
274 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
275 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
276 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
277 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
278
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
279 } else {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
280 User authUser = handleBasicAuthentification(entity);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
281 if (authUser == null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
282 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
283 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
284 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
285 username = authUser.getIdentifier();
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
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
288 String xpointer;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
289 if (jo.has("ranges")) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
290 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
291 String start = ranges.getString("start");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
292 String end = ranges.getString("end");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
293 String startOffset = ranges.getString("startOffset");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
294 String endOffset = ranges.getString("endOffset");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
295
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
296 try {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
297 xpointer = url
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
298 + "#"
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
299 + URLEncoder.encode(String.format(
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
300 "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
301 start, startOffset, end, endOffset), "utf-8");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
302 } catch (UnsupportedEncodingException e) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
303 e.printStackTrace();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
304 setStatus(Status.SERVER_ERROR_INTERNAL);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
305 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
306 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
307 } else {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
308 xpointer = url;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
309 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
310
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
311 // 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
312 // de.mpiwg.itgroup.annotationManager.Constants.NS
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
313 if (!username.startsWith("http"))
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
314 username = NS.MPIWG_PERSONS + username;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
315
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
316 return new Convert.Annotation(xpointer, username, null, text, null);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
317 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents: 10
diff changeset
318
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
319 }