Commit b56b9d23 authored by leMaik's avatar leMaik
Browse files

Make FacesFileHandler and TileFileHandler work. Rename abstract file methods....

Make FacesFileHandler and TileFileHandler work. Rename abstract file methods. Add some more methods.
parent 100d788d
......@@ -17,5 +17,5 @@ public interface DynmapServer {
Collection<Player> getPlayers();
AbstractFile getAsInputStream(String path) throws IOException;
AbstractFile getFile(String path) throws IOException;
}
package net.cubespace.dynmap.multiserver.HTTP.Handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultFileRegion;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.*;
import io.netty.handler.stream.ChunkedStream;
import net.cubespace.dynmap.multiserver.DynmapServer;
import net.cubespace.dynmap.multiserver.HTTP.HandlerUtil;
import net.cubespace.dynmap.multiserver.Main;
import net.cubespace.dynmap.multiserver.util.AbstractFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCEPT_ENCODING;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.IF_MODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaders.Names.VARY;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/**
......@@ -35,9 +25,9 @@ public class FacesFileHandler implements IHandler {
@Override
public void handle(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
//Get the correct DynmapServer
for(DynmapServer dynmapServer : Main.getDynmapServers()) {
File file = new File(dynmapServer.getFolder(), request.getUri());
if(file.exists()) {
for (DynmapServer dynmapServer : Main.getDynmapServers()) {
AbstractFile file = dynmapServer.getFile(request.getUri());
if (file.exists()) {
if (file.isHidden() || !file.exists()) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
......@@ -65,22 +55,14 @@ public class FacesFileHandler implements IHandler {
}
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (FileNotFoundException fnfe) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
}
long fileLength = raf.length();
ReadableByteChannel channel = Channels.newChannel(file.getInputStream());
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HandlerUtil.setContentTypeHeader(response, file);
HandlerUtil.setContentTypeHeader(response, file.getName());
HandlerUtil.setDateAndCacheHeaders(response, file.lastModified());
response.headers().set(CONTENT_LENGTH, fileLength);
response.headers().set(CONTENT_LENGTH, file.length());
response.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
response.headers().set(VARY, ACCEPT_ENCODING);
......@@ -88,7 +70,7 @@ public class FacesFileHandler implements IHandler {
ctx.write(response);
// Write the content.
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength));
ctx.write(new ChunkedStream(file.getInputStream()));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
return;
}
......
package net.cubespace.dynmap.multiserver.HTTP.Handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultFileRegion;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.*;
import io.netty.handler.stream.ChunkedStream;
import net.cubespace.dynmap.multiserver.DynmapServer;
import net.cubespace.dynmap.multiserver.GSON.DynmapWorld;
import net.cubespace.dynmap.multiserver.HTTP.HandlerUtil;
......@@ -19,14 +15,8 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCEPT_ENCODING;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.IF_MODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaders.Names.VARY;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/**
......@@ -38,9 +28,9 @@ public class MarkerHandler implements IHandler {
//Get the correct DynmapServer
String path = "";
String world = request.getUri().split("/")[3].replace("marker_", "").replace(".json", "");
for(DynmapServer dynmapServer : Main.getDynmapServers()) {
for(DynmapWorld dynmapWorld : dynmapServer.getWorlds()) {
if(dynmapWorld.getName().equals(world)) {
for (DynmapServer dynmapServer : Main.getDynmapServers()) {
for (DynmapWorld dynmapWorld : dynmapServer.getWorlds()) {
if (dynmapWorld.getName().equals(world)) {
path = dynmapServer.getFolder().getAbsolutePath() + File.separator + request.getUri();
}
}
......@@ -86,7 +76,7 @@ public class MarkerHandler implements IHandler {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HandlerUtil.setContentTypeHeader(response, file);
HandlerUtil.setContentTypeHeader(response, file.getName());
HandlerUtil.setDateAndCacheHeaders(response, file.lastModified());
response.headers().set(CONTENT_LENGTH, fileLength);
......@@ -97,7 +87,7 @@ public class MarkerHandler implements IHandler {
ctx.write(response);
// Write the content.
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength));
ctx.write(new ChunkedStream(file.getInputStream()));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
}
package net.cubespace.dynmap.multiserver.HTTP.Handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultFileRegion;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.*;
import io.netty.handler.stream.ChunkedStream;
import net.cubespace.dynmap.multiserver.DynmapServer;
import net.cubespace.dynmap.multiserver.GSON.DynmapWorld;
import net.cubespace.dynmap.multiserver.HTTP.HandlerUtil;
import net.cubespace.dynmap.multiserver.Main;
import net.cubespace.dynmap.multiserver.util.AbstractFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCEPT_ENCODING;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.IF_MODIFIED_SINCE;
import static io.netty.handler.codec.http.HttpHeaders.Names.VARY;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/**
......@@ -36,13 +24,13 @@ public class TileFileHandler implements IHandler {
@Override
public void handle(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
//Get the correct DynmapServer
String path = "";
AbstractFile path = null;
String world = request.getUri().split("/")[2];
if(world.equals("_markers_")) {
for(DynmapServer dynmapServer : Main.getDynmapServers()) {
File file = new File(dynmapServer.getFolder(), request.getUri());
if(file.exists()) {
if (world.equals("_markers_")) {
for (DynmapServer dynmapServer : Main.getDynmapServers()) {
AbstractFile file = dynmapServer.getFile(request.getUri());
if (file.exists()) {
if (file.isHidden() || !file.exists()) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
......@@ -70,22 +58,12 @@ public class TileFileHandler implements IHandler {
}
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (FileNotFoundException fnfe) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
}
long fileLength = raf.length();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HandlerUtil.setContentTypeHeader(response, file);
HandlerUtil.setContentTypeHeader(response, file.getName());
HandlerUtil.setDateAndCacheHeaders(response, file.lastModified());
response.headers().set(CONTENT_LENGTH, fileLength);
response.headers().set(CONTENT_LENGTH, file.length());
response.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
response.headers().set(VARY, ACCEPT_ENCODING);
......@@ -93,30 +71,29 @@ public class TileFileHandler implements IHandler {
ctx.write(response);
// Write the content.
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength));
ctx.write(new ChunkedStream(file.getInputStream()));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
return;
}
}
}
for(DynmapServer dynmapServer : Main.getDynmapServers()) {
for(DynmapWorld dynmapWorld : dynmapServer.getWorlds()) {
if(dynmapWorld.getName().equals(world)) {
path = dynmapServer.getFolder().getAbsolutePath() + File.separator + request.getUri();
for (DynmapServer dynmapServer : Main.getDynmapServers()) {
for (DynmapWorld dynmapWorld : dynmapServer.getWorlds()) {
if (dynmapWorld.getName().equals(world)) {
path = dynmapServer.getFile(request.getUri());
}
}
}
File file = new File(path.replace("/", File.separator));
if (file.isHidden() || !file.exists()) {
AbstractFile file = path;
if (file == null || file.isHidden() || !file.exists()) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
}
if (!file.isFile()) {
HandlerUtil.sendError(ctx, FORBIDDEN);
return;
}
......@@ -136,22 +113,12 @@ public class TileFileHandler implements IHandler {
}
}
RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "r");
} catch (FileNotFoundException fnfe) {
HandlerUtil.sendError(ctx, NOT_FOUND);
return;
}
long fileLength = raf.length();
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
HandlerUtil.setContentTypeHeader(response, file);
HandlerUtil.setContentTypeHeader(response, file.getName());
HandlerUtil.setDateAndCacheHeaders(response, file.lastModified());
response.headers().set(CONTENT_LENGTH, fileLength);
response.headers().set(CONTENT_LENGTH, file.length());
response.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
response.headers().set(VARY, ACCEPT_ENCODING);
......@@ -159,7 +126,7 @@ public class TileFileHandler implements IHandler {
ctx.write(response);
// Write the content.
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength));
ctx.write(new ChunkedStream(file.getInputStream()));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
}
}
......@@ -14,19 +14,10 @@ import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.*;
import java.util.regex.Pattern;
import static io.netty.handler.codec.http.HttpHeaders.Names.CACHE_CONTROL;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaders.Names.DATE;
import static io.netty.handler.codec.http.HttpHeaders.Names.EXPIRES;
import static io.netty.handler.codec.http.HttpHeaders.Names.LAST_MODIFIED;
import static io.netty.handler.codec.http.HttpHeaders.Names.LOCATION;
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpResponseStatus.FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_MODIFIED;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
......@@ -91,8 +82,7 @@ public class HandlerUtil {
/**
* When file timestamp is the same as what the browser is sending up, send a "304 Not Modified"
*
* @param ctx
* Context
* @param ctx Context
*/
public static void sendNotModified(ChannelHandlerContext ctx) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
......@@ -105,8 +95,7 @@ public class HandlerUtil {
/**
* Sets the Date header for the HTTP response
*
* @param response
* HTTP response
* @param response HTTP response
*/
public static void setDateHeader(FullHttpResponse response) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
......@@ -119,8 +108,7 @@ public class HandlerUtil {
/**
* Sets the Date and Cache headers for the HTTP Response
*
* @param response
* HTTP response
* @param response HTTP response
*/
public static void setDateAndCacheHeaders(HttpResponse response, long lastModified) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
......@@ -140,12 +128,10 @@ public class HandlerUtil {
/**
* Sets the content type header for the HTTP Response
*
* @param response
* HTTP response
* @param file
* file to extract content type
* @param response HTTP response
* @param file file name
*/
public static void setContentTypeHeader(HttpResponse response, File file) {
public static void setContentTypeHeader(HttpResponse response, String file) {
response.headers().set(CONTENT_TYPE, MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(file));
}
}
......@@ -179,7 +179,7 @@ public class LocalDynmapServer implements DynmapServer {
}
@Override
public AbstractFile getAsInputStream(String path) {
public AbstractFile getFile(String path) {
return new LocalAbstractFile(new File(file, path.replace("/", File.separator)));
}
}
......@@ -8,5 +8,13 @@ public interface AbstractFile {
boolean exists();
long getLastModified();
long lastModified();
long length();
boolean isHidden();
boolean isFile();
String getName();
}
......@@ -23,7 +23,27 @@ public class LocalAbstractFile implements AbstractFile {
}
@Override
public long getLastModified() {
public long lastModified() {
return file.lastModified();
}
@Override
public long length() {
return file.length();
}
@Override
public boolean isHidden() {
return file.isHidden();
}
@Override
public boolean isFile() {
return file.isFile();
}
@Override
public String getName() {
return file.getName();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment