PageRenderTime 2643ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/dynmap/web/handlers/SendMessageHandler.java

http://github.com/webbukkit/dynmap
Java | 119 lines | 102 code | 14 blank | 3 comment | 15 complexity | 43f1ad15c757b4fb6bd08ac7a12887f3 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.dynmap.web.handlers;
  2. import java.io.InputStreamReader;
  3. import java.nio.charset.Charset;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.logging.Logger;
  7. import org.dynmap.Event;
  8. import org.dynmap.web.HttpField;
  9. import org.dynmap.web.HttpHandler;
  10. import org.dynmap.web.HttpMethod;
  11. import org.dynmap.web.HttpRequest;
  12. import org.dynmap.web.HttpResponse;
  13. import org.dynmap.web.HttpStatus;
  14. import org.json.simple.JSONObject;
  15. import org.json.simple.parser.JSONParser;
  16. public class SendMessageHandler implements HttpHandler {
  17. protected static final Logger log = Logger.getLogger("Minecraft");
  18. private static final JSONParser parser = new JSONParser();
  19. public Event<Message> onMessageReceived = new Event<SendMessageHandler.Message>();
  20. private Charset cs_utf8 = Charset.forName("UTF-8");
  21. public int maximumMessageInterval = 1000;
  22. public boolean hideip = false;
  23. public boolean trustclientname = false;
  24. public String spamMessage = "\"You may only chat once every %interval% seconds.\"";
  25. private HashMap<String, WebUser> disallowedUsers = new HashMap<String, WebUser>();
  26. private LinkedList<WebUser> disallowedUserQueue = new LinkedList<WebUser>();
  27. private Object disallowedUsersLock = new Object();
  28. private HashMap<String,String> useralias = new HashMap<String,String>();
  29. private int aliasindex = 1;
  30. @Override
  31. public void handle(String path, HttpRequest request, HttpResponse response) throws Exception {
  32. if (!request.method.equals(HttpMethod.Post)) {
  33. response.status = HttpStatus.MethodNotAllowed;
  34. response.fields.put(HttpField.Accept, HttpMethod.Post);
  35. return;
  36. }
  37. InputStreamReader reader = new InputStreamReader(request.body, cs_utf8);
  38. JSONObject o = (JSONObject)parser.parse(reader);
  39. final Message message = new Message();
  40. if(trustclientname) {
  41. message.name = String.valueOf(o.get("name"));
  42. }
  43. else {
  44. /* If proxied client address, get original */
  45. if(request.fields.containsKey("X-Forwarded-For"))
  46. message.name = request.fields.get("X-Forwarded-For");
  47. /* If from loopback, we're probably getting from proxy - need to trust client */
  48. else if(request.rmtaddr.getAddress().isLoopbackAddress())
  49. message.name = String.valueOf(o.get("name"));
  50. else
  51. message.name = request.rmtaddr.getAddress().getHostAddress();
  52. }
  53. if(hideip) { /* If hiding IP, find or assign alias */
  54. synchronized(disallowedUsersLock) {
  55. String n = useralias.get(message.name);
  56. if(n == null) { /* Make ID */
  57. n = String.format("web-%03d", aliasindex);
  58. aliasindex++;
  59. useralias.put(message.name, n);
  60. }
  61. message.name = n;
  62. }
  63. }
  64. message.message = String.valueOf(o.get("message"));
  65. final long now = System.currentTimeMillis();
  66. synchronized(disallowedUsersLock) {
  67. // Allow users that user that are now allowed to send messages.
  68. while (!disallowedUserQueue.isEmpty()) {
  69. WebUser wu = disallowedUserQueue.getFirst();
  70. if (now >= wu.nextMessageTime) {
  71. disallowedUserQueue.remove();
  72. disallowedUsers.remove(wu.name);
  73. } else {
  74. break;
  75. }
  76. }
  77. WebUser user = disallowedUsers.get(message.name);
  78. if (user == null) {
  79. user = new WebUser() {{
  80. name = message.name;
  81. nextMessageTime = now+maximumMessageInterval;
  82. }};
  83. disallowedUsers.put(user.name, user);
  84. disallowedUserQueue.add(user);
  85. } else {
  86. response.fields.put("Content-Length", "0");
  87. response.status = HttpStatus.Forbidden;
  88. response.getBody();
  89. return;
  90. }
  91. }
  92. onMessageReceived.trigger(message);
  93. response.fields.put(HttpField.ContentLength, "0");
  94. response.status = HttpStatus.OK;
  95. response.getBody();
  96. }
  97. public static class Message {
  98. public String name;
  99. public String message;
  100. }
  101. public static class WebUser {
  102. public long nextMessageTime;
  103. public String name;
  104. }
  105. }