From b1bf963e57050d6c423a80db76f9d36197a0b475 Mon Sep 17 00:00:00 2001 From: ulrich Date: Mon, 05 Apr 2021 11:58:38 +0000 Subject: [PATCH] Neuer Ablageort mit generischem Typ --- src/de/uhilger/mediaz/api/StorageHandler.java | 47 ++++++++++++++++++++++++++++++++++------------- 1 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/de/uhilger/mediaz/api/StorageHandler.java b/src/de/uhilger/mediaz/api/StorageHandler.java index 12c4417..f2936eb 100644 --- a/src/de/uhilger/mediaz/api/StorageHandler.java +++ b/src/de/uhilger/mediaz/api/StorageHandler.java @@ -12,6 +12,7 @@ import de.uhilger.mediaz.Server; import de.uhilger.mediaz.store.FileStorage; import de.uhilger.mediaz.entity.Ablageort; +import de.uhilger.mediaz.entity.Entity; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -66,6 +67,7 @@ String[] elems = path.split(App.getRs(Server.RB_SLASH)); String type = ""; String elemName = ""; + String body = ""; FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); switch(method) { case HTTP_GET: @@ -81,18 +83,7 @@ break; case HTTP_POST: - type = elems[elems.length - 1]; - elemName = bodyLesen(e); - if(type.equalsIgnoreCase("Ablageort")) { - Gson gson = new Gson(); - Ablageort ort = gson.fromJson(elemName, Ablageort.class); - elemName = ort.getName(); - Object o = fs.write(ort); - if(o instanceof File) { - File file = (File) o; - logger.log(Level.INFO, "Datei {0} geschrieben.", file.getAbsolutePath()); - } - } + neu(e); break; case HTTP_DELETE: @@ -109,6 +100,36 @@ OutputStream os = e.getResponseBody(); os.write(response.getBytes()); os.close(); + } + + private void neu(HttpExchange e) throws IOException { + String path = e.getRequestURI().toString(); + String[] elems = path.split(App.getRs(Server.RB_SLASH)); + String type = elems[elems.length - 1]; + String body = bodyLesen(e); + FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); + Gson gson = new Gson(); + logger.log(Level.INFO, "type: {0}", type); + Object o = gson.fromJson(body, fs.typeFromName(type).getType()); + if(o instanceof Entity) { + Object antwortObjekt = fs.write((Entity) o); + if(antwortObjekt instanceof File) { + File file = (File) antwortObjekt; + logger.log(Level.INFO, "Datei {0} geschrieben.", file.getAbsolutePath()); + } + } + } + + private void aendern() { + + } + + private void loeschen() { + + } + + private Entity lesen() { + return null; } @@ -128,7 +149,7 @@ //String json = data.substring(data.indexOf("{")); // {"name":"test1","ort":"test2","url":"test3"} String json = sb.toString(); - logger.info("json: " + json); + logger.log(Level.INFO, "json: {0}", json); return json; } } -- Gitblit v1.9.3