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 +++++++++++++++++------ src/de/uhilger/mediaz/store/FileStorage.java | 25 ++++++++---- src/de/uhilger/mediaz/store/Storage.java | 2 + 3 files changed, 53 insertions(+), 21 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; } } diff --git a/src/de/uhilger/mediaz/store/FileStorage.java b/src/de/uhilger/mediaz/store/FileStorage.java index 1d6373a..0282ed7 100644 --- a/src/de/uhilger/mediaz/store/FileStorage.java +++ b/src/de/uhilger/mediaz/store/FileStorage.java @@ -6,6 +6,7 @@ package de.uhilger.mediaz.store; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import de.uhilger.mediaz.App; import de.uhilger.mediaz.Server; import de.uhilger.mediaz.entity.Ablageort; @@ -18,7 +19,9 @@ import java.util.logging.Logger; import de.uhilger.mediaz.entity.Entity; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Level; /** @@ -41,8 +44,15 @@ private String fileBase; + private Map<String, TypeToken> types; + public FileStorage(String base) { this.fileBase = base; + + //TypeToken<List<String>> list = new TypeToken<List<String>>() {}; + TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; + types = new HashMap(); + types.put(Ablageort.class.getSimpleName(), ttAblageort); } /** @@ -76,7 +86,7 @@ } public Entity readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { - String type = typeFromName(file); + String type = typeNameFromPath(file); StringBuilder sb = new StringBuilder(); FileReader in = new FileReader(file); BufferedReader r = new BufferedReader(in); @@ -99,14 +109,8 @@ } } - private String typeFromName(File file) { - //String path = file.getPath(); - //logger.info(path); + private String typeNameFromPath(File file) { String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); - //for(String part : parts) { - //logger.info(part); - //} - //logger.info("" + parts.length); logger.info(parts[parts.length-2]); return parts[parts.length-2]; } @@ -146,6 +150,11 @@ return list; } + @Override + public TypeToken typeFromName(String name) { + return types.get(name); + } + } diff --git a/src/de/uhilger/mediaz/store/Storage.java b/src/de/uhilger/mediaz/store/Storage.java index 66f7255..2e41528 100644 --- a/src/de/uhilger/mediaz/store/Storage.java +++ b/src/de/uhilger/mediaz/store/Storage.java @@ -17,6 +17,7 @@ */ package de.uhilger.mediaz.store; +import com.google.gson.reflect.TypeToken; import de.uhilger.mediaz.entity.Entity; import java.util.List; @@ -72,4 +73,5 @@ */ public List<String> list(String typ); + public TypeToken typeFromName(String name); } -- Gitblit v1.9.3