Search in sources :

Example 41 with StandardContext

use of org.apache.catalina.core.StandardContext 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);
            }
            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);
                }
            }
        }
    } finally {
        // cleanup temp var passing
        for (final WebAppInfo webApp : appInfo.webApps) {
            appInfo.properties.remove(webApp);
        }
    }
}
Also used : WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) Host(org.apache.catalina.Host) StandardHost(org.apache.catalina.core.StandardHost) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) File(java.io.File) JarFile(java.util.jar.JarFile) URL(java.net.URL)

Example 42 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomee by apache.

the class TomcatWebAppBuilder method undeployWebApps.

/**
 * {@inheritDoc}
 */
@Override
public void undeployWebApps(final AppInfo appInfo) throws Exception {
    final String version = appVersion(appInfo);
    for (final WebAppInfo webApp : appInfo.webApps) {
        final ContextInfo contextInfo = getContextInfo(webApp.host, webApp.contextRoot, version);
        if (contextInfo != null) {
            final StandardContext standardContext = contextInfo.standardContext;
            if (!appInfo.webAppAlone || !appInfo.properties.containsKey("tomee.destroying")) {
                undeploy(standardContext, contextInfo);
                final File extracted = Contexts.warPath(standardContext);
                if (isExtracted(extracted)) {
                    deleteDir(extracted);
                }
                removeContextInfo(standardContext);
            }
        }
    }
}
Also used : WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File) JarFile(java.util.jar.JarFile)

Example 43 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomee by apache.

the class OpenEJBListener method findOpenEjbWar.

private static File findOpenEjbWar(final StandardHost standardHost) {
    // look for openejb war in a Tomcat context
    for (final Container container : standardHost.findChildren()) {
        if (container instanceof StandardContext) {
            final StandardContext standardContext = (StandardContext) container;
            File contextDocBase = new File(standardContext.getDocBase());
            if (!contextDocBase.isDirectory() && standardContext.getOriginalDocBase() != null) {
                contextDocBase = new File(standardContext.getOriginalDocBase());
            }
            if (contextDocBase.isDirectory()) {
                final File openEjbWar = findOpenEjbWarInContext(contextDocBase);
                if (openEjbWar != null) {
                    return openEjbWar;
                }
            }
        }
    }
    return null;
}
Also used : Container(org.apache.catalina.Container) StandardContext(org.apache.catalina.core.StandardContext) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 44 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomee by apache.

the class TomcatHessianRegistry method createNewContext.

private static Context createNewContext(final ClassLoader classLoader, final String rAuthMethod, final String rTransportGuarantee, final String realmName, final String name) {
    String path = name;
    if (path == null) {
        path = "/";
    }
    if (!path.startsWith("/")) {
        path = "/" + path;
    }
    final StandardContext context = new IgnoredStandardContext();
    context.setPath(path);
    context.setDocBase("");
    context.setParentClassLoader(classLoader);
    context.setDelegate(true);
    context.setName(name);
    TomcatWebAppBuilder.class.cast(SystemInstance.get().getComponent(WebAppBuilder.class)).initJ2EEInfo(context);
    // Configure security
    String authMethod = rAuthMethod;
    if (authMethod != null) {
        authMethod = authMethod.toUpperCase();
    }
    String transportGuarantee = rTransportGuarantee;
    if (transportGuarantee != null) {
        transportGuarantee = transportGuarantee.toUpperCase();
    }
    if (authMethod != null & !"NONE".equals(authMethod)) {
        if ("BASIC".equals(authMethod) || "DIGEST".equals(authMethod) || "CLIENT-CERT".equals(authMethod)) {
            // Setup a login configuration
            final LoginConfig loginConfig = new LoginConfig();
            loginConfig.setAuthMethod(authMethod);
            loginConfig.setRealmName(realmName);
            context.setLoginConfig(loginConfig);
            // Setup a default Security Constraint
            final String securityRole = SystemInstance.get().getProperty(TOMEE_HESSIAN_SECURITY_ROLE_PREFIX + name, "default");
            for (final String role : securityRole.split(",")) {
                final SecurityCollection collection = new SecurityCollection();
                collection.addMethod("GET");
                collection.addMethod("POST");
                collection.addPattern("/*");
                collection.setName(role);
                final SecurityConstraint sc = new SecurityConstraint();
                sc.addAuthRole("*");
                sc.addCollection(collection);
                sc.setAuthConstraint(true);
                sc.setUserConstraint(transportGuarantee);
                context.addConstraint(sc);
                context.addSecurityRole(role);
            }
        }
        // Set the proper authenticator
        switch(authMethod) {
            case "BASIC":
                context.addValve(new BasicAuthenticator());
                break;
            case "DIGEST":
                context.addValve(new DigestAuthenticator());
                break;
            case "CLIENT-CERT":
                context.addValve(new SSLAuthenticator());
                break;
            case "NONE":
                context.addValve(new NonLoginAuthenticator());
                break;
        }
        context.getPipeline().addValve(new OpenEJBValve());
    } else {
        throw new IllegalArgumentException("Invalid authMethod: " + authMethod);
    }
    return context;
}
Also used : TomcatWebAppBuilder(org.apache.tomee.catalina.TomcatWebAppBuilder) NonLoginAuthenticator(org.apache.catalina.authenticator.NonLoginAuthenticator) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) SSLAuthenticator(org.apache.catalina.authenticator.SSLAuthenticator) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) OpenEJBValve(org.apache.tomee.catalina.OpenEJBValve) DigestAuthenticator(org.apache.catalina.authenticator.DigestAuthenticator) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 45 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomee by apache.

the class GlobalListenerSupport method hostAdded.

/**
 * Host is added.
 *
 * @param host tomcat host.
 */
private void hostAdded(final StandardHost host) {
    addContextListener(host);
    host.addLifecycleListener(this);
    for (final Container child : host.findChildren()) {
        if (child instanceof StandardContext) {
            final StandardContext context = (StandardContext) child;
            contextAdded(context);
        }
    }
}
Also used : Container(org.apache.catalina.Container) StandardContext(org.apache.catalina.core.StandardContext)

Aggregations

StandardContext (org.apache.catalina.core.StandardContext)91 File (java.io.File)36 Tomcat (org.apache.catalina.startup.Tomcat)24 Context (org.apache.catalina.Context)19 Test (org.junit.Test)19 StandardHost (org.apache.catalina.core.StandardHost)16 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)12 Container (org.apache.catalina.Container)11 Host (org.apache.catalina.Host)11 JarFile (java.util.jar.JarFile)10 IOException (java.io.IOException)9 URL (java.net.URL)9 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)9 HashMap (java.util.HashMap)8 StandardRoot (org.apache.catalina.webresources.StandardRoot)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)6 MalformedURLException (java.net.MalformedURLException)5 ServletContext (javax.servlet.ServletContext)5