mirror of
https://github.com/webbukkit/dynmap.git
synced 2024-12-28 19:47:45 +01:00
Moved stringifyJson to Json class.
This commit is contained in:
parent
9a655676ab
commit
d651d58d63
47
src/main/java/org/dynmap/web/Json.java
Normal file
47
src/main/java/org/dynmap/web/Json.java
Normal file
@ -0,0 +1,47 @@
|
||||
package org.dynmap.web;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class Json {
|
||||
public static 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 "\"" + ((String)o).replace("\"", "\\\"") + "\"";
|
||||
} 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 List<?>) {
|
||||
List<?> l = (List<?>) 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";
|
||||
}
|
||||
}
|
||||
}
|
@ -2,14 +2,13 @@ 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;
|
||||
import org.dynmap.web.Json;
|
||||
|
||||
public class ClientConfigurationHandler implements HttpHandler {
|
||||
private Map<?, ?> configuration;
|
||||
@ -18,7 +17,7 @@ public class ClientConfigurationHandler implements HttpHandler {
|
||||
}
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws IOException {
|
||||
String s = stringifyJson(configuration);
|
||||
String s = Json.stringifyJson(configuration);
|
||||
|
||||
byte[] bytes = s.getBytes();
|
||||
String dateStr = new Date().toString();
|
||||
@ -32,45 +31,4 @@ public class ClientConfigurationHandler implements HttpHandler {
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
84
src/main/java/org/dynmap/web/handlers/FileHandler.java
Normal file
84
src/main/java/org/dynmap/web/handlers/FileHandler.java
Normal file
@ -0,0 +1,84 @@
|
||||
package org.dynmap.web.handlers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.dynmap.web.HttpHandler;
|
||||
import org.dynmap.web.HttpRequest;
|
||||
import org.dynmap.web.HttpResponse;
|
||||
|
||||
public abstract class FileHandler implements HttpHandler {
|
||||
private byte[] readBuffer = new byte[40960];
|
||||
|
||||
private static Map<String, String> mimes = new HashMap<String, String>();
|
||||
static {
|
||||
mimes.put(".html", "text/html");
|
||||
mimes.put(".htm", "text/html");
|
||||
mimes.put(".js", "text/javascript");
|
||||
mimes.put(".png", "image/png");
|
||||
mimes.put(".css", "text/css");
|
||||
mimes.put(".txt", "text/plain");
|
||||
}
|
||||
|
||||
public static final String getMimeTypeFromExtension(String extension) {
|
||||
String m = mimes.get(extension);
|
||||
if (m != null)
|
||||
return m;
|
||||
return "application/octet-steam";
|
||||
}
|
||||
|
||||
protected abstract InputStream getFileInput(String path);
|
||||
|
||||
protected String getExtension(String path) {
|
||||
int dotindex = path.lastIndexOf('.');
|
||||
if (dotindex > 0)
|
||||
return path.substring(dotindex);
|
||||
return null;
|
||||
}
|
||||
|
||||
protected final String formatPath(String path) {
|
||||
int qmark = path.indexOf('?');
|
||||
if (qmark >= 0)
|
||||
path = path.substring(0, qmark);
|
||||
|
||||
if (path.startsWith("/") || path.startsWith("."))
|
||||
return null;
|
||||
if (path.length() == 0)
|
||||
path = getDefaultFilename(path);
|
||||
return path;
|
||||
}
|
||||
|
||||
protected String getDefaultFilename(String path) {
|
||||
return path + "index.html";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(String path, HttpRequest request, HttpResponse response) throws IOException {
|
||||
path = formatPath(path);
|
||||
InputStream fileInput = getFileInput(path);
|
||||
if (fileInput == null) {
|
||||
response.statusCode = 404;
|
||||
response.statusMessage = "Not found";
|
||||
return;
|
||||
}
|
||||
|
||||
String extension = getExtension(path);
|
||||
|
||||
response.fields.put("Content-Type", getMimeTypeFromExtension(extension));
|
||||
response.fields.put("Connection", "close");
|
||||
OutputStream out = response.getBody();
|
||||
try {
|
||||
int readBytes;
|
||||
while ((readBytes = fileInput.read(readBuffer)) > 0) {
|
||||
out.write(readBuffer, 0, readBytes);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
fileInput.close();
|
||||
throw e;
|
||||
}
|
||||
fileInput.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user