@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); } }
@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 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()); }
/** * Set the index template * @param indexTemplate the index template * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.TemplateHandler setIndexTemplate(String indexTemplate) { delegate.setIndexTemplate(indexTemplate); return this; }
@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()); }
@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()); }
/** * Set the index template * @param indexTemplate the index template * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.TemplateHandler setIndexTemplate(String indexTemplate) { delegate.setIndexTemplate(indexTemplate); return this; }
@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); }
@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()); }
/** * Something has happened, so handle it. * @param event the event to handle */ public void handle(io.vertx.rxjava.ext.web.RoutingContext event) { delegate.handle(event.getDelegate()); }
@Test public void testTemplateEngineFail() throws Exception { TemplateEngine engine = new TestEngine(true); router.route().handler(TemplateHandler.create(engine, "somedir", "text/html")); router.exceptionHandler(t -> { assertEquals("eek", t.getMessage()); testComplete(); }); testRequest(HttpMethod.GET, "/foo.html", 500, "Internal Server Error"); await(); }
@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()); }
/** * Something has happened, so handle it. * @param event the event to handle */ public void handle(io.vertx.rxjava.ext.web.RoutingContext event) { delegate.handle(event.getDelegate()); }
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 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()); }
/** * Create a handler * @param engine the template engine * @return the handler */ public static io.vertx.rxjava.ext.web.handler.TemplateHandler create(io.vertx.rxjava.ext.web.common.template.TemplateEngine engine) { io.vertx.rxjava.ext.web.handler.TemplateHandler ret = io.vertx.rxjava.ext.web.handler.TemplateHandler.newInstance(io.vertx.ext.web.handler.TemplateHandler.create(engine.getDelegate())); return ret; }
@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()); }
/** * Create a handler * @param engine the template engine * @return the handler */ public static io.vertx.rxjava.ext.web.handler.TemplateHandler create(io.vertx.rxjava.ext.web.common.template.TemplateEngine engine) { io.vertx.rxjava.ext.web.handler.TemplateHandler ret = io.vertx.rxjava.ext.web.handler.TemplateHandler.newInstance(io.vertx.ext.web.handler.TemplateHandler.create(engine.getDelegate())); return ret; }
@Override public void handle(RoutingContext event) { String path = event.request().path(); LOGGER.info("handling template for url " + event.normalisedPath() + " | " + path); if (path.endsWith("/")) { LOGGER.info("REROUTING TO: " + path); path += "index.html"; event.reroute(path); } else { templateHandler.handle(event); } }
/** * Create a handler * @param engine the template engine * @param templateDirectory the template directory where templates will be looked for * @param contentType the content type header to be used in the response * @return the handler */ public static io.vertx.rxjava.ext.web.handler.TemplateHandler create(io.vertx.rxjava.ext.web.common.template.TemplateEngine engine, String templateDirectory, String contentType) { io.vertx.rxjava.ext.web.handler.TemplateHandler ret = io.vertx.rxjava.ext.web.handler.TemplateHandler.newInstance(io.vertx.ext.web.handler.TemplateHandler.create(engine.getDelegate(), templateDirectory, contentType)); return ret; }