From aec23749d728fa456ba91e1373e0e5ada30cde21 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Fri, 12 Dec 2008 13:04:37 +0000 Subject: [PATCH] SEC-1056: Remove deprecated FilterToBeanProxy: It's gone --- .../security/util/FilterToBeanProxy.java | 193 -------------- .../security/util/FilterToBeanProxyTests.java | 249 ------------------ 2 files changed, 442 deletions(-) delete mode 100644 core/src/main/java/org/springframework/security/util/FilterToBeanProxy.java delete mode 100644 core/src/test/java/org/springframework/security/util/FilterToBeanProxyTests.java diff --git a/core/src/main/java/org/springframework/security/util/FilterToBeanProxy.java b/core/src/main/java/org/springframework/security/util/FilterToBeanProxy.java deleted file mode 100644 index 5e24eb9ab2..0000000000 --- a/core/src/main/java/org/springframework/security/util/FilterToBeanProxy.java +++ /dev/null @@ -1,193 +0,0 @@ -/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.security.util; - -import org.springframework.beans.factory.BeanFactoryUtils; - -import org.springframework.context.ApplicationContext; - -import org.springframework.web.context.support.WebApplicationContextUtils; - -import java.io.IOException; - -import java.util.Map; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - - -/** - *

Delegates Filter requests to a Spring-managed bean.

- * - *

This class acts as a proxy on behalf of a - * target Filter that is defined in the Spring bean context. It is necessary to specify which target - * Filter should be proxied as a filter initialization parameter.

- * - *

On filter initialisation, the class will use Spring's {@link - * WebApplicationContextUtils#getWebApplicationContext(ServletContext sc)} method to obtain an - * ApplicationContext instance. It will expect to find the target Filter in this - * ApplicationContext.

- * - *

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.

- * - *

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.

- * - *

A final optional initialization parameter, lifecycle, determines whether the servlet container - * or the IoC container manages the lifecycle of the proxied filter. When possible you should write your filters to be - * managed via the IoC container interfaces such as {@link org.springframework.beans.factory.InitializingBean} and - * {@link org.springframework.beans.factory.DisposableBean}. If you cannot control the filters you wish to proxy (eg - * you do not have their source code) you might need to allow the servlet container to manage lifecycle via the {@link - * javax.servlet.Filter#init(javax.servlet.FilterConfig)} and {@link javax.servlet.Filter#destroy()} methods. If this - * case, set the lifecycle initialization parameter to servlet-container-managed. If the - * parameter is any other value, servlet container lifecycle methods will not be delegated through to the proxy.

- * - * @deprecated use DelegatingFilterProxy instead - * @author Ben Alex - * @version $Id$ - */ -@SuppressWarnings("unchecked") -public class FilterToBeanProxy implements Filter { - //~ Instance fields ================================================================================================ - - private Filter delegate; - private FilterConfig filterConfig; - private boolean initialized = false; - private boolean servletContainerManaged = false; - - //~ Methods ======================================================================================================== - - public void destroy() { - if ((delegate != null) && servletContainerManaged) { - delegate.destroy(); - } - } - - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - if (!initialized) { - doInit(); - } - - delegate.doFilter(request, response, chain); - } - - private synchronized void doInit() throws ServletException { - if (initialized) { - // already initialized, so don't re-initialize - return; - } - - String targetBean = filterConfig.getInitParameter("targetBean"); - - if ("".equals(targetBean)) { - targetBean = null; - } - - String lifecycle = filterConfig.getInitParameter("lifecycle"); - - if ("servlet-container-managed".equals(lifecycle)) { - servletContainerManaged = true; - } - - ApplicationContext ctx = this.getContext(filterConfig); - - String beanName = null; - - if ((targetBean != null) && ctx.containsBean(targetBean)) { - beanName = targetBean; - } else if (targetBean != null) { - throw new ServletException("targetBean '" + targetBean + "' not found in context"); - } else { - 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 = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx, 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 = ctx.getBean(beanName); - - if (!(object instanceof Filter)) { - throw new ServletException("Bean '" + beanName + "' does not implement javax.servlet.Filter"); - } - - delegate = (Filter) object; - - if (servletContainerManaged) { - delegate.init(filterConfig); - } - - // Set initialized to true at the end of the synchronized method, so - // that invocations of doFilter() before this method has completed will not - // cause NullPointerException - initialized = true; - } - - /** - * 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()); - } - - public void init(FilterConfig filterConfig) throws ServletException { - this.filterConfig = filterConfig; - - String strategy = filterConfig.getInitParameter("init"); - - if ((strategy != null) && strategy.toLowerCase().equals("lazy")) { - return; - } - - doInit(); - } -} diff --git a/core/src/test/java/org/springframework/security/util/FilterToBeanProxyTests.java b/core/src/test/java/org/springframework/security/util/FilterToBeanProxyTests.java deleted file mode 100644 index 71182b37d6..0000000000 --- a/core/src/test/java/org/springframework/security/util/FilterToBeanProxyTests.java +++ /dev/null @@ -1,249 +0,0 @@ -/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.security.util; - -import junit.framework.TestCase; - -import org.springframework.security.MockFilterConfig; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; - -import java.io.IOException; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - - -/** - * Tests {@link FilterToBeanProxy}. - * - * @author Ben Alex - * @version $Id$ - */ -public class FilterToBeanProxyTests extends TestCase { - //~ Constructors =================================================================================================== - - public FilterToBeanProxyTests() { - super(); - } - - public FilterToBeanProxyTests(String arg0) { - super(arg0); - } - - //~ Methods ======================================================================================================== - - private void executeFilterInContainerSimulator(FilterConfig filterConfig, Filter filter, ServletRequest request, - ServletResponse response, FilterChain filterChain) - throws ServletException, IOException { - filter.init(filterConfig); - filter.doFilter(request, response, filterChain); - filter.destroy(); - } - - public static void main(String[] args) { - junit.textui.TestRunner.run(FilterToBeanProxyTests.class); - } - - public final void setUp() throws Exception { - super.setUp(); - } - - public void testDetectsClassNotInClassLoader() throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetClass", "net.sf.DOES.NOT.EXIST"); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - try { - filter.init(config); - fail("Should have thrown ServletException"); - } catch (ServletException expected) { - assertEquals("Class of type net.sf.DOES.NOT.EXIST not found in classloader", expected.getMessage()); - } - } - - public void testDetectsNeitherPropertyBeingSet() throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - try { - filter.init(config); - fail("Should have thrown ServletException"); - } catch (ServletException expected) { - assertEquals("targetClass or targetBean must be specified", expected.getMessage()); - } - } - - public void testDetectsTargetBeanIsNotAFilter() throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetClass", "org.springframework.security.util.MockNotAFilter"); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - try { - filter.init(config); - fail("Should have thrown ServletException"); - } catch (ServletException expected) { - assertEquals("Bean 'mockNotAFilter' does not implement javax.servlet.Filter", expected.getMessage()); - } - } - - public void testDetectsTargetBeanNotInBeanContext() - throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetBean", "WRONG_NAME"); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - try { - filter.init(config); - fail("Should have thrown ServletException"); - } catch (ServletException expected) { - assertEquals("targetBean 'WRONG_NAME' not found in context", expected.getMessage()); - } - } - - public void testDetectsTargetClassNotInBeanContext() - throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetClass", "org.springframework.security.util.FilterToBeanProxyTests"); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - try { - filter.init(config); - fail("Should have thrown ServletException"); - } catch (ServletException expected) { - assertEquals("Bean context must contain at least one bean of type org.springframework.security.util.FilterToBeanProxyTests", - expected.getMessage()); - } - } - - public void testIgnoresEmptyTargetBean() throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetClass", "org.springframework.security.util.MockFilter"); - config.setInitParmeter("targetBean", ""); - - // Setup our expectation that the filter chain will be invoked - MockFilterChain chain = new MockFilterChain(true); - - MockHttpServletResponse response = new MockHttpServletResponse(); - MockHttpServletRequest request = new MockHttpServletRequest(); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - executeFilterInContainerSimulator(config, filter, request, response, 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(); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - executeFilterInContainerSimulator(config, filter, request, response, chain); - } - - public void testNormalOperationWithSpecificBeanName() - throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetBean", "mockFilter"); - - // Setup our expectation that the filter chain will be invoked - MockFilterChain chain = new MockFilterChain(true); - - MockHttpServletResponse response = new MockHttpServletResponse(); - MockHttpServletRequest request = new MockHttpServletRequest(); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - executeFilterInContainerSimulator(config, filter, request, response, chain); - } - - public void testNormalOperationWithTargetClass() throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetClass", "org.springframework.security.util.MockFilter"); - - // Setup our expectation that the filter chain will be invoked - MockFilterChain chain = new MockFilterChain(true); - - MockHttpServletResponse response = new MockHttpServletResponse(); - MockHttpServletRequest request = new MockHttpServletRequest(); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - executeFilterInContainerSimulator(config, filter, request, response, chain); - } - - public void testNullDelegateDoesNotCauseNullPointerException() - throws Exception { - // Setup our filter - MockFilterConfig config = new MockFilterConfig(); - config.setInitParmeter("targetBean", "aFilterThatDoesntExist"); - 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(); - - FilterToBeanProxy filter = new MockFilterToBeanProxy("org/springframework/security/util/filtertest-valid.xml"); - - // do not init (which would hapen if called .doFilter) - filter.destroy(); - } - - //~ Inner Classes ================================================================================================== - - private class MockFilterToBeanProxy extends FilterToBeanProxy { - private String appContextLocation; - - public MockFilterToBeanProxy(String appContextLocation) { - this.appContextLocation = appContextLocation; - } - - protected ApplicationContext getContext(FilterConfig filterConfig) { - return new ClassPathXmlApplicationContext(appContextLocation); - } - } -}