From 2b5c60f130bf5d6e0ad4521d327b81947cd2eab7 Mon Sep 17 00:00:00 2001 From: ulrich Date: Mon, 05 Apr 2021 13:42:39 +0000 Subject: [PATCH] Alle Faelle (Liste, Neu, Lesen, Aendern, Loeschen) fuer Ablageort fertig --- src/de/uhilger/mediaz/api/StorageHandler.java | 64 +++++++++++++++++++++++-------- src/de/uhilger/mediaz/store/FileStorage.java | 41 +++++++++++++++----- src/de/uhilger/mediaz/store/Storage.java | 2 + 3 files changed, 80 insertions(+), 27 deletions(-) diff --git a/src/de/uhilger/mediaz/api/StorageHandler.java b/src/de/uhilger/mediaz/api/StorageHandler.java index 22d9de0..024900b 100644 --- a/src/de/uhilger/mediaz/api/StorageHandler.java +++ b/src/de/uhilger/mediaz/api/StorageHandler.java @@ -22,6 +22,7 @@ import com.sun.net.httpserver.HttpHandler; import de.uhilger.mediaz.App; import de.uhilger.mediaz.Server; +import static de.uhilger.mediaz.Server.RB_SLASH; import de.uhilger.mediaz.store.FileStorage; import de.uhilger.mediaz.entity.Entity; import java.io.BufferedReader; @@ -30,6 +31,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -45,13 +47,18 @@ /* - - HTTP GET: lies einen Ablageort und schreibe JSON - HTTP PUT: schreibe einen neuen Ablageort auf die Platte - HTTP POST: schreibe Aenderungen auf die Platte - HTTP DELETE: loesche den Ablageort + Das REST-Muster sieht je Entitaet fuenf Faelle vor (Beispiel Ablageort): + + 1. HTTP GET Ablageort/[Name]: Liefere den Ablageort als JSON + 2. HTTP GET Ablageort/: Liefere einer Liste von Ablageorten als JSON + 3. HTTP PUT: schreibe einen neuen Ablageort auf die Platte + 4. HTTP POST: schreibe Aenderungen auf die Platte + 5. HTTP DELETE: loesche den Ablageort Beispiele: + + HTTP GET an /mz/api/store/Ablageort/ + liefert eine Liste der Namen vorhandener Ablageorte HTTP GET an /mz/api/store/Ablageort/Katalog liest den Ablageort namens "Katalog" @@ -68,9 +75,16 @@ */ + /** Name der HTTP Methode GET */ public static final String HTTP_GET = "GET"; + + /** Name der HTTP Methode PUT */ public static final String HTTP_PUT = "PUT"; + + /** Name der HTTP Methode POST */ public static final String HTTP_POST = "POST"; + + /** Name der HTTP Methode DELETE */ public static final String HTTP_DELETE = "DELETE"; @Override @@ -90,7 +104,7 @@ break; case HTTP_PUT: - response = "PUT noch bauen."; + response = aendern(e); break; case HTTP_POST: @@ -98,7 +112,12 @@ break; case HTTP_DELETE: - response = "DELETE noch bauen."; + boolean geloescht = loeschen(e); + if(geloescht) { + response = "geloescht"; + } else { + response = "nicht geloescht"; + } break; } logger.info(response); @@ -129,23 +148,34 @@ return type + FileHandler.STR_BLANK + filename; } - private void aendern() { - + private String aendern(HttpExchange e) throws IOException { + return neu(e); // einstweilen wird einfach ueberschrieben } - private void loeschen() { - - } - - private String lesen(HttpExchange e) { + private boolean loeschen(HttpExchange e) { String path = e.getRequestURI().toString(); String[] elems = path.split(App.getRs(Server.RB_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))); - //Entity entity = fs.read(type, elemName); - //return entity; - return fs.readJson(type, elemName); + return fs.delete(type, elemName); + } + + private String lesen(HttpExchange e) { + String path = e.getRequestURI().toString(); + 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); + Gson gson = new Gson(); + return gson.toJson(list); + } else { + String type = elems[elems.length - 2]; + String elemName = elems[elems.length - 1]; + return fs.readJson(type, elemName); + } } diff --git a/src/de/uhilger/mediaz/store/FileStorage.java b/src/de/uhilger/mediaz/store/FileStorage.java index 3819699..90a99a1 100644 --- a/src/de/uhilger/mediaz/store/FileStorage.java +++ b/src/de/uhilger/mediaz/store/FileStorage.java @@ -1,7 +1,19 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + Mediazentrale - Personal Media Center + Copyright (C) 2021 Ulrich Hilger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ package de.uhilger.mediaz.store; @@ -25,7 +37,7 @@ import java.util.logging.Level; /** - * Ablage fuer Dateien + * Ablage fuer Dateien der Mediazentrale * * [Basispfad]/[Typ]/[Name] * @@ -66,20 +78,20 @@ * Der Name der Ablage muss eindeutig sein * * - * @param o + * @param entity */ - public File writeToFile(Entity o) throws IOException { - String className = o.getClass().getSimpleName(); + public File writeToFile(Entity entity) throws IOException { + String className = entity.getClass().getSimpleName(); logger.finer(className); File dir = new File(fileBase, className); dir.mkdirs(); - File file = new File(dir, o.getName()); + File file = new File(dir, entity.getName()); if(file.exists()) { file.delete(); } FileWriter fw = new FileWriter(file); Gson gson = new Gson(); - fw.write(gson.toJson(o)); + fw.write(gson.toJson(entity)); fw.flush(); fw.close(); return file; @@ -105,7 +117,6 @@ private String typeNameFromPath(File file) { String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); - logger.info(parts[parts.length-2]); return parts[parts.length-2]; } @@ -162,6 +173,16 @@ return new File(dir, name); } + @Override + public boolean delete(String typ, String name) { + File file = getFile(typ, name); + if(file.exists()) { + return file.delete(); + } else { + return false; + } + } + } diff --git a/src/de/uhilger/mediaz/store/Storage.java b/src/de/uhilger/mediaz/store/Storage.java index d892954..1f8d95c 100644 --- a/src/de/uhilger/mediaz/store/Storage.java +++ b/src/de/uhilger/mediaz/store/Storage.java @@ -76,4 +76,6 @@ public List<String> list(String typ); public TypeToken typeFromName(String name); + + public boolean delete(String typ, String name); } -- Gitblit v1.9.3