Module wicket.bootstrap.extensions
Class StaticResourceRewriteMapper
java.lang.Object
de.agilecoders.wicket.extensions.request.StaticResourceRewriteMapper
- All Implemented Interfaces:
IRequestMapper
Enables a Wicket application to have its static resources proxied by a CDN, for example
by Amazon Cloudfront. This works by intercepting Wicket's default behavior for rendering
URLs of resource references, and then rewriting those URLs by prepending a CDN hostname
(or any arbitrary URL fragment). The web browser will therefore make requests to the
CDN host instead of the Wicket app.
Here's an example. Normally a CSS resource reference is rendered by Wicket like this:
/wicket/resource/com.mycompany.WicketApplication/test.cssWith
StaticResourceRewriteMapper installed, that resource reference URL is transformed into this:
http://age39p8hg23.cloudfront.net/wicket/resource/com.mycompany.WicketApplication/test.cssPlease note:
StaticResourceRewriteMapper will not rewrite resource reference URLs that
include query string parameters. Our reasoning is that parameterized URLs usually
indicate that the resource is dynamic, and therefore not appropriate for serving via
CDN. Furthermore it should be noted that Amazon CloudFront will refuse to proxy
URLs that contain query string parameters (it strips the parameters off).
When configuring the CDN host, the easiest setup is a reverse-proxy. For example, with
Amazon CloudFront, you would specify your Wicket app as the custom origin, and specify
the CloudFront host when constructing this StaticResourceRewriteMapper. It's that easy.
public class MyApplication extends WebApplication
{
@Override
protected void init() {
super.init();
// Enable CDN when in deployment mode
if(usesDeploymentConfig()) {
StaticResourceRewriteMapper.withBaseUrl("//age39p8hg23.cloudfront.net").install(this);
}
}
}
Notice in this example that we've used "//" instead of "http://" for the CDN URL.
This trick ensures that "http" or "https" will be automatically selected by the
browser based on the enclosing web page.
For those familiar with Ruby on Rails, StaticResourceRewriteMapper is inspired by the Rails
action_controller.asset_host configuration setting.
This class is a fork of https://github.com/55minutes/fiftyfive-wicket/blob/v3.2/fiftyfive-wicket-core/src/main/java/fiftyfive/wicket/resource/SimpleCDN.java-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classbuilder class forStaticResourceRewriteMapper -
Method Summary
Modifier and TypeMethodDescriptionintgetCompatibilityScore(Request request) Always return0, sinceStaticResourceRewriteMapperdoes not play any part in handling requests (they will be handled by Wicket's default mechanism).mapHandler(IRequestHandler requestHandler) If therequestHandleris aResourceReferenceRequestHandler, delegate to Wicket's default mapper for creating an appropriate URL, and then prepend thebaseUrlthat was provided to theStaticResourceRewriteMapperconstructor.mapRequest(Request request) Always returnnull, sinceStaticResourceRewriteMapperdoes not play any part in handling requests (they will be handled by Wicket's default mechanism).withBaseUrl(String baseUrl) creates a newStaticResourceRewriteMapper.Builderwith given base url
-
Method Details
-
withBaseUrl
creates a newStaticResourceRewriteMapper.Builderwith given base url- Parameters:
baseUrl- the base url- Returns:
- new builder instance
-
mapHandler
If therequestHandleris aResourceReferenceRequestHandler, delegate to Wicket's default mapper for creating an appropriate URL, and then prepend thebaseUrlthat was provided to theStaticResourceRewriteMapperconstructor.- Specified by:
mapHandlerin interfaceIRequestMapper- Returns:
- a rewritten Url to the resource, or
nullifrequestHandleris not for a resource reference
-
mapRequest
Always returnnull, sinceStaticResourceRewriteMapperdoes not play any part in handling requests (they will be handled by Wicket's default mechanism).- Specified by:
mapRequestin interfaceIRequestMapper
-
getCompatibilityScore
Always return0, sinceStaticResourceRewriteMapperdoes not play any part in handling requests (they will be handled by Wicket's default mechanism).- Specified by:
getCompatibilityScorein interfaceIRequestMapper
-