1 17 package org.apache.geronimo.jetty6; 18 19 import java.io.File ; 20 import java.net.URL ; 21 import java.security.PermissionCollection ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.Set ; 27 28 import javax.transaction.TransactionManager ; 29 30 import org.apache.geronimo.testsupport.TestSupport; 31 32 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator; 33 import org.apache.geronimo.connector.outbound.connectiontracking.GeronimoTransactionListener; 34 import org.apache.geronimo.jetty6.connector.HTTPConnector; 35 import org.apache.geronimo.security.SecurityServiceImpl; 36 import org.apache.geronimo.security.deploy.DefaultPrincipal; 37 import org.apache.geronimo.security.deploy.PrincipalInfo; 38 import org.apache.geronimo.security.jaas.GeronimoLoginConfiguration; 39 import org.apache.geronimo.security.jaas.JaasLoginModuleUse; 40 import org.apache.geronimo.security.jaas.LoginModuleGBean; 41 import org.apache.geronimo.security.jaas.server.JaasLoginService; 42 import org.apache.geronimo.security.jacc.ApplicationPolicyConfigurationManager; 43 import org.apache.geronimo.security.jacc.ApplicationPrincipalRoleConfigurationManager; 44 import org.apache.geronimo.security.jacc.ComponentPermissions; 45 import org.apache.geronimo.security.jacc.PrincipalRoleMapper; 46 import org.apache.geronimo.security.realm.GenericSecurityRealm; 47 import org.apache.geronimo.system.serverinfo.BasicServerInfo; 48 import org.apache.geronimo.system.serverinfo.ServerInfo; 49 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 50 import org.mortbay.jetty.security.Authenticator; 51 import org.mortbay.jetty.security.FormAuthenticator; 52 53 54 57 public class AbstractWebModuleTest extends TestSupport { 58 protected ClassLoader cl; 59 protected final static String securityRealmName = "demo-properties-realm"; 60 private HTTPConnector connector; 61 protected JettyContainerImpl container; 62 private TransactionManager transactionManager; 63 private ConnectionTrackingCoordinator connectionTrackingCoordinator; 64 private URL configurationBaseURL; 65 protected SessionHandlerFactory sessionHandlerFactory = null; 66 67 protected void setUpStaticContentServlet(JettyServletRegistration webModule) throws Exception { 68 Map <String , String > staticContentServletInitParams = new HashMap <String , String >(); 69 staticContentServletInitParams.put("acceptRanges", "true"); 70 staticContentServletInitParams.put("dirAllowed", "true"); 71 staticContentServletInitParams.put("putAllowed", "false"); 72 staticContentServletInitParams.put("delAllowed", "false"); 73 staticContentServletInitParams.put("redirectWelcome", "false"); 74 staticContentServletInitParams.put("minGzipLength", "8192"); 75 76 new JettyServletHolder("test:name=staticservlet", 77 "default", 78 "org.mortbay.jetty.servlet.DefaultServlet", 79 null, 80 staticContentServletInitParams, 81 null, 82 Collections.singleton("/"), 83 null, 84 webModule); 85 86 } 87 88 protected JettyWebAppContext setUpAppContext(String realmName, String securityRealmName, Authenticator authenticator, String policyContextId, PermissionCollection excludedPermissions, DefaultPrincipal defaultPrincipal, PermissionCollection checkedPermissions, String uriString) throws Exception { 89 90 JettyWebAppContext app = new JettyWebAppContext(null, 91 null, 92 Collections.EMPTY_MAP, 93 cl, 94 new URL (configurationBaseURL, uriString), 95 null, 96 null, 97 "context", 98 null, 99 null, 100 false, 101 null, 102 null, 103 null, 104 null, 105 authenticator, 106 realmName, 107 null, 108 0, 109 sessionHandlerFactory, 110 null, 111 policyContextId, 112 securityRealmName, 113 defaultPrincipal, 114 checkedPermissions, 115 excludedPermissions, 116 null, 117 transactionManager, 118 connectionTrackingCoordinator, 119 container, 120 null, 121 null, 122 null); 123 app.setContextPath("/test"); 124 app.doStart(); 125 return app; 126 } 127 128 protected JettyWebAppContext setUpSecureAppContext(Map roleDesignates, Map principalRoleMap, ComponentPermissions componentPermissions, DefaultPrincipal defaultPrincipal, PermissionCollection checked, Set securityRoles) throws Exception { 129 String policyContextId = "TEST"; 130 PrincipalRoleMapper roleMapper = new ApplicationPrincipalRoleConfigurationManager(principalRoleMap); 131 Map contextIDToPermissionsMap = new HashMap (); 132 contextIDToPermissionsMap.put(policyContextId, componentPermissions); 133 ApplicationPolicyConfigurationManager jacc = new ApplicationPolicyConfigurationManager(contextIDToPermissionsMap, roleDesignates, cl, roleMapper); 134 jacc.doStart(); 135 136 FormAuthenticator formAuthenticator = new FormAuthenticator(); 137 formAuthenticator.setLoginPage("/auth/logon.html?param=test"); 138 formAuthenticator.setErrorPage("/auth/logonError.html?param=test"); 139 return setUpAppContext("Test JAAS Realm", 140 "demo-properties-realm", 141 formAuthenticator, 142 policyContextId, 143 componentPermissions.getExcludedPermissions(), 144 defaultPrincipal, 145 checked, "war3/"); 146 147 } 148 149 protected void setUpSecurity() throws Exception { 150 String domainName = "demo-properties-realm"; 151 152 ServerInfo serverInfo = new BasicServerInfo("."); 153 154 new SecurityServiceImpl(cl, serverInfo, "org.apache.geronimo.security.jacc.GeronimoPolicyConfigurationFactory", "org.apache.geronimo.security.jacc.GeronimoPolicy", null, null, null, null); 155 156 Properties options = new Properties (); 157 options.setProperty("usersURI", new File (BASEDIR, "src/test/resources/data/users.properties").toURI().toString()); 158 options.setProperty("groupsURI", new File (BASEDIR, "src/test/resources/data/groups.properties").toURI().toString()); 159 160 LoginModuleGBean loginModule = new LoginModuleGBean("org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule", null, true, true, cl); 161 loginModule.setLoginDomainName(domainName); 162 loginModule.setOptions(options); 163 164 JaasLoginModuleUse loginModuleUse = new JaasLoginModuleUse(loginModule, null, "REQUIRED", null); 165 166 JaasLoginService loginService = new JaasLoginService("HmacSHA1", "secret", cl, null); 167 168 PrincipalInfo.PrincipalEditor principalEditor = new PrincipalInfo.PrincipalEditor(); 169 principalEditor.setAsText("metro,org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal,false"); 170 GenericSecurityRealm realm = new GenericSecurityRealm(domainName, loginModuleUse, true, true, (PrincipalInfo) principalEditor.getValue(), serverInfo, cl, null, loginService); 171 172 loginService.setRealms(Collections.singleton(realm)); 173 loginService.doStart(); 174 175 GeronimoLoginConfiguration loginConfiguration = new GeronimoLoginConfiguration(); 176 loginConfiguration.setConfigurations(Collections.singleton(realm)); 177 loginConfiguration.doStart(); 178 179 } 180 181 protected void tearDownSecurity() throws Exception { 182 } 183 184 protected void setUp() throws Exception { 185 cl = this.getClass().getClassLoader(); 186 187 configurationBaseURL = cl.getResource("deployables/"); 188 189 container = new JettyContainerImpl("test:name=JettyContainer", null); 190 container.doStart(); 191 connector = new HTTPConnector(container); 192 connector.setPort(5678); 193 connector.setMaxThreads(50); 194 connector.doStart(); 196 197 TransactionManagerImpl transactionManager = new TransactionManagerImpl(); 198 this.transactionManager = transactionManager; 199 connectionTrackingCoordinator = new ConnectionTrackingCoordinator(); 200 transactionManager.addTransactionAssociationListener(new GeronimoTransactionListener(connectionTrackingCoordinator)); 201 } 202 203 protected void tearDown() throws Exception { 204 connector.doStop(); 205 } 206 } 207 | Popular Tags |