Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 4:3599b29c393f
store seems to work now :-)
| author | casties |
|---|---|
| date | Mon, 02 Jul 2012 22:39:46 +0200 |
| parents | |
| children | 629e15b345aa |
comparison
equal
deleted
inserted
replaced
| 3:47b53ae385d1 | 4:3599b29c393f |
|---|---|
| 1 /** | |
| 2 * Implements the "search" uri of the Annotator API. | |
| 3 */ | |
| 4 package de.mpiwg.itgroup.annotations.restlet; | |
| 5 | |
| 6 import java.util.List; | |
| 7 | |
| 8 import org.json.JSONArray; | |
| 9 import org.json.JSONException; | |
| 10 import org.json.JSONObject; | |
| 11 import org.restlet.data.Form; | |
| 12 import org.restlet.data.Status; | |
| 13 import org.restlet.ext.json.JsonRepresentation; | |
| 14 import org.restlet.representation.Representation; | |
| 15 import org.restlet.resource.Get; | |
| 16 | |
| 17 import de.mpiwg.itgroup.annotations.Annotation; | |
| 18 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
| 19 | |
| 20 /** | |
| 21 * Implements the "search" uri of the Annotator API. see | |
| 22 * <https://github.com/okfn/annotator/wiki/Storage> | |
| 23 * | |
| 24 * @author casties | |
| 25 * | |
| 26 */ | |
| 27 public class AnnotatorSearch extends AnnotatorResourceImpl { | |
| 28 | |
| 29 protected String getAllowedMethodsForHeader() { | |
| 30 return "OPTIONS,GET"; | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * result for JSON content-type. optional search parameters: uri user limit | |
| 35 * offset | |
| 36 * | |
| 37 * @param entity | |
| 38 * @return | |
| 39 */ | |
| 40 @Get("json") | |
| 41 public Representation doGetJSON(Representation entity) { | |
| 42 logger.debug("AnnotatorSearch doGetJSON!"); | |
| 43 setCorsHeaders(); | |
| 44 // TODO: what to do with authentication? | |
| 45 boolean authenticated = isAuthenticated(entity); | |
| 46 logger.debug("request authenticated=" + authenticated); | |
| 47 | |
| 48 Form form = getRequest().getResourceRef().getQueryAsForm(); | |
| 49 String uri = form.getFirstValue("uri"); | |
| 50 String user = form.getFirstValue("user"); | |
| 51 | |
| 52 String limit = form.getFirstValue("limit"); | |
| 53 String offset = form.getFirstValue("offset"); | |
| 54 | |
| 55 AnnotationStore searcher = getAnnotationStore(); | |
| 56 | |
| 57 JSONArray ja; | |
| 58 | |
| 59 List<Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset); | |
| 60 | |
| 61 ja = new JSONArray(); | |
| 62 for (Annotation annot : annots) { | |
| 63 JSONObject jo = createAnnotatorJson(annot); | |
| 64 if (jo != null) { | |
| 65 ja.put(createAnnotatorJson(annot)); | |
| 66 } else { | |
| 67 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
| 68 return null; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 JSONObject result = new JSONObject(); | |
| 73 try { | |
| 74 result.put("rows", ja); | |
| 75 result.put("total", ja.length()); | |
| 76 } catch (JSONException e) { | |
| 77 e.printStackTrace(); | |
| 78 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | |
| 79 return null; | |
| 80 } | |
| 81 | |
| 82 logger.debug("sending:"); | |
| 83 logger.debug(result); | |
| 84 return new JsonRepresentation(result); | |
| 85 } | |
| 86 | |
| 87 } |
