- java.lang.Object
-
- jakarta.faces.application.ResourceHandler
-
- jakarta.faces.application.ResourceHandlerWrapper
-
- org.omnifaces.resourcehandler.DefaultResourceHandler
-
- org.omnifaces.resourcehandler.CDNResourceHandler
-
- All Implemented Interfaces:
FacesWrapper<ResourceHandler>
public class CDNResourceHandler extends DefaultResourceHandler
This
ResourceHandler
implementation allows the developer to provide external (CDN) URLs instead of the default local URLs for Faces resources. This also works on auto-included resources provided asResourceDependency
by the Faces implementation and/or component libraries. For example, Faces's ownjakarta.faces:jsf.js
resource or PrimeFaces'primefaces:jquery/jquery.js
resource could be pointed to a CDN.Installation
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>
Standard 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=https://cdn.example.com/url
key=value pairs. The key represents the default Faces 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 is an example configuration:
<context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name> <param-value> js/script1.js=https://cdn.example.com/js/script1.js, somelib:js/script2.js=https://cdn.example.com/somelib/js/script2.js, otherlib:style.css=https://cdn.example.com/otherlib/style.css, somelib:images/logo.png=https://cdn.example.com/somelib/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 library="somelib" name="images/logo.png" />
Will be rendered as:
<script type="text/javascript" src="https://cdn.example.com/js/script1.js"></script> <script type="text/javascript" src="https://cdn.example.com/somelib/js/script2.js"></script> <link type="text/css" rel="stylesheet" href="https://cdn.example.com/otherlib/style.css" /> <img src="https://cdn.example.com/logo.png" />
Here is a real world example with Bootstrap:
<context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name> <param-value> cdn:bootstrap.css=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css, cdn:bootstrap.js=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js </param-value> </context-param>
With the above configuration, the following resources:
<h:outputStylesheet library="cdn" name="bootstrap.css" /> <h:outputScript library="cdn" name="bootstrap.js" />
Will be rendered as:
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Wildcard configuration
You can also use the wildcard syntax to map every single resource of a specific library to a common CDN URL. To achieve that, just use
*
as the sole resource name and make sure that the CDN URL ends with/*
. Here's an example:<context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name> <param-value>jquery-cdn:*=https://code.jquery.com/*</param-value> </context-param>
With the above configuration, the following resources:<h:outputScript library="jquery-cdn" name="jquery-1.9.1.js" /> <h:outputScript library="jquery-cdn" name="ui/1.10.3/jquery-ui.js" />
Will be rendered as:
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
EL expressions
The CDN resource handler supports evaluating EL expessions in the CDN URL. Here's an example:
<context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name> <param-value>jquery-cdn:*=https://#{settings.jqueryCDN}/*</param-value> </context-param>
The EL expression is resolved on a per-request basis.
Conditionally disable CDN resource handler
If you'd like to supply a context parameter which conditionally disables the CDN resource handler, then set the context parameter "org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED" accordingly.
<context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name> <param-value>true</param-value> </context-param> <!-- or --> <context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name> <param-value>#{facesContext.application.projectStage eq 'Development'}</param-value> </context-param> <!-- or --> <context-param> <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name> <param-value>#{someBean.someBooleanProperty}</param-value> </context-param>
The EL expression is resolved on a per-request basis.
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. TheCombinedResourceHandler
will therefore automatically exclude all CDN resources from combining.- Since:
- 1.2
- Author:
- Bauke Scholtz
- See Also:
RemappedResource
,DefaultResourceHandler
-
-
Field Summary
Fields Modifier and Type Field Description static String
PARAM_NAME_CDN_DISABLED
The context parameter name to conditionally disable CDN resource handler.static String
PARAM_NAME_CDN_RESOURCES
The context parameter name to specify CDN URLs for the given resource identifiers.-
Fields inherited from class org.omnifaces.resourcehandler.DefaultResourceHandler
FACES_SCRIPT_RESOURCE_NAME, RES_NOT_FOUND
-
Fields inherited from class jakarta.faces.application.ResourceHandler
JSF_SCRIPT_LIBRARY_NAME, JSF_SCRIPT_RESOURCE_NAME, LOCALE_PREFIX, RESOURCE_CONTRACT_XML, RESOURCE_EXCLUDES_DEFAULT_VALUE, RESOURCE_EXCLUDES_PARAM_NAME, RESOURCE_IDENTIFIER, WEBAPP_CONTRACTS_DIRECTORY_PARAM_NAME, WEBAPP_RESOURCES_DIRECTORY_PARAM_NAME
-
-
Constructor Summary
Constructors Constructor Description CDNResourceHandler(ResourceHandler wrapped)
Creates a new instance of this CDN resource handler which wraps the given resource handler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Resource
decorateResource(Resource resource, String resourceName, String libraryName)
If the given resource is notnull
and the CDN resource handler is not (conditionally) disabled for the current request, then the CDN resources will be consulted if any CDN URL is available for the given resource.-
Methods inherited from class org.omnifaces.resourcehandler.DefaultResourceHandler
createResource, createResource, createResource, createResourceFromLibrary, decorateResource, getLibraryName
-
Methods inherited from class jakarta.faces.application.ResourceHandlerWrapper
createResourceFromId, createViewResource, getRendererTypeForResourceName, getViewResources, getViewResources, getWrapped, handleResourceRequest, isResourceRendered, isResourceRequest, isResourceURL, libraryExists, markResourceRendered
-
-
-
-
Field Detail
-
PARAM_NAME_CDN_RESOURCES
public static final String PARAM_NAME_CDN_RESOURCES
The context parameter name to specify CDN URLs for the given resource identifiers.- See Also:
- Constant Field Values
-
PARAM_NAME_CDN_DISABLED
public static final String PARAM_NAME_CDN_DISABLED
The context parameter name to conditionally disable CDN resource handler. @since 2.0- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CDNResourceHandler
public CDNResourceHandler(ResourceHandler wrapped)
Creates a new instance of this CDN resource handler which wraps the given resource handler. 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:
IllegalArgumentException
- When the context parameter is missing or is in invalid format.
-
-
Method Detail
-
decorateResource
public Resource decorateResource(Resource resource, String resourceName, String libraryName)
If the given resource is notnull
and the CDN resource handler is not (conditionally) disabled for the current request, then the CDN resources will be consulted if any CDN URL is available for the given resource. If there is none, then just return the Faces default resource, otherwise return a wrapped resource whoseResource.getRequestPath()
returns the CDN URL as is been set in the "org.omnifaces.CDN_RESOURCE_HANDLER_URLS" context parameter.- Overrides:
decorateResource
in classDefaultResourceHandler
- Parameters:
resource
- The resource to be decorated.resourceName
- The resource name.libraryName
- The library name.- Returns:
- The decorated resource.
-
-