WebServer.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import java.io.IOException;
  2. import java.nio.charset.*;
  3. import java.util.List;
  4. import java.util.Random;
  5. import java.util.concurrent.CopyOnWriteArrayList;
  6. import java.util.concurrent.ThreadLocalRandom;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8. import org.codehaus.jackson.map.ObjectMapper;
  9. import org.vertx.java.core.Handler;
  10. import org.vertx.java.core.eventbus.Message;
  11. import org.vertx.java.core.http.HttpServerRequest;
  12. import org.vertx.java.core.json.JsonArray;
  13. import org.vertx.java.core.json.JsonObject;
  14. import org.vertx.java.deploy.Verticle;
  15. public class WebServer
  16. extends Verticle
  17. implements Handler<HttpServerRequest>
  18. {
  19. private final ObjectMapper mapper = new ObjectMapper();
  20. @Override
  21. public void start() throws Exception
  22. {
  23. this.getVertx().createHttpServer().requestHandler(this).listen(8080);
  24. }
  25. @Override
  26. public void handle(HttpServerRequest req)
  27. {
  28. if (req.path.equals("/json"))
  29. {
  30. handleJson(req);
  31. }
  32. else if (req.path.equals("/db"))
  33. {
  34. handleDb(req);
  35. }
  36. else
  37. {
  38. req.response.statusCode = 404;
  39. req.response.end();
  40. }
  41. }
  42. public static class HelloMessage
  43. {
  44. public final String message = "Hello, world";
  45. }
  46. private void handleJson(HttpServerRequest req)
  47. {
  48. String result;
  49. try
  50. {
  51. result = mapper.writeValueAsString(new HelloMessage());
  52. }
  53. catch (IOException e)
  54. {
  55. req.response.statusCode = 500;
  56. req.response.end();
  57. return;
  58. }
  59. int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
  60. req.response.putHeader("Content-Type", "application/json; charset=UTF-8");
  61. req.response.putHeader("Content-Length", contentLength);
  62. req.response.write(result);
  63. req.response.end();
  64. }
  65. private void handleDb(final HttpServerRequest req)
  66. {
  67. int queriesParam = 1;
  68. try
  69. {
  70. queriesParam = Integer.parseInt(req.params().get("queries"));
  71. }
  72. catch (NumberFormatException e)
  73. {
  74. // do nothing
  75. }
  76. final DbHandler dbh = new DbHandler(req, queriesParam);
  77. final Random random = ThreadLocalRandom.current();
  78. for (int i = 0; i < queriesParam; i++)
  79. {
  80. this.getVertx().eventBus().send(
  81. "hello.persistor",
  82. new JsonObject()
  83. .putString("action", "findone")
  84. .putString("collection", "world")
  85. .putObject("matcher", new JsonObject().putNumber("id", (random.nextInt(10000) + 1))),
  86. dbh);
  87. }
  88. }
  89. class DbHandler implements Handler<Message<JsonObject>>
  90. {
  91. private final HttpServerRequest req;
  92. private final int queries;
  93. private final List<Object> worlds = new CopyOnWriteArrayList<>();
  94. public DbHandler(HttpServerRequest request, int queriesParam)
  95. {
  96. this.req = request;
  97. this.queries = queriesParam;
  98. }
  99. @Override
  100. public void handle(Message<JsonObject> reply)
  101. {
  102. final JsonObject body = reply.body;
  103. if ("ok".equals(body.getString("status")))
  104. {
  105. this.worlds.add(body.getObject("result"));
  106. }
  107. if (this.worlds.size() == this.queries)
  108. {
  109. // All queries have completed; send the response.
  110. // final JsonArray arr = new JsonArray(worlds);
  111. try
  112. {
  113. final String result = mapper.writeValueAsString(worlds);
  114. final int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
  115. this.req.response.putHeader("Content-Type", "application/json; charset=UTF-8");
  116. this.req.response.putHeader("Content-Length", contentLength);
  117. this.req.response.write(result);
  118. this.req.response.end();
  119. }
  120. catch (IOException e)
  121. {
  122. req.response.statusCode = 500;
  123. req.response.end();
  124. }
  125. }
  126. }
  127. }
  128. }