@Override public void handle(Void aVoid) { if (!finished.compareAndSet(false, true)) return; VertxHttpServerAdapter.setCurrentMethodAndPath( context.request().rawMethod(), context.currentRoute().getPath() ); try { serverHandler.handleSend(context.response(), context.failure(), span); } finally { VertxHttpServerAdapter.setCurrentMethodAndPath(null, null); } } }
private String getAndCheckRoutePath(RoutingContext ctx) { Route currentRoute = ctx.currentRoute(); String path = currentRoute.getPath(); if (path == null) { throw new IllegalStateException("Sub routers must be mounted on constant paths (no regex or patterns)"); } return path; }
public static String pathOffset(String path, RoutingContext context) { int prefixLen = 0; String mountPoint = context.mountPoint(); if (mountPoint != null) { prefixLen = mountPoint.length(); } String routePath = context.currentRoute().getPath(); if (routePath != null) { prefixLen += routePath.length(); // special case we need to verify if a trailing slash is present and exclude if (routePath.charAt(routePath.length() - 1) == '/') { prefixLen--; } } return prefixLen != 0 ? path.substring(prefixLen) : path; }
@Override public void handle(RoutingContext context) { Route currentRoute = context.currentRoute(); context.vertx().executeBlocking(fut -> { decoratedHandler.handle(new RoutingContextDecorator(currentRoute, context)); fut.complete(); }, ordered, res -> { if (res.failed()) { // This means an exception was thrown from the blocking handler context.fail(res.cause()); } }); }
@Test public void testDefaultIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/index"), any()); }
@Test public void testSimpleTemplate() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/about"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/about"), any()); }
@Test public void testSetIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.setIndexTemplate("home"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/home"), any()); }
@Test public void testTurnOffIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.setIndexTemplate(null); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/"), any()); }
@Override public void start(Future<Void> startFuture) throws Exception { Router router = Router.router(vertx); router.get("/test").handler(routingCount -> vertx.setTimer(5000, timerId -> { HttpServerResponse response = routingCount.response(); if (routingCount.currentRoute() == null) { response.setStatusCode(HttpURLConnection.HTTP_INTERNAL_ERROR) .end(); } else { response.setStatusCode(HttpURLConnection.HTTP_NO_CONTENT) .end(); } })); vertx.createHttpServer() .requestHandler(router) .listen(PORT, asyncResult -> { if (asyncResult.succeeded()) { startFuture.complete(); } else { startFuture.fail(asyncResult.cause()); } }); } }
@Override public Route currentRoute() { return delegate.currentRoute(); }
@Test public void testBlockingHandlerFailureThrowException() throws Exception { List<Thread> threads = new ArrayList<>(); List<Context> contexts = new ArrayList<>(); router.route().handler(rc -> { threads.add(Thread.currentThread()); contexts.add(rc.vertx().getOrCreateContext()); rc.next(); }); router.route().blockingHandler(rc -> { assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); throw new RuntimeException("foo"); }); router.route().failureHandler(rc -> { assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); Throwable t = rc.failure(); assertNotNull(t); assertTrue(t instanceof RuntimeException); assertEquals("foo", t.getMessage()); rc.response().setStatusCode(500).end(); }); testRequest(HttpMethod.GET, "/", 500, "Internal Server Error"); }
@Test public void testBlockingHandlerFailure() throws Exception { List<Thread> threads = new ArrayList<>(); List<Context> contexts = new ArrayList<>(); router.route().handler(rc -> { threads.add(Thread.currentThread()); contexts.add(rc.vertx().getOrCreateContext()); rc.response().setChunked(true); rc.next(); }); router.route().blockingHandler(rc -> { assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.fail(501); }); router.route().failureHandler(rc -> { assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().setStatusCode(rc.statusCode()).end(); }); testRequest(HttpMethod.GET, "/", 501, "Not Implemented"); }
threads.add(Thread.currentThread()); contexts.add(rc.vertx().getOrCreateContext()); assertTrue(rc.currentRoute() != null); rc.response().setChunked(true); rc.response().write("A"); assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("B"); rc.next(); assertTrue(!threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("C"); rc.next(); assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("D"); rc.next(); assertTrue(threads.get(0).equals(Thread.currentThread())); assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext())); assertTrue(rc.currentRoute()!=null); rc.response().write("E"); rc.response().end();
private String getAndCheckRoutePath(RoutingContext ctx) { Route currentRoute = ctx.currentRoute(); String path = currentRoute.getPath(); if (path == null) { throw new IllegalStateException("Sub routers must be mounted on constant paths (no regex or patterns)"); } return path; }
/** * @return the current route this context is being routed through. */ public io.vertx.rxjava.ext.web.Route currentRoute() { io.vertx.rxjava.ext.web.Route ret = io.vertx.rxjava.ext.web.Route.newInstance(delegate.currentRoute()); return ret; }
/** * @return the current route this context is being routed through. */ public io.vertx.rxjava.ext.web.Route currentRoute() { io.vertx.rxjava.ext.web.Route ret = io.vertx.rxjava.ext.web.Route.newInstance(delegate.currentRoute()); return ret; }
@Override public void handle(RoutingContext context) { Route currentRoute = context.currentRoute(); context.vertx().executeBlocking(fut -> { decoratedHandler.handle(new RoutingContextDecorator(currentRoute, context)); fut.complete(); }, ordered, res -> { if (res.failed()) { // This means an exception was thrown from the blocking handler context.fail(res.cause()); } }); }
@Test public void testDefaultIndex() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/index"), any()); }
@Test public void testSimpleTemplate() { TemplateEngine templateEngine = mock(TemplateEngine.class); RoutingContext routingContext = mock(RoutingContext.class); when(routingContext.normalisedPath()).thenReturn("/about"); Route currentRoute = mock(Route.class); when(currentRoute.getPath()).thenReturn("/"); when(routingContext.currentRoute()).thenReturn(currentRoute); TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext"); templateHandler.handle(routingContext); verify(templateEngine).render(any(JsonObject.class), eq("templates/about"), any()); }
assertEquals("blah2", rc.get("key2")); assertEquals("blah3", rc.get("key3")); rc.response().setStatusMessage(rc.currentRoute().getPath()).end(); });