org.omnifaces.resourcehandler
Class CDNResourceHandler

java.lang.Object
  extended by javax.faces.application.ResourceHandler
      extended by javax.faces.application.ResourceHandlerWrapper
          extended by org.omnifaces.resourcehandler.CDNResourceHandler
All Implemented Interfaces:
javax.faces.FacesWrapper<javax.faces.application.ResourceHandler>

public class CDNResourceHandler
extends javax.faces.application.ResourceHandlerWrapper

This ResourceHandler implementation allows the developer to provide CDN URLs instead of the default local URLs for JSF resources as provided by <h:outputScript>, <h:outputStylesheet> and <h:graphicImage> when the current JSF project stage is not set to Development.

To get it to run, this handler needs be registered as follows in faces-config.xml:

 <application>
   <resource-handler>org.omnifaces.resourcehandler.CDNResourceHandler</resource-handler>
 </application>
 

Configuration

To configure the CDN URLs, a "org.omnifaces.CDN_RESOURCE_HANDLER_URLS" context parameter has to be provided wherein the CDN resources are been specified as a comma separated string of libraryName:resourceName=http://cdn.example.com/url key=value pairs. The key represents the default JSF resource identifier and the value represents the full CDN URL, including the scheme. The CDN URL is not validated by this resource handler, so you need to make absolutely sure yourself that it is valid. Here's an example:

 <context-param>
   <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
   <param-value>
     js/script1.js=http://cdn.example.com/js/script1.js,
     somelib:js/script2.js=http://cdn.example.com/somelib/js/script2.js,
     otherlib:style.css=http://cdn.example.com/otherlib/style.css,
     images/logo.png=http://cdn.example.com/logo.png
   </param-value>
 </context-param>
 

With the above configuration, the following resources:

 <h:outputScript name="js/script1.js" />
 <h:outputScript library="somelib" name="js/script2.js" />
 <h:outputStylesheet library="otherlib" name="style.css" />
 <h:graphicImage name="images/logo.png" />
 

Will during non-Development stage be rendered as:

 <script type="text/javascript" src="http://cdn.example.com/js/script1.js"></script>
 <script type="text/javascript" src="http://cdn.example.com/somelib/js/script2.js"></script>
 <link type="text/css" rel="stylesheet" href="http://cdn.example.com/otherlib/style.css" />
 <img src="http://cdn.example.com/logo.png" />
 

Note that you can also use this on resources provided as ResourceDependency by the JSF implementation and/or component libraries. For example, JSF's own javax.faces:jsf.js resource which is been used by <f:ajax> can be provided by a CDN URL using the following syntax:

 javax.faces:jsf.js=http://cdn.example.com/jsf.js
 

CombinedResourceHandler

If you're also using the CombinedResourceHandler, then you need to understand that CDN resources can simply not be combined, as that would defeat the CDN purpose. The CombinedResourceHandler will therefore automatically exclude all CDN resources from combining when the current JSF project stage is not set to Development.

Since:
1.2
Author:
Bauke Scholtz

Field Summary
static java.lang.String PARAM_NAME_CDN_RESOURCES
          The context parameter name to specify CDN URLs for the given resource identifiers.
 
Fields inherited from class javax.faces.application.ResourceHandler
LOCALE_PREFIX, RESOURCE_EXCLUDES_DEFAULT_VALUE, RESOURCE_EXCLUDES_PARAM_NAME, RESOURCE_IDENTIFIER
 
Constructor Summary
CDNResourceHandler(javax.faces.application.ResourceHandler wrapped)
          Creates a new instance of this CDN resource handler which wraps the given resource handler.
 
Method Summary
 javax.faces.application.Resource createResource(java.lang.String resourceName, java.lang.String libraryName)
          Creates a new instance of a resource based on the given resource name and library name.
 javax.faces.application.ResourceHandler getWrapped()
           
 
Methods inherited from class javax.faces.application.ResourceHandlerWrapper
createResource, createResource, getRendererTypeForResourceName, handleResourceRequest, isResourceRequest, libraryExists
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PARAM_NAME_CDN_RESOURCES

public static final java.lang.String PARAM_NAME_CDN_RESOURCES
The context parameter name to specify CDN URLs for the given resource identifiers.

See Also:
Constant Field Values
Constructor Detail

CDNResourceHandler

public CDNResourceHandler(javax.faces.application.ResourceHandler wrapped)
Creates a new instance of this CDN resource handler which wraps the given resource handler. If the current JSF project stage is not set to Development, then the CDN resources will be initialized based on the "org.omnifaces.CDN_RESOURCE_HANDLER_URLS" context parameter.

Parameters:
wrapped - The resource handler to be wrapped.
Throws:
java.lang.IllegalArgumentException - When the context parameter is missing or is in invalid format.
Method Detail

createResource

public javax.faces.application.Resource createResource(java.lang.String resourceName,
                                                       java.lang.String libraryName)
Creates a new instance of a resource based on the given resource name and library name. If the current JSF project stage is not set to Development, then the properties file will be consulted if any CDN URL is available for the given resource name and library name. If there is none, then just return the JSF default resource, otherwise return a wrapped resource whose Resource.getRequestPath() returns the CDN URL as is been set in the "org.omnifaces.CDN_RESOURCE_HANDLER_URLS" context parameter.

Overrides:
createResource in class javax.faces.application.ResourceHandlerWrapper

getWrapped

public javax.faces.application.ResourceHandler getWrapped()
Specified by:
getWrapped in interface javax.faces.FacesWrapper<javax.faces.application.ResourceHandler>
Overrides:
getWrapped in class javax.faces.application.ResourceHandlerWrapper