src/de/uhilger/avdirektor/handler/AbstractHandler.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/avdirektor/handler/CmdHandler.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/avdirektor/handler/OMXPlayer.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/avdirektor/handler/PingHandler.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/avdirektor/handler/PlayHandler.java | ●●●●● patch | view | raw | blame | history |
src/de/uhilger/avdirektor/handler/AbstractHandler.java
New file @@ -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(); } } 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; } } 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 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; } } 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;