More http server tinkering.

This commit is contained in:
Steven Lawson 2013-09-03 17:47:42 -04:00
parent e70f8ffff3
commit 4bef1a06a4
3 changed files with 80 additions and 62 deletions

View File

@ -149,9 +149,9 @@ public class Module_help extends TFM_HTTPD_Module
return ".commandName{font-weight:bold;}.commandDescription{padding-left:15px;}li{margin:.15em;padding:.15em;}"; return ".commandName{font-weight:bold;}.commandDescription{padding-left:15px;}li{margin:.15em;padding:.15em;}";
} }
@Override // @Override
public String getScript() // public String getScript()
{ // {
return "$(document).ready(function(){console.log(\"Ready\");});"; // return "$(document).ready(function(){console.log(\"Ready\");});";
} // }
} }

View File

@ -40,6 +40,11 @@ public class Module_schematic extends TFM_HTTPD_Module
@Override @Override
public String getBody() public String getBody()
{ {
if (!SCHEMATIC_FOLDER.exists())
{
return HTMLGenerationTools.paragraph("Can't find WorldEdit schematic folder.");
}
final StringBuilder out = new StringBuilder(); final StringBuilder out = new StringBuilder();
final String[] args = StringUtils.split(uri, "/"); final String[] args = StringUtils.split(uri, "/");
@ -119,29 +124,36 @@ public class Module_schematic extends TFM_HTTPD_Module
final File tempFile = new File(tempFileName); final File tempFile = new File(tempFileName);
if (tempFile.exists()) if (tempFile.exists())
{ {
if (SCHEMATIC_FILENAME.matcher(origFileName).find()) if (tempFile.length() <= FileUtils.ONE_KB * 64L)
{ {
final File targetFile = new File(SCHEMATIC_FOLDER.getPath(), origFileName); if (SCHEMATIC_FILENAME.matcher(origFileName).find())
if (targetFile.exists())
{ {
throw new SchematicUploadException("Schematic exists on the server already."); final File targetFile = new File(SCHEMATIC_FOLDER.getPath(), origFileName);
if (targetFile.exists())
{
throw new SchematicUploadException("Schematic exists on the server already.");
}
else
{
try
{
FileUtils.copyFile(tempFile, targetFile);
}
catch (IOException ex)
{
TFM_Log.severe(ex);
throw new SchematicUploadException();
}
}
} }
else else
{ {
try throw new SchematicUploadException("File name must be alphanumeric with a \".schematic\" extension.");
{
FileUtils.copyFile(tempFile, targetFile);
}
catch (IOException ex)
{
TFM_Log.severe(ex);
throw new SchematicUploadException();
}
} }
} }
else else
{ {
throw new SchematicUploadException("File name must be alphanumeric with a \".schematic\" extension."); throw new SchematicUploadException("Schematic is too big (64kb max).");
} }
} }
else else

View File

@ -9,12 +9,12 @@ import java.util.concurrent.Callable;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.MIME_PLAINTEXT;
import me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.Response; import me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.Response;
import me.StevenLawson.TotalFreedomMod.TFM_ConfigEntry; import me.StevenLawson.TotalFreedomMod.TFM_ConfigEntry;
import me.StevenLawson.TotalFreedomMod.TFM_Log; import me.StevenLawson.TotalFreedomMod.TFM_Log;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class TFM_HTTPD_Manager public class TFM_HTTPD_Manager
@ -129,61 +129,67 @@ public class TFM_HTTPD_Manager
{ {
Response response = null; Response response = null;
final String[] args = StringUtils.split(uri, "/"); try
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
{
switch (moduleType)
{
case HELP:
return new Module_help(uri, method, headers, params, files, socket).getResponse();
case LIST:
return new Module_list(uri, method, headers, params, files, socket).getResponse();
default:
return null;
}
}
});
try final String[] args = StringUtils.split(uri, "/");
final ModuleType moduleType = args.length >= 1 ? ModuleType.getByName(args[0]) : ModuleType.FILE;
if (moduleType.isRunOnBukkitThread())
{ {
response = responseCall.get(); Future<Response> responseCall = Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin, new Callable<Response>()
{
@Override
public Response call() throws Exception
{
switch (moduleType)
{
case HELP:
return new Module_help(uri, method, headers, params, files, socket).getResponse();
case LIST:
return new Module_list(uri, method, headers, params, files, socket).getResponse();
default:
return null;
}
}
});
try
{
response = responseCall.get();
}
catch (Exception ex)
{
TFM_Log.severe(ex);
}
} }
catch (Exception ex) else
{ {
TFM_Log.severe(ex); switch (moduleType)
{
case DUMP:
//response = new Module_dump(uri, method, headers, params, files, socket).getResponse();
response = new Response(Response.Status.OK, MIME_PLAINTEXT, "The DUMP module is disabled. It is intended for debugging use only.");
break;
case SCHEMATIC:
response = new Module_schematic(uri, method, headers, params, files, socket).getResponse();
break;
default:
response = new Module_file(uri, method, headers, params, files, socket).getResponse();
}
} }
} }
else catch (Exception ex)
{ {
switch (moduleType) response = new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Error 500: Internal Server Error\r\n" + ex.getMessage() + "\r\n" + ExceptionUtils.getFullStackTrace(ex));
{
case DUMP:
//response = new Module_dump(uri, method, headers, params, files, socket).getResponse();
response = new Response(Response.Status.OK, MIME_PLAINTEXT, "The DUMP module is disabled. It is intended for debugging use only.");
break;
case SCHEMATIC:
response = new Module_schematic(uri, method, headers, params, files, socket).getResponse();
break;
default:
response = new Module_file(uri, method, headers, params, files, socket).getResponse();
}
} }
if (response == null) 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."); response = new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Error 404: Not Found - The requested resource was not found on this server.");
}
else
{
return response;
} }
return response;
} }
} }