Search in sources :

Example 1 with TomcatSecurityConstaintsToJaccPermissionsTransformer

use of org.apache.tomee.catalina.security.TomcatSecurityConstaintsToJaccPermissionsTransformer in project tomee by apache.

the class TomcatWebAppBuilder method deployWebApps.

// 
// OpenEJB WebAppBuilder
// 
/**
 * {@inheritDoc}
 */
@Override
public void deployWebApps(final AppInfo appInfo, final ClassLoader classLoader) throws Exception {
    try {
        for (final WebAppInfo webApp : appInfo.webApps) {
            // look for context.xml
            final File war = new File(webApp.path);
            URL contextXmlUrl = null;
            if (war.isDirectory()) {
                final File cXml = new File(war, Constants.ApplicationContextXml).getAbsoluteFile();
                if (cXml.exists()) {
                    contextXmlUrl = cXml.toURI().toURL();
                    LOGGER.info("using context file " + cXml.getAbsolutePath());
                }
            } else {
                // war
                try (final JarFile warAsJar = new JarFile(war)) {
                    final JarEntry entry = warAsJar.getJarEntry(Constants.ApplicationContextXml);
                    if (entry != null) {
                        contextXmlUrl = new URL("jar:" + war.getAbsoluteFile().toURI().toURL().toExternalForm() + "!/" + Constants.ApplicationContextXml);
                    }
                }
            }
            if (isAlreadyDeployed(appInfo, webApp)) {
                continue;
            }
            StandardContext standardContext;
            {
                final ClassLoader containerLoader = Helper.get();
                final Host host = hosts.getDefault();
                if (StandardHost.class.isInstance(host) && !StandardContext.class.getName().equals(StandardHost.class.cast(host).getContextClass())) {
                    try {
                        standardContext = StandardContext.class.cast(containerLoader.loadClass(StandardHost.class.cast(host).getContextClass()).newInstance());
                    } catch (final Throwable th) {
                        LOGGER.warning("Can't use context class specified, using default StandardContext", th);
                        standardContext = new StandardContext();
                    }
                } else {
                    standardContext = new StandardContext();
                }
                // should be optional but in maven parent is app loader and not maven loader which is the real parent
                final ClassLoader currentParent = standardContext.getParentClassLoader();
                if (currentParent == null || isParent(currentParent, containerLoader)) {
                    standardContext.setParentClassLoader(containerLoader);
                }
            }
            standardContext.setUnpackWAR(!"false".equalsIgnoreCase(appInfo.properties.getProperty("tomcat.unpackWar")));
            if (contextXmlUrl != null) {
                standardContext.setConfigFile(contextXmlUrl);
            }
            // default context path must start with / but not end with slash
            try {
                if (webApp.defaultContextPath != null && webApp.defaultContextPath.matches("^/\\w*[^/]$")) {
                    standardContext.setPath(webApp.defaultContextPath);
                }
            } catch (final Exception e) {
                // don't fail because it's a hack, just output the exception
                e.printStackTrace();
            }
            if (standardContext.getPath() != null) {
                webApp.contextRoot = standardContext.getPath();
            }
            if (webApp.contextRoot.startsWith("/") || webApp.contextRoot.startsWith(File.separator)) {
                webApp.contextRoot = webApp.contextRoot.substring(1);
            }
            if (webApp.contextRoot.startsWith(File.separator)) {
                webApp.contextRoot = webApp.contextRoot.replaceFirst(File.separator, "");
            }
            // /!\ take care, StandardContext default host = "_" and not null or localhost
            final String hostname = Contexts.getHostname(standardContext);
            if (hostname != null && !"_".equals(hostname)) {
                webApp.host = hostname;
            }
            final ApplicationParameter appParam = new ApplicationParameter();
            appParam.setName(OPENEJB_WEBAPP_MODULE_ID);
            appParam.setValue(webApp.moduleId);
            standardContext.addApplicationParameter(appParam);
            if (!isAlreadyDeployed(appInfo, webApp)) {
                if (standardContext.getPath() == null) {
                    if (webApp.contextRoot != null && webApp.contextRoot.startsWith("/")) {
                        standardContext.setPath(webApp.contextRoot);
                    } else if (isRoot(webApp.contextRoot)) {
                        standardContext.setPath("");
                    } else {
                        standardContext.setPath("/" + webApp.contextRoot);
                    }
                }
                if (standardContext.getDocBase() == null) {
                    standardContext.setDocBase(webApp.path);
                }
                String docBase = standardContext.getDocBase();
                File docBaseFile = new File(docBase);
                if (docBase != null && docBaseFile.isFile() && docBase.endsWith(".war")) {
                    DeploymentLoader.unpack(docBaseFile);
                    if (standardContext.getPath().endsWith(".war")) {
                        standardContext.setPath(removeFirstSlashAndWar("/" + standardContext.getPath()));
                        standardContext.setName(standardContext.getPath());
                        webApp.contextRoot = standardContext.getPath();
                    }
                    standardContext.setDocBase(docBase.substring(0, docBase.length() - 4));
                }
                if (isRoot(standardContext.getName())) {
                    standardContext.setName("");
                    webApp.contextRoot = "";
                }
                if (isAlreadyDeployed(appInfo, webApp)) {
                    // possible because of the previous renaming
                    continue;
                }
                // but here we have all the classloading logic
                if (classLoader != null) {
                    standardContext.setParentClassLoader(classLoader);
                    standardContext.setDelegate(true);
                }
                String host = webApp.host;
                if (host == null) {
                    host = hosts.getDefaultHost();
                    LOGGER.info("using default host: " + host);
                }
                if (classLoader != null) {
                    appInfo.autoDeploy = false;
                    deployWar(standardContext, host, appInfo);
                } else {
                    // force a normal deployment with lazy building of AppInfo
                    deployWar(standardContext, host, null);
                }
                // TODO should we copy the information in the appInfo using the jee object tree or add more to the info tree
                // this might then move to the assembler after webapp is deployed so we can read information from info tree
                // and build up all policy context from there instead of from Tomcat internal objects
                final TomcatSecurityConstaintsToJaccPermissionsTransformer transformer = new TomcatSecurityConstaintsToJaccPermissionsTransformer(standardContext);
                final PolicyContext policyContext = transformer.createResourceAndDataPermissions();
                final JaccPermissionsBuilder jaccPermissionsBuilder = new JaccPermissionsBuilder();
                jaccPermissionsBuilder.install(policyContext);
            }
        }
    } finally {
        // cleanup temp var passing
        for (final WebAppInfo webApp : appInfo.webApps) {
            appInfo.properties.remove(webApp);
        }
    }
}
Also used : ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter) Host(org.apache.catalina.Host) StandardHost(org.apache.catalina.core.StandardHost) JaccPermissionsBuilder(org.apache.openejb.assembler.classic.JaccPermissionsBuilder) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) LifecycleException(org.apache.catalina.LifecycleException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) PolicyContext(org.apache.openejb.assembler.classic.PolicyContext) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File) JarFile(java.util.jar.JarFile) TomcatSecurityConstaintsToJaccPermissionsTransformer(org.apache.tomee.catalina.security.TomcatSecurityConstaintsToJaccPermissionsTransformer)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 Host (org.apache.catalina.Host)1 LifecycleException (org.apache.catalina.LifecycleException)1 StandardContext (org.apache.catalina.core.StandardContext)1 StandardHost (org.apache.catalina.core.StandardHost)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 JaccPermissionsBuilder (org.apache.openejb.assembler.classic.JaccPermissionsBuilder)1 PolicyContext (org.apache.openejb.assembler.classic.PolicyContext)1 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)1 ApplicationParameter (org.apache.tomcat.util.descriptor.web.ApplicationParameter)1 TomcatSecurityConstaintsToJaccPermissionsTransformer (org.apache.tomee.catalina.security.TomcatSecurityConstaintsToJaccPermissionsTransformer)1