1 17 18 package org.apache.geronimo.web25.deployment; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.URI ; 23 import java.net.URISyntaxException ; 24 import java.net.URL ; 25 import java.security.Permission ; 26 import java.security.PermissionCollection ; 27 import java.security.Permissions ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.LinkedList ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import java.util.jar.JarFile ; 38 import java.util.zip.ZipEntry ; 39 40 import javax.security.jacc.WebResourcePermission ; 41 import javax.security.jacc.WebRoleRefPermission ; 42 import javax.security.jacc.WebUserDataPermission ; 43 import javax.xml.namespace.QName ; 44 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 import org.apache.geronimo.common.DeploymentException; 48 import org.apache.geronimo.deployment.util.DeploymentUtil; 49 import org.apache.geronimo.deployment.ModuleIDBuilder; 50 import org.apache.geronimo.deployment.NamespaceDrivenBuilderCollection; 51 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil; 52 import org.apache.geronimo.deployment.xbeans.ServiceDocument; 53 import org.apache.geronimo.gbean.AbstractName; 54 import org.apache.geronimo.gbean.AbstractNameQuery; 55 import org.apache.geronimo.j2ee.deployment.EARContext; 56 import org.apache.geronimo.j2ee.deployment.Module; 57 import org.apache.geronimo.j2ee.deployment.ModuleBuilder; 58 import org.apache.geronimo.j2ee.deployment.NamingBuilder; 59 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 60 import org.apache.geronimo.kernel.Kernel; 61 import org.apache.geronimo.kernel.Naming; 62 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 63 import org.apache.geronimo.kernel.config.ConfigurationStore; 64 import org.apache.geronimo.kernel.repository.Artifact; 65 import org.apache.geronimo.kernel.repository.Environment; 66 import org.apache.geronimo.kernel.repository.ImportType; 67 import org.apache.geronimo.naming.deployment.ResourceEnvironmentSetter; 68 import org.apache.geronimo.security.jacc.ComponentPermissions; 69 import org.apache.geronimo.security.util.URLPattern; 70 import org.apache.geronimo.xbeans.javaee.FilterMappingType; 71 import org.apache.geronimo.xbeans.javaee.RoleNameType; 72 import org.apache.geronimo.xbeans.javaee.SecurityConstraintType; 73 import org.apache.geronimo.xbeans.javaee.SecurityRoleRefType; 74 import org.apache.geronimo.xbeans.javaee.SecurityRoleType; 75 import org.apache.geronimo.xbeans.javaee.ServletMappingType; 76 import org.apache.geronimo.xbeans.javaee.ServletType; 77 import org.apache.geronimo.xbeans.javaee.UrlPatternType; 78 import org.apache.geronimo.xbeans.javaee.WebAppType; 79 import org.apache.geronimo.xbeans.javaee.WebResourceCollectionType; 80 import org.apache.geronimo.xbeans.javaee.WebAppDocument; 81 import org.apache.geronimo.xbeans.geronimo.j2ee.GerSecurityDocument; 82 import org.apache.geronimo.schema.SchemaConversionUtils; 83 import org.apache.xmlbeans.XmlObject; 84 import org.apache.xmlbeans.XmlException; 85 import org.apache.xmlbeans.XmlCursor; 86 import org.apache.xmlbeans.XmlDocumentProperties; 87 88 91 public abstract class AbstractWebModuleBuilder implements ModuleBuilder { 92 private static final Log log = LogFactory.getLog(AbstractWebModuleBuilder.class); 93 94 private static final QName TAGLIB = new QName (SchemaConversionUtils.JAVAEE_NAMESPACE, "taglib"); 95 96 private static final String LINE_SEP = System.getProperty("line.separator"); 97 98 protected static final AbstractNameQuery MANAGED_CONNECTION_FACTORY_PATTERN; 99 private static final AbstractNameQuery ADMIN_OBJECT_PATTERN; 100 protected static final AbstractNameQuery STATELESS_SESSION_BEAN_PATTERN; 101 protected static final AbstractNameQuery STATEFUL_SESSION_BEAN_PATTERN; 102 protected static final AbstractNameQuery ENTITY_BEAN_PATTERN; 103 protected final Kernel kernel; 104 protected final NamespaceDrivenBuilderCollection securityBuilders; 105 protected final NamespaceDrivenBuilderCollection serviceBuilders; 106 protected final ResourceEnvironmentSetter resourceEnvironmentSetter; 107 108 protected final NamingBuilder namingBuilders; 109 110 private static final QName SECURITY_QNAME = GerSecurityDocument.type.getDocumentElementName(); 111 private static final QName SERVICE_QNAME = ServiceDocument.type.getDocumentElementName(); 112 113 118 private static final URI RELATIVE_MODULE_BASE_URI = URI.create("../"); 119 120 protected AbstractWebModuleBuilder(Kernel kernel, Collection securityBuilders, Collection serviceBuilders, NamingBuilder namingBuilders, ResourceEnvironmentSetter resourceEnvironmentSetter) { 121 this.kernel = kernel; 122 this.securityBuilders = new NamespaceDrivenBuilderCollection(securityBuilders, SECURITY_QNAME); 123 this.serviceBuilders = new NamespaceDrivenBuilderCollection(serviceBuilders, SERVICE_QNAME); 124 this.namingBuilders = namingBuilders; 125 this.resourceEnvironmentSetter = resourceEnvironmentSetter; 126 } 127 128 static { 129 MANAGED_CONNECTION_FACTORY_PATTERN = new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JCA_MANAGED_CONNECTION_FACTORY)); 130 ADMIN_OBJECT_PATTERN = new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.JCA_ADMIN_OBJECT)); 131 STATELESS_SESSION_BEAN_PATTERN = new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.STATELESS_SESSION_BEAN)); 132 STATEFUL_SESSION_BEAN_PATTERN = new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.STATEFUL_SESSION_BEAN)); 133 ENTITY_BEAN_PATTERN = new AbstractNameQuery(null, Collections.singletonMap(NameFactory.J2EE_TYPE, NameFactory.ENTITY_BEAN)); 134 135 } 136 137 public NamingBuilder getNamingBuilders() { 138 return namingBuilders; 139 } 140 141 protected Set findGBeanDependencies(EARContext earContext) { 143 Set dependencies = new HashSet (); 144 dependencies.addAll(earContext.listGBeans(MANAGED_CONNECTION_FACTORY_PATTERN)); 145 dependencies.addAll(earContext.listGBeans(ADMIN_OBJECT_PATTERN)); 146 dependencies.addAll(earContext.listGBeans(STATELESS_SESSION_BEAN_PATTERN)); 147 dependencies.addAll(earContext.listGBeans(STATEFUL_SESSION_BEAN_PATTERN)); 148 dependencies.addAll(earContext.listGBeans(ENTITY_BEAN_PATTERN)); 149 return dependencies; 150 } 151 152 public Module createModule(File plan, JarFile moduleFile, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException { 153 return createModule(plan, moduleFile, ".", null, true, null, null, naming, idBuilder); 154 } 155 156 public Module createModule(Object plan, JarFile moduleFile, String targetPath, URL specDDUrl, Environment environment, Object moduleContextInfo, AbstractName earName, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException { 157 return createModule(plan, moduleFile, targetPath, specDDUrl, false, (String ) moduleContextInfo, earName, naming, idBuilder); 158 } 159 160 protected abstract Module createModule(Object plan, JarFile moduleFile, String targetPath, URL specDDUrl, boolean standAlone, String contextRoot, AbstractName earName, Naming naming, ModuleIDBuilder idBuilder) throws DeploymentException; 161 162 170 protected Map buildServletNameToPathMap(WebAppType webApp, String contextRoot) { 171 contextRoot = "/" + contextRoot; 172 Map map = new HashMap (); 173 ServletMappingType[] servletMappings = webApp.getServletMappingArray(); 174 for (int j = 0; j < servletMappings.length; j++) { 175 ServletMappingType servletMapping = servletMappings[j]; 176 String servletName = servletMapping.getServletName().getStringValue().trim(); 177 UrlPatternType[] urlPatterns = servletMapping.getUrlPatternArray(); 178 179 for (int i=0; urlPatterns != null && (i < urlPatterns.length); i++) { 180 map.put(servletName, contextRoot +urlPatterns[i].getStringValue().trim()); 181 } 182 } 183 return map; 184 } 185 186 protected String determineDefaultContextRoot(WebAppType webApp, boolean isStandAlone, JarFile moduleFile, String targetPath) { 187 188 if (webApp != null && webApp.getId() != null) { 189 return webApp.getId(); 190 } 191 192 if (isStandAlone) { 193 return "/" + trimPath(new File (moduleFile.getName()).getName()); 195 } 196 197 return trimPath(targetPath); 199 } 200 201 private String trimPath(String path) { 202 203 if (path == null) { 204 return null; 205 } 206 207 if (path.endsWith(".war")) { 208 path = path.substring(0, path.length() - 4); 209 } 210 if (path.endsWith("/")) { 211 path = path.substring(0, path.length() - 1); 212 } 213 214 return path; 215 } 216 217 public void installModule(JarFile earFile, EARContext earContext, Module module, Collection configurationStores, ConfigurationStore targetConfigurationStore, Collection repositories) throws DeploymentException { 218 EARContext moduleContext; 219 if (module.isStandAlone()) { 220 moduleContext = earContext; 221 } else { 222 Environment environment = module.getEnvironment(); 223 Artifact earConfigId = earContext.getConfigID(); 224 Artifact configId = new Artifact(earConfigId.getGroupId(), earConfigId.getArtifactId() + "_" + module.getTargetPath(), earConfigId.getVersion(), "car"); 225 environment.setConfigId(configId); 226 environment.addDependency(earConfigId, ImportType.ALL); 227 File configurationDir = new File (earContext.getBaseDir(), module.getTargetPath()); 228 configurationDir.mkdirs(); 229 230 try { 232 File inPlaceConfigurationDir = null; 233 if (null != earContext.getInPlaceConfigurationDir()) { 234 inPlaceConfigurationDir = new File (earContext.getInPlaceConfigurationDir(), module.getTargetPath()); 235 } 236 moduleContext = new EARContext(configurationDir, 237 inPlaceConfigurationDir, 238 environment, 239 ConfigurationModuleType.WAR, 240 module.getModuleName(), 241 earContext); 242 } catch (DeploymentException e) { 243 cleanupConfigurationDir(configurationDir); 244 throw e; 245 } 246 } 247 module.setEarContext(moduleContext); 248 module.setRootEarContext(earContext); 249 250 try { 251 JarFile warFile = module.getModuleFile(); 253 Enumeration entries = warFile.entries(); 254 while (entries.hasMoreElements()) { 255 ZipEntry entry = (ZipEntry ) entries.nextElement(); 256 URI targetPath = new URI (null, entry.getName(), null); 257 if (entry.getName().equals("WEB-INF/web.xml")) { 258 moduleContext.addFile(targetPath, module.getOriginalSpecDD()); 259 } else if (entry.getName().startsWith("WEB-INF/lib") && entry.getName().endsWith(".jar")) { 260 moduleContext.addInclude(targetPath, warFile, entry); 261 } else { 262 moduleContext.addFile(targetPath, warFile, entry); 263 } 264 } 265 266 moduleContext.getConfiguration().addToClassPath("WEB-INF/classes/"); 269 270 moduleContext.addManifestClassPath(warFile, RELATIVE_MODULE_BASE_URI); 274 275 } catch (IOException e) { 276 throw new DeploymentException("Problem deploying war", e); 277 } catch (URISyntaxException e) { 278 throw new DeploymentException("Could not construct URI for location of war entry", e); 279 } finally { 280 if (!module.isStandAlone()) { 281 try { 282 moduleContext.flush(); 283 } catch (IOException e) { 284 throw new DeploymentException("Problem closing war context", e); 285 } 286 } 287 } 288 } 289 290 protected WebAppDocument convertToServletSchema(XmlObject xmlObject) throws XmlException { 291 292 String schemaLocationURL = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"; 293 String version = "2.5"; 294 XmlCursor cursor = xmlObject.newCursor(); 295 try { 296 cursor.toStartDoc(); 297 cursor.toFirstChild(); 298 if ("http://java.sun.com/xml/ns/j2ee".equals(cursor.getName().getNamespaceURI())) { 299 SchemaConversionUtils.convertSchemaVersion(cursor, SchemaConversionUtils.JAVAEE_NAMESPACE, schemaLocationURL, version); 300 XmlObject result = xmlObject.changeType(WebAppDocument.type); 301 XmlBeansUtil.validateDD(result); 302 return (WebAppDocument) result; 303 } 304 305 if ("http://java.sun.com/xml/ns/javaee".equals(cursor.getName().getNamespaceURI())) { 306 SchemaConversionUtils.convertSchemaVersion(cursor, SchemaConversionUtils.JAVAEE_NAMESPACE, schemaLocationURL, version); 307 XmlObject result = xmlObject.changeType(WebAppDocument.type); 308 XmlBeansUtil.validateDD(result); 309 return (WebAppDocument) result; 310 } 311 312 XmlDocumentProperties xmlDocumentProperties = cursor.documentProperties(); 314 String publicId = xmlDocumentProperties.getDoctypePublicId(); 315 if ("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN".equals(publicId) || 316 "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN".equals(publicId)) { 317 XmlCursor moveable = xmlObject.newCursor(); 318 try { 319 moveable.toStartDoc(); 320 moveable.toFirstChild(); 321 322 SchemaConversionUtils.convertToSchema(cursor, SchemaConversionUtils.JAVAEE_NAMESPACE, schemaLocationURL, version); 323 cursor.toStartDoc(); 324 cursor.toChild(SchemaConversionUtils.JAVAEE_NAMESPACE, "web-app"); 325 cursor.toFirstChild(); 326 SchemaConversionUtils.convertToDescriptionGroup(SchemaConversionUtils.JAVAEE_NAMESPACE, cursor, moveable); 327 SchemaConversionUtils.convertToJNDIEnvironmentRefsGroup(SchemaConversionUtils.JAVAEE_NAMESPACE, cursor, moveable); 328 cursor.push(); 329 if (cursor.toNextSibling(TAGLIB)) { 330 cursor.toPrevSibling(); 331 moveable.toCursor(cursor); 332 cursor.beginElement("jsp-config", SchemaConversionUtils.JAVAEE_NAMESPACE); 333 while (moveable.toNextSibling(TAGLIB)) { 334 moveable.moveXml(cursor); 335 } 336 } 337 cursor.pop(); 338 do { 339 String name = cursor.getName().getLocalPart(); 340 if ("filter".equals(name) || "servlet".equals(name) || "context-param".equals(name)) { 341 cursor.push(); 342 cursor.toFirstChild(); 343 SchemaConversionUtils.convertToDescriptionGroup(SchemaConversionUtils.JAVAEE_NAMESPACE, cursor, moveable); 344 while (cursor.toNextSibling(SchemaConversionUtils.JAVAEE_NAMESPACE, "init-param")) { 345 cursor.push(); 346 cursor.toFirstChild(); 347 SchemaConversionUtils.convertToDescriptionGroup(SchemaConversionUtils.JAVAEE_NAMESPACE, cursor, moveable); 348 cursor.pop(); 349 } 350 cursor.pop(); 351 } 352 } while (cursor.toNextSibling()); 353 } finally { 354 moveable.dispose(); 355 } 356 } 357 } finally { 358 cursor.dispose(); 359 } 360 XmlObject result = xmlObject.changeType(WebAppDocument.type); 361 if (result != null) { 362 XmlBeansUtil.validateDD(result); 363 return (WebAppDocument) result; 364 } 365 XmlBeansUtil.validateDD(xmlObject); 366 return (WebAppDocument) xmlObject; 367 } 368 369 370 protected void addUnmappedJSPPermissions(Set securityRoles, Map rolePermissions) { 371 for (Iterator iter = securityRoles.iterator(); iter.hasNext();) { 372 String roleName = (String ) iter.next(); 373 addPermissionToRole(roleName, new WebRoleRefPermission ("", roleName), rolePermissions); 374 } 375 } 376 377 protected ComponentPermissions buildSpecSecurityConfig(WebAppType webApp, Set securityRoles, Map rolePermissions) { 378 Map uncheckedPatterns = new HashMap (); 379 Map uncheckedResourcePatterns = new HashMap (); 380 Map uncheckedUserPatterns = new HashMap (); 381 Map excludedPatterns = new HashMap (); 382 Map rolesPatterns = new HashMap (); 383 Set allSet = new HashSet (); Map allMap = new HashMap (); 386 SecurityConstraintType[] securityConstraintArray = webApp.getSecurityConstraintArray(); 387 for (int i = 0; i < securityConstraintArray.length; i++) { 388 SecurityConstraintType securityConstraintType = securityConstraintArray[i]; 389 Map currentPatterns; 390 if (securityConstraintType.isSetAuthConstraint()) { 391 if (securityConstraintType.getAuthConstraint().getRoleNameArray().length == 0) { 392 currentPatterns = excludedPatterns; 393 } else { 394 currentPatterns = rolesPatterns; 395 } 396 } else { 397 currentPatterns = uncheckedPatterns; 398 } 399 400 String transport = ""; 401 if (securityConstraintType.isSetUserDataConstraint()) { 402 transport = securityConstraintType.getUserDataConstraint().getTransportGuarantee().getStringValue().trim().toUpperCase(); 403 } 404 405 WebResourceCollectionType[] webResourceCollectionTypeArray = securityConstraintType.getWebResourceCollectionArray(); 406 for (int j = 0; j < webResourceCollectionTypeArray.length; j++) { 407 WebResourceCollectionType webResourceCollectionType = webResourceCollectionTypeArray[j]; 408 UrlPatternType[] urlPatternTypeArray = webResourceCollectionType.getUrlPatternArray(); 409 for (int k = 0; k < urlPatternTypeArray.length; k++) { 410 UrlPatternType urlPatternType = urlPatternTypeArray[k]; 411 String url = urlPatternType.getStringValue().trim(); 412 URLPattern pattern = (URLPattern) currentPatterns.get(url); 413 if (pattern == null) { 414 pattern = new URLPattern(url); 415 currentPatterns.put(url, pattern); 416 } 417 418 URLPattern allPattern = (URLPattern) allMap.get(url); 419 if (allPattern == null) { 420 allPattern = new URLPattern(url); 421 allSet.add(allPattern); 422 allMap.put(url, allPattern); 423 } 424 425 String [] httpMethodTypeArray = webResourceCollectionType.getHttpMethodArray(); 426 if (httpMethodTypeArray.length == 0) { 427 pattern.addMethod(""); 428 allPattern.addMethod(""); 429 } else { 430 for (int l = 0; l < httpMethodTypeArray.length; l++) { 431 String method = (httpMethodTypeArray[l]==null?null:httpMethodTypeArray[l].trim()); 433 if (method != null) { 434 pattern.addMethod(method); 435 allPattern.addMethod(method); 436 } 437 } 438 } 439 if (currentPatterns == rolesPatterns) { 440 RoleNameType[] roleNameTypeArray = securityConstraintType.getAuthConstraint().getRoleNameArray(); 441 for (int l = 0; l < roleNameTypeArray.length; l++) { 442 RoleNameType roleNameType = roleNameTypeArray[l]; 443 String role = roleNameType.getStringValue().trim(); 444 if (role.equals("*")) { 445 pattern.addAllRoles(securityRoles); 446 } else { 447 pattern.addRole(role); 448 } 449 } 450 } 451 452 pattern.setTransport(transport); 453 } 454 } 455 } 456 457 PermissionCollection excludedPermissions = new Permissions (); 458 PermissionCollection uncheckedPermissions = new Permissions (); 459 460 Iterator iter = excludedPatterns.keySet().iterator(); 461 while (iter.hasNext()) { 462 URLPattern pattern = (URLPattern) excludedPatterns.get(iter.next()); 463 String name = pattern.getQualifiedPattern(allSet); 464 String actions = pattern.getMethods(); 465 466 excludedPermissions.add(new WebResourcePermission (name, actions)); 467 excludedPermissions.add(new WebUserDataPermission (name, actions)); 468 } 469 470 iter = rolesPatterns.keySet().iterator(); 471 while (iter.hasNext()) { 472 URLPattern pattern = (URLPattern) rolesPatterns.get(iter.next()); 473 String name = pattern.getQualifiedPattern(allSet); 474 String actions = pattern.getMethods(); 475 WebResourcePermission permission = new WebResourcePermission (name, actions); 476 477 for (Iterator names = pattern.getRoles().iterator(); names.hasNext();) { 478 String roleName = (String ) names.next(); 479 addPermissionToRole(roleName, permission, rolePermissions); 480 } 481 } 482 483 iter = uncheckedPatterns.keySet().iterator(); 484 while (iter.hasNext()) { 485 URLPattern pattern = (URLPattern) uncheckedPatterns.get(iter.next()); 486 String name = pattern.getQualifiedPattern(allSet); 487 String actions = pattern.getMethods(); 488 489 addOrUpdatePattern(uncheckedResourcePatterns, name, actions); 490 } 491 492 iter = rolesPatterns.keySet().iterator(); 493 while (iter.hasNext()) { 494 URLPattern pattern = (URLPattern) rolesPatterns.get(iter.next()); 495 String name = pattern.getQualifiedPattern(allSet); 496 String actions = pattern.getMethodsWithTransport(); 497 498 addOrUpdatePattern(uncheckedUserPatterns, name, actions); 499 } 500 501 iter = uncheckedPatterns.keySet().iterator(); 502 while (iter.hasNext()) { 503 URLPattern pattern = (URLPattern) uncheckedPatterns.get(iter.next()); 504 String name = pattern.getQualifiedPattern(allSet); 505 String actions = pattern.getMethodsWithTransport(); 506 507 addOrUpdatePattern(uncheckedUserPatterns, name, actions); 508 } 509 510 519 iter = allSet.iterator(); 520 while (iter.hasNext()) { 521 URLPattern pattern = (URLPattern) iter.next(); 522 String name = pattern.getQualifiedPattern(allSet); 523 String actions = pattern.getComplementedMethods(); 524 525 if (actions.length() == 0) { 526 continue; 527 } 528 529 addOrUpdatePattern(uncheckedResourcePatterns, name, actions); 530 addOrUpdatePattern(uncheckedUserPatterns, name, actions); 531 } 532 533 URLPattern pattern = new URLPattern("/"); 534 if (!allSet.contains(pattern)) { 535 String name = pattern.getQualifiedPattern(allSet); 536 String actions = pattern.getComplementedMethods(); 537 538 addOrUpdatePattern(uncheckedResourcePatterns, name, actions); 539 addOrUpdatePattern(uncheckedUserPatterns, name, actions); 540 } 541 542 iter = uncheckedResourcePatterns.keySet().iterator(); 544 while (iter.hasNext()) { 545 UncheckedItem item = (UncheckedItem) iter.next(); 546 String actions = (String ) uncheckedResourcePatterns.get(item); 547 548 uncheckedPermissions.add(new WebResourcePermission (item.getName(), actions)); 549 } 550 iter = uncheckedUserPatterns.keySet().iterator(); 552 while (iter.hasNext()) { 553 UncheckedItem item = (UncheckedItem) iter.next(); 554 String actions = (String ) uncheckedUserPatterns.get(item); 555 556 uncheckedPermissions.add(new WebUserDataPermission (item.getName(), actions)); 557 } 558 559 return new ComponentPermissions(excludedPermissions, uncheckedPermissions, rolePermissions); 560 561 } 562 563 protected void addPermissionToRole(String roleName, Permission permission, Map rolePermissions) { 564 PermissionCollection permissionsForRole = (PermissionCollection ) rolePermissions.get(roleName); 565 if (permissionsForRole == null) { 566 permissionsForRole = new Permissions (); 567 rolePermissions.put(roleName, permissionsForRole); 568 } 569 permissionsForRole.add(permission); 570 } 571 572 private void addOrUpdatePattern(Map patternMap, String name, String actions) { 573 UncheckedItem item = new UncheckedItem(name, actions); 574 String existingActions = (String ) patternMap.get(item); 575 if (existingActions != null) { 576 patternMap.put(item, actions + "," + existingActions); 577 return; 578 } 579 580 patternMap.put(item, actions); 581 } 582 583 protected static Set collectRoleNames(WebAppType webApp) { 584 Set roleNames = new HashSet (); 585 586 SecurityRoleType[] securityRoles = webApp.getSecurityRoleArray(); 587 for (int i = 0; i < securityRoles.length; i++) { 588 roleNames.add(securityRoles[i].getRoleName().getStringValue().trim()); 589 } 590 591 return roleNames; 592 } 593 594 protected static void check(WebAppType webApp) throws DeploymentException { 595 checkURLPattern(webApp); 596 checkMultiplicities(webApp); 597 } 598 599 private static void checkURLPattern(WebAppType webApp) throws DeploymentException { 600 601 FilterMappingType[] filterMappings = webApp.getFilterMappingArray(); 602 for (int i = 0; i < filterMappings.length; i++) { 603 UrlPatternType[] urlPatterns = filterMappings[i].getUrlPatternArray(); 604 for (int j=0; (urlPatterns != null) && (j < urlPatterns.length); j++) { 605 checkString(urlPatterns[j].getStringValue().trim()); 606 } 607 } 608 609 ServletMappingType[] servletMappings = webApp.getServletMappingArray(); 610 for (int i = 0; i < servletMappings.length; i++) { 611 UrlPatternType[] urlPatterns = servletMappings[i].getUrlPatternArray(); 612 for (int j=0; (urlPatterns != null) && (j < urlPatterns.length); j++) { 613 checkString(urlPatterns[j].getStringValue().trim()); 614 } 615 } 616 617 SecurityConstraintType[] constraints = webApp.getSecurityConstraintArray(); 618 for (int i = 0; i < constraints.length; i++) { 619 WebResourceCollectionType[] collections = constraints[i].getWebResourceCollectionArray(); 620 for (int j = 0; j < collections.length; j++) { 621 UrlPatternType[] patterns = collections[j].getUrlPatternArray(); 622 for (int k = 0; k < patterns.length; k++) { 623 checkString(patterns[k].getStringValue().trim()); 624 } 625 } 626 } 627 } 628 629 protected static void checkString(String pattern) throws DeploymentException { 630 if (pattern.indexOf(0x0D) >= 0) throw new DeploymentException("<url-pattern> must not contain CR(#xD)"); 632 if (pattern.indexOf(0x0A) >= 0) throw new DeploymentException("<url-pattern> must not contain LF(#xA)"); 633 } 634 635 private static void checkMultiplicities(WebAppType webApp) throws DeploymentException { 636 if (webApp.getSessionConfigArray().length > 1) throw new DeploymentException("Multiple <session-config> elements found"); 637 if (webApp.getJspConfigArray().length > 1) throw new DeploymentException("Multiple <jsp-config> elements found"); 638 if (webApp.getLoginConfigArray().length > 1) throw new DeploymentException("Multiple <login-config> elements found"); 639 } 640 641 private boolean cleanupConfigurationDir(File configurationDir) 642 { 643 LinkedList cannotBeDeletedList = new LinkedList (); 644 645 if (!DeploymentUtil.recursiveDelete(configurationDir,cannotBeDeletedList)) { 646 log.warn("Unable to delete " + cannotBeDeletedList.size() + 648 " files while recursively deleting directory " 649 + configurationDir + LINE_SEP + 650 "The first file that could not be deleted was:" + LINE_SEP + " "+ 651 ( !cannotBeDeletedList.isEmpty() ? cannotBeDeletedList.getFirst() : "") ); 652 return false; 653 } 654 return true; 655 } 656 657 protected void processRoleRefPermissions(ServletType servletType, Set securityRoles, Map rolePermissions) { 658 String servletName = servletType.getServletName().getStringValue().trim(); 659 SecurityRoleRefType[] securityRoleRefTypeArray = servletType.getSecurityRoleRefArray(); 661 Set unmappedRoles = new HashSet (securityRoles); 662 for (int j = 0; j < securityRoleRefTypeArray.length; j++) { 663 SecurityRoleRefType securityRoleRefType = securityRoleRefTypeArray[j]; 664 String roleName = securityRoleRefType.getRoleName().getStringValue().trim(); 665 String roleLink = securityRoleRefType.getRoleLink().getStringValue().trim(); 666 674 addPermissionToRole(roleLink, new WebRoleRefPermission (servletName, roleName), rolePermissions); 675 unmappedRoles.remove(roleName); 676 } 677 for (Iterator iterator = unmappedRoles.iterator(); iterator.hasNext();) { 678 String roleName = (String ) iterator.next(); 679 addPermissionToRole(roleName, new WebRoleRefPermission (servletName, roleName), rolePermissions); 680 } 681 } 683 684 protected void buildSubstitutionGroups(XmlObject gerWebApp, boolean hasSecurityRealmName, Module module, EARContext earContext) throws DeploymentException { 685 XmlObject[] securityElements = XmlBeansUtil.selectSubstitutionGroupElements(SECURITY_QNAME, gerWebApp); 686 if (securityElements.length > 0 && !hasSecurityRealmName) { 687 throw new DeploymentException("You have supplied a security configuration for web app " + module.getName() + " but no security-realm-name to allow login"); 688 } 689 securityBuilders.build(gerWebApp, earContext, module.getEarContext()); 690 serviceBuilders.build(gerWebApp, earContext, module.getEarContext()); 691 } 692 693 class UncheckedItem { 694 final static int NA = 0x00; 695 final static int INTEGRAL = 0x01; 696 final static int CONFIDENTIAL = 0x02; 697 698 private int transportType = NA; 699 private String name; 700 701 public UncheckedItem(String name, String actions) { 702 setName(name); 703 setTransportType(actions); 704 } 705 706 public boolean equals(Object o) { 707 UncheckedItem item = (UncheckedItem) o; 708 return item.getKey().equals(this.getKey()); 709 } 710 711 public String getKey() { 712 return (name + transportType); 713 } 714 715 public int hashCode() { 716 return getKey().hashCode(); 717 } 718 719 public String getName() { 720 return name; 721 } 722 723 public void setName(String name) { 724 this.name = name; 725 } 726 727 public int getTransportType() { 728 return transportType; 729 } 730 731 public void setTransportType(String actions) { 732 String [] tokens = actions.split(":", 2); 733 if (tokens.length == 2) { 734 if (tokens[1].equals("INTEGRAL")) { 735 this.transportType = INTEGRAL; 736 } else if (tokens[1].equals("CONFIDENTIAL")) { 737 this.transportType = CONFIDENTIAL; 738 } 739 } 740 } 741 } 742 } 743 | Popular Tags |