001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Inital Developer : Matt Wringe
022: *
023: * --------------------------------------------------------------------------
024: * $Id: SecurityGenerator.java 9457 2006-08-24 12:58:41Z sauthieg $
025: * --------------------------------------------------------------------------
026: */package org.objectweb.jonas_ws.wsgen.generator;
027:
028: import org.objectweb.jonas_ws.wsgen.ddmodifier.ContextDDModifier;
029: import org.objectweb.jonas_ws.wsgen.ddmodifier.WebJettyDDModifier;
030: import org.objectweb.jonas_ws.wsgen.ddmodifier.WsEndpointDDModifier;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035:
036: /**
037: * Generates the necessary security files to the generated
038: * webapp for a stateless session bean endpoint.
039: *
040: * @author Matt Wringe
041: */
042: public class SecurityGenerator {
043:
044: /**
045: * Document that contains the security configurations to be used
046: */
047: private Document securityDesc = null;
048:
049: /**
050: * WsEndpointDDModifier used to add security settings to the web.xml
051: */
052: private WsEndpointDDModifier wsddm = null;
053:
054: /**
055: * ContextDDModifier used to add Realm settings to the context.xml
056: */
057: private ContextDDModifier cddm = null;
058:
059: /**
060: * WebJettyDDModifuer used to add Realm settings to web-jetty.xml
061: */
062: private WebJettyDDModifier wjddm = null;
063:
064: /**
065: * The name of the node in securityDesc that contains the login-config settings
066: */
067: private static final String LOGIN_CONFIG = "endpoint-login-config";
068:
069: /**
070: * The name of the node in securityDesc that contains the security-constraint settings
071: */
072: private static final String SECURITY_CONSTRAINT = "endpoint-security-constraint";
073:
074: /**
075: * The name of the node that contains the realm
076: */
077: private static final String REALM = "endpoint-realm";
078:
079: /**
080: * The name of the node that contains the name of the realm
081: */
082: private static final String REALM_NAME = "endpoint-realm-name";
083:
084: /**
085: * The name of the node that contains the security role
086: */
087: private static final String SECURITY_ROLE = "endpoint-security-role";
088:
089: /**
090: * The realm the webapp should use
091: */
092: private String realm = null;
093:
094: /**
095: * The name of the realm that the webapp should use
096: */
097: private String realmName = null;
098:
099: /**
100: * Creates a new SecurityGenerator object
101: * @param securityDesc Dom Document that contains the security settings
102: */
103: public SecurityGenerator(Document securityDesc) {
104: this .securityDesc = securityDesc;
105: }
106:
107: /**
108: * Generates the security settings specified in securityDesc document
109: *
110: * @param ddm Used to add security to the web.xml
111: * @param cddm Used to add a security realm to the context.xml
112: * @param wjddm Used to add a security realn to the web-jetty.xml
113: */
114: public void generate(WsEndpointDDModifier ddm,
115: ContextDDModifier cddm, WebJettyDDModifier wjddm) {
116: this .wsddm = ddm;
117: this .cddm = cddm;
118: this .wjddm = wjddm;
119:
120: if (securityDesc != null) {
121:
122: realm = getRealm();
123: realmName = getRealmName();
124:
125: if (ddm != null) {
126: addEndpointSecurity();
127: }
128: if (cddm != null) {
129: addContextRealm();
130: }
131: if (wjddm != null) {
132: addWebJettyRealm();
133: }
134: }
135: }
136:
137: /**
138: * Add realm settings to the context.xml
139: *
140: */
141: private void addContextRealm() {
142: if (realm != null) {
143: cddm.addContextRealm(realm);
144: }
145: }
146:
147: /**
148: * Add realm settings to web-jetty.xml
149: *
150: */
151: private void addWebJettyRealm() {
152: if (realm != null) {
153: if (realmName != null) {
154: wjddm.configRealm(realmName, realm);
155: } else {
156: wjddm.configRealm(realm);
157: }
158: }
159: }
160:
161: /**
162: * Setup the web security in the web.xml
163: *
164: */
165: private void addEndpointSecurity() {
166:
167: //Add the security constraints
168: NodeList securityConstraints = getEndpointSecurityConstraints();
169: if (securityConstraints != null) {
170: for (int i = 0; i < securityConstraints.getLength(); i++) {
171: //remove the j2ee prefix of this node
172: removePrefix(securityConstraints.item(i));
173: wsddm.addEndpointSecurityConstraint(securityConstraints
174: .item(i));
175: }
176: }
177:
178: //Add the login configs
179: NodeList loginConfigs = getEndpointLoginConfig();
180: if (loginConfigs != null) {
181: for (int i = 0; i < loginConfigs.getLength(); i++) {
182: //remove the j2ee prefix from this node
183: removePrefix(loginConfigs.item(i));
184: wsddm.addEndpointLoginConfig((Element) loginConfigs
185: .item(i));
186: }
187: }
188:
189: //Add the security roles
190: NodeList securityRoles = getEndpointSecurityRole();
191: if (securityRoles != null) {
192: for (int i = 0; i < securityRoles.getLength(); i++) {
193: //remove the j2ee prefix from thie node
194: removePrefix(securityRoles.item(i));
195: wsddm.addSecurityRole(securityRoles.item(i));
196: }
197: }
198: }
199:
200: /**
201: * Returns the DocumentElement for the securityDesc document
202: *
203: * @return DocumentElement for the securityDesc document
204: */
205: private Element getElement() {
206: return securityDesc.getDocumentElement();
207: }
208:
209: /**
210: * Returns the login-config nodes from the securityDesc document
211: * @return the login-config nodes from the securityDesc document
212: */
213: public NodeList getEndpointLoginConfig() {
214: NodeList nodeList = getElement().getElementsByTagName(
215: LOGIN_CONFIG);
216: return nodeList;
217: }
218:
219: /**
220: * Returns the security-constraint nodes from the securityDesc document
221: *
222: * @return the security-constraint nodes from the securityDesc document
223: */
224: public NodeList getEndpointSecurityConstraints() {
225: NodeList nodeList = getElement().getElementsByTagName(
226: 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 getEndpointSecurityRole() {
236: NodeList nodeList = getElement().getElementsByTagName(
237: SECURITY_ROLE);
238: return nodeList;
239: }
240:
241: /**
242: * Returns the context-realm node from the securityDesc document
243: *
244: * @return the realm node from the securityDesc document
245: */
246: public String getRealm() {
247: NodeList nodeList = getElement().getElementsByTagName(REALM);
248: Node node = nodeList.item(0);
249:
250: if (node != null && node.hasChildNodes()) {
251: Node realmNode = nodeList.item(0).getFirstChild();
252: realm = realmNode.getNodeValue();
253: }
254: return realm;
255: }
256:
257: /**
258: * Returns the realm name
259: *
260: * @return the realm name
261: */
262: public String getRealmName() {
263: String realmName = null;
264: NodeList nodeList = getElement().getElementsByTagName(
265: REALM_NAME);
266: Node node = nodeList.item(0);
267:
268: if (node != null && node.hasChildNodes()) {
269: Node realmNameNode = nodeList.item(0).getFirstChild();
270: realmName = realmNameNode.getNodeValue();
271: }
272: return realmName;
273: }
274:
275: /**
276: * Returns the Document that contains the security settings
277: *
278: * @return Document that contains the security settings
279: */
280: public Document getSecurityDesc() {
281: return securityDesc;
282: }
283:
284: /**
285: * Removes the prefix from all the children of a node
286: *
287: * @param node Node
288: */
289: private void removePrefix(Node node) {
290: if (node != null) {
291: if (node.getPrefix() != null) {
292: node.setPrefix(null);
293: }
294: if (node.hasChildNodes()) {
295: for (int i = 0; i < node.getChildNodes().getLength(); i++) {
296: removePrefix(node.getChildNodes().item(i));
297: }
298: }
299: }
300: }
301:
302: }
|