Finished file serving module (default).

Added config options.
This commit is contained in:
Steven Lawson 2013-08-27 20:20:11 -04:00
parent c3f8bd33ff
commit 3819c57adf
5 changed files with 88 additions and 21 deletions

View File

@ -154,7 +154,6 @@ unbannable_usernames:
- grumm
- cavemanfilms
# TwitterBot - Used to allow superadmins to verify themselves using twitter
twitterbot_enabled: false
twitterbot_url: ''
@ -169,3 +168,8 @@ logs_register_url: ''
# Mojang service checker
service_checker_url: http://status.mojang.com/check
# HTTPD
httpd_enabled: true
httpd_public_folder: ./public_html
httpd_port: 28966

View File

@ -14,10 +14,14 @@ import java.util.Map;
import java.util.StringTokenizer;
import static me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.*;
import me.StevenLawson.TotalFreedomMod.TFM_ConfigEntry;
/*
* This class was adapted from https://github.com/NanoHttpd/nanohttpd/blob/master/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java
*/
public class Module_file extends TFM_HTTPD_Module
{
private final File rootDir = new File("./public/");
private final File rootDir = new File(TFM_ConfigEntry.HTTPD_PUBLIC_FOLDER.getString());
private static final Map<String, String> MIME_TYPES = new HashMap<String, String>();
static

View File

@ -36,7 +36,7 @@ public class Module_help extends TFM_HTTPD_Module
return paragraph("Error loading commands.");
}
responseBody.append(heading("Command Help Index", 1));
responseBody.append(heading("Command Help", 1)).append(paragraph("This page is an automatically generated listing of all plugin commands that are currently live on the server. Please note that it does not include vanilla server commands."));
final Map<String, List<Command>> commandsByPlugin = new HashMap<String, List<Command>>();
@ -48,7 +48,7 @@ public class Module_help extends TFM_HTTPD_Module
final Command command = entry.getValue();
if (name.equalsIgnoreCase(command.getName()))
{
String pluginName = "Bukkit";
String pluginName = "Bukkit Default";
if (command instanceof PluginIdentifiableCommand)
{
pluginName = ((PluginIdentifiableCommand) command).getPlugin().getName();

View File

@ -4,6 +4,8 @@ import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import static me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.MIME_PLAINTEXT;
import me.StevenLawson.TotalFreedomMod.TFM_ConfigEntry;
import me.StevenLawson.TotalFreedomMod.TFM_Log;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.apache.commons.lang.StringUtils;
@ -11,7 +13,7 @@ import org.bukkit.Bukkit;
public class TFM_HTTPD_Manager
{
public static final int PORT = 8748;
public static final int PORT = TFM_ConfigEntry.HTTPD_PORT.getInteger();
//
private final TFM_HTTPD httpd = new TFM_HTTPD(PORT);
@ -21,6 +23,11 @@ public class TFM_HTTPD_Manager
public void start()
{
if (!TFM_ConfigEntry.HTTPD_ENABLED.getBoolean())
{
return;
}
try
{
httpd.start();
@ -42,11 +49,54 @@ public class TFM_HTTPD_Manager
public void stop()
{
if (!TFM_ConfigEntry.HTTPD_ENABLED.getBoolean())
{
return;
}
httpd.stop();
TFM_Log.info("TFM HTTPd stopped.");
}
private static enum ModuleType
{
DUMP(false, "dump"),
HELP(true, "help"),
LIST(true, "list"),
FILE(false, "file");
private final boolean runOnBukkitThread;
private final String name;
private ModuleType(boolean runOnBukkitThread, String name)
{
this.runOnBukkitThread = runOnBukkitThread;
this.name = name;
}
public boolean isRunOnBukkitThread()
{
return runOnBukkitThread;
}
public String getName()
{
return name;
}
private static ModuleType getByName(String needle)
{
for (ModuleType type : values())
{
if (type.getName().equalsIgnoreCase(needle))
{
return type;
}
}
return FILE;
}
}
private static class TFM_HTTPD extends NanoHTTPD
{
public TFM_HTTPD(int port)
@ -65,30 +115,24 @@ public class TFM_HTTPD_Manager
Response response = null;
final String[] args = StringUtils.split(uri, "/");
if (args.length >= 1)
final ModuleType moduleType = args.length >= 1 ? ModuleType.getByName(args[0]) : ModuleType.FILE;
if (moduleType.isRunOnBukkitThread())
{
Future<Response> responseCall = Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin, new Callable<Response>()
{
@Override
public Response call() throws Exception
{
if ("dump".equalsIgnoreCase(args[0]))
switch (moduleType)
{
return new Module_dump(uri, method, headers, params, files).getResponse();
case HELP:
return new Module_help(uri, method, headers, params, files).getResponse();
case LIST:
return new Module_list(uri, method, headers, params, files).getResponse();
default:
return null;
}
else if ("list".equalsIgnoreCase(args[0]))
{
return new Module_list(uri, method, headers, params, files).getResponse();
}
else if ("help".equalsIgnoreCase(args[0]))
{
return new Module_help(uri, method, headers, params, files).getResponse();
}
else if ("public".equalsIgnoreCase(args[0]))
{
return new Module_file(uri, method, headers, params, files).getResponse();
}
return null;
}
});
@ -101,6 +145,18 @@ public class TFM_HTTPD_Manager
TFM_Log.severe(ex);
}
}
else
{
switch (moduleType)
{
case DUMP:
response = new Module_dump(uri, method, headers, params, files).getResponse();
break;
default:
response = new Module_file(uri, method, headers, params, files).getResponse();
}
}
if (response == null)
{

View File

@ -31,6 +31,7 @@ public enum TFM_ConfigEntry
PROTECTED_AREAS_ENABLED(Boolean.class, "protected_areas_enabled"),
TOSSMOB_ENABLED(Boolean.class, "tossmob_enabled"),
TWITTERBOT_ENABLED(Boolean.class, "twitterbot_enabled"),
HTTPD_ENABLED(Boolean.class, "httpd_enabled"),
//
AUTO_PROTECT_RADIUS(Double.class, "auto_protect_radius"),
EXPLOSIVE_RADIUS(Double.class, "explosive_radius"),
@ -40,6 +41,7 @@ public enum TFM_ConfigEntry
MOB_LIMITER_MAX(Integer.class, "mob_limiter_max"),
NUKE_MONITOR_COUNT_BREAK(Integer.class, "nuke_monitor_count_break"),
NUKE_MONITOR_COUNT_PLACE(Integer.class, "nuke_monitor_count_place"),
HTTPD_PORT(Integer.class, "httpd_port"),
//
FLATLANDS_GENERATION_PARAMS(String.class, "flatlands_generation_params"),
LOGS_REGISTER_PASSWORD(String.class, "logs_register_password"),
@ -47,6 +49,7 @@ public enum TFM_ConfigEntry
SERVICE_CHECKER_URL(String.class, "service_checker_url"),
TWITTERBOT_SECRET(String.class, "twitterbot_secret"),
TWITTERBOT_URL(String.class, "twitterbot_url"),
HTTPD_PUBLIC_FOLDER(String.class, "httpd_public_folder"),
//
BLOCKED_COMMANDS(List.class, "blocked_commands"),
HOST_SENDER_NAMES(List.class, "host_sender_names"),