public class AssetHandler extends Object implements Route.Handler
Jooby.assets(String)
or variants.
It generates ETag
headers using Asset.etag()
. It handles
If-None-Match
header automatically.
ETag
handling is enabled by default. If you want to disabled etag support
etag(boolean)
.
It generates Last-Modified
header using Asset.lastModified()
. It handles
If-Modified-Since
header automatically.
Asset can be serve from a content delivery network (a.k.a cdn). All you have to do is to set the
assets.cdn
property.
assets.cdn = "http://http://d7471vfo50fqt.cloudfront.net"
Resolved assets are redirected to the cdn.
Constructor and Description |
---|
AssetHandler(String pattern)
Creates a new
AssetHandler . |
AssetHandler(String pattern,
Class<?> loader)
Creates a new
AssetHandler . |
Modifier and Type | Method and Description |
---|---|
AssetHandler |
cdn(String cdn) |
AssetHandler |
etag(boolean etag) |
void |
handle(Request req,
Response rsp)
Callback method for a HTTP request.
|
AssetHandler |
lastModified(boolean enabled) |
AssetHandler |
maxAge(long maxAge) |
protected URL |
resolve(String path)
Resolve a path as a
URL . |
protected void |
send(Request req,
Response rsp,
Asset asset)
Send an asset to the client.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handle
public AssetHandler(String pattern, Class<?> loader)
Creates a new AssetHandler
. The handler accepts a location pattern, that serve for
locating the static resource.
assets("/assets/**", "/")
with:
GET /assets/js/index.js it translates the path to: /assets/js/index.jsGiven
assets("/js/**", "/assets")
with:
GET /js/index.js it translate the path to: /assets/js/index.jsGiven
assets("/webjars/**", "/META-INF/resources/webjars/{0}")
with:
GET /webjars/jquery/2.1.3/jquery.js it translate the path to: /META-INF/resources/webjars/jquery/2.1.3/jquery.js
pattern
- Pattern to locate static resources.loader
- The one who load the static resources.public AssetHandler(String pattern)
Creates a new AssetHandler
. The location pattern can be one of.
/
like in assets("/assets/**", "/")
with:
GET /assets/js/index.js it translates the path to: /assets/js/index.jsGiven
/assets
like in assets("/js/**", "/assets")
with:
GET /js/index.js it translate the path to: /assets/js/index.jsGiven
/META-INF/resources/webjars/{0}
like in
assets("/webjars/**", "/META-INF/resources/webjars/{0}")
with:
GET /webjars/jquery/2.1.3/jquery.js it translate the path to: /META-INF/resources/webjars/jquery/2.1.3/jquery.js
pattern
- Pattern to locate static resources.public AssetHandler etag(boolean etag)
etag
- Turn on/off etag support.public AssetHandler lastModified(boolean enabled)
enabled
- Turn on/off last modified support.public AssetHandler cdn(String cdn)
cdn
- If set, every resolved asset will be serve from it.public AssetHandler maxAge(long maxAge)
maxAge
- Set the cache header max-age value in seconds.public void handle(Request req, Response rsp) throws Exception
Route.Handler
handle
in interface Route.Handler
req
- A HTTP request.rsp
- A HTTP response.Exception
- If something goes wrong. The exception will processed by Jooby.protected void send(Request req, Response rsp, Asset asset) throws Exception
req
- Request.rsp
- Response.asset
- Resolve asset.Exception
- If send fails.Copyright © 2016. All rights reserved.