annotate src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java @ 10:0bdfe01e30b5

checking auth token works now.
author casties
date Tue, 20 Mar 2012 15:55:51 +0100
parents 11baadcdd2c8
children 2f8c72ae4c43
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;
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
8 import java.security.MessageDigest;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
9 import java.security.NoSuchAlgorithmException;
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
10 import java.util.ArrayList;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
11 import java.util.List;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
12 import java.util.regex.Matcher;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
13 import java.util.regex.Pattern;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
14
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
15 import org.apache.log4j.Logger;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
16 import org.joda.time.DateTime;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
17 import org.joda.time.format.DateTimeFormatter;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
18 import org.joda.time.format.ISODateTimeFormat;
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
19 import org.json.JSONArray;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
20 import org.json.JSONException;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
21 import org.json.JSONObject;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
22 import org.restlet.data.Form;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
23 import org.restlet.representation.Representation;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
24 import org.restlet.resource.Options;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
25 import org.restlet.resource.ServerResource;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
26
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
27 import de.mpiwg.itgroup.annotationManager.Constants.NS;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
28 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
29
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
30 /**
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
31 * Base class for Annotator resource classes.
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
32 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
33 * @author dwinter, casties
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
34 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
35 */
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
36 public abstract class AnnotatorResourceImpl extends ServerResource {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
37
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
38 protected Logger logger = Logger.getRootLogger();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
39
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
40 protected String getAllowedMethodsForHeader() {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
41 return "OPTIONS,GET,POST";
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
42 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
43
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
44 /**
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
45 * Handle options request to allow CORS for AJAX.
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
46 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
47 * @param entity
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
48 */
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
49 @Options
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
50 public void doOptions(Representation entity) {
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
51 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
52 if (responseHeaders == null) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
53 responseHeaders = new Form();
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
54 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
55 }
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
56 responseHeaders.add("Access-Control-Allow-Methods", getAllowedMethodsForHeader());
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
57 // echo back Origin and Request-Headers
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
58 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
59 String origin = requestHeaders.getFirstValue("Origin", true);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
60 if (origin == null) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
61 responseHeaders.add("Access-Control-Allow-Origin", "*");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
62 } else {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
63 responseHeaders.add("Access-Control-Allow-Origin", origin);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
64 }
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
65 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
66 if (allowHeaders != null) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
67 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
68 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
69 responseHeaders.add("Access-Control-Allow-Credentials", "true");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
70 responseHeaders.add("Access-Control-Max-Age", "60");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
71 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
72
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
73 /**
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
74 * returns if authentication information from headers is valid.
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
75 *
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
76 * @param entity
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
77 * @return
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
78 */
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
79 public boolean isAuthenticated(Representation entity) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
80 // get authToken
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
81 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
82 String consumerKey = requestHeaders.getFirstValue("x-annotator-consumer-key", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
83 if (consumerKey == null) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
84 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
85 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
86 RestServer restServer = (RestServer) getApplication();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
87 String consumerSecret = restServer.getConsumerSecret(consumerKey);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
88 logger.debug("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
89 if (consumerSecret == null) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
90 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
91 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
92 String userId = requestHeaders.getFirstValue("x-annotator-user-id", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
93 String issueTime = requestHeaders.getFirstValue("x-annotator-auth-token-issue-time", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
94 if (userId == null || issueTime == null) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
95 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
96 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
97 // compute hashed token based on the values we know
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
98 // computed_token = hashlib.sha256(consumer.secret + user_id + issue_time).hexdigest()
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
99 String computedToken;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
100 try {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
101 MessageDigest md = MessageDigest.getInstance("SHA-256");
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
102 String computedString = consumerSecret + userId + issueTime;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
103 md.update(computedString.getBytes("UTF-8"));
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
104 byte[] dg = md.digest();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
105 StringBuffer sb = new StringBuffer();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
106 for (byte b : dg) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
107 sb.append(String.format("%02x", b));
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
108 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
109 computedToken = sb.toString();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
110 } catch (NoSuchAlgorithmException e) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
111 e.printStackTrace();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
112 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
113 } catch (UnsupportedEncodingException 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 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
117 // compare to the token we got
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
118 String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
119 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
120 issueTime));
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
121 if (!computedToken.equals(authToken)) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
122 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
123 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
124 // check token lifetime
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
125 // validity = iso8601.parse_date(issue_time)
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
126 // expiry = validity + datetime.timedelta(seconds=consumer.ttl)
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
127 int tokenTtl = 86400;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
128 DateTime tokenValidity = null;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
129 DateTime tokenExpiry = null;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
130 try {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
131 DateTimeFormatter parser = ISODateTimeFormat.dateTime();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
132 tokenValidity = parser.parseDateTime(issueTime);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
133 String tokenTtlString = requestHeaders.getFirstValue("x-annotator-auth-token-ttl", true);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
134 tokenTtl = Integer.parseInt(tokenTtlString);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
135 tokenExpiry = tokenValidity.plusSeconds(tokenTtl);
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
136 } catch (NumberFormatException e) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
137 e.printStackTrace();
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
138 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
139 if (tokenValidity == null || tokenValidity.isAfterNow() || tokenExpiry.isBeforeNow()) {
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
140 return false;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
141 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
142 // must be ok then
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
143 return true;
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
144 }
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
145
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
146 /**
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
147 * Erzeugt aus einer Annotation, das fuer den Annotator notwendige JSON-Format
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
148 *
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
149 * @param annot
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
150 * @return
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
151 */
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
152 protected JSONObject annot2AnnotatorJSON(Convert.Annotation annot) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
153 JSONObject jo = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
154 try {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
155 jo.put("text", annot.text);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
156 jo.put("uri", annot.url);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
157
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
158 JSONObject userObject = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
159 userObject.put("id", annot.creator);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
160
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
161 RestServer restServer = (RestServer) getApplication();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
162
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
163 String userID = annot.creator;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
164 if (userID.startsWith(NS.MPIWG_PERSONS)) {
10
0bdfe01e30b5 checking auth token works now.
casties
parents: 8
diff changeset
165 userID = userID.replace(NS.MPIWG_PERSONS, ""); // entferne NAMESPACE
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
166 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
167 String userName = restServer.getUserNameFromLdap(userID);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
168 userObject.put("name", userName);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
169
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
170 jo.put("user", userObject);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
171
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
172 List<String> xpointer = new ArrayList<String>();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
173
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
174 if (annot.xpointers == null || annot.xpointers.size() == 0)
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
175 xpointer.add(annot.xpointer);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
176 else {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
177 for (String xpointerString : annot.xpointers) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
178 xpointer.add(xpointerString);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
179 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
180 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
181 jo.put("ranges", transformToRanges(xpointer));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
182 jo.put("id", annot.annotationUri);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
183 return jo;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
184 } catch (JSONException e) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
185 // TODO Auto-generated catch block
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
186 e.printStackTrace();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
187 return null;
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
188 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
189 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
190
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
191 private JSONArray transformToRanges(List<String> xpointers) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
192
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
193 JSONArray ja = new JSONArray();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
194
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
195 Pattern rg = Pattern
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
196 .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
197 Pattern rg1 = Pattern.compile("#xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
8
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
198
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
199 try {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
200 for (String xpointer : xpointers) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
201 String decoded = URLDecoder.decode(xpointer, "utf-8");
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
202 Matcher m = rg.matcher(decoded);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
203
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
204 if (m.find()) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
205 {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
206 JSONObject jo = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
207 jo.put("start", m.group(1));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
208 jo.put("startOffset", m.group(2));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
209 jo.put("end", m.group(3));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
210 jo.put("endOffset", m.group(4));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
211 ja.put(jo);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
212 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
213 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
214 m = rg1.matcher(xpointer);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
215 if (m.find()) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
216 JSONObject jo = new JSONObject();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
217 jo.put("start", m.group(1));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
218 jo.put("startOffset", m.group(2));
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
219
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
220 ja.put(jo);
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
221 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
222 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
223 } catch (JSONException e) {
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
224 // TODO Auto-generated catch block
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
225 e.printStackTrace();
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
226 } catch (UnsupportedEncodingException 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 }
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
230
11baadcdd2c8 start of new Annotator API implementation.
casties
parents:
diff changeset
231 return ja;
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 }