Permalink
Browse files

Add support to conditionally disable CDN and Combined resource handlers

via context param (supporting EL expressions).
  • Loading branch information...
1 parent aae8e62 commit 3cda0b2027293b5995ddd4f7f24d03a4088b293b @BalusC BalusC committed Aug 25, 2014
@@ -87,8 +87,8 @@
* <code>/*</code>. Here's an example:
* <pre>
* &lt;context-param&gt;
- * &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_URLS&lt;/param-name&gt;
- * &lt;param-value&gt;jquery-cdn:*=http://code.jquery.com/*&lt;/param-value&gt;
+ * &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_URLS&lt;/param-name&gt;
+ * &lt;param-value&gt;jquery-cdn:*=http://code.jquery.com/*&lt;/param-value&gt;
* &lt;/context-param&gt;
* </pre>
* With the above configuration, the following resources:
@@ -107,8 +107,30 @@
* <p>The CDN resource handler supports evaluating EL expessions in the CDN URL. Here's an example:</p>
* <pre>
* &lt;context-param&gt;
- * &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_URLS&lt;/param-name&gt;
- * &lt;param-value&gt;jquery-cdn:*=http://#{settings.jqueryCDN}/*&lt;/param-value&gt;
+ * &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_URLS&lt;/param-name&gt;
+ * &lt;param-value&gt;jquery-cdn:*=http://#{settings.jqueryCDN}/*&lt;/param-value&gt;
+ * &lt;/context-param&gt;
+ * </pre>
+ * <p>The EL expression is resolved on a per-request basis.</p>
+ *
+ * <h3>Conditionally disable CDN resource handler</h3>
+ * <p>
+ * If you'd like to supply a context parameter which conditionally disables the CDN resource handler, then set the
+ * context parameter {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_DISABLED} accordingly.
+ * <pre>
+ * &lt;context-param&gt;
+ * &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED&lt;/param-name&gt;
+ * &lt;param-value&gt;true&lt;/param-value&gt;
+ * &lt;/context-param&gt;
+ * &lt;!-- or --&gt;
+ * &lt;context-param&gt;
+ * &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED&lt;/param-name&gt;
+ * &lt;param-value&gt;#{facesContext.application.projectStage eq 'Development'}&lt;/param-value&gt;
+ * &lt;/context-param&gt;
+ * &lt;!-- or --&gt;
+ * &lt;context-param&gt;
+ * &lt;param-name&gt;org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED&lt;/param-name&gt;
+ * &lt;param-value&gt;#{someBean.someBooleanProperty}&lt;/param-value&gt;
* &lt;/context-param&gt;
* </pre>
* <p>The EL expression is resolved on a per-request basis.</p>
@@ -129,6 +151,9 @@
/** The context parameter name to specify CDN URLs for the given resource identifiers. */
public static final String PARAM_NAME_CDN_RESOURCES = "org.omnifaces.CDN_RESOURCE_HANDLER_URLS";
+ /** The context parameter name to conditionally disable CDN resource handler. @since 2.0 */
+ public static final String PARAM_NAME_CDN_DISABLED = "org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED";
+
private static final String ERROR_MISSING_INIT_PARAM =
"Context parameter '" + PARAM_NAME_CDN_RESOURCES + "' is missing in web.xml or web-fragment.xml.";
private static final String ERROR_INVALID_INIT_PARAM =
@@ -141,6 +166,7 @@
// Properties -----------------------------------------------------------------------------------------------------
+ private String disabledParam;
private Map<ResourceIdentifier, String> cdnResources;
// Constructors ---------------------------------------------------------------------------------------------------
@@ -155,6 +181,7 @@
*/
public CDNResourceHandler(ResourceHandler wrapped) {
super(wrapped);
+ disabledParam = getInitParameter(PARAM_NAME_CDN_DISABLED);
cdnResources = initCDNResources();
if (cdnResources == null) {
@@ -176,8 +203,8 @@ public CDNResourceHandler(ResourceHandler wrapped) {
public Resource createResource(String resourceName, String libraryName, String contentType) {
Resource resource = getWrapped().createResource(resourceName, libraryName, contentType);
- if (resource == null) {
- return null;
+ if (resource == null || (disabledParam != null && Boolean.valueOf(String.valueOf(evaluateExpressionGet(disabledParam))))) {
+ return resource;
}
String requestPath = null;
@@ -13,6 +13,7 @@
package org.omnifaces.resourcehandler;
import static org.omnifaces.util.Events.subscribeToApplicationEvent;
+import static org.omnifaces.util.Faces.evaluateExpressionGet;
import static org.omnifaces.util.Faces.getInitParameter;
import java.util.ArrayList;
@@ -133,6 +134,28 @@
* will use an internal workaround to get it to work anyway, but this involves firing a HTTP request for every resource.
* The impact should however be relatively negligible as this is performed on localhost.
*
+ * <h3>Conditionally disable combined resource handler</h3>
+ * <p>
+ * If you'd like to supply a context parameter which conditionally disables the combined resource handler, then set the
+ * context parameter {@value org.omnifaces.resourcehandler.CombinedResourceHandler#PARAM_NAME_DISABLED} accordingly.
+ * <pre>
+ * &lt;context-param&gt;
+ * &lt;param-name&gt;org.omnifaces.COMBINED_RESOURCE_HANDLER_DISABLED&lt;/param-name&gt;
+ * &lt;param-value&gt;true&lt;/param-value&gt;
+ * &lt;/context-param&gt;
+ * &lt;!-- or --&gt;
+ * &lt;context-param&gt;
+ * &lt;param-name&gt;org.omnifaces.COMBINED_RESOURCE_HANDLER_DISABLED&lt;/param-name&gt;
+ * &lt;param-value&gt;#{facesContext.application.projectStage eq 'Development'}&lt;/param-value&gt;
+ * &lt;/context-param&gt;
+ * &lt;!-- or --&gt;
+ * &lt;context-param&gt;
+ * &lt;param-name&gt;org.omnifaces.COMBINED_RESOURCE_HANDLER_DISABLED&lt;/param-name&gt;
+ * &lt;param-value&gt;#{someApplicationScopedBean.someBooleanProperty}&lt;/param-value&gt;
+ * &lt;/context-param&gt;
+ * </pre>
+ * <p>The EL expression is resolved on a per-request basis.</p>
+ *
* <h3>CDNResourceHandler</h3>
* <p>
* If you're also using the {@link CDNResourceHandler} or, at least, have configured its context parameter
@@ -148,6 +171,10 @@
/** The default library name of a combined resource. Make sure that this is never used for other libraries. */
public static final String LIBRARY_NAME = "omnifaces.combined";
+ /** The context parameter name to conditionally disable combined resource handler. @since 2.0 */
+ public static final String PARAM_NAME_DISABLED =
+ "org.omnifaces.COMBINED_RESOURCE_HANDLER_DISABLED";
+
/** The context parameter name to specify resource identifiers which needs to be excluded from combining. */
public static final String PARAM_NAME_EXCLUDED_RESOURCES =
"org.omnifaces.COMBINED_RESOURCE_HANDLER_EXCLUDED_RESOURCES";
@@ -169,6 +196,7 @@
// Properties -----------------------------------------------------------------------------------------------------
+ private String disabledParam;
private Set<ResourceIdentifier> excludedResources;
private Set<ResourceIdentifier> suppressedResources;
private boolean inlineCSS;
@@ -184,6 +212,7 @@
*/
public CombinedResourceHandler(ResourceHandler wrapped) {
super(wrapped);
+ disabledParam = getInitParameter(PARAM_NAME_DISABLED);
excludedResources = initResources(PARAM_NAME_EXCLUDED_RESOURCES);
excludedResources.addAll(initCDNResources());
suppressedResources = initResources(PARAM_NAME_SUPPRESSED_RESOURCES);
@@ -215,6 +244,10 @@ public boolean isListenerForSource(Object source) {
*/
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
+ if (disabledParam != null && Boolean.valueOf(String.valueOf(evaluateExpressionGet(disabledParam)))) {
+ return;
+ }
+
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot view = context.getViewRoot();
CombinedResourceBuilder builder = new CombinedResourceBuilder();

0 comments on commit 3cda0b2

Please sign in to comment.