src/de/uhilger/avdirektor/App.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/avdirektor/Server.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/avdirektor/handler/FileHandler.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/avdirektor/handler/StopServerHandler.java | ●●●●● patch | view | raw | blame | history | |
src/logging.properties | ●●●●● patch | view | raw | blame | history |
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) { 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(); src/de/uhilger/avdirektor/handler/FileHandler.java
New file @@ -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(); } } 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(); 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 de.uhilger.avdirektor.level = FINEST