Search in sources :

Example 51 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TomcatBaseTest method getTomcatInstanceTestWebapp.

/**
     * Make the Tomcat instance preconfigured with test/webapp available to
     * sub-classes.
     * @param addJstl Should JSTL support be added to the test webapp
     * @param start   Should the Tomcat instance be started
     *
     * @return A Tomcat instance pre-configured with the web application located
     *         at test/webapp
     *
     * @throws LifecycleException If a problem occurs while starting the
     *                            instance
     */
public Tomcat getTomcatInstanceTestWebapp(boolean addJstl, boolean start) throws LifecycleException {
    File appDir = new File("test/webapp");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    StandardJarScanner scanner = (StandardJarScanner) ctx.getJarScanner();
    StandardJarScanFilter filter = (StandardJarScanFilter) scanner.getJarScanFilter();
    filter.setTldSkip(filter.getTldSkip() + ",testclasses");
    filter.setPluggabilitySkip(filter.getPluggabilitySkip() + ",testclasses");
    if (addJstl) {
        File lib = new File("webapps/examples/WEB-INF/lib");
        ctx.setResources(new StandardRoot(ctx));
        ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", lib.getAbsolutePath(), null, "/");
    }
    if (start) {
        tomcat.start();
    }
    return tomcat;
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) File(java.io.File) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter)

Example 52 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestHostConfigAutomaticDeployment method testSetContextClassName.

@Test
public void testSetContextClassName() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        StandardHost standardHost = (StandardHost) host;
        standardHost.setContextClass(TesterContext.class.getName());
    }
    // Copy the WAR file
    File war = new File(host.getAppBaseFile(), APP_NAME.getBaseName() + ".war");
    Files.copy(WAR_XML_SOURCE.toPath(), war.toPath());
    // Deploy the copied war
    tomcat.start();
    host.backgroundProcess();
    // Check the Context class
    Context ctxt = (Context) host.findChild(APP_NAME.getName());
    Assert.assertTrue(ctxt instanceof TesterContext);
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) File(java.io.File) Test(org.junit.Test)

Example 53 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestDefaultServlet method testGetWithSubpathmount.

/*
     * Test https://bz.apache.org/bugzilla/show_bug.cgi?id=50026
     * Verify serving of resources from context root with subpath mapping.
     */
@Test
public void testGetWithSubpathmount() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    String contextPath = "/examples";
    File appDir = new File(getBuildDirectory(), "webapps" + contextPath);
    // app dir is relative to server home
    org.apache.catalina.Context ctx = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
    ctx.addApplicationListener(WsContextListener.class.getName());
    // Override the default servlet with our own mappings
    Tomcat.addServlet(ctx, "default2", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default2");
    ctx.addServletMappingDecoded("/servlets/*", "default2");
    ctx.addServletMappingDecoded("/static/*", "default2");
    tomcat.start();
    final ByteChunk res = new ByteChunk();
    // Make sure DefaultServlet isn't exposing special directories
    // by remounting the webapp under a sub-path
    int rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/web.xml", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/doesntexistanywhere", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/META-INF/MANIFEST.MF", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/META-INF/doesntexistanywhere", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    // Make sure DefaultServlet is serving resources relative to the
    // context root regardless of where the it is mapped
    final ByteChunk rootResource = new ByteChunk();
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/index.html", rootResource, null);
    assertEquals(HttpServletResponse.SC_OK, rc);
    final ByteChunk subpathResource = new ByteChunk();
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/servlets/index.html", subpathResource, null);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertFalse(rootResource.toString().equals(subpathResource.toString()));
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/index.html", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Context(org.apache.catalina.Context) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 54 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class Benchmarks method doTestManagerBaseCreateSession.

private void doTestManagerBaseCreateSession(int threadCount, int iterCount) throws LifecycleException {
    // Create a default session manager
    StandardManager mgr = new StandardManager();
    mgr.setPathname(null);
    Host host = new StandardHost();
    host.setName("unittest");
    Context context = new StandardContext();
    context.setPath("");
    context.setParent(host);
    mgr.setContext(context);
    mgr.start();
    mgr.generateSessionId();
    while (mgr.sessionCreationTiming.size() < ManagerBase.TIMING_STATS_CACHE_SIZE) {
        mgr.sessionCreationTiming.add(null);
    }
    while (mgr.sessionExpirationTiming.size() < ManagerBase.TIMING_STATS_CACHE_SIZE) {
        mgr.sessionExpirationTiming.add(null);
    }
    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; i++) {
        threads[i] = new Thread(new TestThreadCreateSession(mgr, iterCount));
    }
    long start = System.currentTimeMillis();
    for (int i = 0; i < threadCount; i++) {
        threads[i].start();
    }
    for (int i = 0; i < threadCount; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }
    long end = System.currentTimeMillis();
    StringBuilder result = new StringBuilder();
    result.append("Threads: ");
    result.append(threadCount);
    result.append(", Time(ms): ");
    result.append(end - start);
    System.out.println(result.toString());
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host)

Example 55 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestContextConfig method testBug54448and54450.

@Test
public void testBug54448and54450() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-fragments");
    Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    Tomcat.addServlet(context, "TestServlet", "org.apache.catalina.startup.TesterServletWithAnnotations");
    context.addServletMappingDecoded("/testServlet", "TestServlet");
    tomcat.enableNaming();
    tomcat.start();
    assertPageContains("/test/testServlet", "envEntry1: 1 envEntry2: 2 envEntry3: 33 envEntry4: 4 envEntry5: 55 envEntry6: 66");
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File) Test(org.junit.Test)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24