From ca9872be43938cf03f23ca9ac05a2acd69429cf3 Mon Sep 17 00:00:00 2001
From: ulrich <undisclosed@undiclosed>
Date: Tue, 06 Apr 2021 14:49:30 +0000
Subject: [PATCH] Katalog (in Arbeit)

---
 src/de/uhilger/mediaz/api/StorageHandler.java |   33 +++++++++++++++++++++------------
 1 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/de/uhilger/mediaz/api/StorageHandler.java b/src/de/uhilger/mediaz/api/StorageHandler.java
index 024900b..f022ada 100644
--- a/src/de/uhilger/mediaz/api/StorageHandler.java
+++ b/src/de/uhilger/mediaz/api/StorageHandler.java
@@ -36,12 +36,12 @@
 import java.util.logging.Logger;
 
 /**
- * HttpHandler fuer die Ablage von Entitaeten der Mediazentrale
+ * HttpHandler fuer die Verwaltung von Entitaeten der Mediazentrale
  * 
  * @author Ulrich Hilger
  * @version 1, 5.4.2021
  */
-public class StorageHandler implements HttpHandler  {
+public class StorageHandler extends AbstractHandler implements HttpHandler {
   
   private static final Logger logger = Logger.getLogger(StorageHandler.class.getName());
 
@@ -120,7 +120,7 @@
         }
         break;
     }
-    logger.info(response);
+    logger.fine(response);
     e.sendResponseHeaders(code, response.length());
     OutputStream os = e.getResponseBody();
     os.write(response.getBytes());
@@ -128,6 +128,14 @@
   }
   
   private String neu(HttpExchange e) throws IOException {
+    return schreiben(e, false);
+  }
+  
+  private String aendern(HttpExchange e) throws IOException {
+    return schreiben(e, true);
+  }
+  
+  private String schreiben(HttpExchange e, boolean overwrite) throws IOException {
     String path = e.getRequestURI().toString();
     String[] elems = path.split(App.getRs(Server.RB_SLASH));
     String type = elems[elems.length - 1];
@@ -135,22 +143,25 @@
     String filename = ""; 
     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
     Gson gson = new Gson();
-    logger.log(Level.INFO, "type: {0}", type);
+    logger.log(Level.FINE, "type: {0}", type);
     Object o = gson.fromJson(body, fs.typeFromName(type).getType());
     if(o instanceof Entity) {
-      Object antwortObjekt = fs.write((Entity) o);
+      Object antwortObjekt = fs.write((Entity) o, overwrite);
       if(antwortObjekt instanceof File) {
         File file = (File) antwortObjekt;
-        logger.log(Level.INFO, "Datei {0} geschrieben.", file.getAbsolutePath());
+        logger.log(Level.FINE, "Datei {0} geschrieben.", file.getAbsolutePath());
         filename = file.getName();
+        return type + FileHandler.STR_BLANK + filename;
       }
     }
-    return type + FileHandler.STR_BLANK + filename;
+    return type + FileHandler.STR_BLANK + " Operation 'neu' fuer bereits existierende Entitaet.";
   }
   
+  /*
   private String aendern(HttpExchange e) throws IOException {
     return neu(e); // einstweilen wird einfach ueberschrieben
   }
+  */
   
   private boolean loeschen(HttpExchange e) {
     String path = e.getRequestURI().toString();
@@ -167,17 +178,15 @@
     FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
     if(path.endsWith(App.getRs(RB_SLASH))) {
       String type = elems[elems.length - 1];
-      logger.info(type);
+      logger.fine(type);
       List list = fs.list(type);
-      Gson gson = new Gson();
-      return gson.toJson(list);
+      return jsonWithEnclosingType(list);
     } else {
       String type = elems[elems.length - 2];
       String elemName = elems[elems.length - 1];
       return fs.readJson(type, elemName);
     }
   }
-  
   
   private String bodyLesen(HttpExchange e) throws IOException {
     InputStream is = e.getRequestBody();
@@ -190,7 +199,7 @@
     }
     r.close();
     String json = sb.toString();
-    logger.log(Level.INFO, "json: {0}", json);
+    logger.log(Level.FINE, "json: {0}", json);
     return json;
   }
 }

--
Gitblit v1.9.3