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>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
</natures> </natures>
</projectDescription> </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"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<modelVersion>4.0.0</modelVersion> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.github.tbmcplugins</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>AliPresents</artifactId> <groupId>com.github.tbmcplugins</groupId>
<version>0.0.1-SNAPSHOT</version> <artifactId>ButtonWebsiteModule</artifactId>
<name>AliPresents</name> <version>0.0.1-SNAPSHOT</version>
<description>A bucket of aaall the stuff Ali makes. It's a bit smelly.</description> <name>ButtonWebsiteModule</name>
<build> <description>Button Website Module</description>
<sourceDirectory>src</sourceDirectory> <build>
<resources> <sourceDirectory>src</sourceDirectory>
<resource> <resources>
<directory>src</directory> <resource>
<excludes> <directory>src</directory>
<exclude>**/*.java</exclude> <excludes>
</excludes> <exclude>**/*.java</exclude>
</resource> </excludes>
<resource> </resource>
<directory>.</directory> <resource>
<includes> <directory>.</directory>
<include>*.yml</include> <includes>
</includes> <include>*.yml</include>
</resource> </includes>
</resources> </resource>
<plugins> </resources>
<plugin> <plugins>
<artifactId>maven-compiler-plugin</artifactId> <plugin>
<version>3.3</version> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <version>3.3</version>
<source>1.8</source> <configuration>
<target>1.8</target> <source>1.8</source>
</configuration> <target>1.8</target>
</plugin> </configuration>
</plugins> </plugin>
</build> </plugins>
<properties> </build>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <properties>
</properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<repository> <!-- This repo fixes issues with transitive dependencies --> <repository> <!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id> <id>jcenter</id>
<url>http://jcenter.bintray.com</url> <url>http://jcenter.bintray.com</url>
</repository> </repository>
<repository> <repository>
<id>jitpack.io</id> <id>jitpack.io</id>
<url>https://jitpack.io</url> <url>https://jitpack.io</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.9.2-R0.1-SNAPSHOT</version> <version>1.9.2-R0.1-SNAPSHOT</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>3.4</version> <version>3.4</version>
</dependency> </dependency>
</dependencies> <!-- 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> </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;
}