KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > SecurityGenerator


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Inital Developer : Matt Wringe
22  *
23  * --------------------------------------------------------------------------
24  * $Id: SecurityGenerator.java,v 1.1 2005/07/18 23:53:30 mwringe Exp $
25  * --------------------------------------------------------------------------
26  */

27
28 package org.objectweb.jonas_ws.wsgen.generator;
29
30 import org.objectweb.jonas_ws.wsgen.ddmodifier.ContextDDModifier;
31 import org.objectweb.jonas_ws.wsgen.ddmodifier.WebJettyDDModifier;
32 import org.objectweb.jonas_ws.wsgen.ddmodifier.WsEndpointDDModifier;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38
39 /**
40  * Generates the necessary security files to the generated
41  * webapp for a stateless session bean endpoint.
42  *
43  * @author Matt Wringe
44  */

45 public class SecurityGenerator {
46
47     /**
48      * Document that contains the security configurations to be used
49      */

50     private Document JavaDoc securityDesc = null;
51
52     /**
53      * WsEndpointDDModifier used to add security settings to the web.xml
54      */

55     private WsEndpointDDModifier wsddm = null;
56
57     /**
58      * ContextDDModifier used to add Realm settings to the context.xml
59      */

60     private ContextDDModifier cddm = null;
61
62     /**
63      * WebJettyDDModifuer used to add Realm settings to web-jetty.xml
64      */

65     private WebJettyDDModifier wjddm = null;
66
67     /**
68      * The name of the node in securityDesc that contains the login-config settings
69      */

70     private static final String JavaDoc LOGIN_CONFIG = "endpoint-login-config";
71
72     /**
73      * The name of the node in securityDesc that contains the security-constraint settings
74      */

75     private static final String JavaDoc SECURITY_CONSTRAINT = "endpoint-security-constraint";
76
77     /**
78      * The name of the node that contains the realm
79      */

80     private static final String JavaDoc REALM = "endpoint-realm";
81
82     /**
83      * The name of the node that contains the name of the realm
84      */

85     private static final String JavaDoc REALM_NAME = "endpoint-realm-name";
86
87     /**
88      * The name of the node that contains the security role
89      */

90     private static final String JavaDoc SECURITY_ROLE = "endpoint-security-role";
91
92     /**
93      * The realm the webapp should use
94      */

95     private String JavaDoc realm = null;
96
97     /**
98      * The name of the realm that the webapp should use
99      */

100     private String JavaDoc realmName = null;
101
102
103     /**
104      * Creates a new SecurityGenerator object
105      * @param securityDesc Dom Document that contains the security settings
106      */

107     public SecurityGenerator(Document JavaDoc securityDesc) {
108         this.securityDesc = securityDesc;
109     }
110
111     /**
112      * Generates the security settings specified in securityDesc document
113      *
114      * @param ddm Used to add security to the web.xml
115      * @param cddm Used to add a security realm to the context.xml
116      * @param wjddm Used to add a security realn to the web-jetty.xml
117      */

118     public void generate(WsEndpointDDModifier ddm, ContextDDModifier cddm, WebJettyDDModifier wjddm) {
119         this.wsddm = ddm;
120         this.cddm = cddm;
121         this.wjddm = wjddm;
122
123         if (securityDesc != null) {
124
125             realm = getRealm();
126             realmName = getRealmName();
127
128             if (ddm != null) {
129                 addEndpointSecurity();
130             }
131             if (cddm != null) {
132                 addContextRealm();
133             }
134             if (wjddm != null) {
135                 addWebJettyRealm();
136             }
137         }
138     }
139
140
141     /**
142      * Add realm settings to the context.xml
143      *
144      */

145     private void addContextRealm() {
146         if (realm != null) {
147             cddm.addContextRealm(realm);
148         }
149     }
150
151     /**
152      * Add realm settings to web-jetty.xml
153      *
154      */

155     private void addWebJettyRealm() {
156         if (realm != null) {
157             if (realmName != null) {
158                 wjddm.configRealm(realmName, realm);
159             } else {
160                 wjddm.configRealm(realm);
161             }
162         }
163     }
164
165     /**
166      * Setup the web security in the web.xml
167      *
168      */

169     private void addEndpointSecurity() {
170
171         //Add the security constraints
172
NodeList JavaDoc securityConstraints = getEndpointSecurityConstraints();
173         if (securityConstraints != null) {
174             for (int i = 0; i < securityConstraints.getLength(); i++) {
175                 //remove the j2ee prefix of this node
176
removePrefix(securityConstraints.item(i));
177                 wsddm.addEndpointSecurityConstraint(securityConstraints.item(i));
178             }
179         }
180
181         //Add the login configs
182
NodeList JavaDoc loginConfigs = getEndpointLoginConfig();
183         if (loginConfigs != null) {
184             for (int i = 0; i < loginConfigs.getLength(); i++) {
185                 //remove the j2ee prefix from this node
186
removePrefix(loginConfigs.item(i));
187                 wsddm.addEndpointLoginConfig(loginConfigs.item(i));
188             }
189         }
190
191         //Add the security roles
192
NodeList JavaDoc securityRoles = getEndpointSecurityRole();
193         if (securityRoles != null) {
194             for (int i = 0; i < securityRoles.getLength(); i++) {
195                 //remove the j2ee prefix from thie node
196
removePrefix(securityRoles.item(i));
197                 wsddm.addSecurityRole(securityRoles.item(i));
198             }
199         }
200     }
201
202     /**
203      * Returns the DocumentElement for the securityDesc document
204      *
205      * @return DocumentElement for the securityDesc document
206      */

207     private Element JavaDoc getElement() {
208         return securityDesc.getDocumentElement();
209     }
210
211     /**
212      * Returns the login-config nodes from the securityDesc document
213      * @return the login-config nodes from the securityDesc document
214      */

215     public NodeList JavaDoc getEndpointLoginConfig() {
216         NodeList JavaDoc nodeList = getElement().getElementsByTagName(LOGIN_CONFIG);
217         return nodeList;
218     }
219
220     /**
221      * Returns the security-constraint nodes from the securityDesc document
222      *
223      * @return the security-constraint nodes from the securityDesc document
224      */

225     public NodeList JavaDoc getEndpointSecurityConstraints() {
226         NodeList JavaDoc nodeList = getElement().getElementsByTagName(SECURITY_CONSTRAINT);
227         return nodeList;
228     }
229
230     /**
231      * Returns the security-role nodes from the securityDesc document
232      *
233      * @return the security-role nodes from the securityDesc document
234      */

235     public NodeList JavaDoc getEndpointSecurityRole() {
236         NodeList JavaDoc nodeList = getElement().getElementsByTagName(SECURITY_ROLE);
237         return nodeList;
238     }
239
240     /**
241      * Returns the context-realm node from the securityDesc document
242      *
243      * @return the realm node from the securityDesc document
244      */

245     public String JavaDoc getRealm() {
246         NodeList JavaDoc nodeList = getElement().getElementsByTagName(REALM);
247         Node JavaDoc node = nodeList.item(0);
248
249         if (node != null && node.hasChildNodes()) {
250             Node JavaDoc realmNode = nodeList.item(0).getFirstChild();
251             realm = realmNode.getNodeValue();
252         }
253         return realm;
254     }
255
256     /**
257      * Returns the realm name
258      *
259      * @return the realm name
260      */

261     public String JavaDoc getRealmName() {
262         String JavaDoc realmName = null;
263         NodeList JavaDoc nodeList = getElement().getElementsByTagName(REALM_NAME);
264         Node JavaDoc node = nodeList.item(0);
265
266         if (node != null && node.hasChildNodes()) {
267             Node JavaDoc realmNameNode = nodeList.item(0).getFirstChild();
268             realmName = realmNameNode.getNodeValue();
269         }
270         return realmName;
271     }
272
273     /**
274      * Returns the Document that contains the security settings
275      *
276      * @return Document that contains the security settings
277      */

278     public Document JavaDoc getSecurityDesc() {
279         return securityDesc;
280     }
281
282     /**
283      * Removes the prefix from all the children of a node
284      *
285      * @param node Node
286      */

287     private void removePrefix (Node JavaDoc node) {
288         if (node != null) {
289             if (node.getPrefix() != null) {
290                node.setPrefix(null);
291             }
292             if (node.hasChildNodes()) {
293                 for (int i = 0; i < node.getChildNodes().getLength(); i++) {
294                     removePrefix (node.getChildNodes().item(i));
295                 }
296             }
297         }
298     }
299
300 }
Popular Tags