comparison 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
comparison
equal deleted inserted replaced
12:2e5d526079de 13:9393c9c9b916
49 * @param entity 49 * @param entity
50 * @return 50 * @return
51 */ 51 */
52 @Get("json") 52 @Get("json")
53 public Representation doGetJSON(Representation entity) { 53 public Representation doGetJSON(Representation entity) {
54 54 logger.debug("AnnotatorAnnotations doGetJSON!");
55 doOptions(entity); 55 doOptions(entity);
56 // TODO: what to do with authentication? 56 // TODO: what to do with authentication?
57 boolean authenticated = isAuthenticated(entity); 57 boolean authenticated = isAuthenticated(entity);
58 logger.debug("request authenticated=" + authenticated); 58 logger.debug("request authenticated=" + authenticated);
59 59
71 try { 71 try {
72 List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset); 72 List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
73 73
74 results = new JSONArray(); 74 results = new JSONArray();
75 for (Convert.Annotation annot : annots) { 75 for (Convert.Annotation annot : annots) {
76 JSONObject jo = annot2AnnotatorJSON(annot); 76 JSONObject jo = createAnnotatorJson(annot);
77 if (jo != null) { 77 if (jo != null) {
78 results.put(annot2AnnotatorJSON(annot)); 78 results.put(createAnnotatorJson(annot));
79 } else { 79 } else {
80 setStatus(Status.SERVER_ERROR_INTERNAL, "JSon Error"); 80 setStatus(Status.SERVER_ERROR_INTERNAL, "JSon Error");
81 return null; 81 return null;
82 } 82 }
83 } 83 }
96 logger.debug(results); 96 logger.debug(results);
97 return new JsonRepresentation(results); 97 return new JsonRepresentation(results);
98 } 98 }
99 99
100 /** 100 /**
101 * POST with JSON content-type. json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des 101 * POST with JSON content-type.
102 *
103 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des
102 * Dokumentes) text: text der annotation annoturl: url auf eine Annotation falls extern 104 * Dokumentes) text: text der annotation annoturl: url auf eine Annotation falls extern
103 * 105 *
104 * @return 106 * @return
105 */ 107 */
106 @Post("json") 108 @Post("json")
107 public Representation doPostJson(Representation entity) { 109 public Representation doPostJson(Representation entity) {
108 JsonRepresentation jrep; 110 logger.debug("AnnotatorAnnotations doPostJSON!");
109 Annotation annot; 111 // set headers
110 try { 112 setCorsHeaders();
111 jrep = new JsonRepresentation(entity); 113 Annotation annot = null;
114 try {
115 JsonRepresentation jrep = new JsonRepresentation(entity);
112 JSONObject jo = jrep.getJsonObject(); 116 JSONObject jo = jrep.getJsonObject();
113 if (jo == null) { 117 if (jo == null) {
114 setStatus(Status.SERVER_ERROR_INTERNAL); 118 setStatus(Status.SERVER_ERROR_INTERNAL);
115 return null; 119 return null;
116 } 120 }
121 // get Annotation object from posted JSON
117 annot = createAnnotation(jo, entity); 122 annot = createAnnotation(jo, entity);
118 } catch (IOException e1) { 123 } catch (IOException e1) {
119 setStatus(Status.SERVER_ERROR_INTERNAL); 124 setStatus(Status.SERVER_ERROR_INTERNAL);
120 return null; 125 return null;
121 } catch (JSONException e) { 126 } catch (JSONException e) {
122 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 127 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
123 return null; 128 return null;
124 } 129 }
125 if (annot.xpointer == null || annot.creator == null) { 130 if (annot == null || annot.xpointer == null || annot.creator == null) {
126 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 131 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
127 return null; 132 return null;
128 } 133 }
129 Annotation retVal; 134 Annotation storedAnnot;
130 try { 135 try {
131 retVal = new Convert("file:///annotations").storeAnnotation(annot); 136 // store Annotation
137 storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
132 } catch (TripleStoreStoreError e) { 138 } catch (TripleStoreStoreError e) {
133 e.printStackTrace(); 139 e.printStackTrace();
134 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error"); 140 setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
135 return null; 141 return null;
136 } 142 }
137 JSONObject jo = annot2AnnotatorJSON(retVal); 143 // return stored annotation
144 JSONObject jo = createAnnotatorJson(storedAnnot);
138 JsonRepresentation retRep = new JsonRepresentation(jo); 145 JsonRepresentation retRep = new JsonRepresentation(jo);
139 return retRep; 146 return retRep;
140 } 147 }
141 148
142 /** 149 /**
145 * @param entity 152 * @param entity
146 * @return 153 * @return
147 */ 154 */
148 @Post("html") 155 @Post("html")
149 public Representation doPostHtml(Representation entity) { 156 public Representation doPostHtml(Representation entity) {
157 logger.debug("AnnotatorAnnotations doPostHtml!");
150 Convert.Annotation annot; 158 Convert.Annotation annot;
151 annot = handleForm(entity); 159 annot = handleForm(entity);
152 if (annot.xpointer == null || annot.creator == null) { 160 if (annot.xpointer == null || annot.creator == null) {
153 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 161 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
154 162
175 retVal.replace(">", "&gt;").replace("<", "&lt;")); 183 retVal.replace(">", "&gt;").replace("<", "&lt;"));
176 Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML); 184 Representation retRep = new StringRepresentation(text, MediaType.TEXT_HTML);
177 return retRep; 185 return retRep;
178 } 186 }
179 187
180 188 /**
181 /** 189 *
182 * 190 * @param entity
183 * @param entity should contain a form with the parameters "username", "password", "xpointer","text","uri","type" 191 * should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
184 * 192 *
185 * username,password is optional, if not given BasicAuthentification is used. 193 * username,password is optional, if not given BasicAuthentification is used.
186 * 194 *
187 * 195 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG
188 * 196 * namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
189 * If username given as a URI, the username will be transformed to an URI, username will be added to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
190 * 197 *
191 * @return 198 * @return
192 */ 199 */
193 protected Convert.Annotation handleForm(Representation entity) { 200 protected Convert.Annotation handleForm(Representation entity) {
194 Convert.Annotation annot; 201 Convert.Annotation annot;
211 } 218 }
212 } 219 }
213 User authUser = null; 220 User authUser = null;
214 221
215 if (userFromForm == null) { 222 if (userFromForm == null) {
216 authUser = handleBasicAuthentification(entity); 223 authUser = getHttpAuthUser(entity);
217 } 224 }
218 225
219 // weder BasicAuth noch FormAuth 226 // weder BasicAuth noch FormAuth
220 if (authUser == null && userFromForm == null) { 227 if (authUser == null && userFromForm == null) {
221 setStatus(Status.CLIENT_ERROR_FORBIDDEN); 228 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
226 username = userFromForm.getIdentifier(); 233 username = userFromForm.getIdentifier();
227 } else { 234 } else {
228 username = authUser.getIdentifier(); 235 username = authUser.getIdentifier();
229 } 236 }
230 237
231 //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS 238 // username should be a URI, if not it will set to the MPIWG namespace defined in
232 String usernameOrig=username; 239 // de.mpiwg.itgroup.annotationManager.Constants.NS
240 String usernameOrig = username;
233 if (!username.startsWith("http")) 241 if (!username.startsWith("http"))
234 username=NS.MPIWG_PERSONS+username; 242 username = NS.MPIWG_PERSONS + username;
235 243
236 if (mode.equals("complexAnnotation")){// Annotation mit text in externer ressource 244 if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource
237 245
238 Context context = getContext(); 246 Context context = getContext();
239 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer"); 247 String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
240 248
241
242 AnnotationHandler ah = new AnnotationHandler(drupalPath); 249 AnnotationHandler ah = new AnnotationHandler(drupalPath);
243 JSONObject newAnnot; 250 JSONObject newAnnot;
244 try { 251 try {
245 newAnnot = ah.createAnnotation(title, text, usernameOrig, password); 252 newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
246 } catch (UnknowUserException e1) { 253 } catch (UnknowUserException e1) {
247 setStatus(Status.CLIENT_ERROR_FORBIDDEN); 254 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
248 e1.printStackTrace(); 255 e1.printStackTrace();
249 return null; 256 return null;
250 } 257 }
251 try { 258 try {
252 annot= new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri")); 259 annot = new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
253 } catch (JSONException e) { 260 } catch (JSONException e) {
254 // TODO Auto-generated catch block 261 // TODO Auto-generated catch block
255 e.printStackTrace(); 262 e.printStackTrace();
256 setStatus(Status.SERVER_ERROR_INTERNAL); 263 setStatus(Status.SERVER_ERROR_INTERNAL);
257 return null; 264 return null;
258 } 265 }
259 } else 266 } else
260 annot = new Convert.Annotation(xpointer, username, null, text, 267 annot = new Convert.Annotation(xpointer, username, null, text, type, url);
261 type, url);
262 return annot; 268 return annot;
263 } 269 }
264 270
265 } 271 }