KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > AbstractWebModuleTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty6;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.security.PermissionCollection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.transaction.TransactionManager JavaDoc;
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 /**
55  * @version $Rev: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
56  */

57 public class AbstractWebModuleTest extends TestSupport {
58     protected ClassLoader JavaDoc cl;
59     protected final static String JavaDoc securityRealmName = "demo-properties-realm";
60     private HTTPConnector connector;
61     protected JettyContainerImpl container;
62     private TransactionManager JavaDoc transactionManager;
63     private ConnectionTrackingCoordinator connectionTrackingCoordinator;
64     private URL JavaDoc configurationBaseURL;
65     protected SessionHandlerFactory sessionHandlerFactory = null;
66
67     protected void setUpStaticContentServlet(JettyServletRegistration webModule) throws Exception JavaDoc {
68         Map JavaDoc<String JavaDoc, String JavaDoc> staticContentServletInitParams = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
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 JavaDoc realmName, String JavaDoc securityRealmName, Authenticator authenticator, String JavaDoc policyContextId, PermissionCollection JavaDoc excludedPermissions, DefaultPrincipal defaultPrincipal, PermissionCollection JavaDoc checkedPermissions, String JavaDoc uriString) throws Exception JavaDoc {
89
90         JettyWebAppContext app = new JettyWebAppContext(null,
91                 null,
92                 Collections.EMPTY_MAP,
93                 cl,
94                 new URL JavaDoc(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 JavaDoc roleDesignates, Map JavaDoc principalRoleMap, ComponentPermissions componentPermissions, DefaultPrincipal defaultPrincipal, PermissionCollection JavaDoc checked, Set JavaDoc securityRoles) throws Exception JavaDoc {
129         String JavaDoc policyContextId = "TEST";
130         PrincipalRoleMapper roleMapper = new ApplicationPrincipalRoleConfigurationManager(principalRoleMap);
131         Map JavaDoc contextIDToPermissionsMap = new HashMap JavaDoc();
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 JavaDoc {
150         String JavaDoc 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 JavaDoc options = new Properties JavaDoc();
157         options.setProperty("usersURI", new File JavaDoc(BASEDIR, "src/test/resources/data/users.properties").toURI().toString());
158         options.setProperty("groupsURI", new File JavaDoc(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 JavaDoc {
182     }
183
184     protected void setUp() throws Exception JavaDoc {
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.setMinThreads(10);
195
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 JavaDoc {
204         connector.doStop();
205     }
206 }
207
Popular Tags