java.lang.Object
io.jooby.handler.AssetHandler
- All Implemented Interfaces:
Route.Aware
,Route.Handler
,Serializable
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 Summary
ConstructorDescriptionAssetHandler
(AssetSource... sources) Creates a new asset handler.AssetHandler
(String fallback, AssetSource... sources) Creates a new asset handler that fallback to the given fallback asset when the asset is not found. -
Method Summary
Modifier and TypeMethodDescriptionExecute application code.cacheControl
(Function<String, CacheControl> cacheControl) Sets a custom function that provides caching configuration for each individual asset response overriding the defaults set inAssetHandler
.setETag
(boolean etag) Turn on/off e-tag support.setLastModified
(boolean lastModified) Turn on/off handling ofIf-Modified-Since
header.setMaxAge
(long maxAge) Set cache-control header with the given max-age value.Set cache-control header with the given max-age value.setMediaTypeResolver
(Function<Asset, MediaType> mediaTypeResolver) Allow to customize the default media type and/or the charset of it.Set cache-control header tono-store, must-revalidate
, disables e-tag andIf-Modified-Since
header support.void
Allows a handler to listen for route metadata.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.jooby.Route.Handler
then
-
Constructor Details
-
AssetHandler
Creates a new asset handler that fallback to the given fallback asset when the asset is not found. Instead of produces a404
its fallback to the given asset.
The fallback option makes the asset handler to work like a SPA (Single-Application-Page).{ assets("/?*", new AssetHandler("index.html", AssetSource.create(Paths.get("..."))); }
- Parameters:
fallback
- Fallback asset.sources
- Asset sources. At least one source is required.
-
AssetHandler
Creates a new asset handler.- Parameters:
sources
- Asset sources. At least one source is required.
-
-
Method Details
-
apply
Description copied from interface:Route.Handler
Execute application code.- Specified by:
apply
in interfaceRoute.Handler
- Parameters:
ctx
- Web context.- Returns:
- Route response.
- Throws:
Exception
- If something goes wrong.
-
setETag
Turn on/off e-tag support.- Parameters:
etag
- True for turning on.- Returns:
- This handler.
-
setMediaTypeResolver
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
Turn on/off handling ofIf-Modified-Since
header.- Parameters:
lastModified
- True for turning on. Default is: true.- Returns:
- This handler.
-
setMaxAge
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
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
Set cache-control header tono-store, must-revalidate
, disables e-tag andIf-Modified-Since
header support.- Returns:
- This handler.
-
cacheControl
Sets a custom function that provides caching configuration for each individual asset response overriding the defaults set inAssetHandler
.- Parameters:
cacheControl
- a cache configuration provider function.- Returns:
- this instance.
- See Also:
-
setRoute
Description copied from interface:Route.Aware
Allows a handler to listen for route metadata.- Specified by:
setRoute
in interfaceRoute.Aware
- Parameters:
route
- Route metadata.
-