annotate src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java @ 13:9393c9c9b916

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