public class StaticResourceRewriteMapper extends Object implements org.apache.wicket.request.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| Modifier and Type | Class and Description |
|---|---|
static class |
StaticResourceRewriteMapper.Builder
builder class for
StaticResourceRewriteMapper |
| Modifier and Type | Method and Description |
|---|---|
int |
getCompatibilityScore(org.apache.wicket.request.Request request)
Always return
0, since StaticResourceRewriteMapper does not play any part in handling requests (they will be handled by Wicket’s default mechanism). |
org.apache.wicket.request.Url |
mapHandler(org.apache.wicket.request.IRequestHandler requestHandler)
If the
requestHandler is a ResourceReferenceRequestHandler, delegate to Wicket’s default mapper for creating an appropriate URL, and then prepend the baseUrl that was provided to the StaticResourceRewriteMapper constructor. |
org.apache.wicket.request.IRequestHandler |
mapRequest(org.apache.wicket.request.Request request)
Always return
null, since StaticResourceRewriteMapper does not play any part in handling requests (they will be handled by Wicket’s default mechanism). |
static StaticResourceRewriteMapper.Builder |
withBaseUrl(String baseUrl)
creates a new
StaticResourceRewriteMapper.Builder with given base url |
public static StaticResourceRewriteMapper.Builder withBaseUrl(String baseUrl)
creates a new StaticResourceRewriteMapper.Builder with given base url
baseUrl - the base urlpublic org.apache.wicket.request.Url mapHandler(org.apache.wicket.request.IRequestHandler requestHandler)
If the requestHandler is a ResourceReferenceRequestHandler, delegate to Wicket’s default mapper for creating an appropriate URL, and then prepend the baseUrl that was provided to the StaticResourceRewriteMapper constructor.
mapHandler in interface org.apache.wicket.request.IRequestMappernull if requestHandler is not for a resource referencepublic org.apache.wicket.request.IRequestHandler mapRequest(org.apache.wicket.request.Request request)
Always return null, since StaticResourceRewriteMapper does not play any part in handling requests (they will be handled by Wicket’s default mechanism).
mapRequest in interface org.apache.wicket.request.IRequestMapperpublic int getCompatibilityScore(org.apache.wicket.request.Request request)
Always return 0, since StaticResourceRewriteMapper does not play any part in handling requests (they will be handled by Wicket’s default mechanism).
getCompatibilityScore in interface org.apache.wicket.request.IRequestMapperCopyright © 2011–2014 AgileCoders. All rights reserved.