From 5bf530d39a7e06bdd982a5c2495ff9a04fd83da4 Mon Sep 17 00:00:00 2001
From: undisclosed
Date: Wed, 11 Jan 2023 17:38:22 +0000
Subject: [PATCH] Erste Versuche mit StreamHandler

---
 src/de/uhilger/tango/api/StorageHandler.java |   46 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/src/de/uhilger/tango/api/StorageHandler.java b/src/de/uhilger/tango/api/StorageHandler.java
index 52a0f08..882ab9b 100644
--- a/src/de/uhilger/tango/api/StorageHandler.java
+++ b/src/de/uhilger/tango/api/StorageHandler.java
@@ -18,10 +18,9 @@
 package de.uhilger.tango.api;
 
 import com.google.gson.Gson;
+import com.sun.net.httpserver.HttpContext;
 import com.sun.net.httpserver.HttpExchange;
 import de.uhilger.tango.App;
-import static de.uhilger.tango.App.RB_EP_LISTE;
-import static de.uhilger.tango.App.RB_EP_LISTE_ALLES;
 import de.uhilger.tango.Server;
 import de.uhilger.tango.entity.Ablageort;
 import de.uhilger.tango.store.FileStorage;
@@ -30,6 +29,7 @@
 import static de.uhilger.tango.store.FileStorage.ST_ABLAGEORT;
 import static de.uhilger.tango.store.FileStorage.ST_GERAET;
 import de.uhilger.tango.store.Storage;
+import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.util.Iterator;
@@ -60,6 +60,15 @@
 public class StorageHandler extends AbstractHandler {
   
   private static final Logger logger = Logger.getLogger(StorageHandler.class.getName());
+  
+  public static final String RB_EP_LISTE = "epliste"; 
+  public static final String RB_EP_LISTE_ALLES = "eplisteAlles"; 
+  
+  private String conf;
+  
+  public StorageHandler(String conf) {
+    this.conf = conf;
+  }
 
   @Override
   protected String put(HttpExchange e) throws IOException {
@@ -67,8 +76,8 @@
     String[] elems = path.split(Server.SLASH);
     String type = elems[elems.length - 2];
     String elemName = elems[elems.length - 1]; // alter Name, wenn Aenderung
-    if(!elemName.equalsIgnoreCase(App.getRs(RB_EP_LISTE))) {
-      FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+    if(!elemName.equalsIgnoreCase(getResString(RB_EP_LISTE))) {
+      FileStorage fs = new FileStorage(conf);
       Gson gson = new Gson();
       logger.log(Level.FINE, "type: {0}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()});
       Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType());
@@ -81,7 +90,8 @@
             Entity aoe = fs.read(type, elemName);
             if(aoe instanceof Ablageort) {
               Ablageort ablageort = (Ablageort) aoe;
-              App.getServer().ablageortEntfernen(ablageort.getUrl());      
+              //App.getServer().ablageortEntfernen(ablageort.getUrl());      
+              e.getHttpContext().getServer().removeContext(ablageort.getUrl());
             }
           }
           fs.write(entity, true);
@@ -89,7 +99,8 @@
           if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) {
             if(entity instanceof Ablageort) {
               Ablageort ablageort = (Ablageort) entity;
-              App.getServer().ablageortHinzufuegen(ablageort);
+              //App.getServer().ablageortHinzufuegen(ablageort);
+              ablageortHinzufuegen(e, ablageort);
             }
           }
         } else { // Neu
@@ -98,7 +109,8 @@
           if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) {
             if(entity instanceof Ablageort) {
               Ablageort ablageort = (Ablageort) entity;
-              App.getServer().ablageortHinzufuegen(ablageort);
+              //App.getServer().ablageortHinzufuegen(ablageort);
+              ablageortHinzufuegen(e, ablageort);
             }
           }
         }
@@ -107,8 +119,15 @@
         return "Ungueltiges Objekt im Body.";
       }
     } else {
-      return "Ungueltiger Elementname: " + App.getRs(RB_EP_LISTE);
+      return "Ungueltiger Elementname: " + getResString(RB_EP_LISTE);
     }
+  }
+  
+  private void ablageortHinzufuegen(HttpExchange e, Ablageort ort) {
+    HttpContext c = e.getHttpContext();
+    String ctx = c.getPath();
+    c.getServer().createContext(ctx + ort.getUrl(),  
+          new ListFileHandler(new File(ort.getOrt()).getAbsolutePath(), conf));  
   }
   
   private boolean loeschen(HttpExchange e) {
@@ -116,13 +135,14 @@
     String[] elems = path.split(Server.SLASH);
     String type = elems[elems.length - 2];
     String elemName = elems[elems.length - 1];
-    FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+    FileStorage fs = new FileStorage(conf);
     if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) {
       // im laufenden Server den Context entfernen
       Entity entity = fs.read(type, elemName);
       if(entity instanceof Ablageort) {
         Ablageort ablageort = (Ablageort) entity;
-        App.getServer().ablageortEntfernen(ablageort.getUrl());      
+        //App.getServer().ablageortEntfernen(ablageort.getUrl());      
+        e.getHttpContext().getServer().removeContext(ablageort.getUrl());
       }
     }
     return fs.delete(type, elemName);
@@ -131,10 +151,10 @@
   private String lesen(HttpExchange e) throws IOException, InterruptedException {
     String path = e.getRequestURI().toString();
     String[] elems = path.split(Server.SLASH);
-    FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+    FileStorage fs = new FileStorage(conf);
     if(path.endsWith(Server.SLASH)) {
       List list = null;
-      if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE_ALLES))) {
+      if(elems[elems.length - 1].equalsIgnoreCase(getResString(RB_EP_LISTE_ALLES))) {
         String type = elems[elems.length - 2];
         logger.fine(type);
         if(type.equalsIgnoreCase(ST_GERAET)) {
@@ -143,7 +163,7 @@
           Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType());
           return gson.toJson(list);
         }
-      } else if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE))) {
+      } else if(elems[elems.length - 1].equalsIgnoreCase(getResString(RB_EP_LISTE))) {
         String type = elems[elems.length - 2];
         logger.fine(type);
         list = fs.list(type);

--
Gitblit v1.9.3