|
|
@ -6,6 +6,7 @@ import java.io.FileInputStream; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
|
|
|
|
import java.io.OutputStreamWriter; |
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.net.InetSocketAddress; |
|
|
|
import java.net.InetSocketAddress; |
|
|
|
import java.net.URI; |
|
|
|
import java.net.URI; |
|
|
@ -38,6 +39,7 @@ public class Application { |
|
|
|
server.createContext("/plan", client -> sendPlan(client)); |
|
|
|
server.createContext("/plan", client -> sendPlan(client)); |
|
|
|
server.createContext("/css" , client -> sendFile(client)); |
|
|
|
server.createContext("/css" , client -> sendFile(client)); |
|
|
|
server.createContext("/js" , client -> sendFile(client)); |
|
|
|
server.createContext("/js" , client -> sendFile(client)); |
|
|
|
|
|
|
|
server.createContext("/stream", client -> stream(client)); |
|
|
|
server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool()); |
|
|
|
server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool()); |
|
|
|
server.start(); |
|
|
|
server.start(); |
|
|
|
try { |
|
|
|
try { |
|
|
@ -48,35 +50,6 @@ public class Application { |
|
|
|
Desktop.getDesktop().browse(URI.create("http://localhost:"+config.getInt(PORT)+"/plan")); |
|
|
|
Desktop.getDesktop().browse(URI.create("http://localhost:"+config.getInt(PORT)+"/plan")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void sendFile(HttpExchange client) throws IOException { |
|
|
|
|
|
|
|
URI uri = client.getRequestURI(); |
|
|
|
|
|
|
|
File file = new File(System.getProperty("user.dir")+"/resources"+uri); |
|
|
|
|
|
|
|
LOG.debug("requesting file: {}",file); |
|
|
|
|
|
|
|
if (file.exists()) { |
|
|
|
|
|
|
|
client.getResponseHeaders().add("Content-Type", Files.probeContentType(file.toPath())); |
|
|
|
|
|
|
|
client.sendResponseHeaders(200, file.length()); |
|
|
|
|
|
|
|
OutputStream out = client.getResponseBody(); |
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file); |
|
|
|
|
|
|
|
in.transferTo(out); |
|
|
|
|
|
|
|
in.close(); |
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sendError(client,404,t("Could not find \"{}\"",uri)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void sendError(HttpExchange client, int code, String msg) throws IOException { |
|
|
|
|
|
|
|
client.sendResponseHeaders(code, msg.length()); |
|
|
|
|
|
|
|
LOG.error(msg); |
|
|
|
|
|
|
|
OutputStream out = client.getResponseBody(); |
|
|
|
|
|
|
|
out.write(msg.getBytes(UTF8)); |
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static HashMap<String, String> inflate(byte[] data) { |
|
|
|
|
|
|
|
return inflate(new String(data,UTF8)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static HashMap<String, String> inflate(String data) { |
|
|
|
private static HashMap<String, String> inflate(String data) { |
|
|
|
LOG.debug("inflate({})",data); |
|
|
|
LOG.debug("inflate({})",data); |
|
|
|
HashMap<String, String> params = new HashMap<String, String>(); |
|
|
|
HashMap<String, String> params = new HashMap<String, String>(); |
|
|
@ -91,6 +64,10 @@ public class Application { |
|
|
|
return params; |
|
|
|
return params; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static HashMap<String, String> inflate(byte[] data) { |
|
|
|
|
|
|
|
return inflate(new String(data,UTF8)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void send(HttpExchange client, Object response) throws IOException { |
|
|
|
private static void send(HttpExchange client, Object response) throws IOException { |
|
|
|
byte[] html; |
|
|
|
byte[] html; |
|
|
|
if (response instanceof Page) { |
|
|
|
if (response instanceof Page) { |
|
|
@ -107,6 +84,31 @@ public class Application { |
|
|
|
os.close(); |
|
|
|
os.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void sendError(HttpExchange client, int code, String msg) throws IOException { |
|
|
|
|
|
|
|
client.sendResponseHeaders(code, msg.length()); |
|
|
|
|
|
|
|
LOG.error(msg); |
|
|
|
|
|
|
|
OutputStream out = client.getResponseBody(); |
|
|
|
|
|
|
|
out.write(msg.getBytes(UTF8)); |
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void sendFile(HttpExchange client) throws IOException { |
|
|
|
|
|
|
|
URI uri = client.getRequestURI(); |
|
|
|
|
|
|
|
File file = new File(System.getProperty("user.dir")+"/resources"+uri); |
|
|
|
|
|
|
|
LOG.debug("requesting file: {}",file); |
|
|
|
|
|
|
|
if (file.exists()) { |
|
|
|
|
|
|
|
client.getResponseHeaders().add("Content-Type", Files.probeContentType(file.toPath())); |
|
|
|
|
|
|
|
client.sendResponseHeaders(200, file.length()); |
|
|
|
|
|
|
|
OutputStream out = client.getResponseBody(); |
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file); |
|
|
|
|
|
|
|
in.transferTo(out); |
|
|
|
|
|
|
|
in.close(); |
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sendError(client,404,t("Could not find \"{}\"",uri)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void sendPlan(HttpExchange client) throws IOException { |
|
|
|
private static void sendPlan(HttpExchange client) throws IOException { |
|
|
|
try { |
|
|
|
try { |
|
|
|
HashMap<String, String> params = inflate(client.getRequestBody().readAllBytes()); |
|
|
|
HashMap<String, String> params = inflate(client.getRequestBody().readAllBytes()); |
|
|
@ -117,6 +119,13 @@ public class Application { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void stream(HttpExchange client) throws IOException { |
|
|
|
|
|
|
|
client.getResponseHeaders().set("content-type", "text/event-stream"); |
|
|
|
|
|
|
|
client.sendResponseHeaders(200, 0); |
|
|
|
|
|
|
|
OutputStreamWriter sseWriter = new OutputStreamWriter(client.getResponseBody()); |
|
|
|
|
|
|
|
plan.addClient(sseWriter); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static String t(String text, Object...fills) { |
|
|
|
private static String t(String text, Object...fills) { |
|
|
|
return Translation.get(Application.class, text, fills); |
|
|
|
return Translation.get(Application.class, text, fills); |
|
|
|
} |
|
|
|
} |
|
|
|