1 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 ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 38 39 45 public class SecurityGenerator { 46 47 50 private Document securityDesc = null; 51 52 55 private WsEndpointDDModifier wsddm = null; 56 57 60 private ContextDDModifier cddm = null; 61 62 65 private WebJettyDDModifier wjddm = null; 66 67 70 private static final String LOGIN_CONFIG = "endpoint-login-config"; 71 72 75 private static final String SECURITY_CONSTRAINT = "endpoint-security-constraint"; 76 77 80 private static final String REALM = "endpoint-realm"; 81 82 85 private static final String REALM_NAME = "endpoint-realm-name"; 86 87 90 private static final String SECURITY_ROLE = "endpoint-security-role"; 91 92 95 private String realm = null; 96 97 100 private String realmName = null; 101 102 103 107 public SecurityGenerator(Document securityDesc) { 108 this.securityDesc = securityDesc; 109 } 110 111 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 145 private void addContextRealm() { 146 if (realm != null) { 147 cddm.addContextRealm(realm); 148 } 149 } 150 151 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 169 private void addEndpointSecurity() { 170 171 NodeList securityConstraints = getEndpointSecurityConstraints(); 173 if (securityConstraints != null) { 174 for (int i = 0; i < securityConstraints.getLength(); i++) { 175 removePrefix(securityConstraints.item(i)); 177 wsddm.addEndpointSecurityConstraint(securityConstraints.item(i)); 178 } 179 } 180 181 NodeList loginConfigs = getEndpointLoginConfig(); 183 if (loginConfigs != null) { 184 for (int i = 0; i < loginConfigs.getLength(); i++) { 185 removePrefix(loginConfigs.item(i)); 187 wsddm.addEndpointLoginConfig(loginConfigs.item(i)); 188 } 189 } 190 191 NodeList securityRoles = getEndpointSecurityRole(); 193 if (securityRoles != null) { 194 for (int i = 0; i < securityRoles.getLength(); i++) { 195 removePrefix(securityRoles.item(i)); 197 wsddm.addSecurityRole(securityRoles.item(i)); 198 } 199 } 200 } 201 202 207 private Element getElement() { 208 return securityDesc.getDocumentElement(); 209 } 210 211 215 public NodeList getEndpointLoginConfig() { 216 NodeList nodeList = getElement().getElementsByTagName(LOGIN_CONFIG); 217 return nodeList; 218 } 219 220 225 public NodeList getEndpointSecurityConstraints() { 226 NodeList nodeList = getElement().getElementsByTagName(SECURITY_CONSTRAINT); 227 return nodeList; 228 } 229 230 235 public NodeList getEndpointSecurityRole() { 236 NodeList nodeList = getElement().getElementsByTagName(SECURITY_ROLE); 237 return nodeList; 238 } 239 240 245 public String getRealm() { 246 NodeList nodeList = getElement().getElementsByTagName(REALM); 247 Node node = nodeList.item(0); 248 249 if (node != null && node.hasChildNodes()) { 250 Node realmNode = nodeList.item(0).getFirstChild(); 251 realm = realmNode.getNodeValue(); 252 } 253 return realm; 254 } 255 256 261 public String getRealmName() { 262 String realmName = null; 263 NodeList nodeList = getElement().getElementsByTagName(REALM_NAME); 264 Node node = nodeList.item(0); 265 266 if (node != null && node.hasChildNodes()) { 267 Node realmNameNode = nodeList.item(0).getFirstChild(); 268 realmName = realmNameNode.getNodeValue(); 269 } 270 return realmName; 271 } 272 273 278 public Document getSecurityDesc() { 279 return securityDesc; 280 } 281 282 287 private void removePrefix (Node 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 |