Search in sources :

Example 16 with Context

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

the class TestAsyncContextImpl method doTestTimeoutErrorDispatch.

private void doTestTimeoutErrorDispatch(Boolean asyncError, ErrorPageAsyncMode mode) throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    TimeoutServlet timeout = new TimeoutServlet(null, null);
    Wrapper w1 = Tomcat.addServlet(ctx, "time", timeout);
    w1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/async", "time");
    NonAsyncServlet nonAsync = new NonAsyncServlet();
    Wrapper w2 = Tomcat.addServlet(ctx, "nonAsync", nonAsync);
    w2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/error/nonasync", "nonAsync");
    AsyncErrorPage asyncErrorPage = new AsyncErrorPage(mode);
    Wrapper w3 = Tomcat.addServlet(ctx, "asyncErrorPage", asyncErrorPage);
    w3.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/error/async", "asyncErrorPage");
    if (asyncError != null) {
        ErrorPage ep = new ErrorPage();
        ep.setErrorCode(500);
        if (asyncError.booleanValue()) {
            ep.setLocation("/error/async");
        } else {
            ep.setLocation("/error/nonasync");
        }
        ctx.addErrorPage(ep);
    }
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    TesterAccessLogValve alvGlobal = new TesterAccessLogValve();
    tomcat.getHost().getPipeline().addValve(alvGlobal);
    tomcat.start();
    ByteChunk res = new ByteChunk();
    try {
        getUrl("http://localhost:" + getPort() + "/async", res, null);
    } catch (IOException ioe) {
    // Ignore - expected for some error conditions
    }
    StringBuilder expected = new StringBuilder();
    expected.append("requestInitialized-TimeoutServletGet-");
    if (asyncError != null) {
        if (asyncError.booleanValue()) {
            expected.append("AsyncErrorPageGet-");
            if (mode == ErrorPageAsyncMode.NO_COMPLETE) {
                expected.append("NoOp-");
            } else if (mode == ErrorPageAsyncMode.COMPLETE) {
                expected.append("Complete-");
            } else if (mode == ErrorPageAsyncMode.DISPATCH) {
                expected.append("Dispatch-NonAsyncServletGet-");
            }
        } else {
            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++;
    }
    Assert.assertEquals(expectedTrack, getTrack());
    // Check the access log
    alvGlobal.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
    alv.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + 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) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) IOException(java.io.IOException) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 17 with Context

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

the class TestAsyncContextImpl method doTestAsyncRequestURI.

private void doTestAsyncRequestURI(String uri) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Servlet servlet = new AsyncRequestUriServlet();
    Wrapper wrapper1 = Tomcat.addServlet(ctx, "bug57559", servlet);
    wrapper1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "bug57559");
    tomcat.start();
    ByteChunk body = getUrl("http://localhost:" + getPort() + uri);
    Assert.assertEquals(uri, 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) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) GenericServlet(javax.servlet.GenericServlet)

Example 18 with Context

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

the class TestAsyncContextImpl method testBug53843.

@Test
public void testBug53843() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug53843ServletA servletA = new Bug53843ServletA();
    Wrapper a = Tomcat.addServlet(ctx, "ServletA", servletA);
    a.setAsyncSupported(true);
    Tomcat.addServlet(ctx, "ServletB", new Bug53843ServletB());
    ctx.addServletMappingDecoded("/ServletA", "ServletA");
    ctx.addServletMappingDecoded("/ServletB", "ServletB");
    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());
    assertTrue(servletA.isAsyncWhenExpected());
}
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 19 with Context

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

the class TestAsyncContextImpl method doTestTimeout.

private void doTestTimeout(Boolean completeOnTimeout, Boolean asyncDispatch) throws Exception {
    resetTracker();
    String dispatchUrl = null;
    if (asyncDispatch != null) {
        if (asyncDispatch.booleanValue()) {
            dispatchUrl = "/async";
        } else {
            dispatchUrl = "/nonasync";
        }
    }
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    TimeoutServlet timeout = new TimeoutServlet(completeOnTimeout, dispatchUrl);
    Wrapper wrapper = Tomcat.addServlet(ctx, "time", timeout);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/start", "time");
    if (asyncDispatch != null) {
        if (asyncDispatch.booleanValue()) {
            AsyncStartRunnable asyncStartRunnable = new AsyncStartRunnable();
            Wrapper async = Tomcat.addServlet(ctx, "async", asyncStartRunnable);
            async.setAsyncSupported(true);
            ctx.addServletMappingDecoded(dispatchUrl, "async");
        } else {
            NonAsyncServlet nonAsync = new NonAsyncServlet();
            Tomcat.addServlet(ctx, "nonasync", nonAsync);
            ctx.addServletMappingDecoded(dispatchUrl, "nonasync");
        }
    }
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    TesterAccessLogValve alvGlobal = new TesterAccessLogValve();
    tomcat.getHost().getPipeline().addValve(alvGlobal);
    tomcat.start();
    try {
        getUrl("http://localhost:" + getPort() + "/start");
    } catch (IOException ioe) {
    // Ignore - expected for some error conditions
    }
    StringBuilder expected = new StringBuilder("requestInitialized-");
    expected.append("TimeoutServletGet-");
    if (completeOnTimeout == null) {
        expected.append("requestDestroyed");
    } else if (completeOnTimeout.booleanValue()) {
        expected.append("onTimeout-");
        expected.append("onComplete-");
        expected.append("requestDestroyed");
    } else {
        expected.append("onTimeout-");
        if (asyncDispatch != null) {
            if (asyncDispatch.booleanValue()) {
                expected.append("onStartAsync-Runnable-");
            } else {
                expected.append("NonAsyncServletGet-");
            }
        }
        expected.append("onComplete-");
        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
    if (completeOnTimeout == null || (!completeOnTimeout.booleanValue() && asyncDispatch == null)) {
        alvGlobal.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
        alv.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
    } else {
        long timeoutDelay = TimeoutServlet.ASYNC_TIMEOUT;
        if (asyncDispatch != null && asyncDispatch.booleanValue() && !completeOnTimeout.booleanValue()) {
            // The async dispatch includes a sleep
            timeoutDelay += AsyncStartRunnable.THREAD_SLEEP_TIME;
        }
        alvGlobal.validateAccessLog(1, 200, timeoutDelay, timeoutDelay + TIMEOUT_MARGIN + REQUEST_TIME);
        alv.validateAccessLog(1, 200, timeoutDelay, timeoutDelay + TIMEOUT_MARGIN + 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) IOException(java.io.IOException) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 20 with Context

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

the class TestAsyncContextImpl method testBug49528.

@Test
public void testBug49528() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug49528Servlet servlet = new Bug49528Servlet();
    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
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    assertEquals("OK", bc.toString());
    // Give the async thread a chance to finish (but not too long)
    int counter = 0;
    while (!servlet.isDone() && counter < 10) {
        Thread.sleep(1000);
        counter++;
    }
    assertEquals("1false2true3true4true5false", servlet.getResult());
    // Check the access log
    alv.validateAccessLog(1, 200, Bug49528Servlet.THREAD_SLEEP_TIME, Bug49528Servlet.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) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) 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