Module io.jooby

Class AssetHandler

java.lang.Object
io.jooby.handler.AssetHandler
All Implemented Interfaces:
Route.Aware, Route.Handler, Serializable

public class AssetHandler extends Object implements Route.Handler
Handler for static resources represented by the Asset contract.

It has built-in support for static-static as well as SPAs (single page applications).

Since:
2.0.0
Author:
edgar
See Also:
  • Constructor Details

    • AssetHandler

      public AssetHandler(@NonNull String fallback, AssetSource... sources)
      Creates a new asset handler that fallback to the given fallback asset when the asset is not found. Instead of produces a 404 its fallback to the given asset.
      
       {
          assets("/?*", new AssetHandler("index.html", AssetSource.create(Paths.get("...")));
       }
       
      The fallback option makes the asset handler to work like a SPA (Single-Application-Page).
      Parameters:
      fallback - Fallback asset.
      sources - Asset sources. At least one source is required.
    • AssetHandler

      public AssetHandler(AssetSource... sources)
      Creates a new asset handler.
      Parameters:
      sources - Asset sources. At least one source is required.
  • Method Details

    • apply

      @NonNull public Object apply(@NonNull Context ctx) throws Exception
      Description copied from interface: Route.Handler
      Execute application code.
      Specified by:
      apply in interface Route.Handler
      Parameters:
      ctx - Web context.
      Returns:
      Route response.
      Throws:
      Exception - If something goes wrong.
    • setETag

      public AssetHandler setETag(boolean etag)
      Turn on/off e-tag support.
      Parameters:
      etag - True for turning on.
      Returns:
      This handler.
    • setMediaTypeResolver

      public AssetHandler setMediaTypeResolver(Function<Asset,MediaType> mediaTypeResolver)
      Allow to customize the default media type and/or the charset of it.
      
       // GBK
       var gbk = MediaType.valueOf("text/html;charset=GBK");
      
       Function<Asset, MediaType> overrideCharset = asset -> {
           var defaultType = asset.getContentType();
           // Choose what is best for you
           if (defaultType.matches(gbk)) {
               return gbk;
           }
           return defaultType;
       };
       app.assets("/3267/gbk/?*", new AssetHandler(source).setMediaTypeResolver(overrideCharset));
      
       
      Parameters:
      mediaTypeResolver - Type resolver.
      Returns:
      This handler.
    • setLastModified

      public AssetHandler setLastModified(boolean lastModified)
      Turn on/off handling of If-Modified-Since header.
      Parameters:
      lastModified - True for turning on. Default is: true.
      Returns:
      This handler.
    • setMaxAge

      public AssetHandler setMaxAge(long maxAge)
      Set cache-control header with the given max-age value. If max-age is greater than 0.
      Parameters:
      maxAge - Max-age value in seconds.
      Returns:
      This handler.
    • setMaxAge

      public AssetHandler setMaxAge(Duration maxAge)
      Set cache-control header with the given max-age value. If max-age is greater than 0.
      Parameters:
      maxAge - Max-age value in seconds.
      Returns:
      This handler.
    • setNoCache

      public AssetHandler setNoCache()
      Set cache-control header to no-store, must-revalidate, disables e-tag and If-Modified-Since header support.
      Returns:
      This handler.
    • cacheControl

      public AssetHandler cacheControl(@NonNull Function<String,CacheControl> cacheControl)
      Sets a custom function that provides caching configuration for each individual asset response overriding the defaults set in AssetHandler.
      Parameters:
      cacheControl - a cache configuration provider function.
      Returns:
      this instance.
      See Also:
    • setRoute

      public void setRoute(Route route)
      Description copied from interface: Route.Aware
      Allows a handler to listen for route metadata.
      Specified by:
      setRoute in interface Route.Aware
      Parameters:
      route - Route metadata.