@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); }
@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(); } }
routingContext.put("conn", conn);
@Override public RoutingContext put(String key, Object obj) { inner.put(key, obj); return this; }
@Override public RoutingContext put(String key, Object obj) { return decoratedContext.put(key, obj); }
@Override protected void createInvocation() { super.createInvocation(); ((VertxServerRequestToHttpServletRequest) this.requestEx).getContext() .put(RestConst.REST_INVOCATION_CONTEXT, this.invocation); } }
private Handler<RoutingContext> generateHandler(final int i) { return routingContext -> routingContext.put(Integer.toString(i), i).next(); }
private void onRequest(RoutingContext context) { if (transport == null) { transport = CseContext.getInstance().getTransportManager().findTransport(Const.RESTFUL); } HttpServletRequestEx requestEx = new VertxServerRequestToHttpServletRequest(context); HttpServletResponseEx responseEx = new VertxServerResponseToHttpServletResponse(context.response()); VertxRestInvocation vertxRestInvocation = new VertxRestInvocation(); context.put(RestConst.REST_PRODUCER_INVOCATION, vertxRestInvocation); vertxRestInvocation.invoke(transport, requestEx, responseEx, httpServerFilters); } }
@Override protected void createInvocation() { ReferenceConfig referenceConfig = new ReferenceConfig(); referenceConfig.setMicroserviceVersionRule(microserviceVersionRule); referenceConfig.setTransport(Const.ANY_TRANSPORT); this.invocation = InvocationFactory.forConsumer(referenceConfig, restOperationMeta.getOperationMeta(), null); this.invocation.setSync(false); this.invocation.setEdge(true); this.invocation.getHandlerContext().put(EDGE_INVOCATION_CONTEXT, Vertx.currentContext()); this.invocation.setResponseExecutor(new ReactiveResponseExecutor()); this.routingContext.put(RestConst.REST_INVOCATION_CONTEXT, invocation); } }
@Test public void testRenderDirectly() throws Exception { TemplateEngine engine = new TestEngine(false); router.route().handler(context -> { context.put("foo", "badger"); context.put("bar", "fox"); engine.render(context, "somedir/test-template.html", res -> { if (res.succeeded()) { context.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/html").end(res.result()); } else { context.fail(res.cause()); } }); }); String expected = "<html>\n" + "<body>\n" + "<h1>Test template</h1>\n" + "foo is badger bar is fox<br>\n" + "</body>\n" + "</html>"; testRequest(HttpMethod.GET, "/", 200, "OK", expected); }
@Test public void testMultipleSetHandler() throws Exception { router.get("/path").handler(routingContext -> { routingContext.put("response", "handler1"); routingContext.next(); }).handler(routingContext -> { routingContext.put("response", routingContext.get("response") + "handler2"); routingContext.next(); }).handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.setChunked(true); response.end(routingContext.get("response") + "handler3"); }); testRequest(HttpMethod.GET, "/path", 200, "OK", "handler1handler2handler3"); }
private void testRelativeToRoutePath(String pathPrefix) throws Exception { TemplateEngine engine = new TestEngine(false); router.route().handler(context -> { context.put("foo", "badger"); context.put("bar", "fox"); context.next(); }); Route route = router.route(); if (pathPrefix != null) { route.path(pathPrefix + "*"); } route.handler(TemplateHandler.create(engine, "somedir", "text/html")); String expected = "<html>\n" + "<body>\n" + "<h1>Test template</h1>\n" + "foo is badger bar is fox<br>\n" + "</body>\n" + "</html>"; testRequest(HttpMethod.GET, pathPrefix != null ? pathPrefix + "/test-template.html" : "/test-template.html", 200, "OK", expected); }
@Test public void testMultipleSetHandlerMultipleRouteObject() throws Exception { router.get("/path").handler(routingContext -> { routingContext.put("response", "handler1"); routingContext.next(); }); router.get("/path").handler(routingContext -> { routingContext.put("response", routingContext.get("response") + "handler2"); routingContext.next(); }).handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.setChunked(true); response.end(routingContext.get("response") + "handler3"); }); testRequest(HttpMethod.GET, "/path", 200, "OK", "handler1handler2handler3"); }
@Test public void testGetPutContextData() throws Exception { SomeObject obj = new SomeObject(); router.route().handler(ctx -> { ctx.put("foo", "bar"); ctx.put("blah", obj); ctx.next(); }); router.route().handler(ctx -> { assertEquals("bar", ctx.get("foo")); assertEquals(obj, ctx.get("blah")); ctx.response().end(); }); testRequest(HttpMethod.GET, "/", 200, "OK"); }
@Test public void test_remove_data() throws Exception { router.route().handler(event -> { String foo = event.getBodyAsJson().encode(); event.put("foo", foo); String removedFoo = event.remove("foo"); assertEquals(removedFoo, foo); assertNull(event.get("foo")); event.response().end(); }); testRequest(HttpMethod.POST, "/", req -> { req.setChunked(true); req.write(Buffer.buffer("{ \"foo\": \"bar\" }")); }, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null); }
@Test public void testDisableFlag() { Random random = new Random(); byte[] bytes = new byte[128]; random.nextBytes(bytes); Buffer buffer = Buffer.buffer(bytes); testRoute.produces("application/json").handler(rc -> { rc.put(ResponseContentTypeHandler.DEFAULT_DISABLE_FLAG, true); rc.response().end(buffer); }); client.get(testRoute.getPath(), onSuccess(resp -> { assertNull(contentType(resp)); assertEquals(Integer.valueOf(buffer.length()), contentLength(resp)); resp.bodyHandler(buf -> { assertEquals(buffer, buf); testComplete(); }); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testMultipleSetFailureHandler() throws Exception { router.get("/path").handler(routingContext -> routingContext.fail(500)).failureHandler(routingContext -> { routingContext.put("response", "handler1"); routingContext.next(); }).failureHandler(routingContext -> { routingContext.put("response", routingContext.get("response") + "handler2"); routingContext.next(); }).failureHandler(routingContext -> { HttpServerResponse response = routingContext.response(); response.setChunked(true); response.setStatusMessage("ERROR"); response.setStatusCode(500); response.end(routingContext.get("response") + "handler3"); }); testRequest(HttpMethod.GET, "/path", 500, "ERROR", "handler1handler2handler3"); }
@Override public void handle(RoutingContext context) { HttpServerRequest request = context.request(); if (request.headers().contains(HttpHeaders.UPGRADE, HttpHeaders.WEBSOCKET, true)) { context.next(); return; } // we need to keep state since we can be called again on reroute Boolean handled = context.get(BODY_HANDLED); if (handled == null || !handled) { BHandler handler = new BHandler(context); request.handler(handler); request.endHandler(v -> handler.end()); context.put(BODY_HANDLED, true); } else { // on reroute we need to re-merge the form params if that was desired if (mergeFormAttributes && request.isExpectMultipart()) { request.params().addAll(request.formAttributes()); } context.next(); } }
@Test public void testMultipleSetFailureHandlerCorrectOrder() throws Exception { router.route().failureHandler(routingContext -> { routingContext.put("response", "handler1"); routingContext.next(); }); router.get("/path").handler(routingContext -> routingContext.fail(500)).failureHandler(routingContext -> { routingContext.put("response", routingContext.get("response") + "handler2"); routingContext.next(); }).failureHandler(routingContext -> { HttpServerResponse response = routingContext.response(); response.setChunked(true); response.setStatusMessage("ERROR"); response.setStatusCode(500); response.end(routingContext.get("response") + "handler3"); }); testRequest(HttpMethod.GET, "/path", 500, "ERROR", "handler1handler2handler3"); }