11
|
1 /**
|
|
2 * Implements the "annotations" uri of the Annotator API. see
|
|
3 * <https://github.com/okfn/annotator/wiki/Storage>
|
|
4 */
|
|
5 package de.mpiwg.itgroup.annotationManager.restlet;
|
|
6
|
|
7 import java.io.IOException;
|
|
8 import java.util.List;
|
|
9
|
|
10 import org.json.JSONArray;
|
|
11 import org.json.JSONException;
|
|
12 import org.json.JSONObject;
|
|
13 import org.restlet.Context;
|
|
14 import org.restlet.data.Form;
|
|
15 import org.restlet.data.MediaType;
|
|
16 import org.restlet.data.Status;
|
|
17 import org.restlet.ext.json.JsonRepresentation;
|
|
18 import org.restlet.representation.Representation;
|
|
19 import org.restlet.representation.StringRepresentation;
|
|
20 import org.restlet.resource.Get;
|
|
21 import org.restlet.resource.Post;
|
|
22 import org.restlet.security.User;
|
|
23
|
|
24 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
|
25 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
|
26 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
|
|
27 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
|
|
28 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
|
|
29 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
|
|
30 import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
|
|
31 import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
|
|
32 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
33
|
|
34 /**
|
|
35 * Implements the "annotations" uri of the Annotator API. see <https://github.com/okfn/annotator/wiki/Storage>
|
|
36 *
|
|
37 * @author dwinter, casties
|
|
38 *
|
|
39 */
|
|
40 public class AnnotatorAnnotations extends AnnotatorResourceImpl {
|
|
41
|
|
42 protected String getAllowedMethodsForHeader() {
|
|
43 return "OPTIONS,GET,POST,PUT,DELETE";
|
|
44 }
|
|
45
|
|
46 /**
|
|
47 * GET with JSON content-type.
|
|
48 *
|
|
49 * @param entity
|
|
50 * @return
|
|
51 */
|
|
52 @Get("json")
|
|
53 public Representation doGetJSON(Representation entity) {
|
13
|
54 logger.debug("AnnotatorAnnotations doGetJSON!");
|
11
|
55 doOptions(entity);
|
|
56 // TODO: what to do with authentication?
|
|
57 boolean authenticated = isAuthenticated(entity);
|
|
58 logger.debug("request authenticated=" + authenticated);
|
|
59
|
|
60 // TODO: Annotator read request does not use parameters
|
|
61 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
62 String uri = form.getFirstValue("uri");
|
|
63 String user = form.getFirstValue("user");
|
|
64
|
|
65 String limit = form.getFirstValue("limit");
|
|
66 String offset = form.getFirstValue("offset");
|
|
67
|
|
68 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
|
|
69
|
|
70 JSONArray results;
|
|
71 try {
|
|
72 List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
|
|
73
|
|
74 results = new JSONArray();
|
|
75 for (Convert.Annotation annot : annots) {
|
13
|
76 JSONObject jo = createAnnotatorJson(annot);
|
11
|
77 if (jo != null) {
|
13
|
78 results.put(createAnnotatorJson(annot));
|
11
|
79 } else {
|
|
80 setStatus(Status.SERVER_ERROR_INTERNAL, "JSon Error");
|
|
81 return null;
|
|
82 }
|
|
83 }
|
|
84 } catch (TripleStoreHandlerException e) {
|
|
85 e.printStackTrace();
|
|
86 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
|
|
87 return null;
|
|
88 } catch (TripleStoreSearchError e) {
|
|
89 e.printStackTrace();
|
|
90 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
|
|
91 return null;
|
|
92 }
|
|
93
|
|
94 // annotator read request returns a list of annotation objects
|
|
95 logger.debug("sending:");
|
|
96 logger.debug(results);
|
|
97 return new JsonRepresentation(results);
|
|
98 }
|
|
99
|
|
100 /**
|
13
|
101 * POST with JSON content-type.
|
|
102 *
|
|
103 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des
|
11
|
104 * Dokumentes) text: text der annotation annoturl: url auf eine Annotation falls extern
|
|
105 *
|
|
106 * @return
|
|
107 */
|
|
108 @Post("json")
|
|
109 public Representation doPostJson(Representation entity) {
|
13
|
110 logger.debug("AnnotatorAnnotations doPostJSON!");
|
|
111 // set headers
|
|
112 setCorsHeaders();
|
|
113 Annotation annot = null;
|
11
|
114 try {
|
13
|
115 JsonRepresentation jrep = new JsonRepresentation(entity);
|
11
|
116 JSONObject jo = jrep.getJsonObject();
|
|
117 if (jo == null) {
|
|
118 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
119 return null;
|
|
120 }
|
13
|
121 // get Annotation object from posted JSON
|
11
|
122 annot = createAnnotation(jo, entity);
|
|
123 } catch (IOException e1) {
|
|
124 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
125 return null;
|
|
126 } catch (JSONException e) {
|
|
127 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
128 return null;
|
|
129 }
|
13
|
130 if (annot == null || annot.xpointer == null || annot.creator == null) {
|
11
|
131 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
132 return null;
|
|
133 }
|
13
|
134 Annotation storedAnnot;
|
11
|
135 try {
|
13
|
136 // store Annotation
|
|
137 storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
|
11
|
138 } catch (TripleStoreStoreError e) {
|
|
139 e.printStackTrace();
|
|
140 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
|
|
141 return null;
|
|
142 }
|
13
|
143 // return stored annotation
|
|
144 JSONObject jo = createAnnotatorJson(storedAnnot);
|
11
|
145 JsonRepresentation retRep = new JsonRepresentation(jo);
|
|
146 return retRep;
|
|
147 }
|
|
148
|
|
149 /**
|
|
150 * POST with HTML content-type.
|
|
151 *
|
|
152 * @param entity
|
|
153 * @return
|
|
154 */
|
|
155 @Post("html")
|
|
156 public Representation doPostHtml(Representation entity) {
|
13
|
157 logger.debug("AnnotatorAnnotations doPostHtml!");
|
11
|
158 Convert.Annotation annot;
|
|
159 annot = handleForm(entity);
|
|
160 if (annot.xpointer == null || annot.creator == null) {
|
|
161 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
162
|
|
163 return null;
|
|
164 }
|
|
165
|
|
166 Annotation retValAnnot;
|
|
167 try {
|
|
168 retValAnnot = new Convert("file:///annotations").storeAnnotation(annot);
|
|
169 } catch (TripleStoreStoreError e) {
|
|
170 e.printStackTrace();
|
|
171 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
|
|
172 return null;
|
|
173 }
|
|
174 if (retValAnnot == null) {
|
|
175 return null;
|
|
176 }
|
|
177 String retVal = retValAnnot.annotationUri;
|
|
178 if (retVal == null) {
|
|
179 return null;
|
|
180 }
|
|
181
|
|
182 String text = String.format("<html><body><a href=\"%s\">%s</a></body></html>", retVal.replace(">", "").replace("<", ""),
|
|
183 retVal.replace(">", ">").replace("<", "<"));
|
|
184 Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML);
|
|
185 return retRep;
|
|
186 }
|
|
187
|
|
188 /**
|
|
189 *
|
13
|
190 * @param entity
|
|
191 * should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
|
11
|
192 *
|
13
|
193 * username,password is optional, if not given BasicAuthentification is used.
|
11
|
194 *
|
13
|
195 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG
|
|
196 * namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
|
11
|
197 *
|
|
198 * @return
|
|
199 */
|
|
200 protected Convert.Annotation handleForm(Representation entity) {
|
|
201 Convert.Annotation annot;
|
|
202 Form form = new Form(entity);
|
|
203 String username = form.getValues("username");
|
|
204 String mode = form.getValues("mode");
|
|
205 String password = form.getValues("password");
|
|
206 String xpointer = form.getValues("xpointer");
|
|
207 String text = form.getValues("text");
|
|
208 String title = form.getValues("title");
|
|
209 String url = form.getValues("url");
|
|
210 String type = form.getValues("type");
|
|
211 RestServer restServer = (RestServer) getApplication();
|
|
212
|
|
213 // falls user and password nicht null sind:
|
|
214 User userFromForm = null;
|
|
215 if (username != null && password != null) {
|
|
216 if (restServer.authenticate(username, password, getRequest())) {
|
|
217 userFromForm = new User(username);
|
|
218 }
|
|
219 }
|
|
220 User authUser = null;
|
|
221
|
|
222 if (userFromForm == null) {
|
13
|
223 authUser = getHttpAuthUser(entity);
|
11
|
224 }
|
|
225
|
|
226 // weder BasicAuth noch FormAuth
|
|
227 if (authUser == null && userFromForm == null) {
|
|
228 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
229 return null;
|
|
230 }
|
|
231
|
|
232 if (userFromForm != null) {
|
|
233 username = userFromForm.getIdentifier();
|
|
234 } else {
|
|
235 username = authUser.getIdentifier();
|
|
236 }
|
|
237
|
13
|
238 // username should be a URI, if not it will set to the MPIWG namespace defined in
|
|
239 // de.mpiwg.itgroup.annotationManager.Constants.NS
|
|
240 String usernameOrig = username;
|
11
|
241 if (!username.startsWith("http"))
|
13
|
242 username = NS.MPIWG_PERSONS + username;
|
|
243
|
|
244 if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource
|
|
245
|
11
|
246 Context context = getContext();
|
|
247 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
|
13
|
248
|
11
|
249 AnnotationHandler ah = new AnnotationHandler(drupalPath);
|
|
250 JSONObject newAnnot;
|
|
251 try {
|
|
252 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
|
|
253 } catch (UnknowUserException e1) {
|
|
254 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
|
|
255 e1.printStackTrace();
|
|
256 return null;
|
|
257 }
|
|
258 try {
|
13
|
259 annot = new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
|
11
|
260 } catch (JSONException e) {
|
|
261 // TODO Auto-generated catch block
|
|
262 e.printStackTrace();
|
|
263 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
264 return null;
|
|
265 }
|
|
266 } else
|
13
|
267 annot = new Convert.Annotation(xpointer, username, null, text, type, url);
|
11
|
268 return annot;
|
|
269 }
|
|
270
|
|
271 }
|