From 5898509f4ca658690c4d4bc0e1784a8ecbff4178 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sat, 10 Apr 2021 16:09:30 +0000
Subject: [PATCH] Abspielliste abspielen fertig, stop, pause, weiter fertig
---
src/de/uhilger/mediaz/api/AbstractHandler.java | 156 +++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 129 insertions(+), 27 deletions(-)
diff --git a/src/de/uhilger/mediaz/api/AbstractHandler.java b/src/de/uhilger/mediaz/api/AbstractHandler.java
index 4692d5d..9389ac4 100644
--- a/src/de/uhilger/mediaz/api/AbstractHandler.java
+++ b/src/de/uhilger/mediaz/api/AbstractHandler.java
@@ -1,41 +1,143 @@
/*
- * 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.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
+ * @author Ulrich Hilger
+ * @version 1, 8.4.2021
*/
-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";
+
+ public static final int RTC_OK = 200;
+ public static final int RTC_NOT_FOUND = 404;
+
+ protected int returnCode;
+
+ public AbstractHandler() {
+ this.returnCode = RTC_OK;
}
- protected String jsonWithCustomType(Object o, String typeName) {
- 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();
+ @Override
+ public void handle(HttpExchange e) throws IOException {
+ String method = e.getRequestMethod();
+ String response = "";
+ switch(method) {
+ case HTTP_GET:
+ String json = get(e);
+ if(json != null) {
+ response = json;
+ } else {
+ response = "nicht gefunden";
+ returnCode = RTC_NOT_FOUND;
+ }
+ break;
+
+ case HTTP_PUT:
+ response = put(e);
+ break;
+
+ case HTTP_POST:
+ response = post(e);
+ break;
+
+ case HTTP_DELETE:
+ boolean geloescht = delete(e);
+ if(geloescht) {
+ response = "geloescht";
+ } else {
+ response = "nicht geloescht";
+ }
+ break;
+ }
+ e.sendResponseHeaders(getReturnCode(), response.length());
+ OutputStream os = e.getResponseBody();
+ os.write(response.getBytes());
+ os.close();
}
+ protected void setReturnCode(int code) {
+ this.returnCode = code;
+ }
+
+ protected int getReturnCode() {
+ return returnCode;
+ }
+
+ protected String bodyLesen(HttpExchange e) throws IOException {
+ InputStream is = e.getRequestBody();
+ BufferedReader r = new BufferedReader(new InputStreamReader(is));
+ StringBuilder sb = new StringBuilder();
+ String line = r.readLine();
+ while(line != null) {
+ sb.append(line);
+ line = r.readLine();
+ }
+ r.close();
+ String json = sb.toString();
+ return json;
+ }
+
+ protected String put(HttpExchange e) throws IOException {
+ setReturnCode(RTC_NOT_FOUND);
+ return "nicht unterstuetzt";
+ }
+
+ protected String post(HttpExchange e) {
+ setReturnCode(RTC_NOT_FOUND);
+ return "nicht unterstuetzt";
+ }
+
+ protected boolean delete(HttpExchange e) {
+ setReturnCode(RTC_NOT_FOUND);
+ return false;
+ }
+
+
+ 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