diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java	Mon Jul 02 22:39:46 2012 +0200
@@ -0,0 +1,87 @@
+/**
+ * Implements the "search" uri of the Annotator API.
+ */
+package de.mpiwg.itgroup.annotations.restlet;
+
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.restlet.data.Form;
+import org.restlet.data.Status;
+import org.restlet.ext.json.JsonRepresentation;
+import org.restlet.representation.Representation;
+import org.restlet.resource.Get;
+
+import de.mpiwg.itgroup.annotations.Annotation;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
+
+/**
+ * Implements the "search" uri of the Annotator API. see
+ * <https://github.com/okfn/annotator/wiki/Storage>
+ * 
+ * @author casties
+ * 
+ */
+public class AnnotatorSearch extends AnnotatorResourceImpl {
+
+    protected String getAllowedMethodsForHeader() {
+        return "OPTIONS,GET";
+    }
+
+    /**
+     * result for JSON content-type. optional search parameters: uri user limit
+     * offset
+     * 
+     * @param entity
+     * @return
+     */
+    @Get("json")
+    public Representation doGetJSON(Representation entity) {
+        logger.debug("AnnotatorSearch doGetJSON!");
+        setCorsHeaders();
+        // TODO: what to do with authentication?
+        boolean authenticated = isAuthenticated(entity);
+        logger.debug("request authenticated=" + authenticated);
+
+        Form form = getRequest().getResourceRef().getQueryAsForm();
+        String uri = form.getFirstValue("uri");
+        String user = form.getFirstValue("user");
+
+        String limit = form.getFirstValue("limit");
+        String offset = form.getFirstValue("offset");
+
+        AnnotationStore searcher = getAnnotationStore();
+
+        JSONArray ja;
+
+        List<Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset);
+
+        ja = new JSONArray();
+        for (Annotation annot : annots) {
+            JSONObject jo = createAnnotatorJson(annot);
+            if (jo != null) {
+                ja.put(createAnnotatorJson(annot));
+            } else {
+                setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
+                return null;
+            }
+        }
+
+        JSONObject result = new JSONObject();
+        try {
+            result.put("rows", ja);
+            result.put("total", ja.length());
+        } catch (JSONException e) {
+            e.printStackTrace();
+            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
+            return null;
+        }
+
+        logger.debug("sending:");
+        logger.debug(result);
+        return new JsonRepresentation(result);
+    }
+
+}