Search in sources :

Example 16 with StandardContext

use of org.apache.catalina.core.StandardContext in project sonarqube by SonarSource.

the class TomcatContexts method addContext.

private static StandardContext addContext(Tomcat tomcat, String contextPath, File dir) {
    try {
        StandardContext context = (StandardContext) tomcat.addWebapp(contextPath, dir.getAbsolutePath());
        context.setClearReferencesHttpClientKeepAliveThread(false);
        context.setClearReferencesStopThreads(false);
        context.setClearReferencesStopTimerThreads(false);
        context.setClearReferencesStopTimerThreads(false);
        context.setAntiResourceLocking(false);
        context.setReloadable(false);
        context.setUseHttpOnly(true);
        context.setTldValidation(false);
        context.setXmlValidation(false);
        context.setXmlNamespaceAware(false);
        context.setUseNaming(false);
        context.setDelegate(true);
        context.setJarScanner(new NullJarScanner());
        context.setAllowCasualMultipartParsing(true);
        context.setCookies(false);
        return context;
    } catch (ServletException e) {
        throw new IllegalStateException("Fail to configure webapp from " + dir, e);
    }
}
Also used : ServletException(javax.servlet.ServletException) StandardContext(org.apache.catalina.core.StandardContext)

Example 17 with StandardContext

use of org.apache.catalina.core.StandardContext in project sonarqube by SonarSource.

the class TomcatContextsTest method configure_root_webapp.

@Test
public void configure_root_webapp() throws Exception {
    props.setProperty("foo", "bar");
    StandardContext context = mock(StandardContext.class);
    when(tomcat.addWebapp(anyString(), anyString())).thenReturn(context);
    underTest.configure(tomcat, new Props(props));
    // configure webapp with properties
    verify(context).addParameter("foo", "bar");
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) Props(org.sonar.process.Props) Test(org.junit.Test)

Example 18 with StandardContext

use of org.apache.catalina.core.StandardContext in project jaggery by wso2.

the class TomcatJaggeryWebappsDeployer method handleWebappDeployment.

/**
     * Deployment procedure of Jaggery apps
     *
     * @param webappFile                The Jaggery app file to be deployed
     * @param contextStr                jaggery app context string
     * @param webContextParams          context-params for this Jaggery app
     * @param applicationEventListeners Application event listeners
     * @throws CarbonException If a deployment error occurs
     */
protected void handleWebappDeployment(File webappFile, String contextStr, List<WebContextParameter> webContextParams, List<Object> applicationEventListeners) throws CarbonException {
    String filename = webappFile.getName();
    ArrayList<Object> listeners = new ArrayList<Object>(1);
    // listeners.add(new CarbonServletRequestListener());
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.setAuthConstraint(true);
    SecurityCollection securityCollection = new SecurityCollection();
    securityCollection.setName("ConfigDir");
    securityCollection.setDescription("Jaggery Configuration Dir");
    securityCollection.addPattern("/" + JaggeryCoreConstants.JAGGERY_CONF_FILE);
    securityConstraint.addCollection(securityCollection);
    WebApplicationsHolder webApplicationsHolder = WebAppUtils.getWebappHolder(webappFile.getAbsolutePath(), configurationContext);
    try {
        JSONObject jaggeryConfigObj = readJaggeryConfig(webappFile);
        Tomcat tomcat = DataHolder.getCarbonTomcatService().getTomcat();
        Context context = DataHolder.getCarbonTomcatService().addWebApp(contextStr, webappFile.getAbsolutePath(), new JaggeryDeployerManager.JaggeryConfListener(jaggeryConfigObj, securityConstraint));
        //deploying web app for url mapping inside virtual host
        if (DataHolder.getHotUpdateService() != null) {
            List<String> hostNames = DataHolder.getHotUpdateService().getMappigsPerWebapp(contextStr);
            for (String hostName : hostNames) {
                Host host = DataHolder.getHotUpdateService().addHost(hostName);
                /*                    ApplicationContext.getCurrentApplicationContext().putUrlMappingForApplication(hostName, contextStr);
  */
                Context contextForHost = DataHolder.getCarbonTomcatService().addWebApp(host, "/", webappFile.getAbsolutePath(), new JaggeryDeployerManager.JaggeryConfListener(jaggeryConfigObj, securityConstraint));
                log.info("Deployed JaggeryApp on host: " + contextForHost);
            }
        }
        Manager manager = context.getManager();
        if (isDistributable(context, jaggeryConfigObj)) {
            //Clusterable manager implementation as DeltaManager
            context.setDistributable(true);
            // Using clusterable manager
            CarbonTomcatClusterableSessionManager sessionManager;
            if (manager instanceof CarbonTomcatClusterableSessionManager) {
                sessionManager = (CarbonTomcatClusterableSessionManager) manager;
                sessionManager.setOwnerTenantId(tenantId);
            } else {
                sessionManager = new CarbonTomcatClusterableSessionManager(tenantId);
                context.setManager(sessionManager);
            }
            Object alreadyinsertedSMMap = configurationContext.getProperty(CarbonConstants.TOMCAT_SESSION_MANAGER_MAP);
            if (alreadyinsertedSMMap != null) {
                ((Map<String, CarbonTomcatClusterableSessionManager>) alreadyinsertedSMMap).put(context.getName(), sessionManager);
            } else {
                sessionManagerMap.put(context.getName(), sessionManager);
                configurationContext.setProperty(CarbonConstants.TOMCAT_SESSION_MANAGER_MAP, sessionManagerMap);
            }
        } else {
            if (manager instanceof CarbonTomcatSessionManager) {
                ((CarbonTomcatSessionManager) manager).setOwnerTenantId(tenantId);
            } else if (manager instanceof CarbonTomcatSessionPersistentManager) {
                ((CarbonTomcatSessionPersistentManager) manager).setOwnerTenantId(tenantId);
                log.debug(manager.getInfo() + " enabled Tomcat HTTP Session Persistent mode using " + ((CarbonTomcatSessionPersistentManager) manager).getStore().getInfo());
            } else {
                context.setManager(new CarbonTomcatSessionManager(tenantId));
            }
        }
        context.setReloadable(false);
        JaggeryApplication webapp = new JaggeryApplication(this, context, webappFile);
        webapp.setServletContextParameters(webContextParams);
        webapp.setState("Started");
        webApplicationsHolder.getStartedWebapps().put(filename, webapp);
        webApplicationsHolder.getFaultyWebapps().remove(filename);
        registerApplicationEventListeners(applicationEventListeners, context);
        log.info("Deployed webapp: " + webapp);
    } catch (Throwable e) {
        //catching a Throwable here to avoid web-apps crashing the server during startup
        StandardContext context = new StandardContext();
        context.setName(webappFile.getName());
        context.addParameter(WebappsConstants.FAULTY_WEBAPP, "true");
        JaggeryApplication webapp = new JaggeryApplication(this, context, webappFile);
        webapp.setProperty(WebappsConstants.WEBAPP_FILTER, JaggeryConstants.JAGGERY_WEBAPP_FILTER_PROP);
        String msg = "Error while deploying webapp: " + webapp;
        log.error(msg, e);
        webapp.setFaultReason(new Exception(msg, e));
        webApplicationsHolder.getFaultyWebapps().put(filename, webapp);
        webApplicationsHolder.getStartedWebapps().remove(filename);
        throw new CarbonException(msg, e);
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) Tomcat(org.apache.catalina.startup.Tomcat) JaggeryDeployerManager(org.jaggeryjs.jaggery.core.manager.JaggeryDeployerManager) ArrayList(java.util.ArrayList) CarbonException(org.wso2.carbon.CarbonException) Host(org.apache.catalina.Host) CarbonTomcatClusterableSessionManager(org.wso2.carbon.core.session.CarbonTomcatClusterableSessionManager) Manager(org.apache.catalina.Manager) JaggeryDeployerManager(org.jaggeryjs.jaggery.core.manager.JaggeryDeployerManager) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) CarbonException(org.wso2.carbon.CarbonException) JSONObject(org.json.simple.JSONObject) CarbonTomcatClusterableSessionManager(org.wso2.carbon.core.session.CarbonTomcatClusterableSessionManager) StandardContext(org.apache.catalina.core.StandardContext) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 19 with StandardContext

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

the class TomEEMyFacesContainerInitializer method isFacesServletPresent.

private boolean isFacesServletPresent(final ServletContext ctx) {
    if (ctx instanceof ApplicationContextFacade) {
        try {
            final ApplicationContext appCtx = (ApplicationContext) get(ApplicationContextFacade.class, ctx);
            final Context tomcatCtx = (Context) get(ApplicationContext.class, appCtx);
            if (tomcatCtx instanceof StandardContext) {
                final Container[] servlets = tomcatCtx.findChildren();
                if (servlets != null) {
                    for (final Container s : servlets) {
                        if (s instanceof Wrapper) {
                            if ("javax.faces.webapp.FacesServlet".equals(((Wrapper) s).getServletClass()) || "Faces Servlet".equals(s.getName())) {
                                return true;
                            }
                        }
                    }
                }
            }
        } catch (final Exception e) {
        // no-op
        }
    }
    return false;
}
Also used : ApplicationContext(org.apache.catalina.core.ApplicationContext) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) StandardContext(org.apache.catalina.core.StandardContext) Wrapper(org.apache.catalina.Wrapper) ApplicationContext(org.apache.catalina.core.ApplicationContext) Container(org.apache.catalina.Container) ApplicationContextFacade(org.apache.catalina.core.ApplicationContextFacade) StandardContext(org.apache.catalina.core.StandardContext) ServletException(javax.servlet.ServletException)

Example 20 with StandardContext

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

the class TomcatHessianRegistry method deploy.

@Override
public String deploy(final ClassLoader loader, final HessianServer listener, final String hostname, final String app, final String authMethod, final String transportGuarantee, final String realmName, final String name) throws URISyntaxException {
    Container host = engine.findChild(hostname);
    if (host == null) {
        host = engine.findChild(engine.getDefaultHost());
        if (host == null) {
            throw new IllegalArgumentException("Invalid virtual host '" + engine.getDefaultHost() + "'.  Do you have a matchiing Host entry in the server.xml?");
        }
    }
    final String contextRoot = contextName(app);
    Context context = Context.class.cast(host.findChild(contextRoot));
    if (context == null) {
        Pair<Context, Integer> fakeContext = fakeContexts.get(contextRoot);
        if (fakeContext != null) {
            context = fakeContext.getLeft();
            fakeContext.setValue(fakeContext.getValue() + 1);
        } else {
            context = Context.class.cast(host.findChild(contextRoot));
            if (context == null) {
                fakeContext = fakeContexts.get(contextRoot);
                if (fakeContext == null) {
                    context = createNewContext(loader, authMethod, transportGuarantee, realmName, app);
                    fakeContext = new MutablePair<>(context, 1);
                    fakeContexts.put(contextRoot, fakeContext);
                } else {
                    context = fakeContext.getLeft();
                    fakeContext.setValue(fakeContext.getValue() + 1);
                }
            }
        }
    }
    final String servletMapping = generateServletPath(name);
    Wrapper wrapper = Wrapper.class.cast(context.findChild(servletMapping));
    if (wrapper != null) {
        throw new IllegalArgumentException("Servlet " + servletMapping + " in web application context " + context.getName() + " already exists");
    }
    wrapper = context.createWrapper();
    wrapper.setName(HESSIAN.replace("/", "") + "_" + name);
    wrapper.setServlet(new OpenEJBHessianServlet(listener));
    context.addChild(wrapper);
    context.addServletMappingDecoded(servletMapping, wrapper.getName());
    if ("BASIC".equals(authMethod) && StandardContext.class.isInstance(context)) {
        final StandardContext standardContext = StandardContext.class.cast(context);
        boolean found = false;
        for (final Valve v : standardContext.getPipeline().getValves()) {
            if (LimitedBasicValve.class.isInstance(v) || BasicAuthenticator.class.isInstance(v)) {
                found = true;
                break;
            }
        }
        if (!found) {
            standardContext.addValve(new LimitedBasicValve());
        }
    }
    final List<String> addresses = new ArrayList<>();
    for (final Connector connector : connectors) {
        for (final String mapping : wrapper.findMappings()) {
            final URI address = new URI(connector.getScheme(), null, host.getName(), connector.getPort(), contextRoot + mapping, null, null);
            addresses.add(address.toString());
        }
    }
    return HttpUtil.selectSingleAddress(addresses);
}
Also used : Context(org.apache.catalina.Context) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) Wrapper(org.apache.catalina.Wrapper) Connector(org.apache.catalina.connector.Connector) ArrayList(java.util.ArrayList) URI(java.net.URI) Container(org.apache.catalina.Container) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) OpenEJBValve(org.apache.tomee.catalina.OpenEJBValve) Valve(org.apache.catalina.Valve)

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