From 928498b53df8db9ddaa32d9508934413001e4a11 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Sat, 30 Oct 2004 22:45:35 +0000 Subject: [PATCH] Removed AutoIntegrationFilter --- .../ui/AutoIntegrationFilter.java | 137 ---------- .../ui/AutoIntegrationFilterTests.java | 238 ------------------ 2 files changed, 375 deletions(-) delete mode 100644 core/src/main/java/org/acegisecurity/ui/AutoIntegrationFilter.java delete mode 100644 core/src/test/java/org/acegisecurity/ui/AutoIntegrationFilterTests.java diff --git a/core/src/main/java/org/acegisecurity/ui/AutoIntegrationFilter.java b/core/src/main/java/org/acegisecurity/ui/AutoIntegrationFilter.java deleted file mode 100644 index ae6d17975f..0000000000 --- a/core/src/main/java/org/acegisecurity/ui/AutoIntegrationFilter.java +++ /dev/null @@ -1,137 +0,0 @@ -/* Copyright 2004 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 net.sf.acegisecurity.ui; - -import net.sf.acegisecurity.Authentication; -import net.sf.acegisecurity.adapters.HttpRequestIntegrationFilter; -import net.sf.acegisecurity.adapters.jboss.JbossIntegrationFilter; -import net.sf.acegisecurity.ui.webapp.HttpSessionIntegrationFilter; - -import javax.servlet.ServletRequest; -import javax.servlet.http.HttpServletRequest; - - -/** - * Detects the container and delegates to the appropriate {@link - * AbstractIntegrationFilter}. - * - *

- * This eases the creation of portable, secure applications, as the - * web.xml will not need to refer to a specific integration - * filter. - *

- * - *

- * The filter automatically delegates to {@link HttpRequestIntegrationFilter} - * if any Authentication object is detected in the - * ServletRequest. Failing this, it will delegate to {@link - * HttpSessionIntegrationFilter} if the session object contains an - * Authentication object. Finally, this filter will delegate to - * {@link JbossIntegrationFilter} if the ServletRequest contains - * an instance of JBoss' SimplePrincipal. - *

- * - *

- * If no location can be found containing the Authentication - * object, it will return null. - *

- * - * @author Ben Alex - * @version $Id$ - * - * @see AbstractIntegrationFilter - */ -public class AutoIntegrationFilter extends AbstractIntegrationFilter { - //~ Methods ================================================================ - - public void commitToContainer(ServletRequest request, - Authentication authentication) { - if (request instanceof HttpServletRequest) { - HttpServletRequest httpRequest = (HttpServletRequest) request; - - if (getHttpSessionIntegrationFilter().extractFromContainer(request) != null) { - getHttpSessionIntegrationFilter().commitToContainer(request, - authentication); - - return; - } - - // Do not try JbossIntegrationFilter, as commit is unsupported. - // Do not try HttpRequestIntegrationFilter, as commit is unsupported. - } - } - - public Object extractFromContainer(ServletRequest request) { - if (request instanceof HttpServletRequest) { - HttpServletRequest httpRequest = (HttpServletRequest) request; - - if (httpRequest.getUserPrincipal() instanceof Authentication) { - return getHttpServletRequest().extractFromContainer(request); - } - - if (getHttpSessionIntegrationFilter().extractFromContainer(request) != null) { - return getHttpSessionIntegrationFilter().extractFromContainer(request); - } - - try { - Class simplePrincipalClass = Class.forName( - "org.jboss.security.SimplePrincipal"); - - if (null != httpRequest.getUserPrincipal()) { - if (simplePrincipalClass.isAssignableFrom( - httpRequest.getUserPrincipal().getClass())) { - return getJbossIntegrationFilter().extractFromContainer(request); - } - } - } catch (ClassNotFoundException e) { - // Can't be JBoss principal - // Expected, and normal - fall through - } - } - - return null; - } - - /** - * Allows test case to override the source of - * HttpRequestIntegrationFilter. - * - * @return the HttpRequestIntegrationFilter to use - */ - protected HttpRequestIntegrationFilter getHttpServletRequest() { - return new HttpRequestIntegrationFilter(); - } - - /** - * Allows test case to override the source of - * HttpSessionIntegrationFilter. - * - * @return the HttpRequestIntegrationFilter to use - */ - protected HttpSessionIntegrationFilter getHttpSessionIntegrationFilter() { - return new HttpSessionIntegrationFilter(); - } - - /** - * Allows test case to override the source of - * JbossIntegrationFilter. - * - * @return the JbossIntegrationFilter to use - */ - protected JbossIntegrationFilter getJbossIntegrationFilter() { - return new JbossIntegrationFilter(); - } -} diff --git a/core/src/test/java/org/acegisecurity/ui/AutoIntegrationFilterTests.java b/core/src/test/java/org/acegisecurity/ui/AutoIntegrationFilterTests.java deleted file mode 100644 index 3f59868a3f..0000000000 --- a/core/src/test/java/org/acegisecurity/ui/AutoIntegrationFilterTests.java +++ /dev/null @@ -1,238 +0,0 @@ -/* Copyright 2004 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 net.sf.acegisecurity.ui; - -import junit.framework.TestCase; - -import net.sf.acegisecurity.Authentication; -import net.sf.acegisecurity.GrantedAuthority; -import net.sf.acegisecurity.GrantedAuthorityImpl; -import net.sf.acegisecurity.MockHttpServletRequest; -import net.sf.acegisecurity.adapters.MockPrincipal; -import net.sf.acegisecurity.adapters.PrincipalAcegiUserToken; -import net.sf.acegisecurity.adapters.jboss.JbossIntegrationFilter; -import net.sf.acegisecurity.adapters.jboss.MockInitialContext; -import net.sf.acegisecurity.adapters.jboss.MockJbossIntegrationFilter; -import net.sf.acegisecurity.ui.webapp.HttpSessionIntegrationFilter; - -import org.jboss.security.SimplePrincipal; - -import java.security.Principal; - -import java.util.HashSet; -import java.util.Set; - -import javax.naming.Context; - -import javax.security.auth.Subject; - -import javax.servlet.ServletRequest; - - -/** - * Tests {@link AutoIntegrationFilter}. - * - * @author Ben Alex - * @version $Id$ - */ -public class AutoIntegrationFilterTests extends TestCase { - //~ Constructors =========================================================== - - public AutoIntegrationFilterTests() { - super(); - } - - public AutoIntegrationFilterTests(String arg0) { - super(arg0); - } - - //~ Methods ================================================================ - - public final void setUp() throws Exception { - super.setUp(); - } - - public static void main(String[] args) { - junit.textui.TestRunner.run(AutoIntegrationFilterTests.class); - } - - public void testCommitForHttpSession() { - // This is the object we want to commit - PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", - "someone", "password", - new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); - - // Setup the mock so AutoIntegrationFilter detects HttpSessionIntegrationFilter should be used - MockHttpSessionIntegrationFilter mockHttpSessionIntegrationFilter = new MockHttpSessionIntegrationFilter(new PrincipalAcegiUserToken( - "x", "x", "x", - new GrantedAuthority[] {new GrantedAuthorityImpl("x")})); - - // Setup the AutoIntegrationFilter to use our mock HttpSessionIntegrationFilter object - AutoIntegrationFilter filter = new MockAutoIntegrationFilterHttpSession(mockHttpSessionIntegrationFilter); - - // Test we can commit the new object (this will override the principal with "x" in its properties) - filter.commitToContainer(new MockHttpServletRequest("ignored"), - principal); - - // Test the object was indeed committed and overwrote the principal with "x" in its properties - assertEquals(principal, - mockHttpSessionIntegrationFilter.extractFromContainer( - new MockHttpServletRequest("ignored"))); - } - - public void testDetectsAuthenticationObjectInHttpRequest() { - AutoIntegrationFilter filter = new AutoIntegrationFilter(); - PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", - "someone", "password", - new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); - Object result = filter.extractFromContainer(new MockHttpServletRequest( - principal, null)); - - if (!(result instanceof PrincipalAcegiUserToken)) { - fail("Should have returned PrincipalAcegiUserToken"); - } - - PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result; - assertEquals(principal, result); - } - - public void testDetectsAuthenticationObjectInHttpSession() { - PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", - "someone", "password", - new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); - - AutoIntegrationFilter filter = new MockAutoIntegrationFilterHttpSession(new MockHttpSessionIntegrationFilter( - principal)); - Object result = filter.extractFromContainer(new MockHttpServletRequest( - null, null)); - - if (!(result instanceof PrincipalAcegiUserToken)) { - fail("Should have returned PrincipalAcegiUserToken"); - } - - PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result; - assertEquals(principal, result); - } - - public void testDetectsAuthenticationObjectInJboss() { - // Prepare a mock Jboss environment reflecting completed authentication - PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", - "someone", "password", - new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); - Context context = new MockInitialContext(makeIntoSubject(principal)); - JbossIntegrationFilter jbossFilter = new MockJbossIntegrationFilter(context); - - // Create a SimplePrincipal, which is what JBoss places into HttpRequest - SimplePrincipal simplePrincipal = new SimplePrincipal("TEST"); - - // Now try to extract authentication information via our mock AutoIntegrationFilter - AutoIntegrationFilter filter = new MockAutoIntegrationFilterJboss(jbossFilter); - Object result = filter.extractFromContainer(new MockHttpServletRequest( - simplePrincipal, null)); - - System.out.println(result); - - if (!(result instanceof PrincipalAcegiUserToken)) { - fail("Should have returned PrincipalAcegiUserToken"); - } - - PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result; - assertEquals(principal, result); - } - - public void testHandlesIfHttpRequestIsNullForSomeReason() { - AutoIntegrationFilter filter = new AutoIntegrationFilter(); - assertEquals(null, filter.extractFromContainer(null)); - } - - public void testHandlesIfThereIsNoPrincipal() { - AutoIntegrationFilter filter = new AutoIntegrationFilter(); - assertEquals(null, - filter.extractFromContainer(new MockHttpServletRequest(null, null))); - } - - public void testReturnsNullIfNonAuthenticationObjectInHttpRequest() { - AutoIntegrationFilter filter = new AutoIntegrationFilter(); - assertEquals(null, - filter.extractFromContainer( - new MockHttpServletRequest(new MockPrincipal(), null))); - } - - private Subject makeIntoSubject(Principal principal) { - Set principals = new HashSet(); - principals.add(principal); - - return new Subject(false, principals, new HashSet(), new HashSet()); - } - - //~ Inner Classes ========================================================== - - private class MockAutoIntegrationFilterHttpSession - extends AutoIntegrationFilter { - private HttpSessionIntegrationFilter filter; - - public MockAutoIntegrationFilterHttpSession( - HttpSessionIntegrationFilter filter) { - this.filter = filter; - } - - private MockAutoIntegrationFilterHttpSession() { - super(); - } - - protected HttpSessionIntegrationFilter getHttpSessionIntegrationFilter() { - return this.filter; - } - } - - private class MockAutoIntegrationFilterJboss extends AutoIntegrationFilter { - private JbossIntegrationFilter filter; - - public MockAutoIntegrationFilterJboss(JbossIntegrationFilter filter) { - this.filter = filter; - } - - private MockAutoIntegrationFilterJboss() { - super(); - } - - protected JbossIntegrationFilter getJbossIntegrationFilter() { - return this.filter; - } - } - - private class MockHttpSessionIntegrationFilter - extends HttpSessionIntegrationFilter { - private Object toReturn; - - public MockHttpSessionIntegrationFilter(Object toReturn) { - this.toReturn = toReturn; - } - - private MockHttpSessionIntegrationFilter() { - super(); - } - - public void commitToContainer(ServletRequest request, - Authentication authentication) { - this.toReturn = authentication; - } - - public Object extractFromContainer(ServletRequest request) { - return this.toReturn; - } - } -}