Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2011, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.as.webservices.tomcat;
 
 import static org.jboss.as.webservices.WSLogger.ROOT_LOGGER;
 
 import java.util.List;
 
 import  org.jboss.metadata.ear.spec.EarMetaData;
 import  org.jboss.metadata.javaee.spec.SecurityRolesMetaData;
 import  org.jboss.metadata.merge.javaee.spec.SecurityRolesMetaDataMerger;
 import  org.jboss.metadata.web.jboss.JBossServletsMetaData;
 import  org.jboss.metadata.web.jboss.JBossWebMetaData;
 import  org.jboss.metadata.web.spec.LoginConfigMetaData;
 import  org.jboss.metadata.web.spec.SecurityConstraintMetaData;
 import  org.jboss.metadata.web.spec.ServletMappingMetaData;
 import  org.jboss.metadata.web.spec.WebResourceCollectionsMetaData;

Author(s):
Richard Opalka
 
 final class WebMetaDataCreator {
 
     private static final String EJB_WEBSERVICE_REALM = "EJBWebServiceEndpointServlet Realm";
 
 
     WebMetaDataCreator() {
         super();
     }

    
Creates web meta data for EJB deployments.

Parameters:
dep webservice deployment
 
     void create(final Deployment dep) {
         final DeploymentUnit unit = WSHelper.getRequiredAttachment(depDeploymentUnit.class);
         WarMetaData warMD = ASHelper.getOptionalAttachment(unit.);
         JBossWebMetaData jbossWebMD = warMD != null ? warMD.getMergedJBossWebMetaData() : null;
 
         if (warMD == null) {
             warMD = new WarMetaData();
         }
         if (jbossWebMD == null) {
             jbossWebMD = new JBossWebMetaData();
             warMD.setMergedJBossWebMetaData(jbossWebMD);
             unit.putAttachment(.warMD);
         }
 
         createWebAppDescriptor(depjbossWebMD);
         createJBossWebAppDescriptor(depjbossWebMD);
 
         dep.addAttachment(JBossWebMetaData.classjbossWebMD);
     }

    
Creates web.xml descriptor meta data.

Parameters:
dep webservice deployment
jbossWebMD jboss web meta data
 
     private void createWebAppDescriptor(final Deployment depfinal JBossWebMetaData jbossWebMD) {
         createServlets(depjbossWebMD);
         createServletMappings(depjbossWebMD);
         createSecurityConstraints(depjbossWebMD);
         createLoginConfig(depjbossWebMD);
         createSecurityRoles(depjbossWebMD);
     }

    
Creates jboss-web.xml descriptor meta data.

 <jboss-web>
   <security-domain>java:/jaas/custom-security-domain</security-domain>
   <context-root>/custom-context-root</context-root>
   <virtual-host>host1</virtual-host>
   ...
   <virtual-host>hostN</virtual-host>
 </jboss-web>
 

Parameters:
dep webservice deployment
jbossWebMD jboss web meta data
    private void createJBossWebAppDescriptor(final Deployment depfinal JBossWebMetaData jbossWebMD) {
        // Set security domain
        final String securityDomain = .getSecurityDomain(dep);
        final boolean hasSecurityDomain = securityDomain != null;
        if (hasSecurityDomain) {
            .settingSecurityDomain(securityDomain);
            jbossWebMD.setSecurityDomain(securityDomain);
        }
        // Set virtual host
        final String virtualHost = dep.getService().getVirtualHost();
        if (virtualHost != null) {
            .settingVirtualHost(virtualHost);
            jbossWebMD.setVirtualHosts(Arrays.asList(virtualHost));
        }
    }

    
Creates servlets part of web.xml descriptor.

 <servlet>
   <servlet-name>EJBEndpointShortName</servlet-name>
   <servlet-class>EJBEndpointTargetBeanName</servlet-class>
 </servlet>
 

Parameters:
dep webservice deployment
jbossWebMD jboss web meta data
    private void createServlets(final Deployment depfinal JBossWebMetaData jbossWebMD) {
        .creatingServlets();
        final JBossServletsMetaData servlets = WebMetaDataHelper.getServlets(jbossWebMD);
        for (final Endpoint endpoint : dep.getService().getEndpoints()) {
            final String endpointName = endpoint.getShortName();
            final String endpointClassName = endpoint.getTargetBeanName();
            .creatingServlet(endpointNameendpointClassName);
            WebMetaDataHelper.newServlet(endpointNameendpointClassNameservlets);
        }
    }

    
Creates servlet-mapping part of web.xml descriptor.

 <servlet-mapping>
   <servlet-name>EJBEndpointShortName</servlet-name>
   <url-pattern>EJBEndpointURLPattern</url-pattern>
 </servlet-mapping>
 

Parameters:
dep webservice deployment
jbossWebMD jboss web meta data
    private void createServletMappings(final Deployment depfinal JBossWebMetaData jbossWebMD) {
        final List<ServletMappingMetaData> servletMappings = WebMetaDataHelper.getServletMappings(jbossWebMD);
        for (final Endpoint ep : dep.getService().getEndpoints()) {
            if (ep instanceof HttpEndpoint) {
                final String endpointName = ep.getShortName();
                final List<StringurlPatterns = WebMetaDataHelper.getUrlPatterns(((HttpEndpointep).getURLPattern());
                .creatingServletMapping(endpointNameurlPatterns);
                WebMetaDataHelper.newServletMapping(endpointNameurlPatternsservletMappings);
            }
        }
    }

    
Creates security constraints part of web.xml descriptor.

 <security-constraint>
   <web-resource-collection>
     <web-resource-name>EJBEndpointShortName</web-resource-name>
     <url-pattern>EJBEndpointURLPattern</url-pattern>
     <http-method>GET</http-method>
     <http-method>POST</http-method>
   </web-resource-collection>
   <auth-constraint>
     <role-name>*</role-name>
   </auth-constraint>
   <user-data-constraint>
     <transport-guarantee>EjbTransportGuarantee</transport-guarantee>
   </user-data-constraint>
 </security-constraint>
 

Parameters:
dep webservice deployment
jbossWebMD jboss web meta data
    private void createSecurityConstraints(final Deployment depfinal JBossWebMetaData jbossWebMD) {
        for (final Endpoint ejbEndpoint : dep.getService().getEndpoints()) {
            final boolean secureWsdlAccess = .isSecureWsdlAccess(ejbEndpoint);
            final String transportGuarantee = .getTransportGuarantee(ejbEndpoint);
            final boolean hasTransportGuarantee = transportGuarantee != null;
            final String authMethod = .getAuthMethod(ejbEndpoint);
            final boolean hasAuthMethod = authMethod != null;
            if (ejbEndpoint instanceof HttpEndpoint && (hasAuthMethod || hasTransportGuarantee)) {
                final List<SecurityConstraintMetaData> securityConstraints = WebMetaDataHelper
                        .getSecurityConstraints(jbossWebMD);
                // security-constraint
                final SecurityConstraintMetaData securityConstraint = WebMetaDataHelper
                        .newSecurityConstraint(securityConstraints);
                // web-resource-collection
                final WebResourceCollectionsMetaData webResourceCollections = WebMetaDataHelper
                        .getWebResourceCollections(securityConstraint);
                final String endpointName = ejbEndpoint.getShortName();
                final String urlPattern = ((HttpEndpointejbEndpoint).getURLPattern();
                .creatingWebResourceCollection(endpointNameurlPattern);
                WebMetaDataHelper.newWebResourceCollection(endpointNameurlPatternsecureWsdlAccess,
                        webResourceCollections);
                // auth-constraint
                if (hasAuthMethod) {
                    .creatingAuthConstraint(endpointName);
                    WebMetaDataHelper.newAuthConstraint(WebMetaDataHelper.getAllRoles(), securityConstraint);
                }
                // user-data-constraint
                if (hasTransportGuarantee) {
                    .creatingUserDataConstraint(endpointNametransportGuarantee);
                    WebMetaDataHelper.newUserDataConstraint(transportGuaranteesecurityConstraint);
                }
            }
        }
    }

    
Creates login-config part of web.xml descriptor.

 <login-config>
   <auth-method>EjbDeploymentAuthMethod</auth-method>
   <realm-name>EJBWebServiceEndpointServlet Realm</realm-name>
 </login-config>
 

Parameters:
dep webservice deployment
jbossWebMD jboss web meta data
    private void createLoginConfig(final Deployment depfinal JBossWebMetaData jbossWebMD) {
        final String authMethod = getAuthMethod(dep);
        final boolean hasAuthMethod = authMethod != null;
        final String realmName = getRealmName(dep);
        if (hasAuthMethod) {
            .creatingLoginConfig(authMethod);
            final LoginConfigMetaData loginConfig = WebMetaDataHelper.getLoginConfig(jbossWebMD);
            if (realmName != null) {
                loginConfig.setRealmName(realmName);
            } else {
                loginConfig.setRealmName(.);
            }
            loginConfig.setAuthMethod(authMethod);
        }
    }

    
Creates security roles part of web.xml descriptor.

 <security-role>
   <role-name>role1</role-name>
   ...
   <role-name>roleN</role-name>
 </security-role>
 

Parameters:
dep webservice deployment
jbossWebMD jboss web meta data
    private void createSecurityRoles(final Deployment depfinal JBossWebMetaData jbossWebMD) {
        final String authMethod = getAuthMethod(dep);
        final boolean hasAuthMethod = authMethod != null;
        if (hasAuthMethod) {
            final SecurityRolesMetaData securityRolesMD = .getSecurityRoles(dep);
            final boolean hasSecurityRolesMD = securityRolesMD != null && !securityRolesMD.isEmpty();
            if (hasSecurityRolesMD) {
                .creatingSecurityRoles();
                jbossWebMD.setSecurityRoles(securityRolesMD);
            }
        }
        //merge security roles from the ear
        //TODO: is there somewhere better to put this?
        final DeploymentUnit unit = dep.getAttachment(DeploymentUnit.class);
        DeploymentUnit parent = unit.getParent();
        if (parent != null) {
            final EarMetaData earMetaData = parent.getAttachment(......);
            if (earMetaData != null) {
                if (jbossWebMD.getSecurityRoles() == null) {
                    jbossWebMD.setSecurityRoles(new SecurityRolesMetaData());
                }
                SecurityRolesMetaData earSecurityRolesMetaData = earMetaData.getSecurityRoles();
                if (earSecurityRolesMetaData != null) {
                    SecurityRolesMetaDataMerger.merge(jbossWebMD.getSecurityRoles(), jbossWebMD.getSecurityRoles(), earSecurityRolesMetaData);
                }
            }
        }
    }

    
Returns deployment authentication method.

Parameters:
dep webservice deployment
Returns:
deployment authentication method
    private String getAuthMethod(final Deployment dep) {
        for (final Endpoint ejbEndpoint : dep.getService().getEndpoints()) {
            final String beanAuthMethod = .getAuthMethod(ejbEndpoint);
            final boolean hasBeanAuthMethod = beanAuthMethod != null;
            if (hasBeanAuthMethod) {
                // First found auth-method defines war
                // login-config/auth-method
                return beanAuthMethod;
            }
        }
        return null;
    }
     private String getRealmName(final Deployment dep) {
        for (final Endpoint ejbEndpoint : dep.getService().getEndpoints()) {
            final String realmName = .getRealmName(ejbEndpoint);
            final boolean hasRealmName = realmName != null;
            if (hasRealmName) {
                return realmName;
            }
        }
        return null;
      }
New to GrepCode? Check out our FAQ X