Search in sources :

Example 66 with Wrapper

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

the class TestAsyncContextImpl method doTestDispatchError.

private void doTestDispatchError(int iter, boolean useThread, boolean completeOnError) throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    DispatchingServlet dispatch = new DispatchingServlet(true, completeOnError);
    Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage1", "dispatch");
    ErrorServlet error = new ErrorServlet();
    Tomcat.addServlet(ctx, "error", error);
    ctx.addServletMappingDecoded("/stage2", "error");
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/stage1?iter=");
    url.append(iter);
    if (useThread) {
        url.append("&useThread=y");
    }
    getUrl(url.toString());
    StringBuilder expected = new StringBuilder("requestInitialized-");
    int loop = iter;
    while (loop > 0) {
        expected.append("DispatchingServletGet-");
        if (loop != iter) {
            expected.append("onStartAsync-");
        }
        loop--;
    }
    expected.append("ErrorServletGet-onError-onComplete-requestDestroyed");
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = expected.toString();
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    assertEquals(expectedTrack, getTrack());
    // Check the access log
    alv.validateAccessLog(1, 500, 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) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 67 with Wrapper

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

the class TestAsyncContextImpl method doTestAsyncISE.

private void doTestAsyncISE(boolean useGetRequest) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncISEServlet servlet = new AsyncISEServlet();
    Wrapper w = Tomcat.addServlet(ctx, "AsyncISEServlet", servlet);
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/test", "AsyncISEServlet");
    tomcat.start();
    ByteChunk response = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/test", response, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    boolean hasIse = false;
    try {
        if (useGetRequest) {
            servlet.getAsyncContext().getRequest();
        } else {
            servlet.getAsyncContext().getResponse();
        }
    } catch (IllegalStateException ise) {
        hasIse = true;
    }
    Assert.assertTrue(hasIse);
}
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)

Example 68 with Wrapper

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

the class TestAsyncContextImpl method testBug53337.

@Test
public void testBug53337() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Wrapper a = Tomcat.addServlet(ctx, "ServletA", new Bug53337ServletA());
    a.setAsyncSupported(true);
    Wrapper b = Tomcat.addServlet(ctx, "ServletB", new Bug53337ServletB());
    b.setAsyncSupported(true);
    Tomcat.addServlet(ctx, "ServletC", new Bug53337ServletC());
    ctx.addServletMappingDecoded("/ServletA", "ServletA");
    ctx.addServletMappingDecoded("/ServletB", "ServletB");
    ctx.addServletMappingDecoded("/ServletC", "ServletC");
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/ServletA");
    ByteChunk body = new ByteChunk();
    int rc = getUrl(url.toString(), body, null);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertEquals("OK", body.toString());
}
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) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 69 with Wrapper

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

the class TestAsyncContextImpl method doTestDispatch.

private void doTestDispatch(int iter, boolean useThread) throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    DispatchingServlet dispatch = new DispatchingServlet(false, false);
    Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage1", "dispatch");
    NonAsyncServlet nonasync = new NonAsyncServlet();
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "nonasync", nonasync);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/stage2", "nonasync");
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/stage1?iter=");
    url.append(iter);
    if (useThread) {
        url.append("&useThread=y");
    }
    getUrl(url.toString());
    StringBuilder expected = new StringBuilder("requestInitialized-");
    int loop = iter;
    while (loop > 0) {
        expected.append("DispatchingServletGet-");
        loop--;
    }
    expected.append("NonAsyncServletGet-");
    expected.append("requestDestroyed");
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = expected.toString();
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    assertEquals(expectedTrack, 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) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 70 with Wrapper

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

the class TestAsyncContextImpl method testBug50352.

@Test
public void testBug50352() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncStartRunnable servlet = new AsyncStartRunnable();
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "servlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    getUrl("http://localhost:" + getPort() + "/");
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = "Runnable-onComplete-";
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    assertEquals(expectedTrack, getTrack());
    // Check the access log
    alv.validateAccessLog(1, 200, AsyncStartRunnable.THREAD_SLEEP_TIME, AsyncStartRunnable.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) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

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