annotate src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java @ 33:e5f5848892a2

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