TotalFreedomMod/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java

126 lines
3.6 KiB
Java
Raw Normal View History

2013-08-27 01:48:04 +00:00
package me.StevenLawson.TotalFreedomMod.HTTPD;
import java.io.IOException;
import java.util.Map;
2013-08-27 13:01:12 +00:00
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
2013-08-27 01:48:04 +00:00
import me.StevenLawson.TotalFreedomMod.TFM_Log;
2013-08-27 13:01:12 +00:00
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
2013-08-27 01:48:04 +00:00
import org.apache.commons.lang.StringUtils;
2013-08-27 13:01:12 +00:00
import org.bukkit.Bukkit;
2013-08-27 01:48:04 +00:00
public class TFM_HTTPD_Manager
{
2013-08-27 16:39:28 +00:00
public static final int PORT = 8748;
2013-08-27 01:48:04 +00:00
//
private final TFM_HTTPD httpd = new TFM_HTTPD(PORT);
private TFM_HTTPD_Manager()
{
}
public void start()
{
try
{
httpd.start();
2013-08-27 16:39:28 +00:00
if (httpd.isAlive())
{
TFM_Log.info("TFM HTTPd started. Listening on port: " + httpd.getListeningPort());
}
else
{
TFM_Log.info("Error starting TFM HTTPd.");
}
2013-08-27 01:48:04 +00:00
}
catch (IOException ex)
{
TFM_Log.severe(ex);
}
}
public void stop()
{
httpd.stop();
2013-08-27 16:39:28 +00:00
TFM_Log.info("TFM HTTPd stopped.");
2013-08-27 01:48:04 +00:00
}
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
2013-08-27 13:01:12 +00:00
public Response serve(final String uri, final Method method, final Map<String, String> headers, final Map<String, String> params, final Map<String, String> files)
2013-08-27 01:48:04 +00:00
{
Response response = null;
final String[] args = StringUtils.split(uri, "/");
if (args.length >= 1)
{
2013-08-27 13:01:12 +00:00
Future<Response> responseCall = Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin, new Callable<Response>()
2013-08-27 01:48:04 +00:00
{
2013-08-27 13:01:12 +00:00
@Override
public Response call() throws Exception
{
if ("dump".equalsIgnoreCase(args[0]))
{
return new Module_dump(uri, method, headers, params, files).getResponse();
}
else if ("list".equalsIgnoreCase(args[0]))
{
return new Module_list(uri, method, headers, params, files).getResponse();
}
else if ("help".equalsIgnoreCase(args[0]))
{
2013-08-27 16:40:59 +00:00
return new Module_help(uri, method, headers, params, files).getResponse();
2013-08-27 13:01:12 +00:00
}
else if ("public".equalsIgnoreCase(args[0]))
{
return new Module_file(uri, method, headers, params, files).getResponse();
}
2013-08-27 13:01:12 +00:00
return null;
}
});
try
2013-08-27 01:48:04 +00:00
{
2013-08-27 13:01:12 +00:00
response = responseCall.get();
2013-08-27 01:48:04 +00:00
}
2013-08-27 13:01:12 +00:00
catch (Exception ex)
2013-08-27 01:48:04 +00:00
{
2013-08-27 13:01:12 +00:00
TFM_Log.severe(ex);
2013-08-27 01:48:04 +00:00
}
}
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();
}
}