Permalink
Browse files

Added Servlets#isFacesDevelopment() and fixed some javadoc.

  • Loading branch information...
1 parent 09f187d commit 82ace56de05e22f2d4d27d0e14acbe1cdadd7ea9 @BalusC BalusC committed Jan 3, 2015
@@ -16,8 +16,6 @@
import static java.util.Collections.unmodifiableSet;
import static java.util.Locale.US;
import static java.util.regex.Pattern.quote;
-import static javax.faces.application.ProjectStage.Development;
-import static javax.faces.application.ProjectStage.PROJECT_STAGE_PARAM_NAME;
import static javax.faces.view.facelets.ResourceResolver.FACELETS_RESOURCE_RESOLVER_PARAM_NAME;
import static org.omnifaces.facesviews.FacesServletDispatchMethod.DO_FILTER;
import static org.omnifaces.util.Faces.getApplicationAttribute;
@@ -31,6 +29,7 @@
import static org.omnifaces.util.ResourcePaths.stripPrefixPath;
import static org.omnifaces.util.Servlets.getApplicationAttribute;
import static org.omnifaces.util.Servlets.getRequestBaseURL;
+import static org.omnifaces.util.Servlets.isFacesDevelopment;
import static org.omnifaces.util.Utils.csvToList;
import static org.omnifaces.util.Utils.isEmpty;
import static org.omnifaces.util.Utils.reverse;
@@ -223,8 +222,7 @@ public static void registerFilter(ServletContext servletContext) {
// See FacesViewsForwardingFilter#init
- if (Development.name().equals(servletContext.getInitParameter(PROJECT_STAGE_PARAM_NAME)) &&
- getFacesServletDispatchMethod(servletContext) != DO_FILTER) {
+ if (isFacesDevelopment(servletContext) && getFacesServletDispatchMethod(servletContext) != DO_FILTER) {
// In development mode map this Filter to "*", so we can catch requests to extensionless resources that
// have been dynamically added. Note that resources with mapped extensions are already handled by the FacesViewsResolver.
@@ -174,10 +174,9 @@
// Constructors ---------------------------------------------------------------------------------------------------
/**
- * Creates a new instance of this CDN resource handler which wraps the given resource handler. If the current JSF
- * project stage is <strong>not</strong> set to <code>Development</code>, then the CDN resources will be initialized
- * based on the {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_RESOURCES} context
- * parameter.
+ * Creates a new instance of this CDN resource handler which wraps the given resource handler. The CDN resources
+ * will be initialized based on the {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_RESOURCES}
+ * context parameter.
* @param wrapped The resource handler to be wrapped.
* @throws IllegalArgumentException When the context parameter is missing or is in invalid format.
*/
@@ -195,8 +194,8 @@ public CDNResourceHandler(ResourceHandler wrapped) {
/**
* Delegate to {@link #createResource(String, String, String)} of the wrapped resource handler. If it returns
- * non-<code>null</code> and the current JSF project stage is <strong>not</strong> set to <code>Development</code>,
- * then the properties file will be consulted if any CDN URL is available for the given resource. If there is none,
+ * non-<code>null</code> 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 JSF default resource, otherwise return a wrapped resource whose
* {@link Resource#getRequestPath()} returns the CDN URL as is been set in the
* {@value org.omnifaces.resourcehandler.CDNResourceHandler#PARAM_NAME_CDN_RESOURCES} context parameter.
@@ -14,6 +14,10 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.regex.Pattern.quote;
+import static javax.faces.application.ProjectStage.Development;
+import static javax.faces.application.ProjectStage.PROJECT_STAGE_JNDI_NAME;
+import static javax.faces.application.ProjectStage.PROJECT_STAGE_PARAM_NAME;
+import static org.omnifaces.util.JNDI.getEnvEntry;
import static org.omnifaces.util.Utils.decodeURL;
import static org.omnifaces.util.Utils.encodeURL;
import static org.omnifaces.util.Utils.isEmpty;
@@ -31,6 +35,7 @@
import java.util.Map.Entry;
import java.util.Set;
+import javax.faces.application.Application;
import javax.faces.application.ResourceHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
@@ -72,6 +77,10 @@
private static final String FACES_AJAX_REDIRECT_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<partial-response><redirect url=\"%s\"></redirect></partial-response>";
+ // Variables ------------------------------------------------------------------------------------------------------
+
+ private static Boolean facesDevelopment;
+
// Constructors ---------------------------------------------------------------------------------------------------
private Servlets() {
@@ -455,6 +464,29 @@ public static boolean isFacesResourceRequest(HttpServletRequest request) {
}
/**
+ * Returns <code>true</code> if we're in JSF development stage. This will be the case when the
+ * <code>javax.faces.PROJECT_STAGE</code> context parameter in <code>web.xml</code> is set to
+ * <code>Development</code>.
+ * @param context The involved servlet context.
+ * @return <code>true</code> if we're in development stage, otherwise <code>false</code>.
+ * @since 2.1
+ * @see Application#getProjectStage()
+ */
+ public static boolean isFacesDevelopment(ServletContext context) {
+ if (facesDevelopment != null) {
+ return facesDevelopment;
+ }
+
+ String projectStage = getEnvEntry(PROJECT_STAGE_JNDI_NAME);
+
+ if (projectStage == null) {
+ projectStage = context.getInitParameter(PROJECT_STAGE_PARAM_NAME);
+ }
+
+ return (facesDevelopment = Development.name().equals(projectStage));
+ }
+
+ /**
* Sends a temporary (302) JSF redirect to the given URL, supporting JSF ajax requests. This does exactly the same
* as {@link Faces#redirect(String, String...)}, but without the need for a {@link FacesContext}. The major
* advantage is that you can perform the job inside a servlet filter or even a plain vanilla servlet, where the

0 comments on commit 82ace56

Please sign in to comment.