private void manualEncodeVertxMessageBody(ActiveMQBuffer bodyBuffer, Object body, int type) { switch (type) { case VertxConstants.TYPE_BOOLEAN: bodyBuffer.writeBoolean(((Boolean) body)); break; case VertxConstants.TYPE_BUFFER: Buffer buff = (Buffer) body; int len = buff.length(); bodyBuffer.writeInt(len); bodyBuffer.writeBytes(((Buffer) body).getBytes()); break; case VertxConstants.TYPE_BYTEARRAY: byte[] bytes = (byte[]) body; bodyBuffer.writeInt(bytes.length); bodyBuffer.writeBytes(bytes); break; case VertxConstants.TYPE_BYTE: bodyBuffer.writeByte((byte) body); break; case VertxConstants.TYPE_CHARACTER: bodyBuffer.writeChar((Character) body); break; case VertxConstants.TYPE_DOUBLE: bodyBuffer.writeDouble((double) body); break; case VertxConstants.TYPE_FLOAT: bodyBuffer.writeFloat((Float) body); break; case VertxConstants.TYPE_INT: bodyBuffer.writeInt((Integer) body); break; case VertxConstants.TYPE_LONG: bodyBuffer.writeLong((Long) body); break; case VertxConstants.TYPE_SHORT: bodyBuffer.writeShort((Short) body); break; case VertxConstants.TYPE_STRING: case VertxConstants.TYPE_PING: bodyBuffer.writeString((String) body); break; case VertxConstants.TYPE_JSON_OBJECT: bodyBuffer.writeString(((JsonObject) body).encode()); break; case VertxConstants.TYPE_JSON_ARRAY: bodyBuffer.writeString(((JsonArray) body).encode()); break; case VertxConstants.TYPE_REPLY_FAILURE: ReplyException except = (ReplyException) body; bodyBuffer.writeInt(except.failureType().toInt()); bodyBuffer.writeInt(except.failureCode()); bodyBuffer.writeString(except.getMessage()); break; default: throw new IllegalArgumentException("Invalid body type: " + type); } }
public void start() { container.deployVerticle(StoreRepositoryVerticle.class.getName()); container.deployWorkerVerticle(StaticFilesVerticle.class.getName()); RouteMatcher matcher = new RouteMatcher(); matcher.get( "/todos", (httpServerRequest) -> { vertx .eventBus() .send( StoreRepositoryVerticle.GET_ALL, "", (Message<JsonArray> event) -> { httpServerRequest .response() .putHeader("Content-Type", "application/json") .setStatusCode(200) .end(event.body().encodePrettily()); }); }); matcher.post( "/todos", (httpServerRequest) -> { httpServerRequest.bodyHandler( (buffer) -> { String body = buffer.toString(); vertx .eventBus() .sendWithTimeout( StoreRepositoryVerticle.SAVE, new JsonObject(body), 1000, (AsyncResult<Message<JsonObject>> event) -> { HttpServerResponse response = httpServerRequest.response(); if (event.failed()) { ReplyException rx = (ReplyException) event.cause(); response.setStatusCode(500).setStatusMessage(rx.getMessage()).end(); } else { response.setStatusCode(201).end(event.result().body().encodePrettily()); } }); }); }); matcher.put( "/todos/:id", (httpServerRequest) -> { httpServerRequest.bodyHandler( (buffer) -> { String body = buffer.toString(); vertx .eventBus() .sendWithTimeout( StoreRepositoryVerticle.UPDATE, new JsonObject(body), 1000, (AsyncResult<Message<JsonObject>> event) -> { HttpServerResponse response = httpServerRequest.response(); if (event.failed()) { ReplyException rx = (ReplyException) event.cause(); response.setStatusCode(500).setStatusMessage(rx.getMessage()).end(); } else { response.setStatusCode(204).end(); } }); }); }); matcher.delete( "/todos/:id", (httpServerRequest) -> { String todoId = httpServerRequest.params().get("id"); vertx .eventBus() .sendWithTimeout( StoreRepositoryVerticle.DELETE, todoId, 1000, (event) -> { HttpServerResponse response = httpServerRequest.response(); if (event.failed()) { ReplyException rx = (ReplyException) event.cause(); response.setStatusCode(500).setStatusMessage(rx.getMessage()); } else { response.setStatusCode(204); } response.end(); }); }); matcher.getWithRegEx( ".*", (req) -> { String path; if (req.path().equals("/")) { path = "/index.html"; } else { path = req.path(); } container.logger().info("Static file for path " + path); vertx .eventBus() .sendWithTimeout( StaticFilesVerticle.HANDLER_NAME, path, 3000, (AsyncResult<Message<String>> event) -> { if (event.failed()) { ReplyException ex = (ReplyException) event.cause(); req.response().setStatusCode(404).end(ex.getMessage()); return; } req.response().end(event.result().body()); }); }); vertx.createHttpServer().requestHandler(matcher).listen(8888); container.logger().info("TodoMVCVerticle started"); }