Added base code

Copied code from NorbiPeti/ChatServer
This commit is contained in:
Norbi Peti 2016-10-22 22:21:01 +02:00
parent 9038ffdf91
commit 73f0a96be8
7 changed files with 247 additions and 66 deletions

View file

@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

147
pom.xml
View file

@ -1,67 +1,86 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.tbmcplugins</groupId>
<artifactId>AliPresents</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AliPresents</name>
<description>A bucket of aaall the stuff Ali makes. It's a bit smelly.</description>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>.</directory>
<includes>
<include>*.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.tbmcplugins</groupId>
<artifactId>ButtonWebsiteModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ButtonWebsiteModule</name>
<description>Button Website Module</description>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>.</directory>
<includes>
<include>*.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository> <!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository> <!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9.2-R0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9.2-R0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>

View file

@ -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;
}
}
}

View file

@ -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<Class<? extends
* Page>> pages = rf.getSubTypesOf(Page.class); for (Class<? extends Page> 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);
}
}

View file

@ -0,0 +1,5 @@
package buttondevteam.website.data;
public class Stats {
}

View file

@ -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 "";
}
}

View file

@ -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("<h1>500 Internal Server Error</h1><pre>");
e.printStackTrace(str);
str.print("</pre>");
IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public abstract void handlePage(HttpExchange exchange) throws IOException;
}