This documentation is for an old version of fiftyfive-wicket-all (3.2).


fiftyfive.wicket.resource
Class SimpleCDN

java.lang.Object
  extended by fiftyfive.wicket.resource.SimpleCDN
All Implemented Interfaces:
IRequestMapper

public class SimpleCDN
extends Object
implements 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.css

With SimpleCDN installed, that resource reference URL is transformed into this:

 http://age39p8hg23.cloudfront.net/wicket/resource/com.mycompany.WicketApplication/test.css

Please note: SimpleCDN 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 SimpleCDN. It's that easy.

 public class MyApplication extends WebApplication
 {
     @Override
     protected void init()
     {
         super.init();
         // Enable CDN when in deployment mode
         if(usesDeploymentConfig())
         {
             new SimpleCDN("//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, SimpleCDN is inspired by the Rails action_controller.asset_host configuration setting.

Since:
3.2

Constructor Summary
SimpleCDN(String baseUrl)
          Construct a SimpleCDN that will rewrite resource reference URLs by prepending the given baseUrl.
 
Method Summary
 int getCompatibilityScore(Request request)
          Always return 0, since SimpleCDN does not play any part in handling requests (they will be handled by Wicket's default mechanism).
 void install(WebApplication app)
          Install this SimpleCDN into the given application.
 Url mapHandler(IRequestHandler requestHandler)
          If the requestHandler is a ResourceReferenceRequestHandler, delegate to Wicket's default mapper for creating an appropriate URL, and then prepend the CDN baseUrl that was provided to the SimpleCDN constructor.
 IRequestHandler mapRequest(Request request)
          Always return null, since SimpleCDN does not play any part in handling requests (they will be handled by Wicket's default mechanism).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleCDN

public SimpleCDN(String baseUrl)
Construct a SimpleCDN that will rewrite resource reference URLs by prepending the given baseUrl.

Parameters:
baseUrl - For example, "//age39p8hg23.cloudfront.net"
Method Detail

install

public void install(WebApplication app)
Install this SimpleCDN into the given application. The SimpleCDN instance will not have any effect unless it is installed.


mapHandler

public Url mapHandler(IRequestHandler requestHandler)
If the requestHandler is a ResourceReferenceRequestHandler, delegate to Wicket's default mapper for creating an appropriate URL, and then prepend the CDN baseUrl that was provided to the SimpleCDN constructor.

Specified by:
mapHandler in interface IRequestMapper
Returns:
a rewritten Url to the resource, or null if requestHandler is not for a resource reference

mapRequest

public IRequestHandler mapRequest(Request request)
Always return null, since SimpleCDN does not play any part in handling requests (they will be handled by Wicket's default mechanism).

Specified by:
mapRequest in interface IRequestMapper

getCompatibilityScore

public int getCompatibilityScore(Request request)
Always return 0, since SimpleCDN does not play any part in handling requests (they will be handled by Wicket's default mechanism).

Specified by:
getCompatibilityScore in interface IRequestMapper


Copyright © 2012 55 Minutes. All Rights Reserved.