From 7e85bbc0540a346d78551aec2289ff4446ca441c Mon Sep 17 00:00:00 2001 From: Ben Alex Date: Fri, 16 Apr 2004 12:37:58 +0000 Subject: [PATCH] Relaxed requirement so targetClass OR targetBean can be used (targetBean no longer requires targetClass to be specified as well). --- .../acegisecurity/util/FilterToBeanProxy.java | 77 +++++++++---------- .../util/FilterToBeanProxyTests.java | 20 ++--- docs/reference/src/index.xml | 18 ++--- 3 files changed, 53 insertions(+), 62 deletions(-) diff --git a/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java b/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java index e4321df1ed..50c5be1127 100644 --- a/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java +++ b/core/src/main/java/org/acegisecurity/util/FilterToBeanProxy.java @@ -49,8 +49,8 @@ import javax.servlet.ServletResponse; *

* *

- * To use this filter, it is necessary to specify the following filter - * initialization parameters: + * To use this filter, it is necessary to specify one of the following + * filter initialization parameters: *

* * * + * If both initialization parameters are specified, targetBean + * takes priority. * * @author Ben Alex * @version $Id$ @@ -90,22 +89,6 @@ public class FilterToBeanProxy implements Filter { } public void init(FilterConfig filterConfig) throws ServletException { - String targetClassString = filterConfig.getInitParameter("targetClass"); - - if ((targetClassString == null) || "".equals(targetClassString)) { - throw new ServletException("targetClass must be specified"); - } - - Class targetClass; - - try { - targetClass = Thread.currentThread().getContextClassLoader() - .loadClass(targetClassString); - } catch (ClassNotFoundException ex) { - throw new ServletException("Class of type " + targetClassString - + " not found in classloader"); - } - String targetBean = filterConfig.getInitParameter("targetBean"); if ("".equals(targetBean)) { @@ -114,30 +97,44 @@ public class FilterToBeanProxy implements Filter { ApplicationContext ctx = this.getContext(filterConfig); - Map beans = ctx.getBeansOfType(targetClass, true, true); - - if (beans.size() == 0) { - throw new ServletException( - "Bean context must contain at least one bean of type " - + targetClassString); - } - String beanName = null; - if (targetBean == null) { - // Use first bean found - beanName = (String) beans.keySet().iterator().next(); + if ((targetBean != null) && ctx.containsBean(targetBean)) { + beanName = targetBean; + } else if (targetBean != null) { + throw new ServletException("targetBean '" + targetBean + + "' not found in context"); } else { - // Use the requested bean, providing it can be found - if (beans.containsKey(targetBean)) { - beanName = targetBean; - } else { - throw new ServletException("Bean with name '" + targetBean - + "' cannot be found in bean context"); + String targetClassString = filterConfig.getInitParameter( + "targetClass"); + + if ((targetClassString == null) || "".equals(targetClassString)) { + throw new ServletException( + "targetClass or targetBean must be specified"); } + + Class targetClass; + + try { + targetClass = Thread.currentThread().getContextClassLoader() + .loadClass(targetClassString); + } catch (ClassNotFoundException ex) { + throw new ServletException("Class of type " + targetClassString + + " not found in classloader"); + } + + Map beans = ctx.getBeansOfType(targetClass, true, true); + + if (beans.size() == 0) { + throw new ServletException( + "Bean context must contain at least one bean of type " + + targetClassString); + } + + beanName = (String) beans.keySet().iterator().next(); } - Object object = beans.get(beanName); + Object object = ctx.getBean(beanName); if (!(object instanceof Filter)) { throw new ServletException("Bean '" + beanName diff --git a/core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java b/core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java index 6dab01e474..f13ca8edea 100644 --- a/core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java +++ b/core/src/test/java/org/acegisecurity/util/FilterToBeanProxyTests.java @@ -78,10 +78,9 @@ public class FilterToBeanProxyTests extends TestCase { } } - public void testDetectsMissingTargetClass() throws Exception { + public void testDetectsNeitherPropertyBeingSet() throws Exception { // Setup our filter MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetBean", "mockFilter"); FilterToBeanProxy filter = new MockFilterToBeanProxy( "net/sf/acegisecurity/util/filtertest-valid.xml"); @@ -90,7 +89,8 @@ public class FilterToBeanProxyTests extends TestCase { filter.init(config); fail("Should have thrown ServletException"); } catch (ServletException expected) { - assertEquals("targetClass must be specified", expected.getMessage()); + assertEquals("targetClass or targetBean must be specified", + expected.getMessage()); } } @@ -116,8 +116,6 @@ public class FilterToBeanProxyTests extends TestCase { throws Exception { // Setup our filter MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetClass", - "net.sf.acegisecurity.util.MockFilter"); config.setInitParmeter("targetBean", "WRONG_NAME"); FilterToBeanProxy filter = new MockFilterToBeanProxy( @@ -127,7 +125,7 @@ public class FilterToBeanProxyTests extends TestCase { filter.init(config); fail("Should have thrown ServletException"); } catch (ServletException expected) { - assertEquals("Bean with name 'WRONG_NAME' cannot be found in bean context", + assertEquals("targetBean 'WRONG_NAME' not found in context", expected.getMessage()); } } @@ -171,11 +169,11 @@ public class FilterToBeanProxyTests extends TestCase { chain); } - public void testNormalOperationWithDefault() throws Exception { + public void testNormalOperationWithSpecificBeanName() + throws Exception { // Setup our filter MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetClass", - "net.sf.acegisecurity.util.MockFilter"); + config.setInitParmeter("targetBean", "mockFilter"); // Setup our expectation that the filter chain will be invoked MockFilterChain chain = new MockFilterChain(true); @@ -190,13 +188,11 @@ public class FilterToBeanProxyTests extends TestCase { chain); } - public void testNormalOperationWithSpecificBeanName() - throws Exception { + public void testNormalOperationWithTargetClass() throws Exception { // Setup our filter MockFilterConfig config = new MockFilterConfig(); config.setInitParmeter("targetClass", "net.sf.acegisecurity.util.MockFilter"); - config.setInitParmeter("targetBean", "mockFilter"); // Setup our expectation that the filter chain will be invoked MockFilterChain chain = new MockFilterChain(true); diff --git a/docs/reference/src/index.xml b/docs/reference/src/index.xml index 7412e24e9f..823af44c9f 100644 --- a/docs/reference/src/index.xml +++ b/docs/reference/src/index.xml @@ -525,15 +525,13 @@ delegate the Filter's methods through to a bean which is obtained from the Spring application context. This enables the bean to benefit from the Spring application context lifecycle - support and configuration flexibility. The + support and configuration flexibility. FilterToBeanProxy only requires a single - initialization parameter, targetClass, which will - be used to identify the bean in the application context. In the - unlikely event there is more than one bean in the application context - that matches this class, the targetBean - initialization parameter should be used. This parameter simply - represents the name of the bean in the application context. Like - standard Spring web applications, the + initialization parameter, targetClass or + targetBean. The targetClass + parameter locates the first object in the application context of the + specified class, whilst targetBean locates the + object by bean name. Like standard Spring web applications, the FilterToBeanProxy accesses the application context via WebApplicationContextUtils.getWebApplicationContext(ServletContext), @@ -1569,8 +1567,8 @@ public boolean supports(Class clazz); standard authentication of web browser users, we recommend HTTP Session Authentication). The standard governing HTTP Basic Authentication is defined by RFC 1945, Section 11, and the - BasicProcessingFilter conforms with this RFC. - + BasicProcessingFilter conforms with this + RFC. To implement HTTP Basic Authentication, it is necessary to add the following filter to web.xml, behind a