@Override public void start() throws Exception { // To simplify the development of the web components we use a Router to route all HTTP requests // to organize our code in a reusable way. final Router router = Router.router(vertx); // In order to use a Thymeleaf template we first need to create an engine final ThymeleafTemplateEngine engine = ThymeleafTemplateEngine.create(vertx); // Entry point to the application, this will render a custom JADE template. router.get().handler(ctx -> { // we define a hardcoded title for our application JsonObject data = new JsonObject() .put("welcome", "Hi there!"); // and now delegate to the engine to render it. engine.render(data, "templates/index.html", res -> { if (res.succeeded()) { ctx.response().end(res.result()); } else { ctx.fail(res.cause()); } }); }); // start a HTTP web server on port 8080 vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Override public void start() throws Exception { // To simplify the development of the web components we use a Router to route all HTTP requests // to organize our code in a reusable way. final Router router = Router.router(vertx); // In order to use a template we first need to create an engine final JadeTemplateEngine engine = JadeTemplateEngine.create(vertx); // Entry point to the application, this will render a custom template. router.get().handler(ctx -> { // we define a hardcoded title for our application JsonObject data = new JsonObject() .put("name", "Vert.x Web"); // and now delegate to the engine to render it. engine.render(data, "templates/index.jade", res -> { if (res.succeeded()) { ctx.response().end(res.result()); } else { ctx.fail(res.cause()); } }); }); // start a HTTP web server on port 8080 vertx.createHttpServer().requestHandler(router).listen(8080); } }
ctx.response().end(res.result()); } else { ctx.fail(res.cause());
ctx.response().end(res.result()); } else { ctx.fail(res.cause());
ctx.response().end(res.result()); } else { ctx.fail(res.cause());
ctx.fail(500); return; ctx.fail(500); return; ctx.fail(404); } else { ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); ctx.fail(500); return; ctx.fail(500); } else { mongo.insert("users", newUser, insert -> { ctx.fail(500); return; ctx.fail(500); return; ctx.fail(404); } else { ctx.fail(500); return;
routingContext.fail(res.cause()); } else { SQLConnection conn = res.result();
protected void forbidden(RoutingContext ctx) { final int statusCode = 403; if (responseBody != null) { ctx.response() .setStatusCode(statusCode) .end(responseBody); } else { ctx.fail(statusCode); } }
@Override public void fail(Throwable throwable) { // make sure the fail handler run on the correct context vertx().runOnContext(future -> decoratedContext.fail(throwable)); }
@Override public void fail(int statusCode) { // make sure the fail handler run on the correct context vertx().runOnContext(future -> decoratedContext.fail(statusCode)); }
@Override public void handle(RoutingContext ctx) { // We send a error response after timeout long tid = ctx.vertx().setTimer(timeout, t -> ctx.fail(errorCode)); ctx.addBodyEndHandler(v -> ctx.vertx().cancelTimer(tid)); ctx.next(); } }
@Test public void testDefaultFailureHandlerCallFail() throws Exception { String path = "/blah"; router.route(path).handler(rc -> rc.fail(400)); // Default failure response testRequest(HttpMethod.GET, path, 400, "Bad Request"); }
@Test public void testCustomErrorHandler() throws Exception { router.route("/path").handler(rc -> rc.fail(410)); router.errorHandler(410, context -> context.response().setStatusCode(500).setStatusMessage("Dumb").end()); testRequest(HttpMethod.GET, "/path", 500, "Dumb"); }
@Test public void testFailureHandler2CallFail() throws Exception { String path = "/blah"; router.route(path).handler(rc -> rc.fail(400)); router.route("/bl*").failureHandler(frc -> { assertEquals(400, frc.statusCode()); frc.response().setStatusCode(400).setStatusMessage("oh dear").end(); }); testRequest(HttpMethod.GET, path, 400, "oh dear"); }
@Test public void testFailWithStatusCodeDefaultContentType() throws Exception { int statusCode = 404; String statusMessage = "Not Found"; router.route().handler(rc -> rc.fail(statusCode)); testRequest(HttpMethod.GET, "/", null, resp -> resp.bodyHandler(buff -> { checkTextResponse(buff, resp, statusCode, statusMessage); testComplete(); }), statusCode, statusMessage, null); await(); }
@Test public void testSessionCookieAttack() throws Exception { router.route().handler(CookieHandler.create()); router.route().handler(SessionHandler.create(store)); // faking that there was some auth error router.route().handler(rc -> rc.fail(401)); testRequest(HttpMethod.GET, "/", null, resp -> assertNull(resp.headers().get("set-cookie")), 401, "Unauthorized", null); }