diff 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
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java	Wed Mar 21 11:09:20 2012 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java	Wed Mar 21 15:23:57 2012 +0100
@@ -51,7 +51,7 @@
      */
     @Get("json")
     public Representation doGetJSON(Representation entity) {
-
+        logger.debug("AnnotatorAnnotations doGetJSON!");
         doOptions(entity);
         // TODO: what to do with authentication?
         boolean authenticated = isAuthenticated(entity);
@@ -73,9 +73,9 @@
 
             results = new JSONArray();
             for (Convert.Annotation annot : annots) {
-                JSONObject jo = annot2AnnotatorJSON(annot);
+                JSONObject jo = createAnnotatorJson(annot);
                 if (jo != null) {
-                    results.put(annot2AnnotatorJSON(annot));
+                    results.put(createAnnotatorJson(annot));
                 } else {
                     setStatus(Status.SERVER_ERROR_INTERNAL, "JSon Error");
                     return null;
@@ -98,22 +98,27 @@
     }
 
     /**
-     * POST with JSON content-type. json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des
+     * POST with JSON content-type. 
+     * 
+     * json hash: username: name des users xpointer: xpointer auf den Ausschnitt (incl. der URL des
      * Dokumentes) text: text der annotation annoturl: url auf eine Annotation falls extern
      * 
      * @return
      */
     @Post("json")
     public Representation doPostJson(Representation entity) {
-        JsonRepresentation jrep;
-        Annotation annot;
+        logger.debug("AnnotatorAnnotations doPostJSON!");
+        // set headers
+        setCorsHeaders();
+        Annotation annot = null;
         try {
-            jrep = new JsonRepresentation(entity);
+            JsonRepresentation jrep = new JsonRepresentation(entity);
             JSONObject jo = jrep.getJsonObject();
             if (jo == null) {
                 setStatus(Status.SERVER_ERROR_INTERNAL);
                 return null;
             }
+            // get Annotation object from posted JSON
             annot = createAnnotation(jo, entity);
         } catch (IOException e1) {
             setStatus(Status.SERVER_ERROR_INTERNAL);
@@ -122,19 +127,21 @@
             setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
             return null;
         }
-        if (annot.xpointer == null || annot.creator == null) {
+        if (annot == null || annot.xpointer == null || annot.creator == null) {
             setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
             return null;
         }
-        Annotation retVal;
+        Annotation storedAnnot;
         try {
-            retVal = new Convert("file:///annotations").storeAnnotation(annot);
+            // store Annotation
+            storedAnnot = new Convert("file:///annotations").storeAnnotation(annot);
         } catch (TripleStoreStoreError e) {
             e.printStackTrace();
             setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error");
             return null;
         }
-        JSONObject jo = annot2AnnotatorJSON(retVal);
+        // return stored annotation
+        JSONObject jo = createAnnotatorJson(storedAnnot);
         JsonRepresentation retRep = new JsonRepresentation(jo);
         return retRep;
     }
@@ -147,6 +154,7 @@
      */
     @Post("html")
     public Representation doPostHtml(Representation entity) {
+        logger.debug("AnnotatorAnnotations doPostHtml!");
         Convert.Annotation annot;
         annot = handleForm(entity);
         if (annot.xpointer == null || annot.creator == null) {
@@ -177,16 +185,15 @@
         return retRep;
     }
 
-
     /**
      * 
-     * @param entity should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
-     * 
-     * username,password is optional, if not given BasicAuthentification is used.
+     * @param entity
+     *            should contain a form with the parameters "username", "password", "xpointer","text","uri","type"
      * 
-     * 
+     *            username,password is optional, if not given BasicAuthentification is used.
      * 
-     * 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
+     *            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
      * 
      * @return
      */
@@ -213,7 +220,7 @@
         User authUser = null;
 
         if (userFromForm == null) {
-            authUser = handleBasicAuthentification(entity);
+            authUser = getHttpAuthUser(entity);
         }
 
         // weder BasicAuth noch FormAuth
@@ -228,17 +235,17 @@
             username = authUser.getIdentifier();
         }
 
-        //username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
-        String usernameOrig=username;
+        // username should be a URI, if not it will set to the MPIWG namespace defined in
+        // de.mpiwg.itgroup.annotationManager.Constants.NS
+        String usernameOrig = username;
         if (!username.startsWith("http"))
-            username=NS.MPIWG_PERSONS+username;
-        
-        if (mode.equals("complexAnnotation")){// Annotation mit text in externer ressource
-            
+            username = NS.MPIWG_PERSONS + username;
+
+        if (mode.equals("complexAnnotation")) {// Annotation mit text in externer ressource
+
             Context context = getContext();
             String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
-            
-            
+
             AnnotationHandler ah = new AnnotationHandler(drupalPath);
             JSONObject newAnnot;
             try {
@@ -249,7 +256,7 @@
                 return null;
             }
             try {
-                annot= new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
+                annot = new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
             } catch (JSONException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
@@ -257,8 +264,7 @@
                 return null;
             }
         } else
-            annot = new Convert.Annotation(xpointer, username, null, text,
-                type, url);
+            annot = new Convert.Annotation(xpointer, username, null, text, type, url);
         return annot;
     }