annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 3:47b53ae385d1

merging old code
author casties
date Fri, 29 Jun 2012 20:38:27 +0200
parents
children 3599b29c393f
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 * Implements the "annotations" uri of the Annotator API. see
47b53ae385d1 merging old code
casties
parents:
diff changeset
3 * <https://github.com/okfn/annotator/wiki/Storage>
47b53ae385d1 merging old code
casties
parents:
diff changeset
4 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
5 package de.mpiwg.itgroup.annotations.restlet;
47b53ae385d1 merging old code
casties
parents:
diff changeset
6
47b53ae385d1 merging old code
casties
parents:
diff changeset
7 import java.io.IOException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
8 import java.util.ArrayList;
47b53ae385d1 merging old code
casties
parents:
diff changeset
9 import java.util.List;
47b53ae385d1 merging old code
casties
parents:
diff changeset
10
47b53ae385d1 merging old code
casties
parents:
diff changeset
11 import org.json.JSONArray;
47b53ae385d1 merging old code
casties
parents:
diff changeset
12 import org.json.JSONException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
13 import org.json.JSONObject;
47b53ae385d1 merging old code
casties
parents:
diff changeset
14 import org.restlet.Context;
47b53ae385d1 merging old code
casties
parents:
diff changeset
15 import org.restlet.data.Form;
47b53ae385d1 merging old code
casties
parents:
diff changeset
16 import org.restlet.data.MediaType;
47b53ae385d1 merging old code
casties
parents:
diff changeset
17 import org.restlet.data.Reference;
47b53ae385d1 merging old code
casties
parents:
diff changeset
18 import org.restlet.data.Status;
47b53ae385d1 merging old code
casties
parents:
diff changeset
19 import org.restlet.ext.json.JsonRepresentation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
20 import org.restlet.representation.Representation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
21 import org.restlet.representation.StringRepresentation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
22 import org.restlet.resource.Delete;
47b53ae385d1 merging old code
casties
parents:
diff changeset
23 import org.restlet.resource.Get;
47b53ae385d1 merging old code
casties
parents:
diff changeset
24 import org.restlet.resource.Post;
47b53ae385d1 merging old code
casties
parents:
diff changeset
25 import org.restlet.resource.Put;
47b53ae385d1 merging old code
casties
parents:
diff changeset
26 import org.restlet.security.User;
47b53ae385d1 merging old code
casties
parents:
diff changeset
27
47b53ae385d1 merging old code
casties
parents:
diff changeset
28 import de.mpiwg.itgroup.annotationManager.Constants.NS;
47b53ae385d1 merging old code
casties
parents:
diff changeset
29 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
47b53ae385d1 merging old code
casties
parents:
diff changeset
30 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
47b53ae385d1 merging old code
casties
parents:
diff changeset
31 import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation;
47b53ae385d1 merging old code
casties
parents:
diff changeset
32 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
47b53ae385d1 merging old code
casties
parents:
diff changeset
33 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
47b53ae385d1 merging old code
casties
parents:
diff changeset
34 import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
47b53ae385d1 merging old code
casties
parents:
diff changeset
35 import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
36 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
47b53ae385d1 merging old code
casties
parents:
diff changeset
37
47b53ae385d1 merging old code
casties
parents:
diff changeset
38 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
39 * Implements the "annotations" uri of the Annotator API. see <https://github.com/okfn/annotator/wiki/Storage>
47b53ae385d1 merging old code
casties
parents:
diff changeset
40 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
41 * @author dwinter, casties
47b53ae385d1 merging old code
casties
parents:
diff changeset
42 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
43 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
44 public class AnnotatorAnnotations extends AnnotatorResourceImpl {
47b53ae385d1 merging old code
casties
parents:
diff changeset
45
47b53ae385d1 merging old code
casties
parents:
diff changeset
46 protected String getAllowedMethodsForHeader() {
47b53ae385d1 merging old code
casties
parents:
diff changeset
47 return "OPTIONS,GET,POST,PUT,DELETE";
47b53ae385d1 merging old code
casties
parents:
diff changeset
48 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
49
47b53ae385d1 merging old code
casties
parents:
diff changeset
50 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
51 * GET with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
52 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
53 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
54 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
55 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
56 @Get("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
57 public Representation doGetJSON(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
58 logger.debug("AnnotatorAnnotations doGetJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
59 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
60 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
61 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
62 String id = decodeJsonId(jsonId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
63 logger.debug("annotation-id=" + id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
64
47b53ae385d1 merging old code
casties
parents:
diff changeset
65 // TODO: what to return without id - list of all annotations?
47b53ae385d1 merging old code
casties
parents:
diff changeset
66
47b53ae385d1 merging old code
casties
parents:
diff changeset
67 // TODO: what to do with authentication?
47b53ae385d1 merging old code
casties
parents:
diff changeset
68 boolean authenticated = isAuthenticated(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
69 logger.debug("request authenticated=" + authenticated);
47b53ae385d1 merging old code
casties
parents:
diff changeset
70
47b53ae385d1 merging old code
casties
parents:
diff changeset
71 RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO should ge into config file
47b53ae385d1 merging old code
casties
parents:
diff changeset
72
47b53ae385d1 merging old code
casties
parents:
diff changeset
73 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
74 List<Annotation> annots = searcher.searchById(id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
75 if (annots.size() == 1) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
76 // there should be only one
47b53ae385d1 merging old code
casties
parents:
diff changeset
77 JSONObject result = createAnnotatorJson(annots.get(0));
47b53ae385d1 merging old code
casties
parents:
diff changeset
78 logger.debug("sending:");
47b53ae385d1 merging old code
casties
parents:
diff changeset
79 logger.debug(result);
47b53ae385d1 merging old code
casties
parents:
diff changeset
80 return new JsonRepresentation(result);
47b53ae385d1 merging old code
casties
parents:
diff changeset
81 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
82 JSONArray results;
47b53ae385d1 merging old code
casties
parents:
diff changeset
83 results = new JSONArray();
47b53ae385d1 merging old code
casties
parents:
diff changeset
84 for (Annotation annot : annots) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
85 JSONObject jo = createAnnotatorJson(annot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
86 if (jo != null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
87 results.put(createAnnotatorJson(annot));
47b53ae385d1 merging old code
casties
parents:
diff changeset
88 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
89 setStatus(Status.SERVER_ERROR_INTERNAL, "JSon Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
90 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
91 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
92 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
93 // annotator read request returns a list of annotation objects
47b53ae385d1 merging old code
casties
parents:
diff changeset
94 logger.debug("sending:");
47b53ae385d1 merging old code
casties
parents:
diff changeset
95 logger.debug(results);
47b53ae385d1 merging old code
casties
parents:
diff changeset
96 return new JsonRepresentation(results);
47b53ae385d1 merging old code
casties
parents:
diff changeset
97 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
98 } catch (TripleStoreHandlerException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
99 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
100 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
101 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
102 } catch (TripleStoreSearchError e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
103 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
104 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
105 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
106 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
107 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
108
47b53ae385d1 merging old code
casties
parents:
diff changeset
109 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
110 * POST with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
111 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
112 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des Dokumentes) text: text der
47b53ae385d1 merging old code
casties
parents:
diff changeset
113 * annotation annoturl: url auf eine Annotation falls extern
47b53ae385d1 merging old code
casties
parents:
diff changeset
114 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
115 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
116 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
117 @Post("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
118 public Representation doPostJson(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
119 logger.debug("AnnotatorAnnotations doPostJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
120 // set headers
47b53ae385d1 merging old code
casties
parents:
diff changeset
121 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
122 Annotation annot = null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
123 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
124 JsonRepresentation jrep = new JsonRepresentation(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
125 JSONObject jo = jrep.getJsonObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
126 if (jo == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
127 setStatus(Status.SERVER_ERROR_INTERNAL);
47b53ae385d1 merging old code
casties
parents:
diff changeset
128 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
129 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
130 // make sure id is not set for POST
47b53ae385d1 merging old code
casties
parents:
diff changeset
131 jo.remove("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
132 // get Annotation object from posted JSON
47b53ae385d1 merging old code
casties
parents:
diff changeset
133 annot = createAnnotation(jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
134 } catch (IOException e1) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
135 setStatus(Status.SERVER_ERROR_INTERNAL);
47b53ae385d1 merging old code
casties
parents:
diff changeset
136 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
137 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
138 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
139 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
140 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
141 if (annot == null || annot.xpointer == null || annot.creator == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
142 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
143 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
144 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
145 Annotation storedAnnot;
47b53ae385d1 merging old code
casties
parents:
diff changeset
146 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
147 // store Annotation
47b53ae385d1 merging old code
casties
parents:
diff changeset
148 storedAnnot = new Convert(NS.MPIWG_ANNOT_CTX).storeAnnotation(annot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
149 } catch (TripleStoreStoreError e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
150 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
151 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
152 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
153 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
154 /* according to https://github.com/okfn/annotator/wiki/Storage
47b53ae385d1 merging old code
casties
parents:
diff changeset
155 * we should return 303: see other.
47b53ae385d1 merging old code
casties
parents:
diff changeset
156 * For now we return the annotation.
47b53ae385d1 merging old code
casties
parents:
diff changeset
157 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
158 JSONObject jo = createAnnotatorJson(storedAnnot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
159 JsonRepresentation retRep = new JsonRepresentation(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
160 return retRep;
47b53ae385d1 merging old code
casties
parents:
diff changeset
161 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
162
47b53ae385d1 merging old code
casties
parents:
diff changeset
163 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
164 * POST with HTML content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
165 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
166 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
167 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
168 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
169 @Post("html")
47b53ae385d1 merging old code
casties
parents:
diff changeset
170 public Representation doPostHtml(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
171 logger.debug("AnnotatorAnnotations doPostHtml!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
172 Annotation annot;
47b53ae385d1 merging old code
casties
parents:
diff changeset
173 annot = handleForm(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
174 if (annot.xpointer == null || annot.creator == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
175 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
176
47b53ae385d1 merging old code
casties
parents:
diff changeset
177 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
178 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
179
47b53ae385d1 merging old code
casties
parents:
diff changeset
180 Annotation retValAnnot;
47b53ae385d1 merging old code
casties
parents:
diff changeset
181 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
182 retValAnnot = new Convert(NS.MPIWG_ANNOT_CTX).storeAnnotation(annot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
183 } catch (TripleStoreStoreError e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
184 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
185 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
186 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
187 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
188 if (retValAnnot == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
189 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
190 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
191 String retVal = retValAnnot.getAnnotationUri();
47b53ae385d1 merging old code
casties
parents:
diff changeset
192 if (retVal == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
193 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
194 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
195
47b53ae385d1 merging old code
casties
parents:
diff changeset
196 String text = String.format("<html><body><a href=\"%s\">%s</a></body></html>", retVal.replace(">", "").replace("<", ""),
47b53ae385d1 merging old code
casties
parents:
diff changeset
197 retVal.replace(">", "&gt;").replace("<", "&lt;"));
47b53ae385d1 merging old code
casties
parents:
diff changeset
198 Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML);
47b53ae385d1 merging old code
casties
parents:
diff changeset
199 return retRep;
47b53ae385d1 merging old code
casties
parents:
diff changeset
200 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
201
47b53ae385d1 merging old code
casties
parents:
diff changeset
202 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
203 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
204 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
205 * should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
47b53ae385d1 merging old code
casties
parents:
diff changeset
206 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
207 * username,password is optional, if not given BasicAuthentification is used.
47b53ae385d1 merging old code
casties
parents:
diff changeset
208 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
209 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG
47b53ae385d1 merging old code
casties
parents:
diff changeset
210 * namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
47b53ae385d1 merging old code
casties
parents:
diff changeset
211 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
212 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
213 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
214 protected Annotation handleForm(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
215 Annotation annot;
47b53ae385d1 merging old code
casties
parents:
diff changeset
216 Form form = new Form(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
217 String username = form.getValues("username");
47b53ae385d1 merging old code
casties
parents:
diff changeset
218 String mode = form.getValues("mode");
47b53ae385d1 merging old code
casties
parents:
diff changeset
219 String password = form.getValues("password");
47b53ae385d1 merging old code
casties
parents:
diff changeset
220 String xpointer = form.getValues("xpointer");
47b53ae385d1 merging old code
casties
parents:
diff changeset
221 String text = form.getValues("text");
47b53ae385d1 merging old code
casties
parents:
diff changeset
222 String title = form.getValues("title");
47b53ae385d1 merging old code
casties
parents:
diff changeset
223 String url = form.getValues("url");
47b53ae385d1 merging old code
casties
parents:
diff changeset
224 String type = form.getValues("type");
47b53ae385d1 merging old code
casties
parents:
diff changeset
225 RestServer restServer = (RestServer) getApplication();
47b53ae385d1 merging old code
casties
parents:
diff changeset
226
47b53ae385d1 merging old code
casties
parents:
diff changeset
227 // falls user and password nicht null sind:
47b53ae385d1 merging old code
casties
parents:
diff changeset
228 User userFromForm = null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
229 if (username != null && password != null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
230 if (restServer.authenticate(username, password, getRequest())) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
231 userFromForm = new User(username);
47b53ae385d1 merging old code
casties
parents:
diff changeset
232 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
233 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
234 User authUser = null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
235
47b53ae385d1 merging old code
casties
parents:
diff changeset
236 if (userFromForm == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
237 authUser = getHttpAuthUser(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
238 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
239
47b53ae385d1 merging old code
casties
parents:
diff changeset
240 // weder BasicAuth noch FormAuth
47b53ae385d1 merging old code
casties
parents:
diff changeset
241 if (authUser == null && userFromForm == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
242 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
47b53ae385d1 merging old code
casties
parents:
diff changeset
243 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
244 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
245
47b53ae385d1 merging old code
casties
parents:
diff changeset
246 if (userFromForm != null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
247 username = userFromForm.getIdentifier();
47b53ae385d1 merging old code
casties
parents:
diff changeset
248 } else {
47b53ae385d1 merging old code
casties
parents:
diff changeset
249 username = authUser.getIdentifier();
47b53ae385d1 merging old code
casties
parents:
diff changeset
250 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
251
47b53ae385d1 merging old code
casties
parents:
diff changeset
252 // username should be a URI, if not it will set to the MPIWG namespace defined in
47b53ae385d1 merging old code
casties
parents:
diff changeset
253 // de.mpiwg.itgroup.annotationManager.Constants.NS
47b53ae385d1 merging old code
casties
parents:
diff changeset
254 String usernameOrig = username;
47b53ae385d1 merging old code
casties
parents:
diff changeset
255 if (!username.startsWith("http"))
47b53ae385d1 merging old code
casties
parents:
diff changeset
256 username = NS.MPIWG_PERSONS_URL + username;
47b53ae385d1 merging old code
casties
parents:
diff changeset
257
47b53ae385d1 merging old code
casties
parents:
diff changeset
258 if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource
47b53ae385d1 merging old code
casties
parents:
diff changeset
259
47b53ae385d1 merging old code
casties
parents:
diff changeset
260 Context context = getContext();
47b53ae385d1 merging old code
casties
parents:
diff changeset
261 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
47b53ae385d1 merging old code
casties
parents:
diff changeset
262
47b53ae385d1 merging old code
casties
parents:
diff changeset
263 AnnotationHandler ah = new AnnotationHandler(drupalPath);
47b53ae385d1 merging old code
casties
parents:
diff changeset
264 JSONObject newAnnot;
47b53ae385d1 merging old code
casties
parents:
diff changeset
265 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
266 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
47b53ae385d1 merging old code
casties
parents:
diff changeset
267 } catch (UnknowUserException e1) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
268 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
47b53ae385d1 merging old code
casties
parents:
diff changeset
269 e1.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
270 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
271 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
272 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
273 annot = new Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
47b53ae385d1 merging old code
casties
parents:
diff changeset
274 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
275 // TODO Auto-generated catch block
47b53ae385d1 merging old code
casties
parents:
diff changeset
276 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
277 setStatus(Status.SERVER_ERROR_INTERNAL);
47b53ae385d1 merging old code
casties
parents:
diff changeset
278 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
279 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
280 } else
47b53ae385d1 merging old code
casties
parents:
diff changeset
281 annot = new Annotation(xpointer, username, null, text, type, url);
47b53ae385d1 merging old code
casties
parents:
diff changeset
282 return annot;
47b53ae385d1 merging old code
casties
parents:
diff changeset
283 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
284
47b53ae385d1 merging old code
casties
parents:
diff changeset
285
47b53ae385d1 merging old code
casties
parents:
diff changeset
286 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
287 * PUT with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
288 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
289 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
290 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
291 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
292 @Put("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
293 public Representation doPutJSON(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
294 logger.debug("AnnotatorAnnotations doPutJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
295 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
296 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
297 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
298 String id = decodeJsonId(jsonId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
299 logger.debug("annotation-id=" + id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
300
47b53ae385d1 merging old code
casties
parents:
diff changeset
301 // TODO: what to do with authentication? we should check the owner
47b53ae385d1 merging old code
casties
parents:
diff changeset
302 boolean authenticated = isAuthenticated(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
303 logger.debug("request authenticated=" + authenticated);
47b53ae385d1 merging old code
casties
parents:
diff changeset
304 if (!authenticated) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
305 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
306 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
307 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
308
47b53ae385d1 merging old code
casties
parents:
diff changeset
309 Annotation annot = null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
310 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
311 JsonRepresentation jrep = new JsonRepresentation(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
312 JSONObject jo = jrep.getJsonObject();
47b53ae385d1 merging old code
casties
parents:
diff changeset
313 if (jo == null) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
314 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
315 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
316 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
317 RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO should ge into config file
47b53ae385d1 merging old code
casties
parents:
diff changeset
318 // get stored Annotation
47b53ae385d1 merging old code
casties
parents:
diff changeset
319 List<Annotation> annots = searcher.searchById(id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
320 if (annots.size() < 1) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
321 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
47b53ae385d1 merging old code
casties
parents:
diff changeset
322 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
323 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
324 Annotation storedAnnot = annots.get(0);
47b53ae385d1 merging old code
casties
parents:
diff changeset
325 // delete
47b53ae385d1 merging old code
casties
parents:
diff changeset
326 searcher.deleteById(id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
327 // update from posted JSON
47b53ae385d1 merging old code
casties
parents:
diff changeset
328 annot = updateAnnotation(storedAnnot, jo, entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
329 // store Annotation
47b53ae385d1 merging old code
casties
parents:
diff changeset
330 storedAnnot = new Convert(NS.MPIWG_ANNOT_CTX).storeAnnotation(annot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
331 /* according to https://github.com/okfn/annotator/wiki/Storage
47b53ae385d1 merging old code
casties
parents:
diff changeset
332 * we should return 303: see other.
47b53ae385d1 merging old code
casties
parents:
diff changeset
333 * but the client doesn't like it
47b53ae385d1 merging old code
casties
parents:
diff changeset
334 setStatus(Status.REDIRECTION_SEE_OTHER);
47b53ae385d1 merging old code
casties
parents:
diff changeset
335 // go to same URL as this one
47b53ae385d1 merging old code
casties
parents:
diff changeset
336 Reference thisUrl = this.getReference();
47b53ae385d1 merging old code
casties
parents:
diff changeset
337 this.getResponse().setLocationRef(thisUrl); */
47b53ae385d1 merging old code
casties
parents:
diff changeset
338 // return new annotation
47b53ae385d1 merging old code
casties
parents:
diff changeset
339 jo = createAnnotatorJson(storedAnnot);
47b53ae385d1 merging old code
casties
parents:
diff changeset
340 JsonRepresentation retRep = new JsonRepresentation(jo);
47b53ae385d1 merging old code
casties
parents:
diff changeset
341 return retRep;
47b53ae385d1 merging old code
casties
parents:
diff changeset
342 } catch (TripleStoreHandlerException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
343 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
344 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
345 } catch (TripleStoreSearchError e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
346 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
347 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
348 } catch (JSONException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
349 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
350 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
47b53ae385d1 merging old code
casties
parents:
diff changeset
351 } catch (IOException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
352 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
353 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
354 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
355 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
356 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
357
47b53ae385d1 merging old code
casties
parents:
diff changeset
358 /**
47b53ae385d1 merging old code
casties
parents:
diff changeset
359 * DELETE with JSON content-type.
47b53ae385d1 merging old code
casties
parents:
diff changeset
360 *
47b53ae385d1 merging old code
casties
parents:
diff changeset
361 * @param entity
47b53ae385d1 merging old code
casties
parents:
diff changeset
362 * @return
47b53ae385d1 merging old code
casties
parents:
diff changeset
363 */
47b53ae385d1 merging old code
casties
parents:
diff changeset
364 @Delete("json")
47b53ae385d1 merging old code
casties
parents:
diff changeset
365 public Representation doDeleteJSON(Representation entity) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
366 logger.debug("AnnotatorAnnotations doDeleteJSON!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
367 setCorsHeaders();
47b53ae385d1 merging old code
casties
parents:
diff changeset
368 // id from URI /annotations/{id}
47b53ae385d1 merging old code
casties
parents:
diff changeset
369 String jsonId = (String) getRequest().getAttributes().get("id");
47b53ae385d1 merging old code
casties
parents:
diff changeset
370 String id = decodeJsonId(jsonId);
47b53ae385d1 merging old code
casties
parents:
diff changeset
371 logger.debug("annotation-id=" + id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
372
47b53ae385d1 merging old code
casties
parents:
diff changeset
373 // TODO: what to do with authentication? we should check the owner
47b53ae385d1 merging old code
casties
parents:
diff changeset
374 boolean authenticated = isAuthenticated(entity);
47b53ae385d1 merging old code
casties
parents:
diff changeset
375 logger.debug("request authenticated=" + authenticated);
47b53ae385d1 merging old code
casties
parents:
diff changeset
376 if (!authenticated) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
377 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
47b53ae385d1 merging old code
casties
parents:
diff changeset
378 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
379 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
380
47b53ae385d1 merging old code
casties
parents:
diff changeset
381 RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO should ge into config file
47b53ae385d1 merging old code
casties
parents:
diff changeset
382
47b53ae385d1 merging old code
casties
parents:
diff changeset
383 try {
47b53ae385d1 merging old code
casties
parents:
diff changeset
384 // delete annotation
47b53ae385d1 merging old code
casties
parents:
diff changeset
385 searcher.deleteById(id);
47b53ae385d1 merging old code
casties
parents:
diff changeset
386 setStatus(Status.SUCCESS_NO_CONTENT);
47b53ae385d1 merging old code
casties
parents:
diff changeset
387 } catch (TripleStoreHandlerException e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
388 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
389 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
390 } catch (TripleStoreSearchError e) {
47b53ae385d1 merging old code
casties
parents:
diff changeset
391 e.printStackTrace();
47b53ae385d1 merging old code
casties
parents:
diff changeset
392 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
47b53ae385d1 merging old code
casties
parents:
diff changeset
393 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
394 return null;
47b53ae385d1 merging old code
casties
parents:
diff changeset
395 }
47b53ae385d1 merging old code
casties
parents:
diff changeset
396
47b53ae385d1 merging old code
casties
parents:
diff changeset
397
47b53ae385d1 merging old code
casties
parents:
diff changeset
398 }