From f45e203a66a5471a0dd05e0362566ef96a2b6b31 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Wed, 07 Apr 2021 16:26:34 +0000
Subject: [PATCH] Abspieler-Dropdown (in Arbeit)

---
 src/de/uhilger/mediaz/api/StorageHandler.java |   59 +++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/src/de/uhilger/mediaz/api/StorageHandler.java b/src/de/uhilger/mediaz/api/StorageHandler.java
index 8de56fd..3b8e010 100644
--- a/src/de/uhilger/mediaz/api/StorageHandler.java
+++ b/src/de/uhilger/mediaz/api/StorageHandler.java
@@ -18,10 +18,10 @@
 package de.uhilger.mediaz.api;
 
 import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
 import de.uhilger.mediaz.App;
+import static de.uhilger.mediaz.App.RB_EP_LISTE;
 import de.uhilger.mediaz.Server;
 import static de.uhilger.mediaz.Server.RB_SLASH;
 import de.uhilger.mediaz.store.FileStorage;
@@ -37,12 +37,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());
 
@@ -59,6 +59,9 @@
     Beispiele:
   
     HTTP GET an /mz/api/store/Ablageort/
+    liefert alle Ablageort-Objekte
+  
+    HTTP GET an /mz/api/store/Ablageort/liste/  
     liefert eine Liste der Namen vorhandener Ablageorte
   
     HTTP GET an /mz/api/store/Ablageort/Katalog
@@ -87,6 +90,8 @@
   
   /** Name der HTTP Methode DELETE */
   public static final String HTTP_DELETE = "DELETE";
+  
+  public static final String EP_LISTE = "liste/";
 
   @Override
   public void handle(HttpExchange e) throws IOException {
@@ -121,7 +126,7 @@
         }
         break;
     }
-    logger.info(response);
+    logger.fine(response);
     e.sendResponseHeaders(code, response.length());
     OutputStream os = e.getResponseBody();
     os.write(response.getBytes());
@@ -129,6 +134,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];
@@ -136,22 +149,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}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()});
     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,9 +183,16 @@
     String[] elems = path.split(App.getRs(Server.RB_SLASH));
     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);
-      List list = fs.list(type);
+      List list = null;
+      if(path.endsWith(App.getRs(RB_EP_LISTE))) {
+        String type = elems[elems.length - 2];
+        logger.fine(type);
+        list = fs.list(type);
+      } else {
+        String type = elems[elems.length - 1];
+        logger.fine(type);
+        list = fs.listObjects(type);
+      }
       return jsonWithEnclosingType(list);
     } else {
       String type = elems[elems.length - 2];
@@ -177,18 +200,6 @@
       return fs.readJson(type, elemName);
     }
   }
-  
-  private String jsonWithEnclosingType(Object o) {
-    StringBuilder sb = new StringBuilder();
-    sb.append("{\"");
-    sb.append(o.getClass().getSimpleName());
-    sb.append("\": ");
-    Gson gson = new Gson();
-    sb.append(gson.toJson(o));
-    sb.append("}");
-    return sb.toString();
-  }
-  
   
   private String bodyLesen(HttpExchange e) throws IOException {
     InputStream is = e.getRequestBody();
@@ -201,7 +212,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