Explorar el Código

Made FilterToBeanProxy compatible with ContextLoaderServlet (lazy initialisation on first HTTP request).

Ben Alex hace 21 años
padre
commit
d7c98f95ca

+ 1 - 0
changelog.txt

@@ -5,6 +5,7 @@ Changes in version 0.x (2004-xx-xx)
 * Added Authentication.getDetails() to DaoAuthenticationProvider response
 * Added DaoAuthenticationProvider.hideUserNotFoundExceptions (default=true)
 * Added PasswordAuthenticationProvider for password-validating DAOs (eg LDAP)
+* Added FilterToBeanProxy compatibility with ContextLoaderServlet (lazy inits)
 * Extracted removeUserFromCache(String) to UserCache interface
 * Improved ConfigAttributeEditor so it trims preceding and trailing spaces
 * Fixed EH-CACHE-based caching implementation behaviour when cache exists

+ 42 - 13
core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java

@@ -68,6 +68,15 @@ import javax.servlet.ServletResponse;
  * 
  * If both initialization parameters are specified, <code>targetBean</code>
  * takes priority.
+ * 
+ * <P>
+ * An additional initialization parameter, <code>init</code>, is also
+ * supported. If set to "<code>lazy</code>" the initialization will take place
+ * on the first HTTP request, rather than at filter creation time. This makes
+ * it possible to use <code>FilterToBeanProxy</code> with the Spring
+ * <code>ContextLoaderServlet</code>. Where possible you should not use this
+ * initialization parameter, instead using <code>ContextLoaderListener</code>.
+ * </p>
  *
  * @author Ben Alex
  * @version $Id$
@@ -76,6 +85,8 @@ public class FilterToBeanProxy implements Filter {
     //~ Instance fields ========================================================
 
     private Filter delegate;
+    private FilterConfig filterConfig;
+    private boolean initialized = false;
 
     //~ Methods ================================================================
 
@@ -85,10 +96,41 @@ public class FilterToBeanProxy implements Filter {
 
     public void doFilter(ServletRequest request, ServletResponse response,
         FilterChain chain) throws IOException, ServletException {
+        if (!initialized) {
+            doInit();
+        }
+
         delegate.doFilter(request, response, chain);
     }
 
     public void init(FilterConfig filterConfig) throws ServletException {
+        this.filterConfig = filterConfig;
+
+        String strategy = filterConfig.getInitParameter("init");
+
+        if ((strategy != null) && strategy.toLowerCase().equals("lazy")) {
+            return;
+        }
+
+        doInit();
+    }
+
+    /**
+     * Allows test cases to override where application context obtained from.
+     *
+     * @param filterConfig which can be used to find the
+     *        <code>ServletContext</code>
+     *
+     * @return the Spring application context
+     */
+    protected ApplicationContext getContext(FilterConfig filterConfig) {
+        return WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig
+            .getServletContext());
+    }
+
+    private void doInit() throws ServletException {
+        initialized = true;
+
         String targetBean = filterConfig.getInitParameter("targetBean");
 
         if ("".equals(targetBean)) {
@@ -145,17 +187,4 @@ public class FilterToBeanProxy implements Filter {
 
         delegate.init(filterConfig);
     }
-
-    /**
-     * Allows test cases to override where application context obtained from.
-     *
-     * @param filterConfig which can be used to find the
-     *        <code>ServletContext</code>
-     *
-     * @return the Spring application context
-     */
-    protected ApplicationContext getContext(FilterConfig filterConfig) {
-        return WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig
-            .getServletContext());
-    }
 }

+ 19 - 0
core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java

@@ -169,6 +169,25 @@ public class FilterToBeanProxyTests extends TestCase {
             chain);
     }
 
+    public void testNormalOperationWithLazyTrue() throws Exception {
+        // Setup our filter
+        MockFilterConfig config = new MockFilterConfig();
+        config.setInitParmeter("targetBean", "mockFilter");
+        config.setInitParmeter("init", "lazy");
+
+        // Setup our expectation that the filter chain will be invoked
+        MockFilterChain chain = new MockFilterChain(true);
+
+        MockHttpServletResponse response = new MockHttpServletResponse();
+        MockHttpServletRequest request = new MockHttpServletRequest("/go");
+
+        FilterToBeanProxy filter = new MockFilterToBeanProxy(
+                "net/sf/acegisecurity/util/filtertest-valid.xml");
+
+        executeFilterInContainerSimulator(config, filter, request, response,
+            chain);
+    }
+
     public void testNormalOperationWithSpecificBeanName()
         throws Exception {
         // Setup our filter