001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.jetty6;
017:
018: import java.io.File;
019: import java.net.URL;
020: import java.security.PermissionCollection;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import javax.transaction.TransactionManager;
027:
028: import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator;
029: import org.apache.geronimo.connector.outbound.connectiontracking.GeronimoTransactionListener;
030: import org.apache.geronimo.jetty6.connector.HTTPSocketConnector;
031: import org.apache.geronimo.security.SecurityServiceImpl;
032: import org.apache.geronimo.security.deploy.PrincipalInfo;
033: import org.apache.geronimo.security.deploy.SubjectInfo;
034: import org.apache.geronimo.security.jaas.ConfigurationEntryFactory;
035: import org.apache.geronimo.security.jaas.GeronimoLoginConfiguration;
036: import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
037: import org.apache.geronimo.security.jaas.LoginModuleControlFlag;
038: import org.apache.geronimo.security.jaas.LoginModuleGBean;
039: import org.apache.geronimo.security.jacc.ApplicationPolicyConfigurationManager;
040: import org.apache.geronimo.security.jacc.mappingprovider.ApplicationPrincipalRoleConfigurationManager;
041: import org.apache.geronimo.security.jacc.mappingprovider.GeronimoPolicyConfigurationFactory;
042: import org.apache.geronimo.security.jacc.mappingprovider.GeronimoPolicy;
043: import org.apache.geronimo.security.jacc.ComponentPermissions;
044: import org.apache.geronimo.security.jacc.PrincipalRoleMapper;
045: import org.apache.geronimo.security.jacc.RunAsSource;
046: import org.apache.geronimo.security.realm.GenericSecurityRealm;
047: import org.apache.geronimo.system.serverinfo.BasicServerInfo;
048: import org.apache.geronimo.system.serverinfo.ServerInfo;
049: import org.apache.geronimo.testsupport.TestSupport;
050: import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
051: import org.mortbay.jetty.security.Authenticator;
052: import org.mortbay.jetty.security.FormAuthenticator;
053:
054: /**
055: * @version $Rev: 613094 $ $Date: 2008-01-18 00:00:10 -0800 (Fri, 18 Jan 2008) $
056: */
057: public class AbstractWebModuleTest extends TestSupport {
058: protected ClassLoader cl;
059: protected final static String securityRealmName = "demo-properties-realm";
060: protected HTTPSocketConnector connector;
061: protected JettyContainerImpl container;
062: private TransactionManager transactionManager;
063: private ConnectionTrackingCoordinator connectionTrackingCoordinator;
064: private URL configurationBaseURL;
065: protected PreHandlerFactory preHandlerFactory = null;
066: protected SessionHandlerFactory sessionHandlerFactory = null;
067:
068: protected void setUpStaticContentServlet(
069: JettyServletRegistration webModule) throws Exception {
070: Map<String, String> staticContentServletInitParams = new HashMap<String, String>();
071: staticContentServletInitParams.put("acceptRanges", "true");
072: staticContentServletInitParams.put("dirAllowed", "true");
073: staticContentServletInitParams.put("putAllowed", "false");
074: staticContentServletInitParams.put("delAllowed", "false");
075: staticContentServletInitParams.put("redirectWelcome", "false");
076: staticContentServletInitParams.put("minGzipLength", "8192");
077:
078: new JettyServletHolder("test:name=staticservlet", "default",
079: "org.mortbay.jetty.servlet.DefaultServlet", null,
080: staticContentServletInitParams, null, Collections
081: .singleton("/"), null, webModule);
082:
083: }
084:
085: protected JettyWebAppContext setUpAppContext(String realmName,
086: String securityRealmName, Authenticator authenticator,
087: String policyContextId,
088: PermissionCollection excludedPermissions,
089: RunAsSource runAsSource,
090: PermissionCollection checkedPermissions, String uriString)
091: throws Exception {
092:
093: JettyWebAppContext app = new JettyWebAppContext(null, null,
094: Collections.<String, Object> emptyMap(), cl, new URL(
095: configurationBaseURL, uriString), null, null,
096: "context", null, null, false, null, null, null, null,
097: authenticator, realmName, null, false, 0,
098: sessionHandlerFactory, preHandlerFactory,
099: policyContextId, securityRealmName, runAsSource, null,
100: null, transactionManager,
101: connectionTrackingCoordinator, container, null, null,
102: null, null);
103: app.setContextPath("/test");
104: app.doStart();
105: return app;
106: }
107:
108: protected JettyWebAppContext setUpSecureAppContext(
109: String securityRealmName, Map roleDesignates,
110: Map principalRoleMap,
111: ComponentPermissions componentPermissions,
112: SubjectInfo defaultSubjectInfo,
113: PermissionCollection checked, Set securityRoles)
114: throws Exception {
115: String policyContextId = "TEST";
116: PrincipalRoleMapper roleMapper = new ApplicationPrincipalRoleConfigurationManager(
117: principalRoleMap);
118: Map<String, ComponentPermissions> contextIDToPermissionsMap = new HashMap<String, ComponentPermissions>();
119: contextIDToPermissionsMap.put(policyContextId,
120: componentPermissions);
121: ApplicationPolicyConfigurationManager jacc = new ApplicationPolicyConfigurationManager(
122: contextIDToPermissionsMap, null, roleDesignates, cl,
123: null, roleMapper);
124: jacc.doStart();
125:
126: FormAuthenticator formAuthenticator = new FormAuthenticator();
127: formAuthenticator.setLoginPage("/auth/logon.html?param=test");
128: formAuthenticator
129: .setErrorPage("/auth/logonError.html?param=test");
130: return setUpAppContext("Test JAAS Realm", securityRealmName,
131: formAuthenticator, policyContextId,
132: componentPermissions.getExcludedPermissions(), jacc,
133: checked, "war3/");
134:
135: }
136:
137: protected void setUpSecurity() throws Exception {
138: String domainName = "demo-properties-realm";
139:
140: ServerInfo serverInfo = new BasicServerInfo(".");
141:
142: new SecurityServiceImpl(cl, serverInfo,
143: GeronimoPolicyConfigurationFactory.class.getName(),
144: GeronimoPolicy.class.getName(), null, null, null, null);
145:
146: Map<String, Object> options = new HashMap<String, Object>();
147: options.put("usersURI", new File(BASEDIR,
148: "src/test/resources/data/users.properties").toURI()
149: .toString());
150: options.put("groupsURI", new File(BASEDIR,
151: "src/test/resources/data/groups.properties").toURI()
152: .toString());
153:
154: LoginModuleGBean loginModule = new LoginModuleGBean(
155: "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule",
156: null, true, options, domainName, cl);
157:
158: JaasLoginModuleUse loginModuleUse = new JaasLoginModuleUse(
159: loginModule, null, LoginModuleControlFlag.REQUIRED);
160:
161: PrincipalInfo.PrincipalEditor principalEditor = new PrincipalInfo.PrincipalEditor();
162: principalEditor
163: .setAsText("metro,org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal");
164: GenericSecurityRealm realm = new GenericSecurityRealm(
165: domainName, loginModuleUse, true, serverInfo, cl, null);
166:
167: GeronimoLoginConfiguration loginConfiguration = new GeronimoLoginConfiguration();
168: loginConfiguration.setConfigurations(Collections
169: .<ConfigurationEntryFactory> singleton(realm));
170: loginConfiguration.doStart();
171:
172: }
173:
174: protected void tearDownSecurity() throws Exception {
175: }
176:
177: protected void setUp() throws Exception {
178: cl = this .getClass().getClassLoader();
179:
180: configurationBaseURL = cl.getResource("deployables/");
181:
182: ServerInfo serverInfo = new BasicServerInfo(".");
183: container = new JettyContainerImpl("test:name=JettyContainer",
184: null, new File(BASEDIR, "target/var/jetty").toString(),
185: serverInfo);
186: container.doStart();
187: connector = new HTTPSocketConnector(container, null);
188: connector.setPort(5678);
189: connector.setMaxThreads(50);
190: connector.doStart();
191:
192: TransactionManagerImpl transactionManager = new TransactionManagerImpl();
193: this .transactionManager = transactionManager;
194: connectionTrackingCoordinator = new ConnectionTrackingCoordinator();
195: transactionManager
196: .addTransactionAssociationListener(new GeronimoTransactionListener(
197: connectionTrackingCoordinator));
198: }
199:
200: protected void tearDown() throws Exception {
201: connector.doStop();
202: Thread.sleep(1000);
203: }
204: }
|