PageRenderTime 897ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/dynmap/web/handlers/FileHandler.java

http://github.com/webbukkit/dynmap
Java | 121 lines | 104 code | 17 blank | 0 comment | 16 complexity | dd44575699c8d39e8d177a8720ebaae0 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.dynmap.web.handlers;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.Map;
  8. import java.util.logging.Logger;
  9. import org.dynmap.web.HttpField;
  10. import org.dynmap.web.HttpHandler;
  11. import org.dynmap.web.HttpRequest;
  12. import org.dynmap.web.HttpResponse;
  13. import org.dynmap.web.HttpStatus;
  14. public abstract class FileHandler implements HttpHandler {
  15. protected static final Logger log = Logger.getLogger("Minecraft");
  16. private LinkedList<byte[]> bufferpool = new LinkedList<byte[]>();
  17. private Object lock = new Object();
  18. private static final int MAX_FREE_IN_POOL = 2;
  19. private static Map<String, String> mimes = new HashMap<String, String>();
  20. static {
  21. mimes.put(".html", "text/html");
  22. mimes.put(".htm", "text/html");
  23. mimes.put(".js", "text/javascript");
  24. mimes.put(".png", "image/png");
  25. mimes.put(".jpg", "image/jpeg");
  26. mimes.put(".css", "text/css");
  27. mimes.put(".txt", "text/plain");
  28. }
  29. public static final String getMimeTypeFromExtension(String extension) {
  30. String m = mimes.get(extension);
  31. if (m != null)
  32. return m;
  33. return "application/octet-steam";
  34. }
  35. protected abstract InputStream getFileInput(String path, HttpRequest request, HttpResponse response);
  36. protected void closeFileInput(String path, InputStream in) throws IOException {
  37. in.close();
  38. }
  39. protected String getExtension(String path) {
  40. int dotindex = path.lastIndexOf('.');
  41. if (dotindex > 0)
  42. return path.substring(dotindex);
  43. return null;
  44. }
  45. protected final String formatPath(String path) {
  46. int qmark = path.indexOf('?');
  47. if (qmark >= 0)
  48. path = path.substring(0, qmark);
  49. if (path.startsWith("/") || path.startsWith("."))
  50. return null;
  51. if (path.length() == 0)
  52. path = getDefaultFilename(path);
  53. return path;
  54. }
  55. protected String getDefaultFilename(String path) {
  56. return path + "index.html";
  57. }
  58. private byte[] allocateReadBuffer() {
  59. byte[] buf;
  60. synchronized(lock) {
  61. buf = bufferpool.poll();
  62. }
  63. if(buf == null) {
  64. buf = new byte[40960];
  65. }
  66. return buf;
  67. }
  68. private void freeReadBuffer(byte[] buf) {
  69. synchronized(lock) {
  70. if(bufferpool.size() < MAX_FREE_IN_POOL)
  71. bufferpool.push(buf);
  72. }
  73. }
  74. @Override
  75. public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
  76. InputStream fileInput = null;
  77. try {
  78. path = formatPath(path);
  79. fileInput = getFileInput(path, request, response);
  80. if (fileInput == null) {
  81. response.status = HttpStatus.NotFound;
  82. return;
  83. }
  84. String extension = getExtension(path);
  85. String mimeType = getMimeTypeFromExtension(extension);
  86. response.fields.put(HttpField.ContentType, mimeType);
  87. response.status = HttpStatus.OK;
  88. OutputStream out = response.getBody();
  89. byte[] readBuffer = allocateReadBuffer();
  90. try {
  91. int readBytes;
  92. while ((readBytes = fileInput.read(readBuffer)) > 0) {
  93. out.write(readBuffer, 0, readBytes);
  94. }
  95. } finally {
  96. freeReadBuffer(readBuffer);
  97. }
  98. } finally {
  99. if (fileInput != null) {
  100. try { closeFileInput(path, fileInput); fileInput = null; } catch (IOException ex) { }
  101. }
  102. }
  103. }
  104. }