From b5cc80f43d78a4fef88a06449f218b30517fa117 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Tue, 23 Mar 2021 14:16:23 +0000
Subject: [PATCH] AbstractHandler hinzugefuegt

---
 src/de/uhilger/avdirektor/handler/CmdHandler.java      |   39 ---------
 src/de/uhilger/avdirektor/handler/PingHandler.java     |   25 ++---
 src/de/uhilger/avdirektor/handler/PlayHandler.java     |    4 -
 src/de/uhilger/avdirektor/handler/OMXPlayer.java       |   47 -----------
 src/de/uhilger/avdirektor/handler/AbstractHandler.java |  112 ++++++++++++++++++++++++++++
 5 files changed, 123 insertions(+), 104 deletions(-)

diff --git a/src/de/uhilger/avdirektor/handler/AbstractHandler.java b/src/de/uhilger/avdirektor/handler/AbstractHandler.java
new file mode 100644
index 0000000..8dc3820
--- /dev/null
+++ b/src/de/uhilger/avdirektor/handler/AbstractHandler.java
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+package de.uhilger.avdirektor.handler;
+
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author ulrich
+ */
+public abstract class AbstractHandler extends OMXPlayer implements HttpHandler {
+
+  private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName());
+  
+  protected String cmd;
+  protected Map map;
+  
+  @Override
+  public void handle(HttpExchange t) throws IOException {
+    logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
+    StringBuilder params = buildParams(t);
+    String antwort = process(t, params.toString());
+    sendResponse(t, cmd, antwort);
+  }
+  
+  protected abstract String process(HttpExchange t, String params);
+  
+  protected StringBuilder buildParams(HttpExchange t) {
+    map = getQueryMap(t);
+    StringBuilder params = new StringBuilder();
+    return params;
+  }
+  
+  protected void sendResponse(HttpExchange t, String cmd, String antwort) throws IOException {
+    String response = getResponseString(map, cmd, antwort);
+    t.sendResponseHeaders(200, response.length());
+    OutputStream os = t.getResponseBody();
+    os.write(response.getBytes());
+    os.close();    
+  }
+  
+  public void setCmd(String cmd) {
+    this.cmd = cmd;
+  }
+  
+  public String getCmd(String cmd) {
+    return this.cmd;
+  }
+
+  /* --- --- */
+  
+  protected String getParam(Map map, String key) {
+    Object o = map.get(key);
+    if(o != null) {
+      return o.toString();
+    } else {
+      return null;
+    }
+  }
+  
+  /*
+    Den Query-Teil einer URL in die Parameter zerlegen
+  
+    Die Zerlegung erfolgt mit String.split nach 
+    & und dann nach =
+  */
+  protected Map getQueryMap(HttpExchange t) {
+    HashMap map = new HashMap();
+    String query = t.getRequestURI().getQuery();
+    if(query != null && query.length() > 0) {
+      String qParts[] = query.split("&");
+      for(String qPart : qParts) {
+        logger.finer("qPart: " + qPart);
+        String pParts[] = qPart.split("=");
+        map.put(pParts[0], pParts[1]);
+        logger.finer("pParts[0]: " + pParts[0] + ", pParts[1]: " + pParts[1]);
+      }
+    }
+    return map;
+  }
+  
+  protected String getResponseString(Map map, String cmd, String antwort) {
+    Set keys = map.keySet();
+    StringBuilder buf = new StringBuilder();
+    buf.append(cmd);
+    buf.append(System.lineSeparator());
+    keys.forEach((Object key) -> {
+      buf.append("key: ");
+      buf.append(key);
+      buf.append(System.lineSeparator());
+      buf.append("value: "); 
+      buf.append(map.get(key));
+      buf.append(System.lineSeparator());
+      //logger.log(Level.FINE, "key {0} value {1}", new Object[]{key, map.get(key)});
+    });
+    buf.append(antwort);
+    return buf.toString();
+  }
+    
+  
+}
diff --git a/src/de/uhilger/avdirektor/handler/CmdHandler.java b/src/de/uhilger/avdirektor/handler/CmdHandler.java
index 7e3193e..38f2b6f 100644
--- a/src/de/uhilger/avdirektor/handler/CmdHandler.java
+++ b/src/de/uhilger/avdirektor/handler/CmdHandler.java
@@ -1,10 +1,6 @@
 package de.uhilger.avdirektor.handler;
 
 import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -12,23 +8,12 @@
  *
  * @author ulrich
  */
-public class CmdHandler extends OMXPlayer implements HttpHandler {
+public class CmdHandler extends AbstractHandler {
 
   private static final Logger logger = Logger.getLogger(CmdHandler.class.getName());
   
-  protected String cmd;
-  protected Map map;
-  
   public CmdHandler(String cmd) {
     this.cmd = cmd;
-  }
-  
-  @Override
-  public void handle(HttpExchange t) throws IOException {
-    logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
-    StringBuilder params = buildParams(t);
-    String antwort = process(t, params.toString());
-    sendResponse(t, cmd, antwort);
   }
   
   protected String process(HttpExchange t, String params) {
@@ -37,26 +22,4 @@
     return antwort;
   }
   
-  protected StringBuilder buildParams(HttpExchange t) {
-    map = getQueryMap(t);
-    StringBuilder params = new StringBuilder();
-    return params;
-  }
-  
-  protected void sendResponse(HttpExchange t, String cmd, String antwort) throws IOException {
-    String response = getResponseString(map, cmd, antwort);
-    t.sendResponseHeaders(200, response.length());
-    OutputStream os = t.getResponseBody();
-    os.write(response.getBytes());
-    os.close();    
-  }
-  
-  public void setCmd(String cmd) {
-    this.cmd = cmd;
-  }
-  
-  public String getCmd(String cmd) {
-    return this.cmd;
-  }
-
 }
diff --git a/src/de/uhilger/avdirektor/handler/OMXPlayer.java b/src/de/uhilger/avdirektor/handler/OMXPlayer.java
index 33cb7bc..8a27f69 100644
--- a/src/de/uhilger/avdirektor/handler/OMXPlayer.java
+++ b/src/de/uhilger/avdirektor/handler/OMXPlayer.java
@@ -192,53 +192,6 @@
     return antwort;
   }
   
-  /* --- --- */
-  
-  protected String getParam(Map map, String key) {
-    Object o = map.get(key);
-    if(o != null) {
-      return o.toString();
-    } else {
-      return null;
-    }
-  }
-  
-  /*
-    Den Query-Teil einer URL in die Parameter zerlegen
-  */
-  protected Map getQueryMap(HttpExchange t) {
-    HashMap map = new HashMap();
-    String query = t.getRequestURI().getQuery();
-    if(query != null && query.length() > 0) {
-      String qParts[] = query.split("&");
-      for(String qPart : qParts) {
-        logger.finer("qPart: " + qPart);
-        String pParts[] = qPart.split("=");
-        map.put(pParts[0], pParts[1]);
-        logger.finer("pParts[0]: " + pParts[0] + ", pParts[1]: " + pParts[1]);
-      }
-    }
-    return map;
-  }
-  
-  protected String getResponseString(Map map, String cmd, String antwort) {
-    Set keys = map.keySet();
-    StringBuilder buf = new StringBuilder();
-    buf.append(cmd);
-    buf.append(System.lineSeparator());
-    keys.forEach((Object key) -> {
-      buf.append("key: ");
-      buf.append(key);
-      buf.append(System.lineSeparator());
-      buf.append("value: "); 
-      buf.append(map.get(key));
-      buf.append(System.lineSeparator());
-      //logger.log(Level.FINE, "key {0} value {1}", new Object[]{key, map.get(key)});
-    });
-    buf.append(antwort);
-    return buf.toString();
-  }
-    
   /* ------ Implementierung ProzessLauscher ----------------- */
   
   @Override
diff --git a/src/de/uhilger/avdirektor/handler/PingHandler.java b/src/de/uhilger/avdirektor/handler/PingHandler.java
index 219391c..94d51fa 100644
--- a/src/de/uhilger/avdirektor/handler/PingHandler.java
+++ b/src/de/uhilger/avdirektor/handler/PingHandler.java
@@ -6,6 +6,8 @@
 package de.uhilger.avdirektor.handler;
 
 import com.sun.net.httpserver.HttpExchange;
+import java.io.IOException;
+import java.io.OutputStream;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -21,23 +23,16 @@
     super(cmd);
   }
 
-  /*
-  @Override
-  public void handle(HttpExchange t) throws IOException {
-    logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
-    
-    String response = "ping";
-    t.sendResponseHeaders(200, response.length());
+  protected String process(HttpExchange t, String params) {
+    logger.log(Level.FINE, cmd);
+    return cmd;
+  }  
+  
+  protected void sendResponse(HttpExchange t, String cmd, String antwort) throws IOException {
+    t.sendResponseHeaders(200, antwort.length());
     OutputStream os = t.getResponseBody();
-    os.write(response.getBytes());
+    os.write(antwort.getBytes());
     os.close();    
   }
-  */
-  
-  protected String process(HttpExchange t, String params) {
-    String antwort = cmd;
-    logger.log(Level.FINE, antwort);
-    return antwort;
-  }  
   
 }
diff --git a/src/de/uhilger/avdirektor/handler/PlayHandler.java b/src/de/uhilger/avdirektor/handler/PlayHandler.java
index 3a8db5d..5b597e0 100644
--- a/src/de/uhilger/avdirektor/handler/PlayHandler.java
+++ b/src/de/uhilger/avdirektor/handler/PlayHandler.java
@@ -1,10 +1,6 @@
 package de.uhilger.avdirektor.handler;
 
 import com.sun.net.httpserver.HttpExchange;
-import com.sun.net.httpserver.HttpHandler;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 

--
Gitblit v1.9.3