Search in sources :

Example 76 with Wrapper

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

the class TestAsyncContextImpl method testAsyncContextListenerClearing.

// https://bz.apache.org/bugzilla/show_bug.cgi?id=57326
@Test
public void testAsyncContextListenerClearing() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Servlet stage1 = new DispatchingServletTracking("/stage2", true);
    Wrapper wrapper1 = Tomcat.addServlet(ctx, "stage1", stage1);
    wrapper1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage1", "stage1");
    Servlet stage2 = new DispatchingServletTracking("/stage3", false);
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "stage2", stage2);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage2", "stage2");
    Servlet stage3 = new NonAsyncServlet();
    Tomcat.addServlet(ctx, "stage3", stage3);
    ctx.addServletMappingDecoded("/stage3", "stage3");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    getUrl("http://localhost:" + getPort() + "/stage1");
    assertEquals("doGet-startAsync-doGet-startAsync-onStartAsync-NonAsyncServletGet-onComplete-", getTrack());
    // Check the access log
    alv.validateAccessLog(1, 200, 0, REQUEST_TIME);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 77 with Wrapper

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

the class TestAsyncContextImpl method doTestDispatchWithSpaces.

private void doTestDispatchWithSpaces(boolean async) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context context = tomcat.addContext("", null);
    if (async) {
        Servlet s = new AsyncDispatchUrlWithSpacesServlet();
        Wrapper w = Tomcat.addServlet(context, "space", s);
        w.setAsyncSupported(true);
    } else {
        Tomcat.addServlet(context, "space", new ForwardDispatchUrlWithSpacesServlet());
    }
    context.addServletMappingDecoded("/space/*", "space");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/sp%61ce/foo%20bar", responseBody, null);
    Assert.assertEquals(200, rc);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet)

Example 78 with Wrapper

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

the class TestAsyncContextImpl method testBug50753.

@Test
public void testBug50753() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug50753Servlet servlet = new Bug50753Servlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "servlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    // Call the servlet once
    Map<String, List<String>> headers = new LinkedHashMap<>();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, headers);
    assertEquals(200, rc);
    assertEquals("OK", bc.toString());
    List<String> testHeader = headers.get("A");
    assertNotNull(testHeader);
    assertEquals(1, testHeader.size());
    assertEquals("xyz", testHeader.get(0));
    // Check the access log
    alv.validateAccessLog(1, 200, Bug50753Servlet.THREAD_SLEEP_TIME, Bug50753Servlet.THREAD_SLEEP_TIME + REQUEST_TIME);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) LinkedHashMap(java.util.LinkedHashMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 79 with Wrapper

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

the class TestConnector method testStop.

@Test
public void testStop() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/", "tester");
    Connector connector = tomcat.getConnector();
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
    assertEquals(200, rc);
    assertEquals("OK", bc.toString());
    rc = -1;
    bc.recycle();
    connector.stop();
    try {
        rc = getUrl("http://localhost:" + getPort() + "/", bc, 1000, null, null);
    } catch (SocketTimeoutException ste) {
        // May also see this with NIO
        // Make sure the test passes if we do
        rc = 503;
    }
    assertEquals(503, rc);
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) SocketTimeoutException(java.net.SocketTimeoutException) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterServlet(org.apache.catalina.startup.TesterServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 80 with Wrapper

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

the class TestApplicationMapping method doTestMappingAsync.

private void doTestMappingAsync(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext(contextPath, null);
    Wrapper w = Tomcat.addServlet(ctx, "Async", new AsyncServlet());
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded(mapping, "Async");
    Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
    ctx.addServletMappingDecoded("/mapping", "Mapping");
    tomcat.start();
    ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
    String body = bc.toString();
    Assert.assertTrue(body, body.contains("MatchValue=[mapping]"));
    Assert.assertTrue(body, body.contains("Pattern=[/mapping]"));
    Assert.assertTrue(body, body.contains("MatchType=[EXACT]"));
    Assert.assertTrue(body, body.contains("ServletName=[Mapping]"));
    Assert.assertTrue(body, body.contains("AsyncMatchValue=[" + matchValue + "]"));
    Assert.assertTrue(body, body.contains("AsyncPattern=[" + mapping + "]"));
    Assert.assertTrue(body, body.contains("AsyncMatchType=[" + matchType + "]"));
    Assert.assertTrue(body, body.contains("AsyncServletName=[Async]"));
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Aggregations

Wrapper (org.apache.catalina.Wrapper)109 Context (org.apache.catalina.Context)57 Tomcat (org.apache.catalina.startup.Tomcat)48 AsyncContext (javax.servlet.AsyncContext)33 Test (org.junit.Test)31 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)28 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)24 TesterContext (org.apache.tomcat.unittest.TesterContext)24 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)22 IOException (java.io.IOException)18 StandardWrapper (org.apache.catalina.core.StandardWrapper)16 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)14 File (java.io.File)13 Container (org.apache.catalina.Container)13 ServletException (javax.servlet.ServletException)9 HashMap (java.util.HashMap)7 StandardContext (org.apache.catalina.core.StandardContext)7 Servlet (javax.servlet.Servlet)6 ArrayList (java.util.ArrayList)5