From f70acbb491c6421623cca57292a75f1820efad4d Mon Sep 17 00:00:00 2001 From: ulrich Date: Thu, 06 May 2021 16:41:59 +0000 Subject: [PATCH] Nicht erforderliche globale Variablen entfernt --- src/de/uhilger/tango/api/StorageHandler.java | 43 +++++++++++++++++++++++++++++++------------ 1 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/de/uhilger/tango/api/StorageHandler.java b/src/de/uhilger/tango/api/StorageHandler.java index 4b87724..1e23628 100644 --- a/src/de/uhilger/tango/api/StorageHandler.java +++ b/src/de/uhilger/tango/api/StorageHandler.java @@ -18,6 +18,7 @@ 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; @@ -30,6 +31,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 +62,12 @@ public class StorageHandler extends AbstractHandler { private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); + + private String conf; + + public StorageHandler(String conf) { + this.conf = conf; + } @Override protected String put(HttpExchange e) throws IOException { @@ -67,8 +75,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 +89,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 +98,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 +108,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 +118,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 +134,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 +150,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,12 +162,12 @@ 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); if(type.equalsIgnoreCase(ST_ABLAGEORT)) { - list.add("Livestreams"); + //list.add("Livestreams"); } } else { String type = elems[elems.length - 1]; -- Gitblit v1.9.3