From ac11942f36b8c38244930260a0ead02c9325a75e Mon Sep 17 00:00:00 2001 From: ulrich Date: Wed, 24 Mar 2021 23:17:40 +0000 Subject: [PATCH] FileHandler hinzugefuegt --- src/de/uhilger/avdirektor/handler/FileHandler.java | 57 ++++++++++++++++++++++++++++ src/logging.properties | 4 +- src/de/uhilger/avdirektor/Server.java | 2 + src/de/uhilger/avdirektor/handler/StopServerHandler.java | 1 src/de/uhilger/avdirektor/App.java | 10 +++- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/de/uhilger/avdirektor/App.java b/src/de/uhilger/avdirektor/App.java index d4b7035..c9e1747 100644 --- a/src/de/uhilger/avdirektor/App.java +++ b/src/de/uhilger/avdirektor/App.java @@ -1,5 +1,6 @@ package de.uhilger.avdirektor; +import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.logging.Level; @@ -23,6 +24,10 @@ private static final Logger logger = Logger.getLogger(App.class.getName()); + public static final String IP_PORT = "port"; + public static final String IP_WWW_DATA = "www-data"; + public static final String IP_NFS_PREFIX = "nfs-prefix"; + private static HashMap initParams; private static Process playerproc; @@ -35,9 +40,8 @@ String[] argParts = arg.split("="); initParams.put(argParts[0], argParts[1]); } - - - Server server = new Server(Integer.parseInt(getInitParameter("port"))); + + Server server = new Server(Integer.parseInt(getInitParameter(IP_PORT))); try { server.start(); } catch (IOException ex) { diff --git a/src/de/uhilger/avdirektor/Server.java b/src/de/uhilger/avdirektor/Server.java index a5916d6..468e47f 100644 --- a/src/de/uhilger/avdirektor/Server.java +++ b/src/de/uhilger/avdirektor/Server.java @@ -2,6 +2,7 @@ import com.sun.net.httpserver.HttpServer; import de.uhilger.avdirektor.handler.CmdHandler; +import de.uhilger.avdirektor.handler.FileHandler; import de.uhilger.avdirektor.handler.OMXPlayer; import de.uhilger.avdirektor.handler.PingHandler; import de.uhilger.avdirektor.handler.PlayHandler; @@ -41,6 +42,7 @@ server.createContext("/avd/pause", new CmdHandler(OMXPlayer.CMD_PAUSE_RESUME)); server.createContext("/avd/ping", new PingHandler(OMXPlayer.F_PING)); server.createContext("/avd/server/stop", new StopServerHandler()); + server.createContext("/avd/ui", new FileHandler(App.getInitParameter(App.IP_WWW_DATA))); //server.setExecutor(null); // creates a default executor server.setExecutor(Executors.newFixedThreadPool(20)); server.start(); diff --git a/src/de/uhilger/avdirektor/handler/FileHandler.java b/src/de/uhilger/avdirektor/handler/FileHandler.java new file mode 100644 index 0000000..1583694 --- /dev/null +++ b/src/de/uhilger/avdirektor/handler/FileHandler.java @@ -0,0 +1,57 @@ +package de.uhilger.avdirektor.handler; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import de.uhilger.avdirektor.App; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.logging.Logger; + +/** + * + * @author ulrich + */ +public class FileHandler implements HttpHandler { + + private static final Logger logger = Logger.getLogger(FileHandler.class.getName()); + + private String basePath; + + public FileHandler(String basePath) { + this.basePath = basePath; + } + + @Override + public void handle(HttpExchange t) throws IOException { + String ctxPath = t.getHttpContext().getPath(); + String uriPath = t.getRequestURI().getPath(); + String fName = uriPath.substring(ctxPath.length()); + if(fName.endsWith("/")) { + fName += "index.html"; + } + OutputStream os = t.getResponseBody(); + File outFile = new File(basePath, fName); + if(outFile.exists()) { + t.sendResponseHeaders(200, outFile.length()); + InputStream in = new FileInputStream(outFile); + int b = in.read(); + while(b > -1) { + os.write(b); + b = in.read(); + } + in.close(); + } else { + String response = fName + " not found."; + byte[] bytes = response.getBytes(StandardCharsets.UTF_8); + t.sendResponseHeaders(404, bytes.length); + os.write(bytes); + } + os.flush(); + os.close(); + } + +} diff --git a/src/de/uhilger/avdirektor/handler/StopServerHandler.java b/src/de/uhilger/avdirektor/handler/StopServerHandler.java index b28a8bf..8812fe8 100644 --- a/src/de/uhilger/avdirektor/handler/StopServerHandler.java +++ b/src/de/uhilger/avdirektor/handler/StopServerHandler.java @@ -21,6 +21,7 @@ exchange.sendResponseHeaders(200, response.length()); OutputStream os = exchange.getResponseBody(); os.write(response.getBytes()); + os.flush(); os.close(); Logger.getLogger(StopServerHandler.class.getName()).info("stopping app."); App.stop(); diff --git a/src/logging.properties b/src/logging.properties index 6b0ab55..aa7cc61 100644 --- a/src/logging.properties +++ b/src/logging.properties @@ -27,7 +27,7 @@ # Note that the ConsoleHandler also has a separate level # setting to limit messages printed to the console. # .level= FINE -.level = NONE +.level = OFF ############################################################ # Handler specific properties. @@ -69,4 +69,4 @@ # de.uhilger.wbx.handlers = java.util.logging.ConsoleHandler # de.uhilger.wbx.level = FINEST de.uhilger.avdirektor.handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler -de.uhilger.avdirektor.level = INFO \ No newline at end of file +de.uhilger.avdirektor.level = FINEST \ No newline at end of file -- Gitblit v1.9.3