|
@@ -1,14 +1,13 @@
|
|
import java.nio.charset.*;
|
|
import java.nio.charset.*;
|
|
-
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Collections;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Random;
|
|
|
|
|
|
+import java.text.DateFormat;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.*;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
import org.vertx.java.core.Handler;
|
|
import org.vertx.java.core.Handler;
|
|
import org.vertx.java.core.buffer.Buffer;
|
|
import org.vertx.java.core.buffer.Buffer;
|
|
import org.vertx.java.core.eventbus.Message;
|
|
import org.vertx.java.core.eventbus.Message;
|
|
import org.vertx.java.core.http.HttpServerRequest;
|
|
import org.vertx.java.core.http.HttpServerRequest;
|
|
|
|
+import org.vertx.java.core.http.HttpServerResponse;
|
|
import org.vertx.java.core.json.JsonObject;
|
|
import org.vertx.java.core.json.JsonObject;
|
|
import org.vertx.java.core.json.impl.Json;
|
|
import org.vertx.java.core.json.impl.Json;
|
|
import org.vertx.java.platform.Verticle;
|
|
import org.vertx.java.platform.Verticle;
|
|
@@ -18,44 +17,63 @@ public class WebServer extends Verticle implements Handler<HttpServerRequest> {
|
|
private static String helloWorld = "Hello, World!";
|
|
private static String helloWorld = "Hello, World!";
|
|
private static Buffer helloWorldBuffer = new Buffer(helloWorld);
|
|
private static Buffer helloWorldBuffer = new Buffer(helloWorld);
|
|
private static String helloWorldContentLength = String.valueOf(helloWorldBuffer.length());
|
|
private static String helloWorldContentLength = String.valueOf(helloWorldBuffer.length());
|
|
|
|
+ private static DateFormat DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyyy HH:mm:ss z");
|
|
|
|
+
|
|
|
|
+ private String dateString;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void start() {
|
|
public void start() {
|
|
vertx.createHttpServer().requestHandler(WebServer.this).listen(8080);
|
|
vertx.createHttpServer().requestHandler(WebServer.this).listen(8080);
|
|
|
|
+ vertx.setPeriodic(1000, new Handler<Long>() {
|
|
|
|
+ @Override
|
|
|
|
+ public void handle(Long timerID) {
|
|
|
|
+ formatDate();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ formatDate();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void formatDate() {
|
|
|
|
+ dateString = DATE_FORMAT.format(new Date());
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void handle(HttpServerRequest req) {
|
|
public void handle(HttpServerRequest req) {
|
|
String path = req.path();
|
|
String path = req.path();
|
|
- switch (path.charAt(1)) {
|
|
|
|
- case 'p':
|
|
|
|
|
|
+ switch (path) {
|
|
|
|
+ case "/plaintext":
|
|
handlePlainText(req);
|
|
handlePlainText(req);
|
|
break;
|
|
break;
|
|
- case 'j':
|
|
|
|
|
|
+ case "/json":
|
|
handleJson(req);
|
|
handleJson(req);
|
|
break;
|
|
break;
|
|
- case 'd':
|
|
|
|
|
|
+ case "/db":
|
|
handleDbMongo(req);
|
|
handleDbMongo(req);
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
req.response().setStatusCode(404);
|
|
req.response().setStatusCode(404);
|
|
req.response().end();
|
|
req.response().end();
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private void handlePlainText(HttpServerRequest req) {
|
|
private void handlePlainText(HttpServerRequest req) {
|
|
- req.response().putHeader("Content-Type", "text/plain; charset=UTF-8");
|
|
|
|
- req.response().putHeader("Content-Length", helloWorldContentLength);
|
|
|
|
- req.response().end(helloWorldBuffer);
|
|
|
|
|
|
+ HttpServerResponse resp = req.response();
|
|
|
|
+ resp.putHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
+ resp.putHeader("Content-Length", helloWorldContentLength);
|
|
|
|
+ resp.putHeader("Server", "vert.x");
|
|
|
|
+ resp.putHeader("Date", dateString);
|
|
|
|
+ resp.end(helloWorldBuffer);
|
|
}
|
|
}
|
|
|
|
|
|
private void handleJson(HttpServerRequest req) {
|
|
private void handleJson(HttpServerRequest req) {
|
|
|
|
+ HttpServerResponse resp = req.response();
|
|
String result = Json.encode(Collections.singletonMap("message", "Hello, world!"));
|
|
String result = Json.encode(Collections.singletonMap("message", "Hello, world!"));
|
|
int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
|
|
int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
|
|
- req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
- req.response().putHeader("Content-Length", String.valueOf(contentLength));
|
|
|
|
- req.response().end(result);
|
|
|
|
|
|
+ resp.putHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
+ resp.putHeader("Content-Length", String.valueOf(contentLength));
|
|
|
|
+ resp.putHeader("Server", "vert.x");
|
|
|
|
+ resp.putHeader("Date", dateString);
|
|
|
|
+ resp.end(result);
|
|
}
|
|
}
|
|
|
|
|
|
private void handleDbMongo(final HttpServerRequest req) {
|
|
private void handleDbMongo(final HttpServerRequest req) {
|
|
@@ -63,7 +81,7 @@ public class WebServer extends Verticle implements Handler<HttpServerRequest> {
|
|
try {
|
|
try {
|
|
queriesParam = Integer.parseInt(req.params().get("queries"));
|
|
queriesParam = Integer.parseInt(req.params().get("queries"));
|
|
} catch (NumberFormatException e) {
|
|
} catch (NumberFormatException e) {
|
|
- // do nothing
|
|
|
|
|
|
+ e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
final MongoHandler dbh = new MongoHandler(req, queriesParam);
|
|
final MongoHandler dbh = new MongoHandler(req, queriesParam);
|
|
@@ -80,29 +98,33 @@ public class WebServer extends Verticle implements Handler<HttpServerRequest> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private static class MongoHandler implements Handler<Message<JsonObject>> {
|
|
|
|
|
|
+ private class MongoHandler implements Handler<Message<JsonObject>> {
|
|
private final HttpServerRequest req;
|
|
private final HttpServerRequest req;
|
|
private final int queries;
|
|
private final int queries;
|
|
- private final List<Object> worlds = new ArrayList<>();
|
|
|
|
|
|
+ private final List<Object> worlds;
|
|
|
|
|
|
public MongoHandler(HttpServerRequest request, int queriesParam) {
|
|
public MongoHandler(HttpServerRequest request, int queriesParam) {
|
|
- this.req = request;
|
|
|
|
- this.queries = queriesParam;
|
|
|
|
|
|
+ req = request;
|
|
|
|
+ queries = queriesParam;
|
|
|
|
+ worlds = new ArrayList<>(queriesParam);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void handle(Message<JsonObject> reply) {
|
|
public void handle(Message<JsonObject> reply) {
|
|
- final JsonObject body = reply.body();
|
|
|
|
|
|
+ JsonObject body = reply.body();
|
|
|
|
|
|
if ("ok".equals(body.getString("status"))) {
|
|
if ("ok".equals(body.getString("status"))) {
|
|
- this.worlds.add(body.getObject("result"));
|
|
|
|
- if (this.worlds.size() == this.queries) {
|
|
|
|
|
|
+ worlds.add(body.getObject("result"));
|
|
|
|
+ if (worlds.size() == this.queries) {
|
|
// All queries have completed; send the response.
|
|
// All queries have completed; send the response.
|
|
- final String result = Json.encode(worlds);
|
|
|
|
- final int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
|
|
|
|
- this.req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
- this.req.response().putHeader("Content-Length", String.valueOf(contentLength));
|
|
|
|
- this.req.response().end(result);
|
|
|
|
|
|
+ String result = Json.encode(worlds);
|
|
|
|
+ int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
|
|
|
|
+ HttpServerResponse resp = req.response();
|
|
|
|
+ resp.putHeader("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
+ resp.putHeader("Content-Length", String.valueOf(contentLength));
|
|
|
|
+ resp.putHeader("Server", "vert.x");
|
|
|
|
+ resp.putHeader("Date", dateString);
|
|
|
|
+ resp.end(result);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
System.err.println("Failed to execute query");
|
|
System.err.println("Failed to execute query");
|