WebServer.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import java.nio.charset.*;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.concurrent.ThreadLocalRandom;
  7. import org.vertx.java.core.Handler;
  8. import org.vertx.java.core.buffer.Buffer;
  9. import org.vertx.java.core.eventbus.Message;
  10. import org.vertx.java.core.http.HttpServerRequest;
  11. import org.vertx.java.core.json.JsonObject;
  12. import org.vertx.java.core.json.impl.Json;
  13. import org.vertx.java.platform.Verticle;
  14. public class WebServer extends Verticle implements Handler<HttpServerRequest> {
  15. private static String helloWorld = "Hello, World!";
  16. private static Buffer helloWorldBuffer = new Buffer(helloWorld);
  17. private static String helloWorldContentLength = String.valueOf(helloWorldBuffer.length());
  18. @Override
  19. public void start() {
  20. vertx.createHttpServer().requestHandler(WebServer.this).listen(8080);
  21. }
  22. @Override
  23. public void handle(HttpServerRequest req) {
  24. String path = req.path();
  25. switch (path.charAt(1)) {
  26. case 'p':
  27. handlePlainText(req);
  28. break;
  29. case 'j':
  30. handleJson(req);
  31. break;
  32. case 'd':
  33. handleDbMongo(req);
  34. break;
  35. default:
  36. req.response().setStatusCode(404);
  37. req.response().end();
  38. }
  39. }
  40. private void handlePlainText(HttpServerRequest req) {
  41. req.response().putHeader("Content-Type", "text/plain; charset=UTF-8");
  42. req.response().putHeader("Content-Length", helloWorldContentLength);
  43. req.response().end(helloWorldBuffer);
  44. }
  45. private void handleJson(HttpServerRequest req) {
  46. String result = Json.encode(Collections.singletonMap("message", "Hello, world!"));
  47. int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
  48. req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
  49. req.response().putHeader("Content-Length", String.valueOf(contentLength));
  50. req.response().end(result);
  51. }
  52. private void handleDbMongo(final HttpServerRequest req) {
  53. int queriesParam = 1;
  54. try {
  55. queriesParam = Integer.parseInt(req.params().get("queries"));
  56. } catch (NumberFormatException e) {
  57. // do nothing
  58. }
  59. final MongoHandler dbh = new MongoHandler(req, queriesParam);
  60. final Random random = ThreadLocalRandom.current();
  61. for (int i = 0; i < queriesParam; i++) {
  62. vertx.eventBus().send(
  63. "hello.persistor",
  64. new JsonObject()
  65. .putString("action", "findone")
  66. .putString("collection", "world")
  67. .putObject("matcher", new JsonObject().putNumber("id", (random.nextInt(10000) + 1))),
  68. dbh);
  69. }
  70. }
  71. private static class MongoHandler implements Handler<Message<JsonObject>> {
  72. private final HttpServerRequest req;
  73. private final int queries;
  74. private final List<Object> worlds = new ArrayList<>();
  75. public MongoHandler(HttpServerRequest request, int queriesParam) {
  76. this.req = request;
  77. this.queries = queriesParam;
  78. }
  79. @Override
  80. public void handle(Message<JsonObject> reply) {
  81. final JsonObject body = reply.body();
  82. if ("ok".equals(body.getString("status"))) {
  83. this.worlds.add(body.getObject("result"));
  84. if (this.worlds.size() == this.queries) {
  85. // All queries have completed; send the response.
  86. final String result = Json.encode(worlds);
  87. final int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
  88. this.req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
  89. this.req.response().putHeader("Content-Length", String.valueOf(contentLength));
  90. this.req.response().end(result);
  91. }
  92. } else {
  93. System.err.println("Failed to execute query");
  94. }
  95. }
  96. }
  97. }