From d7c98f95ca4b0b844869f7895f5506a73d33c324 Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Wed, 1 Sep 2004 02:37:55 +0000 Subject: [PATCH] Made FilterToBeanProxy compatible with ContextLoaderServlet (lazy initialisation on first HTTP request). --- changelog.txt | 1 + .../acegisecurity/util/FilterToBeanProxy.java | 55 ++++++++++++++----- .../util/FilterToBeanProxyTests.java | 19 +++++++ 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/changelog.txt b/changelog.txt index 5408a4618e..911ea1bb9c 100644 --- a/changelog.txt +++ b/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 diff --git a/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java b/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java index 50c5be1127..58cce2acd2 100644 --- a/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java +++ b/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java @@ -68,6 +68,15 @@ import javax.servlet.ServletResponse; * * If both initialization parameters are specified, targetBean * takes priority. + * + *

+ * An additional initialization parameter, init, is also + * supported. If set to "lazy" the initialization will take place + * on the first HTTP request, rather than at filter creation time. This makes + * it possible to use FilterToBeanProxy with the Spring + * ContextLoaderServlet. Where possible you should not use this + * initialization parameter, instead using ContextLoaderListener. + *

* * @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 + * ServletContext + * + * @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 - * ServletContext - * - * @return the Spring application context - */ - protected ApplicationContext getContext(FilterConfig filterConfig) { - return WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig - .getServletContext()); - } } diff --git a/core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java b/core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java index f13ca8edea..cb209311b7 100644 --- a/core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java +++ b/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