annotate src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java @ 18:383c0fee2d37

PUT returns annotation now.
author casties
date Fri, 23 Mar 2012 12:13:48 +0100
parents b0ef5c860464
children a681113fd0eb
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 }
18
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
153 /* according to https://github.com/okfn/annotator/wiki/Storage
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
154 * we should return 303: see other.
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
155 * For now we return the annotation.
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
156 */
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
157 JSONObject jo = createAnnotatorJson(storedAnnot);
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
158 JsonRepresentation retRep = new JsonRepresentation(jo);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
159 return retRep;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
160 }
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 /**
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
163 * POST with HTML content-type.
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 * @param entity
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
166 * @return
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
167 */
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
168 @Post("html")
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
169 public Representation doPostHtml(Representation entity) {
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
170 logger.debug("AnnotatorAnnotations doPostHtml!");
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
171 Annotation annot;
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
172 annot = handleForm(entity);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
173 if (annot.xpointer == null || annot.creator == null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
174 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
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 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
177 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
178
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
179 Annotation retValAnnot;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
180 try {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
181 retValAnnot = new Convert("file:///annotations").storeAnnotation(annot);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
182 } catch (TripleStoreStoreError e) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
183 e.printStackTrace();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
184 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
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 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
187 if (retValAnnot == null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
188 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
189 }
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
190 String retVal = retValAnnot.getAnnotationUri();
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
191 if (retVal == null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
192 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
193 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
194
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
195 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
196 retVal.replace(">", "&gt;").replace("<", "&lt;"));
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
197 Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
198 return retRep;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
199 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
200
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
201 /**
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 * @param entity
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
204 * 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
205 *
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
206 * username,password is optional, if not given BasicAuthentification is used.
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
207 *
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
208 * 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
209 * namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
210 *
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
211 * @return
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
212 */
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
213 protected Annotation handleForm(Representation entity) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
214 Annotation annot;
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
215 Form form = new Form(entity);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
216 String username = form.getValues("username");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
217 String mode = form.getValues("mode");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
218 String password = form.getValues("password");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
219 String xpointer = form.getValues("xpointer");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
220 String text = form.getValues("text");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
221 String title = form.getValues("title");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
222 String url = form.getValues("url");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
223 String type = form.getValues("type");
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
224 RestServer restServer = (RestServer) getApplication();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
225
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
226 // falls user and password nicht null sind:
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
227 User userFromForm = null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
228 if (username != null && password != null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
229 if (restServer.authenticate(username, password, getRequest())) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
230 userFromForm = new User(username);
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 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
233 User authUser = null;
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 if (userFromForm == null) {
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
236 authUser = getHttpAuthUser(entity);
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
237 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
238
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
239 // weder BasicAuth noch FormAuth
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
240 if (authUser == null && userFromForm == null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
241 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
242 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
243 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
244
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
245 if (userFromForm != null) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
246 username = userFromForm.getIdentifier();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
247 } else {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
248 username = authUser.getIdentifier();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
249 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
250
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
251 // 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
252 // de.mpiwg.itgroup.annotationManager.Constants.NS
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
253 String usernameOrig = username;
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
254 if (!username.startsWith("http"))
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
255 username = NS.MPIWG_PERSONS + username;
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
256
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
257 if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource
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 Context context = getContext();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
260 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
13
9393c9c9b916 saves annotations now!
casties
parents: 11
diff changeset
261
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
262 AnnotationHandler ah = new AnnotationHandler(drupalPath);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
263 JSONObject newAnnot;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
264 try {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
265 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
266 } catch (UnknowUserException e1) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
267 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
268 e1.printStackTrace();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
269 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
270 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
271 try {
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
272 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
273 } catch (JSONException e) {
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
274 // TODO Auto-generated catch block
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
275 e.printStackTrace();
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
276 setStatus(Status.SERVER_ERROR_INTERNAL);
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
277 return null;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
278 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
279 } else
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
280 annot = new Annotation(xpointer, username, null, text, type, url);
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
281 return annot;
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
282 }
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
283
17
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 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
286 * PUT with JSON content-type.
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 * @param entity
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
289 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
290 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
291 @Put("json")
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
292 public Representation doPutJSON(Representation entity) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
293 logger.debug("AnnotatorAnnotations doPutJSON!");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
294 setCorsHeaders();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
295 // id from URI /annotations/{id}
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
296 String jsonId = (String) getRequest().getAttributes().get("id");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
297 String id = decodeJsonId(jsonId);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
298 logger.debug("annotation-id=" + id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
299
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
300 // TODO: what to do with authentication? we should check the owner
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
301 boolean authenticated = isAuthenticated(entity);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
302 logger.debug("request authenticated=" + authenticated);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
303 if (!authenticated) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
304 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
305 return null;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
306 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
307
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
308 Annotation annot = null;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
309 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
310 JsonRepresentation jrep = new JsonRepresentation(entity);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
311 JSONObject jo = jrep.getJsonObject();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
312 if (jo == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
313 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
314 return null;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
315 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
316 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
317 // get stored Annotation
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
318 List<Annotation> annots = searcher.searchById(id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
319 if (annots.size() < 1) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
320 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
321 return null;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
322 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
323 Annotation storedAnnot = annots.get(0);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
324 // delete
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
325 searcher.deleteById(id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
326 // update from posted JSON
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
327 annot = updateAnnotation(storedAnnot, jo, entity);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
328 // store Annotation
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
329 storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
330 /* according to https://github.com/okfn/annotator/wiki/Storage
18
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
331 * we should return 303: see other.
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
332 * For now we return the annotation.
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
333 */
18
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
334 jo = createAnnotatorJson(storedAnnot);
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
335 JsonRepresentation retRep = new JsonRepresentation(jo);
383c0fee2d37 PUT returns annotation now.
casties
parents: 17
diff changeset
336 return retRep;
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
337 } catch (TripleStoreHandlerException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
338 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
339 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
340 } catch (TripleStoreSearchError 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.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
343 } catch (JSONException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
344 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
345 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
346 } catch (IOException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
347 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
348 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
349 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
350 return null;
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 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
354 * DELETE with JSON content-type.
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
355 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
356 * @param entity
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
357 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
358 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
359 @Delete("json")
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
360 public Representation doDeleteJSON(Representation entity) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
361 logger.debug("AnnotatorAnnotations doDeleteJSON!");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
362 setCorsHeaders();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
363 // id from URI /annotations/{id}
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
364 String jsonId = (String) getRequest().getAttributes().get("id");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
365 String id = decodeJsonId(jsonId);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
366 logger.debug("annotation-id=" + id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
367
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
368 // TODO: what to do with authentication? we should check the owner
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
369 boolean authenticated = isAuthenticated(entity);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
370 logger.debug("request authenticated=" + authenticated);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
371 if (!authenticated) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
372 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
373 return null;
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
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
376 RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
377
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
378 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
379 // delete annotation
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
380 searcher.deleteById(id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
381 setStatus(Status.SUCCESS_NO_CONTENT);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
382 } catch (TripleStoreHandlerException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
383 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
384 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreHandler Error");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
385 } catch (TripleStoreSearchError e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
386 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
387 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStoreSearch Error");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
388 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
389 return null;
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
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
392
11
2f8c72ae4c43 working on create and read api for annotator.
casties
parents:
diff changeset
393 }