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
ConstructorsConstructorDescriptionAssetHandler(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.notFound(SneakyThrows.Consumer<Context> handler) Sets a custom handler for404asset/resource.setETag(boolean etag) Turn on/off e-tag support.setLastModified(boolean lastModified) Turn on/off handling ofIf-Modified-Sinceheader.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-Sinceheader support.voidAllows a handler to listen for route metadata.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 a404its 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.HandlerExecute application code.- Specified by:
applyin 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-Sinceheader.- 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-Sinceheader 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:
-
notFound
Sets a custom handler for404asset/resource. By default, generates a404status code response.- Parameters:
handler- Handler.- Returns:
- This handler.
-
setRoute
Description copied from interface:Route.AwareAllows a handler to listen for route metadata.- Specified by:
setRoutein interfaceRoute.Aware- Parameters:
route- Route metadata.
-