super.start();
address = getOptionalStringConfig("address", Topic.STORE);
JsonObject rest = getOptionalObjectConfig("rest", new JsonObject());
String path = rest.getString("prefix", "/store");
HttpServer server = vertx.createHttpServer().setCompressionSupported(true);
RouteMatcher matcher = new RouteMatcher();
String snapshot = path + "/:docType/:docId";
String ops = path + "/:docType/:docId" + Topic.OPS;
Handler<HttpServerRequest> handler = new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest req) {
JsonObject message = parseRequest(req);
eb.sendWithTimeout(address, message, StoreModule.REPLY_TIMEOUT,
new Handler<AsyncResult<Message<Object>>>() {
@Override
public void handle(AsyncResult<Message<Object>> ar) {
if (ar.failed()) {
req.response().setStatusCode(500).setStatusMessage(ar.cause().getMessage()).end();
return;
}
Object body = ar.result().body();
if (body == null) {
req.response().setStatusCode(404).end();
return;
}
if ("HEAD".equals(req.method())) {
req.response().end(body.toString());
} else {
req.response().headers().set("Content-Type", "application/json");
req.response().end(((JsonObject) body).encode());
}
}
});
}
};
matcher.get(snapshot, handler);
matcher.head(snapshot, handler);
matcher.post(snapshot, new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest req) {
final JsonObject message = parseRequest(req);
req.bodyHandler(new Handler<Buffer>() {
@Override
public void handle(Buffer body) {
message.putObject(Key.OP_DATA, new JsonObject(body.toString()));
eb.sendWithTimeout(address, message, StoreModule.REPLY_TIMEOUT,
new Handler<AsyncResult<Message<JsonObject>>>() {
@Override
public void handle(AsyncResult<Message<JsonObject>> ar) {
if (ar.failed()) {
req.response().setStatusCode(500).setStatusMessage(ar.cause().getMessage())
.end();
return;
}
JsonObject body = ar.result().body();
req.response().headers().set("Content-Type", "application/json");
req.response().end(body.encode());
}
});
}
});
}
});
matcher.get(ops, new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest req) {
final JsonObject message = parseRequest(req);
Long from = null;
Long to = null;