Codota Logo
StaticHandler
Code IndexAdd Codota to your IDE (free)

How to use
StaticHandler
in
io.vertx.ext.web.handler

Best Java code snippets using io.vertx.ext.web.handler.StaticHandler (Showing top 20 results out of 315)

  • Common ways to obtain StaticHandler
private void myMethod () {
StaticHandler s =
  • Codota IconString str;StaticHandler.create(str)
  • Smart code suggestions by Codota
}
origin: vert-x3/vertx-examples

@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");
}
origin: vert-x3/vertx-examples

router.route("/private/*").handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("private"));
router.route().handler(StaticHandler.create());
origin: vert-x3/vertx-examples

router.route().handler(StaticHandler.create().setCachingEnabled(false));
origin: gentics/mesh

public void addStaticHandler() {
  StaticHandler staticHandler = StaticHandler.create("elastichead");
  staticHandler.setDirectoryListing(false);
  staticHandler.setCachingEnabled(false);
  staticHandler.setIndexPage("index.html");
  route("/*").method(GET).handler(staticHandler);
}
origin: vert-x3/vertx-web

@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", "");
}
origin: vert-x3/vertx-web

@Override
public void setUp() throws Exception {
 super.setUp();
 stat = StaticHandler.create("webroot").setDirectoryListing(true).setDirectoryTemplate("custom_dir_template.html");
 router.route().handler(stat);
}
origin: vert-x3/vertx-web

@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());
}
origin: vert-x3/vertx-web

@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);
}
origin: vert-x3/vertx-web

@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);
}
origin: vert-x3/vertx-web

@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);
}
origin: vert-x3/vertx-web

@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);
}
origin: vert-x3/vertx-web

@Test
public void testDirectoryListingHtml() throws Exception {
 stat.setDirectoryListing(true);
 testDirectoryListingHtmlCustomTemplate("META-INF/vertx/web/vertx-web-directory.html");
}
origin: io.vertx/vertx-rx-java

/**
 * 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;
}
origin: vert-x3/vertx-web

@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);
}
origin: vert-x3/vertx-web

@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);
}
origin: vert-x3/vertx-web

@Test
public void testGetOtherIndex() throws Exception {
 stat.setIndexPage("otherpage.html");
 testRequest(HttpMethod.GET, "/", 200, "OK", "<html><body>Other page</body></html>");
}
origin: vert-x3/vertx-web

@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>");
}
origin: vert-x3/vertx-web

@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>");
 }
}
origin: vert-x3/vertx-web

@Test
public void testCustomDirectoryListingHtml() throws Exception {
 stat.setDirectoryListing(true);
 String dirTemplate = "custom_dir_template.html";
 stat.setDirectoryTemplate(dirTemplate);
 testDirectoryListingHtmlCustomTemplate(dirTemplate);
}
origin: gentics/mesh

@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);
}
io.vertx.ext.web.handlerStaticHandler

Javadoc

A handler for serving static resources from the file system or classpath.

Most used methods

  • create
    Create a handler, specifying web-root and a classloader used to load the resources.
  • setCachingEnabled
    Set whether cache header handling is enabled
  • setDirectoryListing
    Set whether directory listing is enabled
  • setIndexPage
    Set the index page
  • setWebRoot
    Set the web root
  • setAllowRootFileSystemAccess
    Enable/Disable access to the root of the filesystem
  • setHttp2PushMapping
    Set the file mapping for http2push and link preload
  • setAlwaysAsyncFS
    Set whether async filesystem access should always be used
  • setCacheEntryTimeout
    Set the server cache entry timeout when caching is enabled
  • setDefaultContentEncoding
    Set the default content encoding for text related files. This allows overriding the system settings
  • setDirectoryTemplate
    Set the directory template to be used when directory listing
  • setEnableRangeSupport
    Set whether range requests (resumable downloads; media streaming) should be enabled.
  • setDirectoryTemplate,
  • setEnableRangeSupport,
  • setFilesReadOnly,
  • setIncludeHidden,
  • setMaxAgeSeconds,
  • setMaxAvgServeTimeNs,
  • skipCompressionForMediaTypes,
  • skipCompressionForSuffixes,
  • handle,
  • setEnableFSTuning

Popular in Java

  • Making http post requests using okhttp
  • addToBackStack (FragmentTransaction)
  • putExtra (Intent)
  • getApplicationContext (Context)
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • FlowLayout (java.awt)
    A flow layout arranges components in a left-to-right flow, much like lines of text in a paragraph. F
  • RandomAccessFile (java.io)
    Allows reading from and writing to a file in a random-access manner. This is different from the uni-
  • MalformedURLException (java.net)
    Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a s
  • URI (java.net)
    Represents a Uniform Resource Identifier (URI) reference. Aside from some minor deviations noted bel
  • Logger (org.slf4j)
    The main user interface to logging. It is expected that logging takes place through concrete impleme
Codota Logo
  • Products

    Search for Java codeSearch for JavaScript codeEnterprise
  • IDE Plugins

    IntelliJ IDEAWebStormAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogCodota Academy Plugin user guide Terms of usePrivacy policyJava Code IndexJavascript Code Index
Get Codota for your IDE now