Moved handlers to their own (independent) classes.
This commit is contained in:
parent
883eba6890
commit
2a79aea7bb
7 changed files with 274 additions and 244 deletions
|
|
@ -0,0 +1,76 @@
|
|||
package org.dynmap.web.handlers;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.dynmap.web.HttpHandler;
|
||||
import org.dynmap.web.HttpRequest;
|
||||
import org.dynmap.web.HttpResponse;
|
||||
|
||||
public class ClientConfigurationHandler implements HttpHandler {
|
||||
private Map<?, ?> configuration;
|
||||
public ClientConfigurationHandler(Map<?, ?> configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws IOException {
|
||||
String s = stringifyJson(configuration);
|
||||
|
||||
byte[] bytes = s.getBytes();
|
||||
String dateStr = new Date().toString();
|
||||
|
||||
response.fields.put("Date", dateStr);
|
||||
response.fields.put("Content-Type", "text/plain");
|
||||
response.fields.put("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
|
||||
response.fields.put("Last-modified", dateStr);
|
||||
response.fields.put("Content-Length", Integer.toString(bytes.length));
|
||||
BufferedOutputStream out = new BufferedOutputStream(response.getBody());
|
||||
out.write(s.getBytes());
|
||||
out.flush();
|
||||
}
|
||||
|
||||
public String stringifyJson(Object o) {
|
||||
if (o == null) {
|
||||
return "null";
|
||||
} else if (o instanceof Boolean) {
|
||||
return ((Boolean) o) ? "true" : "false";
|
||||
} else if (o instanceof String) {
|
||||
return "\"" + o + "\"";
|
||||
} else if (o instanceof Integer || o instanceof Long || o instanceof Float || o instanceof Double) {
|
||||
return o.toString();
|
||||
} else if (o instanceof LinkedHashMap<?, ?>) {
|
||||
LinkedHashMap<?, ?> m = (LinkedHashMap<?, ?>) o;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
boolean first = true;
|
||||
for (Object key : m.keySet()) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
sb.append(",");
|
||||
|
||||
sb.append(stringifyJson(key));
|
||||
sb.append(": ");
|
||||
sb.append(stringifyJson(m.get(key)));
|
||||
}
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
} else if (o instanceof ArrayList<?>) {
|
||||
ArrayList<?> l = (ArrayList<?>) o;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int count = 0;
|
||||
for (int i = 0; i < l.size(); i++) {
|
||||
sb.append(count++ == 0 ? "[" : ",");
|
||||
sb.append(stringifyJson(l.get(i)));
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
} else {
|
||||
return "undefined";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
package org.dynmap.web.handlers;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.dynmap.ChatQueue;
|
||||
import org.dynmap.MapManager;
|
||||
import org.dynmap.PlayerList;
|
||||
import org.dynmap.TileUpdate;
|
||||
import org.dynmap.web.HttpHandler;
|
||||
import org.dynmap.web.HttpRequest;
|
||||
import org.dynmap.web.HttpResponse;
|
||||
|
||||
public class ClientUpdateHandler implements HttpHandler {
|
||||
private MapManager mapManager;
|
||||
private PlayerList playerList;
|
||||
private World world;
|
||||
public ClientUpdateHandler(MapManager mapManager, PlayerList playerList, World world) {
|
||||
this.mapManager = mapManager;
|
||||
this.playerList = playerList;
|
||||
this.world = world;
|
||||
}
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws IOException {
|
||||
int current = (int) (System.currentTimeMillis() / 1000);
|
||||
long cutoff = 0;
|
||||
|
||||
if (path.length() > 0) {
|
||||
try {
|
||||
cutoff = ((long) Integer.parseInt(path)) * 1000;
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
long relativeTime = world.getTime() % 24000;
|
||||
sb.append(current + " " + relativeTime + "\n");
|
||||
|
||||
Player[] players = playerList.getVisiblePlayers();
|
||||
for (Player player : players) {
|
||||
sb.append("player " + player.getName() + " " + player.getLocation().getX() + " " + player.getLocation().getY() + " " + player.getLocation().getZ() + "\n");
|
||||
}
|
||||
|
||||
TileUpdate[] tileUpdates = mapManager.staleQueue.getTileUpdates(cutoff);
|
||||
for (TileUpdate tu : tileUpdates) {
|
||||
sb.append("tile " + tu.tile.getName() + "\n");
|
||||
}
|
||||
|
||||
ChatQueue.ChatMessage[] messages = mapManager.chatQueue.getChatMessages(cutoff);
|
||||
for (ChatQueue.ChatMessage cu : messages) {
|
||||
sb.append("chat " + cu.playerName + " " + cu.message + "\n");
|
||||
}
|
||||
|
||||
//debugger.debug("Sending " + players.length + " players, " + tileUpdates.length + " tile-updates, and " + messages.length + " chats. " + path + ";" + cutoff);
|
||||
|
||||
byte[] bytes = sb.toString().getBytes();
|
||||
|
||||
String dateStr = new Date().toString();
|
||||
response.fields.put("Date", dateStr);
|
||||
response.fields.put("Content-Type", "text/plain");
|
||||
response.fields.put("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
|
||||
response.fields.put("Last-modified", dateStr);
|
||||
response.fields.put("Content-Length", Integer.toString(bytes.length));
|
||||
BufferedOutputStream out = new BufferedOutputStream(response.getBody());
|
||||
out.write(bytes);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
29
src/main/java/org/dynmap/web/handlers/FilesystemHandler.java
Normal file
29
src/main/java/org/dynmap/web/handlers/FilesystemHandler.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package org.dynmap.web.handlers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.dynmap.web.FileHandler;
|
||||
|
||||
public class FilesystemHandler extends FileHandler {
|
||||
private File root;
|
||||
public FilesystemHandler(File root) {
|
||||
if (!root.isDirectory())
|
||||
throw new IllegalArgumentException();
|
||||
this.root = root;
|
||||
}
|
||||
@Override
|
||||
protected InputStream getFileInput(String path) {
|
||||
File file = new File(root, path);
|
||||
if (file.getAbsolutePath().startsWith(root.getAbsolutePath()) && file.isFile()) {
|
||||
try {
|
||||
return new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
17
src/main/java/org/dynmap/web/handlers/JarFileHandler.java
Normal file
17
src/main/java/org/dynmap/web/handlers/JarFileHandler.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package org.dynmap.web.handlers;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.dynmap.web.FileHandler;
|
||||
|
||||
public class JarFileHandler extends FileHandler {
|
||||
private String root;
|
||||
public JarFileHandler(String root) {
|
||||
if (root.endsWith("/")) root = root.substring(0, root.length()-1);
|
||||
this.root = root;
|
||||
}
|
||||
@Override
|
||||
protected InputStream getFileInput(String path) {
|
||||
return this.getClass().getResourceAsStream(root + "/" + path);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue