diff --git a/.project b/.project index c01cdad..325442c 100644 --- a/.project +++ b/.project @@ -10,8 +10,14 @@ + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature diff --git a/pom.xml b/pom.xml index b788546..16cc6b5 100644 --- a/pom.xml +++ b/pom.xml @@ -1,67 +1,86 @@ - - 4.0.0 - com.github.tbmcplugins - AliPresents - 0.0.1-SNAPSHOT - AliPresents - A bucket of aaall the stuff Ali makes. It's a bit smelly. - - src - - - src - - **/*.java - - - - . - - *.yml - - - - - - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - - - - - - UTF-8 - + + 4.0.0 + com.github.tbmcplugins + ButtonWebsiteModule + 0.0.1-SNAPSHOT + ButtonWebsiteModule + Button Website Module + + src + + + src + + **/*.java + + + + . + + *.yml + + + + + + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + + UTF-8 + - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - jcenter - http://jcenter.bintray.com - - - jitpack.io - https://jitpack.io - - + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + jcenter + http://jcenter.bintray.com + + + jitpack.io + https://jitpack.io + + - - - org.spigotmc - spigot-api - 1.9.2-R0.1-SNAPSHOT - - - - org.apache.commons - commons-lang3 - 3.4 - - + + + org.spigotmc + spigot-api + 1.9.2-R0.1-SNAPSHOT + + + + org.apache.commons + commons-lang3 + 3.4 + + + + org.reflections + reflections + 0.9.10 + + + + org.javassist + javassist + 3.20.0-GA + + + + org.apache.commons + commons-io + 1.3.2 + + diff --git a/src/buttondevteam/io/IOHelper.java b/src/buttondevteam/io/IOHelper.java new file mode 100644 index 0000000..3367986 --- /dev/null +++ b/src/buttondevteam/io/IOHelper.java @@ -0,0 +1,56 @@ +package buttondevteam.io; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import org.apache.commons.io.IOUtils; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.sun.net.httpserver.HttpExchange; + +public class IOHelper { + public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException { + try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) { + try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) { + exchange.sendResponseHeaders(code, bis.available()); + byte[] buffer = new byte[512]; + int count; + while ((count = bis.read(buffer)) != -1) { + out.write(buffer, 0, count); + } + } + } + exchange.getResponseBody().close(); + } + + public static String GetPOST(HttpExchange exchange) { + try { + if (exchange.getRequestBody().available() == 0) + return ""; + String content = IOUtils.toString(exchange.getRequestBody(), "UTF-8"); + return content; + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } + + public static JsonObject GetPOSTJSON(HttpExchange exchange) { + try { + String content = GetPOST(exchange); + if (content.length() == 0) + return null; + JsonElement e = new JsonParser().parse(content); + if (e == null) + return null; + JsonObject obj = e.getAsJsonObject(); + return obj; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/src/buttondevteam/website/ButtonWebsiteModule.java b/src/buttondevteam/website/ButtonWebsiteModule.java index e811d9f..0593df0 100644 --- a/src/buttondevteam/website/ButtonWebsiteModule.java +++ b/src/buttondevteam/website/ButtonWebsiteModule.java @@ -1,5 +1,33 @@ -package buttondevteam; +package buttondevteam.website; -public class ButtonWebsiteModule{ +import java.net.InetAddress; +import java.net.InetSocketAddress; +import org.bukkit.plugin.java.JavaPlugin; +import com.sun.net.httpserver.HttpServer; +import buttondevteam.website.page.*; + +public class ButtonWebsiteModule extends JavaPlugin { + @Override + public void onEnable() { + try { + this.getLogger().info("Starting webserver..."); + HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); + /* + * Reflections rf = new Reflections( new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(Page.class.getClassLoader())) + * .addClassLoader(Page.class.getClassLoader()).addScanners(new SubTypesScanner()) .filterInputsBy((String pkg) -> pkg.contains(Page.class.getPackage().getName()))); Set> pages = rf.getSubTypesOf(Page.class); for (Class page : pages) { try { if (Modifier.isAbstract(page.getModifiers())) continue; Page p = page.newInstance(); + * addPage(server, p); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } + */ //^^ This code would load the pages dynamically - But we'll only have like, one page... + addPage(server, new IndexPage()); + server.start(); + this.getLogger().info("Webserver started"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void addPage(HttpServer server, Page page) { + server.createContext("/" + page.GetName(), page); + } } diff --git a/src/buttondevteam/website/data/Stats.java b/src/buttondevteam/website/data/Stats.java new file mode 100644 index 0000000..d051729 --- /dev/null +++ b/src/buttondevteam/website/data/Stats.java @@ -0,0 +1,5 @@ +package buttondevteam.website.data; + +public class Stats { + +} diff --git a/src/buttondevteam/website/page/IndexPage.java b/src/buttondevteam/website/page/IndexPage.java new file mode 100644 index 0000000..8a687c7 --- /dev/null +++ b/src/buttondevteam/website/page/IndexPage.java @@ -0,0 +1,26 @@ +package buttondevteam.website.page; + +import java.io.IOException; + +import com.google.gson.Gson; +import com.sun.net.httpserver.HttpExchange; + +import buttondevteam.io.IOHelper; +import buttondevteam.website.data.Stats; + +public class IndexPage extends Page { + + @Override + public void handlePage(HttpExchange exchange) throws IOException { + Gson gson = new Gson(); + Stats request = gson.fromJson(IOHelper.GetPOSTJSON(exchange), Stats.class); // TODO: Change to a request class + Stats response = new Stats(); + IOHelper.SendResponse(200, gson.toJson(response), exchange); + } + + @Override + public String GetName() { + return ""; + } + +} diff --git a/src/buttondevteam/website/page/Page.java b/src/buttondevteam/website/page/Page.java new file mode 100644 index 0000000..ffe21c3 --- /dev/null +++ b/src/buttondevteam/website/page/Page.java @@ -0,0 +1,41 @@ +package buttondevteam.website.page; + +import java.io.IOException; +import java.io.PrintStream; +import org.apache.commons.io.output.ByteArrayOutputStream; + +import com.sun.net.httpserver.*; + +import buttondevteam.io.IOHelper; + +/** + * Add to {@link Main}.Pages + */ +public abstract class Page implements HttpHandler { + public abstract String GetName(); + + @Override + public void handle(HttpExchange exchange) { + try { + if (exchange.getRequestURI().getPath().equals("/" + GetName())) + handlePage(exchange); + else { + IOHelper.SendResponse(404, "404 Not found", exchange); + } + } catch (Exception e) { + e.printStackTrace(); + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream str = new PrintStream(baos); + str.print("

500 Internal Server Error

");
+				e.printStackTrace(str);
+				str.print("
"); + IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } + + public abstract void handlePage(HttpExchange exchange) throws IOException; +}