package dk.thoerup.bukkit.hoeruputils.web; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.text.SimpleDateFormat; import java.util.Date; import org.bukkit.OfflinePlayer; import org.bukkit.Server; import org.bukkit.plugin.Plugin; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; @SuppressWarnings("restriction") public class BanListener implements HttpHandler { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Server server; long cachedTime = 0; String cachedText = ""; public BanListener(Plugin plugin, Server server) { this.server = server; } String formatTime(String input) { return formatTime( Long.parseLong(input) ); } String formatTime(long input) { Date d = new Date( input ); return format.format(d ); } @Override public void handle(HttpExchange http) throws IOException { String text; synchronized(this) { //is this necessary ? long now = System.currentTimeMillis(); if ( (now-cachedTime) > 5000) { text = buildText(); cachedText = text; cachedTime = now; } else { text = cachedText; } } byte bytes[] = text.getBytes(); http.getResponseHeaders().add("Content-Type", "text/plain"); http.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length ); final OutputStream os = http.getResponseBody(); os.write(bytes); os.close(); http.close(); } private String buildText() { StringBuilder sb = new StringBuilder(); sb.append("

Banned Players

"); sb.append("\n"); sb.append("

Banned IPs

"); sb.append("\n"); return sb.toString(); } }