annotate src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java @ 17:b0ef5c860464

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