comparison src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java @ 3:e5f0906c107c

Neue Startseite Header bei Post werden jetzt richtig gesetzt.
author dwinter
date Fri, 25 Nov 2011 08:43:25 +0100
parents 6888ae3287b8
children c32080f364c6
comparison
equal deleted inserted replaced
2:6888ae3287b8 3:e5f0906c107c
38 38
39 public class AddAndSearchAnnotations extends ServerResource { 39 public class AddAndSearchAnnotations extends ServerResource {
40 40
41 private Logger logger = Logger.getRootLogger(); 41 private Logger logger = Logger.getRootLogger();
42 42
43 /**
44 *
45 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt
46 * (incl. der URL des Dokumentes) text: text der annotation annoturl: url
47 * auf eine Annotation falls extern
48 *
49 * @return
50 */
51 43
52 44
53 @Options 45 @Options
54 public void doOptions(Representation entity) { 46 public void doOptions(Representation entity) {
55 Form responseHeaders = (Form) getResponse().getAttributes().get( 47 Form responseHeaders = (Form) getResponse().getAttributes().get(
80 72
81 // 73 //
82 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file 74 RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
83 75
84 String retString="<html><body><table>"; 76 String retString="<html><body><table>";
85 String lineFormat="<tr><td>%s</td>" + 77 String lineFormat="<tr><td><a href=\"%s\">%s</a></td>" +
86 "<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>"; 78 "<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>";
87 try { 79 try {
88 80
89 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset); 81 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
90 82
94 RestServer restServer = (RestServer) getApplication(); 86 RestServer restServer = (RestServer) getApplication();
95 String userName=restServer.getUserNameFromLdap(annot.creator); 87 String userName=restServer.getUserNameFromLdap(annot.creator);
96 List<String> xpointer = new ArrayList<String>(); 88 List<String> xpointer = new ArrayList<String>();
97 89
98 if (annot.xpointers==null || annot.xpointers.size()==0) 90 if (annot.xpointers==null || annot.xpointers.size()==0)
99 retString+=String.format(lineFormat, userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri); 91 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,annot.xpointer,annot.xpointer,annot.annotationUri,annot.annotationUri);
100 else { 92 else {
101 for(String xpointerString:annot.xpointers){ 93 for(String xpointerString:annot.xpointers){
102 retString+=String.format(lineFormat, userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri); 94 retString+=String.format(lineFormat, userName,userName,annot.url,annot.url,annot.time,annot.text,xpointerString,xpointerString,annot.annotationUri,annot.annotationUri);
103
104 } 95 }
105 } 96 }
106 97
107 } 98 }
108 } catch (TripleStoreHandlerException e) { 99 } catch (TripleStoreHandlerException e) {
122 logger.debug("sending:"); 113 logger.debug("sending:");
123 logger.debug(retString); 114 logger.debug(retString);
124 return new StringRepresentation(retString,MediaType.TEXT_HTML); 115 return new StringRepresentation(retString,MediaType.TEXT_HTML);
125 } 116 }
126 117
118 /**
119 * Erzeugt aus einer Annotation, das fŸr den Annotator notwendige JSON-Format
120 * @param annot
121 * @return
122 */
123 public JSONObject annot2AnnotatorJSON(Convert.Annotation annot){
124 JSONObject jo = new JSONObject();
125 try {
126 jo.put("text", annot.text);
127 jo.put("uri",annot.url);
128
129 JSONObject userObject= new JSONObject();
130 userObject.put("id",annot.creator);
131
132 RestServer restServer = (RestServer) getApplication();
133
134 String userID= annot.creator;
135 if (userID.startsWith(NS.MPIWG_PERSONS)){
136 userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
137 }
138 String userName=restServer.getUserNameFromLdap(userID);
139 userObject.put("name",userName);
140
141 jo.put("user",userObject);
142
143 List<String> xpointer = new ArrayList<String>();
144
145 if (annot.xpointers==null || annot.xpointers.size()==0)
146 xpointer.add(annot.xpointer);
147 else {
148 for(String xpointerString:annot.xpointers){
149 xpointer.add(xpointerString);
150 }
151 }
152 jo.put("ranges", transformToRanges(xpointer));
153 jo.put("id", annot.annotationUri);
154 return jo;
155 } catch (JSONException e) {
156 // TODO Auto-generated catch block
157 e.printStackTrace();
158 return null;
159 }
160 }
127 161
128 @Get("json") 162 @Get("json")
129 public Representation doGetJSON(Representation entity){ 163 public Representation doGetJSON(Representation entity){
130 164
131 doOptions(entity); 165 doOptions(entity);
145 179
146 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset); 180 List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
147 181
148 ja = new JSONArray(); 182 ja = new JSONArray();
149 for (Convert.Annotation annot:annots){ 183 for (Convert.Annotation annot:annots){
150 JSONObject jo = new JSONObject(); 184 // JSONObject jo = new JSONObject();
151 jo.put("text", annot.text); 185 // jo.put("text", annot.text);
152 jo.put("uri",annot.url); 186 // jo.put("uri",annot.url);
153 187 //
154 JSONObject userObject= new JSONObject(); 188 // JSONObject userObject= new JSONObject();
155 userObject.put("id",annot.creator); 189 // userObject.put("id",annot.creator);
156 190 //
157 RestServer restServer = (RestServer) getApplication(); 191 // RestServer restServer = (RestServer) getApplication();
158 192 //
159 String userID= annot.creator; 193 // String userID= annot.creator;
160 if (userID.startsWith(NS.MPIWG_PERSONS)){ 194 // if (userID.startsWith(NS.MPIWG_PERSONS)){
161 userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE 195 // userID=userID.replace(NS.MPIWG_PERSONS, ""); //entferne NAMESPACE
162 } 196 // }
163 String userName=restServer.getUserNameFromLdap(userID); 197 // String userName=restServer.getUserNameFromLdap(userID);
164 userObject.put("name",userName); 198 // userObject.put("name",userName);
165 199 //
166 jo.put("user",userObject); 200 // jo.put("user",userObject);
167 201 //
168 List<String> xpointer = new ArrayList<String>(); 202 // List<String> xpointer = new ArrayList<String>();
169 203 //
170 if (annot.xpointers==null || annot.xpointers.size()==0) 204 // if (annot.xpointers==null || annot.xpointers.size()==0)
171 xpointer.add(annot.xpointer); 205 // xpointer.add(annot.xpointer);
172 else { 206 // else {
173 for(String xpointerString:annot.xpointers){ 207 // for(String xpointerString:annot.xpointers){
174 xpointer.add(xpointerString); 208 // xpointer.add(xpointerString);
175 } 209 // }
176 } 210 // }
177 jo.put("ranges", transformToRanges(xpointer)); 211 // jo.put("ranges", transformToRanges(xpointer));
178 ja.put(jo); 212 JSONObject jo = annot2AnnotatorJSON(annot);
213 if (jo!=null){
214 ja.put(annot2AnnotatorJSON(annot));
215 } else {
216 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
217 return null;
218 }
179 } 219 }
180 } catch (TripleStoreHandlerException e) { 220 } catch (TripleStoreHandlerException e) {
181 // TODO Auto-generated catch block 221 // TODO Auto-generated catch block
182 e.printStackTrace(); 222 e.printStackTrace();
183 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error"); 223 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreHandler Error");
185 } catch (TripleStoreSearchError e) { 225 } catch (TripleStoreSearchError e) {
186 // TODO Auto-generated catch block 226 // TODO Auto-generated catch block
187 e.printStackTrace(); 227 e.printStackTrace();
188 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error"); 228 setStatus(Status.SERVER_ERROR_INTERNAL,"TripleStoreSearch Error");
189 return null; 229 return null;
190 } catch (JSONException e) { 230 }
191 // TODO Auto-generated catch block
192 e.printStackTrace();
193 setStatus(Status.SERVER_ERROR_INTERNAL,"JSon Error");
194 return null;
195 }
196 231
197 JSONObject retObject = new JSONObject(); 232 JSONObject retObject = new JSONObject();
198 try { 233 try {
199 retObject.put("rows",ja); 234 retObject.put("rows",ja);
200 retObject.put("total",ja.length()); 235 retObject.put("total",ja.length());
260 295
261 296
262 297
263 } 298 }
264 299
300
301 /**
302 *
303 * json hash: username: name des users xpointer: xpointer auf den Ausschnitt
304 * (incl. der URL des Dokumentes) text: text der annotation annoturl: url
305 * auf eine Annotation falls extern
306 *
307 * @return
308 */
309
265 @Post("json") 310 @Post("json")
266 public Representation doPostJson(Representation entity) { 311 public Representation doPostJson(Representation entity) {
267 312
268 String retVal = doPost(entity); 313 Annotation retVal = doPost(entity);
269 JSONObject jo; 314
270 try { 315
271 jo = new JSONObject("{\"annotUrl\":\"" + retVal + "\"}"); 316 // JSONObject jo;
272 } catch (JSONException e) { 317 // try {
273 setStatus(Status.SERVER_ERROR_INTERNAL); 318 // jo = new JSONObject("{\"annotUrl\":\"" + retVal + "\"}");
274 return null; 319 // } catch (JSONException e) {
275 } 320 // setStatus(Status.SERVER_ERROR_INTERNAL);
321 // return null;
322 // }
323
324 if (retVal==null)
325 return null;
326 JSONObject jo = annot2AnnotatorJSON(retVal);
276 JsonRepresentation retRep = new JsonRepresentation(jo); 327 JsonRepresentation retRep = new JsonRepresentation(jo);
277 return retRep; 328 return retRep;
278 } 329 }
279 330
280 @Post("html") 331 @Post("html")
281 public Representation doPostHtml(Representation entity) { 332 public Representation doPostHtml(Representation entity) {
282 String retVal = doPost(entity); 333 Annotation retValAnnot = doPost(entity);
334 String retVal=retValAnnot.annotationUri;
283 if (retVal == null) { 335 if (retVal == null) {
284 return null; 336 return null;
285 } 337 }
286 String text = String.format( 338 String text = String.format(
287 "<html><body><a href=\"%s\">%s</a></body></html>", retVal 339 "<html><body><a href=\"%s\">%s</a></body></html>", retVal
290 Representation retRep = new StringRepresentation(text, 342 Representation retRep = new StringRepresentation(text,
291 MediaType.TEXT_HTML); 343 MediaType.TEXT_HTML);
292 return retRep; 344 return retRep;
293 } 345 }
294 346
295 public String doPost(Representation entity) { 347 public Convert.Annotation doPost(Representation entity) {
348
349 doOptions(entity);
296 Convert.Annotation annot; 350 Convert.Annotation annot;
297 // versuche basic authentifizierung und hole den Benutzer von dort. 351 // versuche basic authentifizierung und hole den Benutzer von dort.
298 352
299 // User authUser;= handleBasicAuthentification(entity); 353 // User authUser;= handleBasicAuthentification(entity);
300 354
488 } 542 }
489 543
490 /** 544 /**
491 * uses the specification from the annotator project. 545 * uses the specification from the annotator project.
492 * 546 *
493 * @see{https://github.com/okfn/annotator/wiki/Annotation-format The user 547 * @see{https://github.com/okfn/annotator/wiki/Annotation-format} The user
494 * object must 548 * object must
495 * contain an 549 * contain an
496 * id and 550 * id and
497 * password or 551 * password or
498 * basic 552 * basic