mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2024-12-27 20:27:37 +00:00
Getting HTTP server framework ready...
This commit is contained in:
parent
dfb6df63c8
commit
174043fa58
@ -0,0 +1,37 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.apache.commons.lang3.StringEscapeUtils.*;
|
||||
|
||||
public class HTMLGenerationTools
|
||||
{
|
||||
private HTMLGenerationTools()
|
||||
{
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static String paragraph(String data)
|
||||
{
|
||||
return "<p>" + escapeHtml4(data) + "</p>\r\n";
|
||||
}
|
||||
|
||||
public static String mapToHTMLList(Map<String, String> map)
|
||||
{
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
output.append("<ul>\r\n");
|
||||
|
||||
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
Map.Entry<String, String> entry = it.next();
|
||||
output.append("<li>").append(escapeHtml4(entry.getKey() + " = " + entry.getValue())).append("</li>\r\n");
|
||||
}
|
||||
|
||||
output.append("</ul>\r\n");
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
}
|
41
src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_dump.java
Normal file
41
src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_dump.java
Normal file
@ -0,0 +1,41 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import java.util.Map;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import static me.StevenLawson.TotalFreedomMod.HTTPD.HTMLGenerationTools.*;
|
||||
|
||||
public class Module_dump extends TFM_HTTPD_Module
|
||||
{
|
||||
public Module_dump(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
|
||||
{
|
||||
super(uri, method, headers, params, files);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBody()
|
||||
{
|
||||
StringBuilder responseBody = new StringBuilder();
|
||||
|
||||
String[] args = StringUtils.split(uri, "/");
|
||||
|
||||
responseBody
|
||||
.append(paragraph("URI: " + uri))
|
||||
.append(paragraph("args (Length: " + args.length + "): " + StringUtils.join(args, ",")))
|
||||
.append(paragraph("Method: " + method.toString()))
|
||||
.append(paragraph("Headers:"))
|
||||
.append(mapToHTMLList(headers))
|
||||
.append(paragraph("Params:"))
|
||||
.append(mapToHTMLList(params))
|
||||
.append(paragraph("Files:"))
|
||||
.append(mapToHTMLList(files));
|
||||
|
||||
return responseBody.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle()
|
||||
{
|
||||
return "TotalFreedomMod :: Request Debug Dumper";
|
||||
}
|
||||
}
|
62
src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_help.java
Normal file
62
src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_help.java
Normal file
@ -0,0 +1,62 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class Module_help extends TFM_HTTPD_Module
|
||||
{
|
||||
public Module_help(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
|
||||
{
|
||||
super(uri, method, headers, params, files);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBody()
|
||||
{
|
||||
final StringBuilder body = new StringBuilder();
|
||||
|
||||
Plugin[] plugins = Bukkit.getPluginManager().getPlugins();
|
||||
for (Plugin plugin : plugins)
|
||||
{
|
||||
Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands();
|
||||
if (commands != null)
|
||||
{
|
||||
Iterator<Map.Entry<String, Map<String, Object>>> it1 = commands.entrySet().iterator();
|
||||
while (it1.hasNext())
|
||||
{
|
||||
Map.Entry<String, Map<String, Object>> next1 = it1.next();
|
||||
String key1 = next1.getKey();
|
||||
Map<String, Object> value1 = next1.getValue();
|
||||
|
||||
Iterator<Map.Entry<String, Object>> it2 = value1.entrySet().iterator();
|
||||
while (it2.hasNext())
|
||||
{
|
||||
Map.Entry<String, Object> next2 = it2.next();
|
||||
String key2 = next2.getKey();
|
||||
Object value2 = next2.getValue();
|
||||
|
||||
body
|
||||
.append("<p>")
|
||||
.append(StringEscapeUtils.escapeHtml(key1))
|
||||
.append(".")
|
||||
.append(StringEscapeUtils.escapeHtml(key2))
|
||||
.append(" = ")
|
||||
.append(StringEscapeUtils.escapeHtml(value2.toString()))
|
||||
.append("</p>\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return body.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle()
|
||||
{
|
||||
return "Module_help";
|
||||
}
|
||||
}
|
72
src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_list.java
Normal file
72
src/me/StevenLawson/TotalFreedomMod/HTTPD/Module_list.java
Normal file
@ -0,0 +1,72 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import java.util.Map;
|
||||
import me.StevenLawson.TotalFreedomMod.TFM_SuperadminList;
|
||||
import me.StevenLawson.TotalFreedomMod.TFM_Util;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Module_list extends TFM_HTTPD_Module
|
||||
{
|
||||
public Module_list(String uri, NanoHTTPD.Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
|
||||
{
|
||||
super(uri, method, headers, params, files);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBody()
|
||||
{
|
||||
final StringBuilder body = new StringBuilder();
|
||||
|
||||
final Player[] onlinePlayers = Bukkit.getOnlinePlayers();
|
||||
|
||||
body.append("<p>There are ").append(onlinePlayers.length).append("/").append(Bukkit.getMaxPlayers()).append(" players online:</p>\r\n");
|
||||
|
||||
body.append("<ul>\r\n");
|
||||
|
||||
for (Player player : onlinePlayers)
|
||||
{
|
||||
String prefix = "";
|
||||
if (TFM_SuperadminList.isUserSuperadmin(player))
|
||||
{
|
||||
if (TFM_SuperadminList.isSeniorAdmin(player))
|
||||
{
|
||||
prefix = "[SrA]";
|
||||
}
|
||||
else
|
||||
{
|
||||
prefix = "[SA]";
|
||||
}
|
||||
|
||||
if (TFM_Util.DEVELOPERS.contains(player.getName()))
|
||||
{
|
||||
prefix = "[Dev]";
|
||||
}
|
||||
|
||||
if (player.getName().equals("markbyron"))
|
||||
{
|
||||
prefix = "[Owner]";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (player.isOp())
|
||||
{
|
||||
prefix = "[OP]";
|
||||
}
|
||||
}
|
||||
|
||||
body.append("<li>").append(prefix).append(player.getName()).append("</li>\r\n");
|
||||
}
|
||||
|
||||
body.append("</ul>\r\n");
|
||||
|
||||
return body.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle()
|
||||
{
|
||||
return "Total Freedom - Online Users";
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import me.StevenLawson.TotalFreedomMod.TFM_Log;
|
||||
|
||||
public class TFM_HTTPDManager
|
||||
{
|
||||
public static final int PORT = 28966;
|
||||
//
|
||||
private final TFM_HTTPD httpd = new TFM_HTTPD(PORT);
|
||||
|
||||
private TFM_HTTPDManager()
|
||||
{
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpd.start();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
TFM_Log.severe(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
httpd.stop();
|
||||
}
|
||||
|
||||
private static class TFM_HTTPD extends NanoHTTPD
|
||||
{
|
||||
public TFM_HTTPD(int port)
|
||||
{
|
||||
super(port);
|
||||
}
|
||||
|
||||
public TFM_HTTPD(String hostname, int port)
|
||||
{
|
||||
super(hostname, port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files)
|
||||
{
|
||||
return new Response("<p>OK - " + new Date().toString() + "</p>");
|
||||
}
|
||||
}
|
||||
|
||||
public static TFM_HTTPDManager getInstance()
|
||||
{
|
||||
return TFM_HTTPDManagerHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class TFM_HTTPDManagerHolder
|
||||
{
|
||||
private static final TFM_HTTPDManager INSTANCE = new TFM_HTTPDManager();
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import me.StevenLawson.TotalFreedomMod.TFM_Log;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
public class TFM_HTTPD_Manager
|
||||
{
|
||||
public static final int PORT = 28966;
|
||||
//
|
||||
private final TFM_HTTPD httpd = new TFM_HTTPD(PORT);
|
||||
|
||||
private TFM_HTTPD_Manager()
|
||||
{
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpd.start();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
TFM_Log.severe(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
httpd.stop();
|
||||
}
|
||||
|
||||
private static class TFM_HTTPD extends NanoHTTPD
|
||||
{
|
||||
public TFM_HTTPD(int port)
|
||||
{
|
||||
super(port);
|
||||
}
|
||||
|
||||
public TFM_HTTPD(String hostname, int port)
|
||||
{
|
||||
super(hostname, port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
|
||||
{
|
||||
Response response = null;
|
||||
|
||||
final String[] args = StringUtils.split(uri, "/");
|
||||
if (args.length >= 1)
|
||||
{
|
||||
if ("dump".equalsIgnoreCase(args[0]))
|
||||
{
|
||||
response = new Module_dump(uri, method, headers, params, files).getResponse();
|
||||
}
|
||||
else if ("list".equalsIgnoreCase(args[0]))
|
||||
{
|
||||
response = new Module_list(uri, method, headers, params, files).getResponse();
|
||||
}
|
||||
else if ("help".equalsIgnoreCase(args[0]))
|
||||
{
|
||||
//The issue is that plugin.getDescription().getCommands() only shows commands in the plugin.yml file.
|
||||
//I need to make another version of this that uses the CommandMap.
|
||||
response = new Module_help(uri, method, headers, params, files).getResponse();
|
||||
}
|
||||
}
|
||||
|
||||
if (response == null)
|
||||
{
|
||||
return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Error 404: Not Found - The requested resource was not found on this server.");
|
||||
}
|
||||
else
|
||||
{
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static TFM_HTTPD_Manager getInstance()
|
||||
{
|
||||
return TFM_HTTPDManagerHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class TFM_HTTPDManagerHolder
|
||||
{
|
||||
private static final TFM_HTTPD_Manager INSTANCE = new TFM_HTTPD_Manager();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import java.util.Map;
|
||||
import me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.*;
|
||||
|
||||
public abstract class TFM_HTTPD_Module
|
||||
{
|
||||
protected final String uri;
|
||||
protected final Method method;
|
||||
protected final Map<String, String> headers;
|
||||
protected final Map<String, String> params;
|
||||
protected final Map<String, String> files;
|
||||
|
||||
public TFM_HTTPD_Module(String uri, Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files)
|
||||
{
|
||||
this.uri = uri;
|
||||
this.method = method;
|
||||
this.headers = headers;
|
||||
this.params = params;
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
public abstract String getBody();
|
||||
|
||||
public abstract String getTitle();
|
||||
|
||||
public final Response getResponse()
|
||||
{
|
||||
return new TFM_HTTPD_PageBuilder(getBody(), getTitle()).getResponse();
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package me.StevenLawson.TotalFreedomMod.HTTPD;
|
||||
|
||||
import me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.Response;
|
||||
|
||||
public class TFM_HTTPD_PageBuilder
|
||||
{
|
||||
private static final String TEMPLATE =
|
||||
"<!DOCTYPE html>\n"
|
||||
+ "<html>\n"
|
||||
+ "<head>\n"
|
||||
+ "<title>{$TITLE}</title>\n"
|
||||
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
|
||||
+ "</head>\n"
|
||||
+ "<body>{$BODY}</body>\n"
|
||||
+ "</html>\n";
|
||||
//
|
||||
private String body;
|
||||
private String title;
|
||||
|
||||
public TFM_HTTPD_PageBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
public TFM_HTTPD_PageBuilder(String body, String title)
|
||||
{
|
||||
this.body = body;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public void setBody(String body)
|
||||
{
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public void setTitle(String title)
|
||||
{
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Response getResponse()
|
||||
{
|
||||
return new Response(TEMPLATE.replace("{$TITLE}", title).replace("{$BODY}", body));
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ import java.io.InputStream;
|
||||
import java.util.*;
|
||||
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
|
||||
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
|
||||
import me.StevenLawson.TotalFreedomMod.HTTPD.TFM_HTTPDManager;
|
||||
import me.StevenLawson.TotalFreedomMod.HTTPD.TFM_HTTPD_Manager;
|
||||
import me.StevenLawson.TotalFreedomMod.Listener.*;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.exception.ExceptionUtils;
|
||||
@ -173,7 +173,7 @@ public class TotalFreedomMod extends JavaPlugin
|
||||
}
|
||||
}.runTaskLater(plugin, 20L);
|
||||
|
||||
TFM_HTTPDManager.getInstance().start();
|
||||
TFM_HTTPD_Manager.getInstance().start();
|
||||
|
||||
TFM_Log.info("Plugin enabled.");
|
||||
}
|
||||
@ -183,7 +183,7 @@ public class TotalFreedomMod extends JavaPlugin
|
||||
{
|
||||
server.getScheduler().cancelTasks(plugin);
|
||||
|
||||
TFM_HTTPDManager.getInstance().stop();
|
||||
TFM_HTTPD_Manager.getInstance().stop();
|
||||
|
||||
TFM_Log.info("Plugin disabled.");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user