/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/web/BanListener.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/web/BanListener.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1938 - (show annotations) (download)
Wed Mar 20 15:10:54 2013 UTC (11 years, 2 months ago) by torben
File size: 1938 byte(s)
another try
1 package dk.thoerup.bukkit.hoeruputils.web;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.HttpURLConnection;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8
9 import org.bukkit.OfflinePlayer;
10 import org.bukkit.Server;
11 import org.bukkit.plugin.Plugin;
12
13 import com.sun.net.httpserver.HttpExchange;
14 import com.sun.net.httpserver.HttpHandler;
15
16
17
18 public class BanListener implements HttpHandler {
19
20 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
21
22 Server server;
23
24 long cachedTime = 0;
25 String cachedText = "";
26
27 public BanListener(Plugin plugin, Server server) {
28
29 this.server = server;
30 }
31
32 String formatTime(String input) {
33 return formatTime( Long.parseLong(input) );
34 }
35 String formatTime(long input) {
36 Date d = new Date( input );
37 return format.format(d );
38 }
39
40
41
42 @Override
43 public void handle(HttpExchange http) throws IOException {
44 /* if(!event.path[0].equalsIgnoreCase("webstatus")) {
45 return;
46 }*/
47
48 String text;
49
50 synchronized(this) { //is this necessary ?
51 long now = System.currentTimeMillis();
52 if ( (now-cachedTime) > 5000) {
53 text = buildText();
54 cachedText = text;
55 cachedTime = now;
56 } else {
57 text = cachedText;
58 }
59 }
60
61 byte bytes[] = text.getBytes();
62 http.sendResponseHeaders(HttpURLConnection.HTTP_OK, bytes.length );
63 final OutputStream os = http.getResponseBody();
64 os.write(bytes);
65 os.close();
66
67 }
68
69 private String buildText() {
70 StringBuilder sb = new StringBuilder();
71
72 sb.append("<h2>Banned Players</h2>");
73 sb.append("<ul>\n");
74 for(OfflinePlayer player : server.getBannedPlayers() ) {
75 sb.append("<li>").append( player.getName() ).append("</li>\n");
76 }
77 sb.append("</ul>\n");
78
79
80 sb.append("<h2>Banned IPs</h2>");
81 sb.append("<ul>\n");
82 for(String ip : server.getIPBans() ) {
83 sb.append("<li>").append( ip ).append("</li>\n");
84 }
85 sb.append("</ul>\n");
86
87
88 return sb.toString();
89
90 }
91
92
93
94 }

  ViewVC Help
Powered by ViewVC 1.1.20