@Override public void start() { Router router = Router.router(vertx); // Serve the static pages router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); System.out.println("Server is started"); }
router.route("/private/*").handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("private")); router.route().handler(StaticHandler.create());
router.route().handler(StaticHandler.create().setCachingEnabled(false));
public void addStaticHandler() { StaticHandler staticHandler = StaticHandler.create("elastichead"); staticHandler.setDirectoryListing(false); staticHandler.setCachingEnabled(false); staticHandler.setIndexPage("index.html"); route("/*").method(GET).handler(staticHandler); }
@Test public void testServerFileSystemPath() throws Exception { router.clear(); File file = File.createTempFile("vertx", "tmp"); file.deleteOnExit(); // remap stat to the temp dir try { stat = StaticHandler.create(file.getParent()); fail(); } catch (IllegalArgumentException e) { // expected } stat = StaticHandler.create().setAllowRootFileSystemAccess(true).setWebRoot(file.getParent()); router.route().handler(stat); testRequest(HttpMethod.GET, "/" + file.getName(), 200, "OK", ""); }
@Override public void setUp() throws Exception { super.setUp(); stat = StaticHandler.create("webroot").setDirectoryListing(true).setDirectoryTemplate("custom_dir_template.html"); router.route().handler(stat); }
@Test(expected = IllegalArgumentException.class) public void testAccessToRootPath() throws Exception { router.clear(); File file = File.createTempFile("vertx", "tmp"); file.deleteOnExit(); // remap stat to the temp dir stat = StaticHandler.create().setWebRoot(file.getParent()); }
@Test public void testSkipCompressionForSuffixes() throws Exception { StaticHandler staticHandler = StaticHandler.create() .skipCompressionForSuffixes(Collections.singleton("jpg")); List<String> uris = Arrays.asList("/testCompressionSuffix.html", "/somedir/range.jpg", "/somedir/range.jpeg", "/somedir3/coin.png"); List<String> expectedContentEncodings = Arrays.asList("gzip", HttpHeaders.IDENTITY.toString(), "gzip", "gzip"); testSkipCompression(staticHandler, uris, expectedContentEncodings); }
@Test public void testSkipCompressionForMediaTypes() throws Exception { StaticHandler staticHandler = StaticHandler.create() .skipCompressionForMediaTypes(Collections.singleton("image/jpeg")); List<String> uris = Arrays.asList("/testCompressionSuffix.html", "/somedir/range.jpg", "/somedir/range.jpeg", "/somedir3/coin.png"); List<String> expectedContentEncodings = Arrays.asList("gzip", HttpHeaders.IDENTITY.toString(), HttpHeaders.IDENTITY.toString(), "gzip"); testSkipCompression(staticHandler, uris, expectedContentEncodings); }
@Test public void testCacheFilesFileDeleted() throws Exception { File webroot = new File(".vertx/webroot"), pageFile = new File(webroot, "deleted.html"); if (!pageFile.exists()) { webroot.mkdirs(); pageFile.createNewFile(); } String page = '/' + pageFile.getName(); stat.setFilesReadOnly(false); stat.setWebRoot(webroot.getPath()); stat.setCacheEntryTimeout(3600 * 1000); long modified = Utils.secondsFactor(pageFile.lastModified()); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", null); pageFile.delete(); testRequest(HttpMethod.GET, page, 404, "Not Found"); testRequest(HttpMethod.GET, page, req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 404, "Not Found", null); }
@Test public void testLinkPreload() throws Exception { List<Http2PushMapping> mappings = new ArrayList<>(); mappings.add(new Http2PushMapping("style.css", "style", false)); mappings.add(new Http2PushMapping("coin.png", "image", false)); stat.setHttp2PushMapping(mappings) .setWebRoot("webroot/somedir3"); testRequest(HttpMethod.GET, "/testLinkPreload.html", null, res -> { List<String> linkHeaders = res.headers().getAll("Link"); assertTrue(linkHeaders.contains("<style.css>; rel=preload; as=style")); assertTrue(linkHeaders.contains("<coin.png>; rel=preload; as=image")); }, 200, "OK", null); }
@Test public void testDirectoryListingHtml() throws Exception { stat.setDirectoryListing(true); testDirectoryListingHtmlCustomTemplate("META-INF/vertx/web/vertx-web-directory.html"); }
/** * Set whether cache header handling is enabled * @param enabled true if enabled * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.handler.StaticHandler setCachingEnabled(boolean enabled) { delegate.setCachingEnabled(enabled); return this; }
@Test public void testCacheFilesEntryCached() throws Exception { stat.setFilesReadOnly(false); stat.setWebRoot("src/test/filesystemwebroot"); File resource = new File("src/test/filesystemwebroot", "fspage.html"); long modified = resource.lastModified(); testRequest(HttpMethod.GET, "/fspage.html", null, res -> { String lastModified = res.headers().get("last-modified"); assertEquals(modified, toDateTime(lastModified)); // Now update the web resource resource.setLastModified(modified + 1000); }, 200, "OK", "<html><body>File system page</body></html>"); // But it should still return not modified as the entry is cached testRequest(HttpMethod.GET, "/fspage.html", req -> req.putHeader("if-modified-since", dateTimeFormatter.format(modified)), null, 304, "Not Modified", null); }
@Test public void testDirectoryListingTextNoHidden() throws Exception { stat.setDirectoryListing(true); stat.setIncludeHidden(false); Set<String> expected = new HashSet<>(Arrays.asList("foo.json", "a", "index.html", "otherpage.html", "somedir", "somedir2", "somedir3", "testCompressionSuffix.html", "file with spaces.html")); testRequest(HttpMethod.GET, "/", null, resp -> { resp.bodyHandler(buff -> { assertEquals("text/plain", resp.headers().get("content-type")); String sBuff = buff.toString(); String[] elems = sBuff.split("\n"); assertEquals(expected.size(), elems.length); for (String elem: elems) { assertTrue(expected.contains(elem)); } }); }, 200, "OK", null); }
@Test public void testGetOtherIndex() throws Exception { stat.setIndexPage("otherpage.html"); testRequest(HttpMethod.GET, "/", 200, "OK", "<html><body>Other page</body></html>"); }
@Test public void testServeFilesFromFilesystem() throws Exception { stat.setWebRoot("src/test/filesystemwebroot"); testRequest(HttpMethod.GET, "/fspage.html", 200, "OK", "<html><body>File system page</body></html>"); }
@Test public void testFSBlockingTuning() throws Exception { stat.setCachingEnabled(false); stat.setMaxAvgServeTimeNs(10000); for (int i = 0; i < 2000; i++) { testRequest(HttpMethod.GET, "/otherpage.html", null, res -> { String cacheControl = res.headers().get("cache-control"); String lastModified = res.headers().get("last-modified"); assertNull(cacheControl); assertNull(lastModified); }, 200, "OK", "<html><body>Other page</body></html>"); } }
@Test public void testCustomDirectoryListingHtml() throws Exception { stat.setDirectoryListing(true); String dirTemplate = "custom_dir_template.html"; stat.setDirectoryTemplate(dirTemplate); testDirectoryListingHtmlCustomTemplate(dirTemplate); }
@Override public void registerEndPoints() { secureAll(); InternalEndpointRoute queryEndpoint = createRoute(); queryEndpoint.method(POST); queryEndpoint.exampleRequest(graphqlExamples.createQueryRequest()); queryEndpoint.exampleResponse(OK, graphqlExamples.createResponse(), "Basic GraphQL response."); queryEndpoint.description("Endpoint which accepts GraphQL queries."); queryEndpoint.path("/"); queryEndpoint.blockingHandler(rc -> { GraphQLContext gc = new GraphQLContextImpl(rc); String body = gc.getBodyAsString(); queryHandler.handleQuery(gc, body); }, false); StaticHandler staticHandler = StaticHandler.create("graphiql"); staticHandler.setDirectoryListing(false); staticHandler.setCachingEnabled(false); staticHandler.setIndexPage("index.html"); // Redirect handler route("/browser").method(GET).handler(rc -> { if (rc.request().path().endsWith("/browser")) { rc.response().setStatusCode(302); rc.response().headers().set("Location", rc.request().path() + "/"); rc.response().end(); } else { rc.next(); } }); route("/browser/*").method(GET).handler(staticHandler); }