Relaxed requirement so targetClass OR targetBean can be used (targetBean no longer requires targetClass to be specified as well).

This commit is contained in:
Ben Alex 2004-04-16 12:37:58 +00:00
parent 38835da164
commit 7e85bbc054
3 changed files with 53 additions and 62 deletions

View File

@ -49,8 +49,8 @@ import javax.servlet.ServletResponse;
* </p> * </p>
* *
* <p> * <p>
* To use this filter, it is necessary to specify the following filter * To use this filter, it is necessary to specify <b>one</b> of the following
* initialization parameters: * filter initialization parameters:
* </p> * </p>
* *
* <ul> * <ul>
@ -62,13 +62,12 @@ import javax.servlet.ServletResponse;
* <code>ApplicationContext</code>. * <code>ApplicationContext</code>.
* </li> * </li>
* <li> * <li>
* <code>targetBean</code> (optional) indicates the bean name of the target * <code>targetBean</code> indicates the bean name of the target class.
* class. This parameter should be specified if there is more than one bean in
* the <code>ApplicationContext</code> of the same type as defined by the
* <code>targetClass</code> parameter.
* </li> * </li>
* </ul> * </ul>
* *
* If both initialization parameters are specified, <code>targetBean</code>
* takes priority.
* *
* @author Ben Alex * @author Ben Alex
* @version $Id$ * @version $Id$
@ -90,22 +89,6 @@ public class FilterToBeanProxy implements Filter {
} }
public void init(FilterConfig filterConfig) throws ServletException { 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"); String targetBean = filterConfig.getInitParameter("targetBean");
if ("".equals(targetBean)) { if ("".equals(targetBean)) {
@ -114,30 +97,44 @@ public class FilterToBeanProxy implements Filter {
ApplicationContext ctx = this.getContext(filterConfig); 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; String beanName = null;
if (targetBean == null) { if ((targetBean != null) && ctx.containsBean(targetBean)) {
// Use first bean found beanName = targetBean;
beanName = (String) beans.keySet().iterator().next(); } else if (targetBean != null) {
throw new ServletException("targetBean '" + targetBean
+ "' not found in context");
} else { } else {
// Use the requested bean, providing it can be found String targetClassString = filterConfig.getInitParameter(
if (beans.containsKey(targetBean)) { "targetClass");
beanName = targetBean;
} else { if ((targetClassString == null) || "".equals(targetClassString)) {
throw new ServletException("Bean with name '" + targetBean throw new ServletException(
+ "' cannot be found in bean context"); "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)) { if (!(object instanceof Filter)) {
throw new ServletException("Bean '" + beanName throw new ServletException("Bean '" + beanName

View File

@ -78,10 +78,9 @@ public class FilterToBeanProxyTests extends TestCase {
} }
} }
public void testDetectsMissingTargetClass() throws Exception { public void testDetectsNeitherPropertyBeingSet() throws Exception {
// Setup our filter // Setup our filter
MockFilterConfig config = new MockFilterConfig(); MockFilterConfig config = new MockFilterConfig();
config.setInitParmeter("targetBean", "mockFilter");
FilterToBeanProxy filter = new MockFilterToBeanProxy( FilterToBeanProxy filter = new MockFilterToBeanProxy(
"net/sf/acegisecurity/util/filtertest-valid.xml"); "net/sf/acegisecurity/util/filtertest-valid.xml");
@ -90,7 +89,8 @@ public class FilterToBeanProxyTests extends TestCase {
filter.init(config); filter.init(config);
fail("Should have thrown ServletException"); fail("Should have thrown ServletException");
} catch (ServletException expected) { } 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 { throws Exception {
// Setup our filter // Setup our filter
MockFilterConfig config = new MockFilterConfig(); MockFilterConfig config = new MockFilterConfig();
config.setInitParmeter("targetClass",
"net.sf.acegisecurity.util.MockFilter");
config.setInitParmeter("targetBean", "WRONG_NAME"); config.setInitParmeter("targetBean", "WRONG_NAME");
FilterToBeanProxy filter = new MockFilterToBeanProxy( FilterToBeanProxy filter = new MockFilterToBeanProxy(
@ -127,7 +125,7 @@ public class FilterToBeanProxyTests extends TestCase {
filter.init(config); filter.init(config);
fail("Should have thrown ServletException"); fail("Should have thrown ServletException");
} catch (ServletException expected) { } 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()); expected.getMessage());
} }
} }
@ -171,11 +169,11 @@ public class FilterToBeanProxyTests extends TestCase {
chain); chain);
} }
public void testNormalOperationWithDefault() throws Exception { public void testNormalOperationWithSpecificBeanName()
throws Exception {
// Setup our filter // Setup our filter
MockFilterConfig config = new MockFilterConfig(); MockFilterConfig config = new MockFilterConfig();
config.setInitParmeter("targetClass", config.setInitParmeter("targetBean", "mockFilter");
"net.sf.acegisecurity.util.MockFilter");
// Setup our expectation that the filter chain will be invoked // Setup our expectation that the filter chain will be invoked
MockFilterChain chain = new MockFilterChain(true); MockFilterChain chain = new MockFilterChain(true);
@ -190,13 +188,11 @@ public class FilterToBeanProxyTests extends TestCase {
chain); chain);
} }
public void testNormalOperationWithSpecificBeanName() public void testNormalOperationWithTargetClass() throws Exception {
throws Exception {
// Setup our filter // Setup our filter
MockFilterConfig config = new MockFilterConfig(); MockFilterConfig config = new MockFilterConfig();
config.setInitParmeter("targetClass", config.setInitParmeter("targetClass",
"net.sf.acegisecurity.util.MockFilter"); "net.sf.acegisecurity.util.MockFilter");
config.setInitParmeter("targetBean", "mockFilter");
// Setup our expectation that the filter chain will be invoked // Setup our expectation that the filter chain will be invoked
MockFilterChain chain = new MockFilterChain(true); MockFilterChain chain = new MockFilterChain(true);

View File

@ -525,15 +525,13 @@
delegate the <literal>Filter</literal>'s methods through to a bean delegate the <literal>Filter</literal>'s methods through to a bean
which is obtained from the Spring application context. This enables which is obtained from the Spring application context. This enables
the bean to benefit from the Spring application context lifecycle the bean to benefit from the Spring application context lifecycle
support and configuration flexibility. The support and configuration flexibility.
<literal>FilterToBeanProxy</literal> only requires a single <literal>FilterToBeanProxy</literal> only requires a single
initialization parameter, <literal>targetClass</literal>, which will initialization parameter, <literal>targetClass</literal> or
be used to identify the bean in the application context. In the <literal>targetBean</literal>. The <literal>targetClass</literal>
unlikely event there is more than one bean in the application context parameter locates the first object in the application context of the
that matches this class, the <literal>targetBean</literal> specified class, whilst <literal>targetBean</literal> locates the
initialization parameter should be used. This parameter simply object by bean name. Like standard Spring web applications, the
represents the name of the bean in the application context. Like
standard Spring web applications, the
<literal>FilterToBeanProxy</literal> accesses the application context <literal>FilterToBeanProxy</literal> accesses the application context
via<literal> via<literal>
WebApplicationContextUtils.getWebApplicationContext(ServletContext)</literal>, WebApplicationContextUtils.getWebApplicationContext(ServletContext)</literal>,
@ -1569,8 +1567,8 @@ public boolean supports(Class clazz);</programlisting></para>
standard authentication of web browser users, we recommend HTTP standard authentication of web browser users, we recommend HTTP
Session Authentication). The standard governing HTTP Basic Session Authentication). The standard governing HTTP Basic
Authentication is defined by RFC 1945, Section 11, and the Authentication is defined by RFC 1945, Section 11, and the
<literal>BasicProcessingFilter</literal> conforms with this RFC. <literal>BasicProcessingFilter</literal> conforms with this
</para> RFC.</para>
<para>To implement HTTP Basic Authentication, it is necessary to add <para>To implement HTTP Basic Authentication, it is necessary to add
the following filter to <literal>web.xml</literal>, behind a the following filter to <literal>web.xml</literal>, behind a