annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 4:3599b29c393f

store seems to work now :-)
author casties
date Mon, 02 Jul 2012 22:39:46 +0200
parents 47b53ae385d1
children bbf0cc5bee29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
1 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
2 * Base class for Annotator resource classes.
47b53ae385d1 merging old code
casties
parents:
diff changeset
3 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
4 package de.mpiwg.itgroup.annotations.restlet;
47b53ae385d1 merging old code
casties
parents:
diff changeset
5
47b53ae385d1 merging old code
casties
parents:
diff changeset
6 import java.io.UnsupportedEncodingException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
7 import java.net.URLDecoder;
47b53ae385d1 merging old code
casties
parents:
diff changeset
8 import java.net.URLEncoder;
47b53ae385d1 merging old code
casties
parents:
diff changeset
9 import java.security.InvalidKeyException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
10 import java.security.SignatureException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
11 import java.util.ArrayList;
47b53ae385d1 merging old code
casties
parents:
diff changeset
12 import java.util.List;
47b53ae385d1 merging old code
casties
parents:
diff changeset
13 import java.util.regex.Matcher;
47b53ae385d1 merging old code
casties
parents:
diff changeset
14 import java.util.regex.Pattern;
47b53ae385d1 merging old code
casties
parents:
diff changeset
15
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
16 import javax.servlet.ServletContext;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
17
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
18 import net.oauth.jsontoken.Checker;
47b53ae385d1 merging old code
casties
parents:
diff changeset
19 import net.oauth.jsontoken.JsonToken;
47b53ae385d1 merging old code
casties
parents:
diff changeset
20 import net.oauth.jsontoken.JsonTokenParser;
47b53ae385d1 merging old code
casties
parents:
diff changeset
21 import net.oauth.jsontoken.SystemClock;
47b53ae385d1 merging old code
casties
parents:
diff changeset
22 import net.oauth.jsontoken.crypto.HmacSHA256Verifier;
47b53ae385d1 merging old code
casties
parents:
diff changeset
23 import net.oauth.jsontoken.crypto.Verifier;
47b53ae385d1 merging old code
casties
parents:
diff changeset
24
47b53ae385d1 merging old code
casties
parents:
diff changeset
25 import org.apache.commons.codec.binary.Base64;
47b53ae385d1 merging old code
casties
parents:
diff changeset
26 import org.apache.log4j.Logger;
47b53ae385d1 merging old code
casties
parents:
diff changeset
27 import org.json.JSONArray;
47b53ae385d1 merging old code
casties
parents:
diff changeset
28 import org.json.JSONException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
29 import org.json.JSONObject;
47b53ae385d1 merging old code
casties
parents:
diff changeset
30 import org.restlet.data.Form;
47b53ae385d1 merging old code
casties
parents:
diff changeset
31 import org.restlet.data.Status;
47b53ae385d1 merging old code
casties
parents:
diff changeset
32 import org.restlet.representation.Representation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
33 import org.restlet.resource.Options;
47b53ae385d1 merging old code
casties
parents:
diff changeset
34 import org.restlet.resource.ServerResource;
47b53ae385d1 merging old code
casties
parents:
diff changeset
35
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
36 import de.mpiwg.itgroup.annotations.Annotation;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
37 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
38 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
39 import de.mpiwg.itgroup.annotations.old.NS;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
40
47b53ae385d1 merging old code
casties
parents:
diff changeset
41 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
42 * Base class for Annotator resource classes.
47b53ae385d1 merging old code
casties
parents:
diff changeset
43 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
44 * @author dwinter, casties
47b53ae385d1 merging old code
casties
parents:
diff changeset
45 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
46 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
47 public abstract class AnnotatorResourceImpl extends ServerResource {
47b53ae385d1 merging old code
casties
parents:
diff changeset
48
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
49 protected static Logger logger = Logger.getLogger(AnnotatorResourceImpl.class);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
50
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
51 private AnnotationStore store;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
52
47b53ae385d1 merging old code
casties
parents:
diff changeset
53 protected String getAllowedMethodsForHeader() {
47b53ae385d1 merging old code
casties
parents:
diff changeset
54 return "OPTIONS,GET,POST";
47b53ae385d1 merging old code
casties
parents:
diff changeset
55 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
56
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
57 protected AnnotationStore getAnnotationStore() {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
58 if (store == null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
59 ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes()
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
60 .get("org.restlet.ext.servlet.ServletContext");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
61 logger.debug("Getting AnnotationStore from Context");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
62 store = (AnnotationStore) sc.getAttribute(RestServer.ANNSTORE_KEY);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
63 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
64 return store;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
65 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
66
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
67 public String encodeJsonId(String id) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
68 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
69 return Base64.encodeBase64URLSafeString(id.getBytes("UTF-8"));
47b53ae385d1 merging old code
casties
parents:
diff changeset
70 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
71 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
72 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
73 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
74
47b53ae385d1 merging old code
casties
parents:
diff changeset
75 public String decodeJsonId(String id) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
76 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
77 return new String(Base64.decodeBase64(id), "UTF-8");
47b53ae385d1 merging old code
casties
parents:
diff changeset
78 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
79 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
80 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
81 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
82
47b53ae385d1 merging old code
casties
parents:
diff changeset
83 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
84 * Handle options request to allow CORS for AJAX.
47b53ae385d1 merging old code
casties
parents:
diff changeset
85 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
86 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
87 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
88 @Options
47b53ae385d1 merging old code
casties
parents:
diff changeset
89 public void doOptions(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
90 logger.debug("AnnotatorResourceImpl doOptions!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
91 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
92 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
93
47b53ae385d1 merging old code
casties
parents:
diff changeset
94 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
95 * set headers to allow CORS for AJAX.
47b53ae385d1 merging old code
casties
parents:
diff changeset
96 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
97 protected void setCorsHeaders() {
47b53ae385d1 merging old code
casties
parents:
diff changeset
98 Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
47b53ae385d1 merging old code
casties
parents:
diff changeset
99 if (responseHeaders == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
100 responseHeaders = new Form();
47b53ae385d1 merging old code
casties
parents:
diff changeset
101 getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
47b53ae385d1 merging old code
casties
parents:
diff changeset
102 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
103 responseHeaders.add("Access-Control-Allow-Methods", getAllowedMethodsForHeader());
47b53ae385d1 merging old code
casties
parents:
diff changeset
104 // echo back Origin and Request-Headers
47b53ae385d1 merging old code
casties
parents:
diff changeset
105 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
47b53ae385d1 merging old code
casties
parents:
diff changeset
106 String origin = requestHeaders.getFirstValue("Origin", true);
47b53ae385d1 merging old code
casties
parents:
diff changeset
107 if (origin == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
108 responseHeaders.add("Access-Control-Allow-Origin", "*");
47b53ae385d1 merging old code
casties
parents:
diff changeset
109 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
110 responseHeaders.add("Access-Control-Allow-Origin", origin);
47b53ae385d1 merging old code
casties
parents:
diff changeset
111 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
112 String allowHeaders = requestHeaders.getFirstValue("Access-Control-Request-Headers", true);
47b53ae385d1 merging old code
casties
parents:
diff changeset
113 if (allowHeaders != null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
114 responseHeaders.add("Access-Control-Allow-Headers", allowHeaders);
47b53ae385d1 merging old code
casties
parents:
diff changeset
115 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
116 responseHeaders.add("Access-Control-Allow-Credentials", "true");
47b53ae385d1 merging old code
casties
parents:
diff changeset
117 responseHeaders.add("Access-Control-Max-Age", "60");
47b53ae385d1 merging old code
casties
parents:
diff changeset
118 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
119
47b53ae385d1 merging old code
casties
parents:
diff changeset
120 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
121 * returns if authentication information from headers is valid.
47b53ae385d1 merging old code
casties
parents:
diff changeset
122 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
123 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
124 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
125 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
126 public boolean isAuthenticated(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
127 return (checkAuthToken(entity) != null);
47b53ae385d1 merging old code
casties
parents:
diff changeset
128 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
129
47b53ae385d1 merging old code
casties
parents:
diff changeset
130 /**
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
131 * checks Annotator Auth plugin authentication information from headers.
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
132 * returns userId if successful.
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
133 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
134 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
135 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
136 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
137 public String checkAuthToken(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
138 Form requestHeaders = (Form) getRequest().getAttributes().get("org.restlet.http.headers");
47b53ae385d1 merging old code
casties
parents:
diff changeset
139 String authToken = requestHeaders.getFirstValue("x-annotator-auth-token", true);
47b53ae385d1 merging old code
casties
parents:
diff changeset
140 // decode token first to get consumer key
47b53ae385d1 merging old code
casties
parents:
diff changeset
141 JsonToken token = new JsonTokenParser(null, null).deserialize(authToken);
47b53ae385d1 merging old code
casties
parents:
diff changeset
142 String userId = token.getParamAsPrimitive("userId").getAsString();
47b53ae385d1 merging old code
casties
parents:
diff changeset
143 String consumerKey = token.getParamAsPrimitive("consumerKey").getAsString();
47b53ae385d1 merging old code
casties
parents:
diff changeset
144 // get stored consumer secret for key
47b53ae385d1 merging old code
casties
parents:
diff changeset
145 RestServer restServer = (RestServer) getApplication();
47b53ae385d1 merging old code
casties
parents:
diff changeset
146 String consumerSecret = restServer.getConsumerSecret(consumerKey);
47b53ae385d1 merging old code
casties
parents:
diff changeset
147 logger.debug("requested consumer key=" + consumerKey + " secret=" + consumerSecret);
47b53ae385d1 merging old code
casties
parents:
diff changeset
148 if (consumerSecret == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
149 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
150 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
151 // logger.debug(String.format("token=%s tokenString=%s signatureAlgorithm=%s",token,token.getTokenString(),token.getSignatureAlgorithm()));
47b53ae385d1 merging old code
casties
parents:
diff changeset
152 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
153 List<Verifier> verifiers = new ArrayList<Verifier>();
47b53ae385d1 merging old code
casties
parents:
diff changeset
154 // we only do HS256 yet
47b53ae385d1 merging old code
casties
parents:
diff changeset
155 verifiers.add(new HmacSHA256Verifier(consumerSecret.getBytes("UTF-8")));
47b53ae385d1 merging old code
casties
parents:
diff changeset
156 // verify token signature(should really be static...)
47b53ae385d1 merging old code
casties
parents:
diff changeset
157 new JsonTokenParser(new SystemClock(), null, (Checker[]) null).verify(token, verifiers);
47b53ae385d1 merging old code
casties
parents:
diff changeset
158 } catch (SignatureException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
159 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
160 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
161 } catch (InvalidKeyException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
162 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
163 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
164 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
165 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
166 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
167 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
168 // must be ok then
47b53ae385d1 merging old code
casties
parents:
diff changeset
169 logger.debug("auth OK! user=" + userId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
170 return userId;
47b53ae385d1 merging old code
casties
parents:
diff changeset
171 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
172
47b53ae385d1 merging old code
casties
parents:
diff changeset
173 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
174 * creates Annotator-JSON from an Annotation object.
47b53ae385d1 merging old code
casties
parents:
diff changeset
175 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
176 * @param annot
47b53ae385d1 merging old code
casties
parents:
diff changeset
177 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
178 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
179 public JSONObject createAnnotatorJson(Annotation annot) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
180 boolean makeUserObject = true;
47b53ae385d1 merging old code
casties
parents:
diff changeset
181 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
182 try {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
183 jo.put("text", annot.getBodyText());
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
184 jo.put("uri", annot.getTargetBaseUri());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
185
47b53ae385d1 merging old code
casties
parents:
diff changeset
186 if (makeUserObject) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
187 // create user object
47b53ae385d1 merging old code
casties
parents:
diff changeset
188 JSONObject userObject = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
189 // save creator as uri
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
190 userObject.put("uri", annot.getCreatorUri());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
191 // make short user id
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
192 String userId = annot.getCreatorUri();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
193 if (userId != null && userId.startsWith(NS.MPIWG_PERSONS_URL)) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
194 userId = userId.replace(NS.MPIWG_PERSONS_URL, ""); // entferne
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
195 // NAMESPACE
47b53ae385d1 merging old code
casties
parents:
diff changeset
196 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
197 // save as id
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
198 userObject.put("id", userId);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
199 // get full name
47b53ae385d1 merging old code
casties
parents:
diff changeset
200 RestServer restServer = (RestServer) getApplication();
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
201 String userName = restServer.getUserNameFromLdap(userId);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
202 userObject.put("name", userName);
47b53ae385d1 merging old code
casties
parents:
diff changeset
203 // save user object
47b53ae385d1 merging old code
casties
parents:
diff changeset
204 jo.put("user", userObject);
47b53ae385d1 merging old code
casties
parents:
diff changeset
205 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
206 // save user as string
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
207 jo.put("user", annot.getCreatorUri());
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
208 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
209
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
210 if (annot.getTargetFragment() != null) {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
211 // we only look at the first xpointer
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
212 List<String> fragments = new ArrayList<String>();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
213 fragments.add(annot.getTargetFragment());
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
214 FragmentTypes xt = annot.getFragmentType();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
215 if (xt == FragmentTypes.XPOINTER) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
216 jo.put("ranges", transformToRanges(fragments));
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
217 } else if (xt == FragmentTypes.AREA) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
218 jo.put("areas", transformToAreas(fragments));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
219 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
220 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
221 // encode Annotation URL (=id) in base64
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
222 String annotUrl = annot.getUri();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
223 String annotId = encodeJsonId(annotUrl);
47b53ae385d1 merging old code
casties
parents:
diff changeset
224 jo.put("id", annotId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
225 return jo;
47b53ae385d1 merging old code
casties
parents:
diff changeset
226 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
227 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
228 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
229 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
230 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
231 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
232
47b53ae385d1 merging old code
casties
parents:
diff changeset
233 private JSONArray transformToRanges(List<String> xpointers) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
234
47b53ae385d1 merging old code
casties
parents:
diff changeset
235 JSONArray ja = new JSONArray();
47b53ae385d1 merging old code
casties
parents:
diff changeset
236
47b53ae385d1 merging old code
casties
parents:
diff changeset
237 Pattern rg = Pattern
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
238 .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
239 Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
240
47b53ae385d1 merging old code
casties
parents:
diff changeset
241 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
242 for (String xpointer : xpointers) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
243 String decoded = URLDecoder.decode(xpointer, "utf-8");
47b53ae385d1 merging old code
casties
parents:
diff changeset
244 Matcher m = rg.matcher(decoded);
47b53ae385d1 merging old code
casties
parents:
diff changeset
245
47b53ae385d1 merging old code
casties
parents:
diff changeset
246 if (m.find()) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
247 {
47b53ae385d1 merging old code
casties
parents:
diff changeset
248 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
249 jo.put("start", m.group(1));
47b53ae385d1 merging old code
casties
parents:
diff changeset
250 jo.put("startOffset", m.group(2));
47b53ae385d1 merging old code
casties
parents:
diff changeset
251 jo.put("end", m.group(3));
47b53ae385d1 merging old code
casties
parents:
diff changeset
252 jo.put("endOffset", m.group(4));
47b53ae385d1 merging old code
casties
parents:
diff changeset
253 ja.put(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
254 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
255 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
256 m = rg1.matcher(xpointer);
47b53ae385d1 merging old code
casties
parents:
diff changeset
257 if (m.find()) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
258 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
259 jo.put("start", m.group(1));
47b53ae385d1 merging old code
casties
parents:
diff changeset
260 jo.put("startOffset", m.group(2));
47b53ae385d1 merging old code
casties
parents:
diff changeset
261
47b53ae385d1 merging old code
casties
parents:
diff changeset
262 ja.put(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
263 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
264 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
265 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
266 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
267 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
268 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
269 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
270 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
271 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
272
47b53ae385d1 merging old code
casties
parents:
diff changeset
273 return ja;
47b53ae385d1 merging old code
casties
parents:
diff changeset
274 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
275
47b53ae385d1 merging old code
casties
parents:
diff changeset
276 private JSONArray transformToAreas(List<String> xpointers) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
277
47b53ae385d1 merging old code
casties
parents:
diff changeset
278 JSONArray ja = new JSONArray();
47b53ae385d1 merging old code
casties
parents:
diff changeset
279
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
280 Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
281
47b53ae385d1 merging old code
casties
parents:
diff changeset
282 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
283 for (String xpointer : xpointers) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
284 String decoded = URLDecoder.decode(xpointer, "utf-8");
47b53ae385d1 merging old code
casties
parents:
diff changeset
285 Matcher m = rg.matcher(decoded);
47b53ae385d1 merging old code
casties
parents:
diff changeset
286
47b53ae385d1 merging old code
casties
parents:
diff changeset
287 if (m.find()) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
288 {
47b53ae385d1 merging old code
casties
parents:
diff changeset
289 JSONObject jo = new JSONObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
290 String unit = m.group(1);
47b53ae385d1 merging old code
casties
parents:
diff changeset
291 jo.put("x", m.group(2));
47b53ae385d1 merging old code
casties
parents:
diff changeset
292 jo.put("y", m.group(3));
47b53ae385d1 merging old code
casties
parents:
diff changeset
293 jo.put("width", m.group(4));
47b53ae385d1 merging old code
casties
parents:
diff changeset
294 jo.put("height", m.group(5));
47b53ae385d1 merging old code
casties
parents:
diff changeset
295 ja.put(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
296 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
297 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
298 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
299 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
300 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
301 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
302 } catch (UnsupportedEncodingException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
303 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
304 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
305 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
306
47b53ae385d1 merging old code
casties
parents:
diff changeset
307 return ja;
47b53ae385d1 merging old code
casties
parents:
diff changeset
308 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
309
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
310 protected String parseArea(JSONObject area) throws JSONException, UnsupportedEncodingException {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
311 String x = area.getString("x");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
312 String y = area.getString("y");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
313 String width = "0";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
314 String height = "0";
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
315 if (area.has("width")) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
316 width = area.getString("width");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
317 height = area.getString("height");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
318 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
319 String fragment = URLEncoder.encode(String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height), "utf-8");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
320 return fragment;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
321 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
322
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
323 protected String parseRange(JSONObject range) throws JSONException, UnsupportedEncodingException {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
324 String start = range.getString("start");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
325 String end = range.getString("end");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
326 String startOffset = range.getString("startOffset");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
327 String endOffset = range.getString("endOffset");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
328
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
329 String fragment = URLEncoder.encode(String.format(
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
330 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start,
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
331 startOffset, end, endOffset), "utf-8");
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
332 return fragment;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
333 }
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
334
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
335 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
336 * creates an Annotation object with data from JSON.
47b53ae385d1 merging old code
casties
parents:
diff changeset
337 *
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
338 * uses the specification from the annotator project: {@link https
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
339 * ://github.com/okfn/annotator/wiki/Annotation-format}
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
340 *
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
341 * The username will be transformed to an URI if not given already as URI,
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
342 * if not it will set to the MPIWG namespace defined in
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
343 * de.mpiwg.itgroup.annotationManager.Constants.NS
47b53ae385d1 merging old code
casties
parents:
diff changeset
344 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
345 * @param jo
47b53ae385d1 merging old code
casties
parents:
diff changeset
346 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
347 * @throws JSONException
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
348 * @throws UnsupportedEncodingException
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
349 */
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
350 public Annotation createAnnotation(JSONObject jo, Representation entity) throws JSONException, UnsupportedEncodingException {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
351 return updateAnnotation(new Annotation(), jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
352 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
353
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
354 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException,
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
355 UnsupportedEncodingException {
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
356 // annotated uri
47b53ae385d1 merging old code
casties
parents:
diff changeset
357 if (jo.has("uri")) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
358 annot.setTargetBaseUri(jo.getString("uri"));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
359 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
360 // annotation text
47b53ae385d1 merging old code
casties
parents:
diff changeset
361 if (jo.has("text")) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
362 annot.setBodyText(jo.getString("text"));
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
363 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
364 // check authentication
47b53ae385d1 merging old code
casties
parents:
diff changeset
365 String authUser = checkAuthToken(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
366 if (authUser == null) {
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
367 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
368 * // try http auth User httpUser = getHttpAuthUser(entity); if
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
369 * (httpUser == null) {
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
370 */
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
371 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
372 return null;
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
373 /*
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
374 * } authUser = httpUser.getIdentifier();
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
375 */
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
376 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
377 // username not required, if no username given authuser will be used
47b53ae385d1 merging old code
casties
parents:
diff changeset
378 String username = null;
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
379 String userUri = annot.getCreatorUri();
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
380 if (jo.has("user")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
381 if (jo.get("user") instanceof String) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
382 // user is just a String
47b53ae385d1 merging old code
casties
parents:
diff changeset
383 username = jo.getString("user");
47b53ae385d1 merging old code
casties
parents:
diff changeset
384 // TODO: what if username and authUser are different?
47b53ae385d1 merging old code
casties
parents:
diff changeset
385 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
386 // user is an object
47b53ae385d1 merging old code
casties
parents:
diff changeset
387 JSONObject user = jo.getJSONObject("user");
47b53ae385d1 merging old code
casties
parents:
diff changeset
388 if (user.has("id")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
389 username = user.getString("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
390 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
391 if (user.has("uri")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
392 userUri = user.getString("uri");
47b53ae385d1 merging old code
casties
parents:
diff changeset
393 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
394 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
395 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
396 if (username == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
397 username = authUser;
47b53ae385d1 merging old code
casties
parents:
diff changeset
398 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
399 // username should be a URI, if not it will set to the MPIWG namespace
47b53ae385d1 merging old code
casties
parents:
diff changeset
400 // defined in
47b53ae385d1 merging old code
casties
parents:
diff changeset
401 // de.mpiwg.itgroup.annotationManager.Constants.NS
47b53ae385d1 merging old code
casties
parents:
diff changeset
402 if (userUri == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
403 if (username.startsWith("http")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
404 userUri = username;
47b53ae385d1 merging old code
casties
parents:
diff changeset
405 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
406 userUri = NS.MPIWG_PERSONS_URL + username;
47b53ae385d1 merging old code
casties
parents:
diff changeset
407 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
408 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
409 // TODO: should we overwrite the creator?
47b53ae385d1 merging old code
casties
parents:
diff changeset
410
47b53ae385d1 merging old code
casties
parents:
diff changeset
411 // create xpointer from the first range/area
47b53ae385d1 merging old code
casties
parents:
diff changeset
412 if (jo.has("ranges")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
413 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
414 annot.setFragmentType(FragmentTypes.XPOINTER);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
415 String fragment = parseRange(ranges);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
416 annot.setTargetFragment(fragment);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
417 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
418 if (jo.has("areas")) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
419 JSONObject area = jo.getJSONArray("areas").getJSONObject(0);
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
420 annot.setFragmentType(FragmentTypes.AREA);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
421 String fragment = parseArea(area);
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
422 annot.setTargetFragment(fragment);
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
423 }
4
3599b29c393f store seems to work now :-)
casties
parents: 3
diff changeset
424 return annot;
3
47b53ae385d1 merging old code
casties
parents:
diff changeset
425 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
426
47b53ae385d1 merging old code
casties
parents:
diff changeset
427 }