@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().blockingHandler(routingContext -> { // Blocking handlers are allowed to block the calling thread // So let's simulate a blocking action or long running operation try { Thread.sleep(5000); } catch (Exception ignore) { } // Now call the next handler routingContext.next(); }, false); router.route().handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end("Hello World!"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Override public void handle(RoutingContext context) { TracingHandler tracingHandler = context.get(TracingHandler.class.getName()); if (tracingHandler != null) { // then we already have a span if (!context.failed()) { // re-routed, so re-attach the end handler context.addHeadersEndHandler(tracingHandler); } context.next(); return; } Span span = serverHandler.handleReceive(extractor, context.request()); TracingHandler handler = new TracingHandler(context, span); context.put(TracingHandler.class.getName(), handler); context.addHeadersEndHandler(handler); try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) { context.next(); } }
@Override public void start() throws Exception { final Router router = Router.router(vertx); // Populate context with data router.route().handler(ctx -> { ctx.put("title", "Vert.x Web Example Using Rocker"); ctx.put("name", "Rocker"); ctx.next(); }); // Render a custom template. // Note: you need a compile-time generator for Rocker to work properly // See the pom.xml for an example router.route().handler(TemplateHandler.create(RockerTemplateEngine.create())); vertx.createHttpServer().requestHandler(router).listen(8080); } }
@Override public void start() { Router router = Router.router(vertx); // Serve the dynamic pages router.route("/dynamic/*") .handler(ctx -> { // put the context into the template render context ctx.put("context", ctx); ctx.next(); }) .handler(TemplateHandler.create(MVELTemplateEngine.create(vertx))); // Serve the static pages router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); }
ctx.next(); });
routingContext.next();
routingContext.next(); });
ctx.fail(403); } else { ctx.next();
@Override public void next() { if (!super.iterateNext()) { // We didn't route request to anything so go to parent inner.next(); } }
private void authorizeUser(RoutingContext ctx, User user) { authorize(user, authZ -> { if (authZ.failed()) { processException(ctx, authZ.cause()); return; } // success, allowed to continue ctx.next(); }); }
@Override public void handle(RoutingContext ctx) { long start = System.nanoTime(); ctx.addHeadersEndHandler(v -> { long duration = MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS); ctx.response().putHeader(HEADER_NAME, duration + "ms"); }); ctx.next(); } }
@Override public void next() { // make sure the next handler run on the correct context vertx().runOnContext(future -> decoratedContext.next()); }
private void log(RoutingContext routingContext) { vertx.eventBus().publish(Key.SET_HISTORY_TO_REDIS, null); routingContext.next(); }
@Override public void handle(RoutingContext context) { AccessLogParam<RoutingContext> accessLogParam = getRoutingContextAccessLogParam(context); context.response().endHandler(event -> LOGGER.info(accessLogGenerator.generateLog(accessLogParam))); context.next(); }
@Override public void handle(RoutingContext ctx) { if (icon == null) { init(ctx.vertx()); } if ("/favicon.ico".equals(ctx.request().path())) { ctx.response().headers().addAll(icon.headers); ctx.response().end(icon.body); } else { ctx.next(); } } }
@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(); } }
@Override public void setUp() throws Exception { // Use two servers so we test with HTTP request/response with load balanced SockJSSession access numServers = 2; preSockJSHandlerSetup = router -> { router.route().handler(BodyHandler.create()); // simulate an async handler router.route().handler(rtx -> rtx.vertx().executeBlocking(f -> f.complete(true), r -> rtx.next())); }; super.setUp(); }
@Test public void testDecodingErrorCustomHandler() throws Exception { String BAD_PARAM = "~!@\\||$%^&*()_=-%22;;%27%22:%3C%3E/?]}{"; router.errorHandler(400, context -> context.response().setStatusCode(500).setStatusMessage("Dumb").end()); router.route().handler(rc -> rc.next()); router.route("/path").handler(rc -> rc.response().setStatusCode(500).end()); testRequest(HttpMethod.GET, "/path?q=" + BAD_PARAM, 500,"Dumb"); }
@Test public void testNextAfterResponseEnded() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { rc.response().end(); rc.next(); // Call next }); router.route(path).handler(rc -> assertTrue(rc.response().ended())); testRequest(HttpMethod.GET, path, 200, "OK"); }
@Test public void testRouteNoPath2() throws Exception { router.route().handler(rc -> { rc.response().setStatusMessage(rc.request().path()); rc.next(); }); router.route().handler(rc -> rc.response().setStatusCode(200).end()); for (HttpMethod meth : METHODS) { testNoPath(meth); } }