Skip to content

Commit

Permalink
Update to 1.13.2
Browse files Browse the repository at this point in the history
  • Loading branch information
warriordog committed Feb 13, 2019
1 parent 82c06b4 commit a724d0f
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 127 deletions.
60 changes: 31 additions & 29 deletions src/main/java/net/acomputerdog/webchat/PluginWebChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.logging.Level;

/**
* Plugin main class
*/
public class PluginWebChat extends JavaPlugin implements Listener {
/*
Delay in milliseconds between chat messages
*/
public int chatDelay = 750;
public int maxLines = 50;
public int webPort = 8080;
private int chatDelay = 750;
private int maxLines = 50;
private int webPort = 8080;

private WebServer webServer;
private ChatList chatList;
Expand All @@ -46,20 +50,13 @@ public void onEnable() {
calendar.setTimeZone(TimeZone.getTimeZone("EST"));

getLogger().info("Starting web server...");
try {
webServer = new WebServer(this);
webServer.start();
} catch (IOException e) {
getLogger().severe("Exception starting web server!");
e.printStackTrace();
stopServer();
}
webServer = new WebServer(this);
webServer.start();
getLogger().info("Server started.");

getServer().getPluginManager().registerEvents(this, this);
} catch (Exception e) {
getLogger().severe("Exception during startup! Plugin will be disabled!");
e.printStackTrace();
getLogger().log(Level.SEVERE, "Exception during startup. Plugin will be disabled!", e);
getServer().getPluginManager().disablePlugin(this);
}
}
Expand All @@ -85,7 +82,7 @@ private void loadConfig() throws IOException, InvalidConfigurationException {

YamlConfiguration conf = new YamlConfiguration();
conf.load(new File(getDataFolder(), "filter.yml"));
chatFilter = new ChatFilter(this, conf);
chatFilter = new ChatFilter(conf);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand Down Expand Up @@ -122,7 +119,7 @@ public void onServerCommand(ServerCommandEvent e) {
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerCommand(PlayerCommandPreprocessEvent e) {
String command = e.getMessage();//getCommand();
String command = e.getMessage();
String cmdName = getCommandName(command).toLowerCase();
String playerName = e.getPlayer().getName();
if (cmdName.equals("/say")) {
Expand Down Expand Up @@ -156,7 +153,8 @@ private void stopServer() {
if (webServer != null) {
try {
webServer.stop();
} catch (Throwable ignored) {
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Exception stopping web server", e);
}
}
}
Expand Down Expand Up @@ -197,12 +195,6 @@ private String prefix(int val) {
}

private String getCommandName(String command) {
if (command == null) {
return null;
}
if (command.isEmpty()) {
return "";
}
int idx = command.indexOf(' ');
if (idx > 0) {
return command.substring(0, idx);
Expand All @@ -212,17 +204,27 @@ private String getCommandName(String command) {
}

private String getCommandArgs(String command) {
if (command == null) {
return null;
}
if (command.isEmpty()) {
return "";
}
int idx = command.indexOf(' ');
if (idx > 0 && idx < command.length() - 1) {
return command.substring(idx + 1, command.length());
return command.substring(idx + 1);
} else {
return null;
}
}

public int getWebPort() {
return webPort;
}

public void stop() {
getServer().getPluginManager().disablePlugin(this);
}

public int getChatDelay() {
return chatDelay;
}

public int getMaxLines() {
return maxLines;
}
}
25 changes: 12 additions & 13 deletions src/main/java/net/acomputerdog/webchat/chat/ChatFilter.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package net.acomputerdog.webchat.chat;

import net.acomputerdog.webchat.PluginWebChat;
import org.bukkit.configuration.Configuration;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Filters chat messages
*/
public class ChatFilter {
private final PluginWebChat plugin;

private final boolean filterIn;
private final boolean filterOut;
private final boolean strictFilter;
Expand All @@ -18,8 +18,7 @@ public class ChatFilter {
private final int maxLineLength;
private final Pattern[] filterPatterns;

public ChatFilter(PluginWebChat plugin, Configuration conf) {
this.plugin = plugin;
public ChatFilter(Configuration conf) {

filterIn = conf.getBoolean("filter_in", true);
filterOut = conf.getBoolean("filter_out", true);
Expand Down Expand Up @@ -71,25 +70,25 @@ private String filterPatterns(String line) {
int end = matcher.end();
int length = end - start;

String newLine;
StringBuilder newLine = new StringBuilder();
if (!strictFilter) {
newLine = line.substring(0, start + 1);
newLine.append(line, 0, start + 1);
for (int i = 0; i < length - 2; i++) {
newLine = newLine + "*";
newLine.append('*');
}
if (end > 0 && end <= line.length()) {
newLine += line.substring(end - 1, line.length());
newLine.append(line.substring(end - 1));
}
} else {
newLine = line.substring(0, start);
newLine.append(line, 0, start);
for (int i = 0; i < length; i++) {
newLine = newLine + "*";
newLine.append('*');
}
if (end < line.length()) {
newLine = line.substring(end);
newLine.append(end);
}
}
line = newLine;
line = newLine.toString();
}
}
return line;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/acomputerdog/webchat/chat/ChatList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.concurrent.Semaphore;
import java.util.function.Consumer;

/**
* Stores list of chat messages
*/
public class ChatList {
private final BoundedSet<String> lines;
private final Semaphore lock;
Expand All @@ -15,7 +18,7 @@ public class ChatList {
private int version;

public ChatList(PluginWebChat plugin) {
lines = new BoundedSet<>(plugin.maxLines);
lines = new BoundedSet<>(plugin.getMaxLines());
lock = new Semaphore(1, true);
filter = plugin.getChatFilter();
version = 0;
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/net/acomputerdog/webchat/net/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,37 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* HTTP web server
*/
public class WebServer {
private final HttpServer server;
private final Thread serverThread;
private final Logger logger;
private final PluginWebChat plugin;

public WebServer(PluginWebChat plugin) throws IOException {
public WebServer(PluginWebChat plugin) throws IOException, URISyntaxException {
this.plugin = plugin;
this.logger = plugin.getLogger();
//set timeouts. Is either 10 or 100 seconds (unsure do to closed source/undocumented code)
//should still set in JVM arguments
System.setProperty("sun.net.httpserver.maxReqTime", "10");
System.setProperty("sun.net.httpserver.maxRspTime", "10");
this.server = HttpServer.create(new InetSocketAddress(plugin.webPort), 0);
this.server = HttpServer.create(new InetSocketAddress(plugin.getWebPort()), 0);
this.serverThread = new Thread(() -> {
try {
server.start();
} catch (Throwable t) {
logger.severe("Uncaught exception in server thread! Server stopping!");
t.printStackTrace();
plugin.getServer().getPluginManager().disablePlugin(plugin);
} catch (Exception e) {
logger.log(Level.SEVERE, "Uncaught exception in server thread, server stopping.", e);
plugin.stop();
}
});
serverThread.setName("web_server");
SimpleHandler main = new SimpleHandler(this, getClass().getResourceAsStream("/main.html"));
SimpleHandler main = new SimpleHandler(this, getClass().getResource("/main.html").toURI());
server.createContext("/", main);
server.createContext("/main.html", main);
server.createContext("/chat", new ChatHandler(this, plugin));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

import java.io.IOException;

/**
* Outdated endpoint to retrieve entire chat log
*
* TODO why is this still here?
*
* @deprecated Inefficient, use ChatUpdateHandler instead
*/
@Deprecated
public class ChatHandler extends WebHandler {
private final PluginWebChat plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import java.io.IOException;

/**
* API endpoint that retrieves all chat messages after a specified point in time
*
* TODO use actual time instead of version
*/
public class ChatUpdateHandler extends WebHandler {
private final PluginWebChat plugin;

Expand All @@ -17,34 +22,42 @@ public ChatUpdateHandler(WebServer server, PluginWebChat plugin) {

@Override
public void handleExchange(HttpExchange exchange) throws IOException {
if (!exchange.getRequestMethod().equals("GET")) {
sendResponse(exchange, "405 Method not allowed: only GET is accepted.", 405);
return;
}
String request = exchange.getRequestURI().getQuery();
int version = getOldVersion(request);
ChatList chat = plugin.getChatList();
if (version != chat.getVersion()) {
sendResponse(exchange, chat.toString());
} else {
sendEmptyResponse(exchange);
try {
if (!exchange.getRequestMethod().equals("GET")) {
sendResponse(exchange, "405 Method not allowed: only GET is accepted.", 405);
return;
}
String request = exchange.getRequestURI().getQuery();
int version = getOldVersion(request);
ChatList chat = plugin.getChatList();
if (version != chat.getVersion()) {
sendResponse(exchange, chat.toString());
} else {
sendEmptyResponse(exchange);
}
} catch (IllegalArgumentException e) {
// TODO proper malformed request response
sendErrorResponse(exchange, "Invalid query parameter");
}
}

private int getOldVersion(String request) {
int version = 0;
if (request != null) {
String[] params = request.split("&");
for (String param : params) {
String[] var = param.split("=");
if (var.length == 2 && "version".equals(var[0])) {
try {
version = Integer.parseInt(var[1]);
return Integer.parseInt(var[1]);
} catch (NumberFormatException ignored) {
throw new IllegalArgumentException("version is not an integer");
}
}
}

throw new IllegalArgumentException("chat version argument is missing");
} else {
throw new IllegalArgumentException("request is null");
}
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import java.io.IOException;

/**
* API endpoint that gets the "version" of the current chat list
*/
public class ChatVersionHandler extends WebHandler {
private final PluginWebChat plugin;

Expand Down
Loading

0 comments on commit a724d0f

Please sign in to comment.