From fcc734a744409e307d334a1f8f4bfcfc0a319cb1 Mon Sep 17 00:00:00 2001 From: ulrich Date: Thu, 08 Apr 2021 19:41:43 +0000 Subject: [PATCH] Titel auf Abspielliste, Abspielliste anzeigen, Eintrag auswaehlen --- src/de/uhilger/mediaz/api/AbstractHandler.java | 101 +++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 79 insertions(+), 22 deletions(-) diff --git a/src/de/uhilger/mediaz/api/AbstractHandler.java b/src/de/uhilger/mediaz/api/AbstractHandler.java index 4692d5d..45befae 100644 --- a/src/de/uhilger/mediaz/api/AbstractHandler.java +++ b/src/de/uhilger/mediaz/api/AbstractHandler.java @@ -5,37 +5,94 @@ */ package de.uhilger.mediaz.api; -import com.google.gson.Gson; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.util.logging.Logger; + /** * * @author ulrich */ -public class AbstractHandler { +public abstract class AbstractHandler extends JsonHelper implements HttpHandler { - protected String jsonWithEnclosingType(Object o) { - /* - StringBuilder sb = new StringBuilder(); - sb.append("{\""); - sb.append(o.getClass().getSimpleName()); - sb.append("\": "); - Gson gson = new Gson(); - sb.append(gson.toJson(o)); - sb.append("}"); - return sb.toString(); - */ - return jsonWithCustomType(o, o.getClass().getSimpleName()); + private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName()); + + /** 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 + public void handle(HttpExchange e) throws IOException { + String method = e.getRequestMethod(); + String response = ""; + int code = 200; + switch(method) { + case HTTP_GET: + String json = get(e); + if(json != null) { + response = json; + } else { + response = "nicht gefunden"; + code = 404; + } + break; + + case HTTP_PUT: + response = put(e); + break; + + case HTTP_POST: + response = post(e); + code = 404; + break; + + case HTTP_DELETE: + boolean geloescht = delete(e); + if(geloescht) { + response = "geloescht"; + } else { + response = "nicht geloescht"; + } + break; + } + e.sendResponseHeaders(code, response.length()); + OutputStream os = e.getResponseBody(); + os.write(response.getBytes()); + os.close(); } - protected String jsonWithCustomType(Object o, String typeName) { + protected String bodyLesen(HttpExchange e) throws IOException { + InputStream is = e.getRequestBody(); + BufferedReader r = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); - sb.append("{\""); - sb.append(typeName); - sb.append("\": "); - Gson gson = new Gson(); - sb.append(gson.toJson(o)); - sb.append("}"); - return sb.toString(); + String line = r.readLine(); + while(line != null) { + sb.append(line); + line = r.readLine(); + } + r.close(); + String json = sb.toString(); + return json; } + + + protected abstract String get(HttpExchange e); + protected abstract String put(HttpExchange e) throws IOException; + protected abstract String post(HttpExchange e); + protected abstract boolean delete(HttpExchange e); } -- Gitblit v1.9.3