PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/webservices/server-integration/src/main/java/org/jboss/as/webservices/tomcat/WebMetaDataCreator.java

#
Java | 366 lines | 174 code | 43 blank | 149 comment | 37 complexity | 8c6019a0112301a83886266988f1edbb MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.webservices.tomcat;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import org.jboss.as.server.deployment.DeploymentUnit;
  26. import org.jboss.as.web.deployment.WarMetaData;
  27. import org.jboss.as.webservices.util.ASHelper;
  28. import org.jboss.as.webservices.util.WebMetaDataHelper;
  29. import org.jboss.metadata.ear.spec.EarMetaData;
  30. import org.jboss.metadata.javaee.spec.SecurityRolesMetaData;
  31. import org.jboss.metadata.merge.javaee.spec.SecurityRolesMetaDataMerger;
  32. import org.jboss.metadata.web.jboss.JBossServletsMetaData;
  33. import org.jboss.metadata.web.jboss.JBossWebMetaData;
  34. import org.jboss.metadata.web.spec.LoginConfigMetaData;
  35. import org.jboss.metadata.web.spec.SecurityConstraintMetaData;
  36. import org.jboss.metadata.web.spec.ServletMappingMetaData;
  37. import org.jboss.metadata.web.spec.WebResourceCollectionsMetaData;
  38. import org.jboss.ws.common.integration.WSHelper;
  39. import org.jboss.wsf.spi.deployment.Deployment;
  40. import org.jboss.wsf.spi.deployment.Endpoint;
  41. import org.jboss.wsf.spi.deployment.HttpEndpoint;
  42. import static org.jboss.as.webservices.WSLogger.ROOT_LOGGER;
  43. /**
  44. * @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
  45. */
  46. final class WebMetaDataCreator {
  47. private static final String EJB_WEBSERVICE_REALM = "EJBWebServiceEndpointServlet Realm";
  48. private final SecurityMetaDataAccessorEJB ejb21SecurityAccessor = new SecurityMetaDataAccessorEJB21();
  49. private final SecurityMetaDataAccessorEJB ejb3SecurityAccessor = new SecurityMetaDataAccessorEJB3();
  50. WebMetaDataCreator() {
  51. super();
  52. }
  53. /**
  54. * Creates web meta data for EJB deployments.
  55. *
  56. * @param dep webservice deployment
  57. */
  58. void create(final Deployment dep) {
  59. final DeploymentUnit unit = WSHelper.getRequiredAttachment(dep, DeploymentUnit.class);
  60. WarMetaData warMD = ASHelper.getOptionalAttachment(unit, WarMetaData.ATTACHMENT_KEY);
  61. JBossWebMetaData jbossWebMD = warMD != null ? warMD.getMergedJBossWebMetaData() : null;
  62. if (warMD == null) {
  63. warMD = new WarMetaData();
  64. }
  65. if (jbossWebMD == null) {
  66. jbossWebMD = new JBossWebMetaData();
  67. warMD.setMergedJBossWebMetaData(jbossWebMD);
  68. unit.putAttachment(WarMetaData.ATTACHMENT_KEY, warMD);
  69. }
  70. createWebAppDescriptor(dep, jbossWebMD);
  71. createJBossWebAppDescriptor(dep, jbossWebMD);
  72. dep.addAttachment(JBossWebMetaData.class, jbossWebMD);
  73. }
  74. /**
  75. * Creates web.xml descriptor meta data.
  76. *
  77. * @param dep webservice deployment
  78. * @param jbossWebMD jboss web meta data
  79. */
  80. private void createWebAppDescriptor(final Deployment dep, final JBossWebMetaData jbossWebMD) {
  81. ROOT_LOGGER.creatingWebXmlDescriptor();
  82. createServlets(dep, jbossWebMD);
  83. createServletMappings(dep, jbossWebMD);
  84. createSecurityConstraints(dep, jbossWebMD);
  85. createLoginConfig(dep, jbossWebMD);
  86. createSecurityRoles(dep, jbossWebMD);
  87. }
  88. /**
  89. * Creates jboss-web.xml descriptor meta data.
  90. * <p/>
  91. * <pre>
  92. * &lt;jboss-web&gt;
  93. * &lt;security-domain&gt;java:/jaas/custom-security-domain&lt;/security-domain&gt;
  94. * &lt;context-root&gt;/custom-context-root&lt;/context-root&gt;
  95. * &lt;virtual-host&gt;host1&lt;/virtual-host&gt;
  96. * ...
  97. * &lt;virtual-host&gt;hostN&lt;/virtual-host&gt;
  98. * &lt;/jboss-web&gt;
  99. * </pre>
  100. *
  101. * @param dep webservice deployment
  102. * @param jbossWebMD jboss web meta data
  103. */
  104. private void createJBossWebAppDescriptor(final Deployment dep, final JBossWebMetaData jbossWebMD) {
  105. ROOT_LOGGER.creatingJBossWebXmlDescriptor();
  106. final SecurityMetaDataAccessorEJB ejbMDAccessor = getEjbSecurityMetaDataAccessor(dep);
  107. // Set security domain
  108. final String securityDomain = ejbMDAccessor.getSecurityDomain(dep);
  109. final boolean hasSecurityDomain = securityDomain != null;
  110. if (hasSecurityDomain) {
  111. ROOT_LOGGER.settingSecurityDomain(securityDomain);
  112. jbossWebMD.setSecurityDomain(securityDomain);
  113. }
  114. // Set virtual host
  115. final String virtualHost = dep.getService().getVirtualHost();
  116. if (virtualHost != null) {
  117. ROOT_LOGGER.settingVirtualHost(virtualHost);
  118. jbossWebMD.setVirtualHosts(Arrays.asList(virtualHost));
  119. }
  120. }
  121. /**
  122. * Creates servlets part of web.xml descriptor.
  123. * <p/>
  124. * <pre>
  125. * &lt;servlet&gt;
  126. * &lt;servlet-name&gt;EJBEndpointShortName&lt;/servlet-name&gt;
  127. * &lt;servlet-class&gt;EJBEndpointTargetBeanName&lt;/servlet-class&gt;
  128. * &lt;/servlet&gt;
  129. * </pre>
  130. *
  131. * @param dep webservice deployment
  132. * @param jbossWebMD jboss web meta data
  133. */
  134. private void createServlets(final Deployment dep, final JBossWebMetaData jbossWebMD) {
  135. ROOT_LOGGER.creatingServlets();
  136. final JBossServletsMetaData servlets = WebMetaDataHelper.getServlets(jbossWebMD);
  137. for (final Endpoint endpoint : dep.getService().getEndpoints()) {
  138. final String endpointName = endpoint.getShortName();
  139. final String endpointClassName = endpoint.getTargetBeanName();
  140. ROOT_LOGGER.creatingServlet(endpointName, endpointClassName);
  141. WebMetaDataHelper.newServlet(endpointName, endpointClassName, servlets);
  142. }
  143. }
  144. /**
  145. * Creates servlet-mapping part of web.xml descriptor.
  146. * <p/>
  147. * <pre>
  148. * &lt;servlet-mapping&gt;
  149. * &lt;servlet-name&gt;EJBEndpointShortName&lt;/servlet-name&gt;
  150. * &lt;url-pattern&gt;EJBEndpointURLPattern&lt;/url-pattern&gt;
  151. * &lt;/servlet-mapping&gt;
  152. * </pre>
  153. *
  154. * @param dep webservice deployment
  155. * @param jbossWebMD jboss web meta data
  156. */
  157. private void createServletMappings(final Deployment dep, final JBossWebMetaData jbossWebMD) {
  158. ROOT_LOGGER.creatingServletMappings();
  159. final List<ServletMappingMetaData> servletMappings = WebMetaDataHelper.getServletMappings(jbossWebMD);
  160. for (final Endpoint ep : dep.getService().getEndpoints()) {
  161. if (ep instanceof HttpEndpoint) {
  162. final String endpointName = ep.getShortName();
  163. final List<String> urlPatterns = WebMetaDataHelper.getUrlPatterns(((HttpEndpoint) ep).getURLPattern());
  164. ROOT_LOGGER.creatingServletMapping(endpointName, urlPatterns);
  165. WebMetaDataHelper.newServletMapping(endpointName, urlPatterns, servletMappings);
  166. }
  167. }
  168. }
  169. /**
  170. * Creates security constraints part of web.xml descriptor.
  171. * <p/>
  172. * <pre>
  173. * &lt;security-constraint&gt;
  174. * &lt;web-resource-collection&gt;
  175. * &lt;web-resource-name&gt;EJBEndpointShortName&lt;/web-resource-name&gt;
  176. * &lt;url-pattern&gt;EJBEndpointURLPattern&lt;/url-pattern&gt;
  177. * &lt;http-method&gt;GET&lt;/http-method&gt;
  178. * &lt;http-method&gt;POST&lt;/http-method&gt;
  179. * &lt;/web-resource-collection&gt;
  180. * &lt;auth-constraint&gt;
  181. * &lt;role-name&gt;*&lt;/role-name&gt;
  182. * &lt;/auth-constraint&gt;
  183. * &lt;user-data-constraint&gt;
  184. * &lt;transport-guarantee&gt;EjbTransportGuarantee&lt;/transport-guarantee&gt;
  185. * &lt;/user-data-constraint&gt;
  186. * &lt;/security-constraint&gt;
  187. * </pre>
  188. *
  189. * @param dep webservice deployment
  190. * @param jbossWebMD jboss web meta data
  191. */
  192. private void createSecurityConstraints(final Deployment dep, final JBossWebMetaData jbossWebMD) {
  193. ROOT_LOGGER.creatingSecurityConstraints();
  194. final SecurityMetaDataAccessorEJB ejbMDAccessor = getEjbSecurityMetaDataAccessor(dep);
  195. for (final Endpoint ejbEndpoint : dep.getService().getEndpoints()) {
  196. final boolean secureWsdlAccess = ejbMDAccessor.isSecureWsdlAccess(ejbEndpoint);
  197. final String transportGuarantee = ejbMDAccessor.getTransportGuarantee(ejbEndpoint);
  198. final boolean hasTransportGuarantee = transportGuarantee != null;
  199. final String authMethod = ejbMDAccessor.getAuthMethod(ejbEndpoint);
  200. final boolean hasAuthMethod = authMethod != null;
  201. if (ejbEndpoint instanceof HttpEndpoint && (hasAuthMethod || hasTransportGuarantee)) {
  202. final List<SecurityConstraintMetaData> securityConstraints = WebMetaDataHelper
  203. .getSecurityConstraints(jbossWebMD);
  204. // security-constraint
  205. final SecurityConstraintMetaData securityConstraint = WebMetaDataHelper
  206. .newSecurityConstraint(securityConstraints);
  207. // web-resource-collection
  208. final WebResourceCollectionsMetaData webResourceCollections = WebMetaDataHelper
  209. .getWebResourceCollections(securityConstraint);
  210. final String endpointName = ejbEndpoint.getShortName();
  211. final String urlPattern = ((HttpEndpoint) ejbEndpoint).getURLPattern();
  212. ROOT_LOGGER.creatingWebResourceCollection(endpointName, urlPattern);
  213. WebMetaDataHelper.newWebResourceCollection(endpointName, urlPattern, secureWsdlAccess,
  214. webResourceCollections);
  215. // auth-constraint
  216. if (hasAuthMethod) {
  217. ROOT_LOGGER.creatingAuthConstraint(endpointName);
  218. WebMetaDataHelper.newAuthConstraint(WebMetaDataHelper.getAllRoles(), securityConstraint);
  219. }
  220. // user-data-constraint
  221. if (hasTransportGuarantee) {
  222. ROOT_LOGGER.creatingUserDataConstraint(endpointName, transportGuarantee);
  223. WebMetaDataHelper.newUserDataConstraint(transportGuarantee, securityConstraint);
  224. }
  225. }
  226. }
  227. }
  228. /**
  229. * Creates login-config part of web.xml descriptor.
  230. * <p/>
  231. * <pre>
  232. * &lt;login-config&gt;
  233. * &lt;auth-method&gt;EjbDeploymentAuthMethod&lt;/auth-method&gt;
  234. * &lt;realm-name&gt;EJBWebServiceEndpointServlet Realm&lt;/realm-name&gt;
  235. * &lt;/login-config&gt;
  236. * </pre>
  237. *
  238. * @param dep webservice deployment
  239. * @param jbossWebMD jboss web meta data
  240. */
  241. private void createLoginConfig(final Deployment dep, final JBossWebMetaData jbossWebMD) {
  242. final String authMethod = getAuthMethod(dep);
  243. final boolean hasAuthMethod = authMethod != null;
  244. if (hasAuthMethod) {
  245. ROOT_LOGGER.creatingLoginConfig(EJB_WEBSERVICE_REALM, authMethod);
  246. final LoginConfigMetaData loginConfig = WebMetaDataHelper.getLoginConfig(jbossWebMD);
  247. loginConfig.setRealmName(WebMetaDataCreator.EJB_WEBSERVICE_REALM);
  248. loginConfig.setAuthMethod(authMethod);
  249. }
  250. }
  251. /**
  252. * Creates security roles part of web.xml descriptor.
  253. * <p/>
  254. * <pre>
  255. * &lt;security-role&gt;
  256. * &lt;role-name&gt;role1&lt;/role-name&gt;
  257. * ...
  258. * &lt;role-name&gt;roleN&lt;/role-name&gt;
  259. * &lt;/security-role&gt;
  260. * </pre>
  261. *
  262. * @param dep webservice deployment
  263. * @param jbossWebMD jboss web meta data
  264. */
  265. private void createSecurityRoles(final Deployment dep, final JBossWebMetaData jbossWebMD) {
  266. final String authMethod = getAuthMethod(dep);
  267. final boolean hasAuthMethod = authMethod != null;
  268. if (hasAuthMethod) {
  269. final SecurityMetaDataAccessorEJB ejbMDAccessor = getEjbSecurityMetaDataAccessor(dep);
  270. final SecurityRolesMetaData securityRolesMD = ejbMDAccessor.getSecurityRoles(dep);
  271. final boolean hasSecurityRolesMD = securityRolesMD != null;
  272. if (hasSecurityRolesMD) {
  273. ROOT_LOGGER.creatingSecurityRoles();
  274. jbossWebMD.setSecurityRoles(securityRolesMD);
  275. }
  276. }
  277. //merge security roles from the ear
  278. //TODO: is there somewhere better to put this?
  279. final DeploymentUnit unit = dep.getAttachment(DeploymentUnit.class);
  280. DeploymentUnit parent = unit.getParent();
  281. if (parent != null) {
  282. final EarMetaData earMetaData = parent.getAttachment(org.jboss.as.ee.structure.Attachments.EAR_METADATA);
  283. if (earMetaData != null) {
  284. if (jbossWebMD.getSecurityRoles() == null) {
  285. jbossWebMD.setSecurityRoles(new SecurityRolesMetaData());
  286. }
  287. SecurityRolesMetaData earSecurityRolesMetaData = earMetaData.getSecurityRoles();
  288. if (earSecurityRolesMetaData != null) {
  289. SecurityRolesMetaDataMerger.merge(jbossWebMD.getSecurityRoles(), jbossWebMD.getSecurityRoles(), earSecurityRolesMetaData);
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * Returns deployment authentication method.
  296. *
  297. * @param dep webservice deployment
  298. * @return deployment authentication method
  299. */
  300. private String getAuthMethod(final Deployment dep) {
  301. final SecurityMetaDataAccessorEJB ejbMDAccessor = getEjbSecurityMetaDataAccessor(dep);
  302. for (final Endpoint ejbEndpoint : dep.getService().getEndpoints()) {
  303. final String beanAuthMethod = ejbMDAccessor.getAuthMethod(ejbEndpoint);
  304. final boolean hasBeanAuthMethod = beanAuthMethod != null;
  305. if (hasBeanAuthMethod) {
  306. // First found auth-method defines war
  307. // login-config/auth-method
  308. return beanAuthMethod;
  309. }
  310. }
  311. return null;
  312. }
  313. /**
  314. * Returns security builder associated with EJB deployment.
  315. *
  316. * @param dep webservice EJB deployment
  317. * @return security builder for EJB deployment
  318. */
  319. private SecurityMetaDataAccessorEJB getEjbSecurityMetaDataAccessor(final Deployment dep) {
  320. final boolean isJaxws = WSHelper.isJaxwsDeployment(dep);
  321. return isJaxws ? ejb3SecurityAccessor : ejb21SecurityAccessor;
  322. }
  323. }