Search in sources :

Example 16 with Wrapper

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

the class TestAsyncContextImpl method prepareApplicationWithGenericServlet.

private void prepareApplicationWithGenericServlet(String contextPath) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext(contextPath, null);
    DispatchingGenericServlet dispatch = new DispatchingGenericServlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/dispatch", "dispatch");
    CustomGenericServlet customGeneric = new CustomGenericServlet();
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "customGeneric", customGeneric);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/target", "customGeneric");
    tomcat.start();
}
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)

Example 17 with Wrapper

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

the class TestAsyncContextImpl method testBug54178.

@Test
public void testBug54178() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug54178ServletA bug54178ServletA = new Bug54178ServletA();
    Wrapper wrapper = Tomcat.addServlet(ctx, "bug54178ServletA", bug54178ServletA);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/bug54178ServletA", "bug54178ServletA");
    Bug54178ServletB bug54178ServletB = new Bug54178ServletB();
    Tomcat.addServlet(ctx, "bug54178ServletB", bug54178ServletB);
    ctx.addServletMappingDecoded("/bug54178ServletB", "bug54178ServletB");
    tomcat.start();
    ByteChunk body = new ByteChunk();
    int rc = -1;
    try {
        rc = getUrl("http://localhost:" + getPort() + "/bug54178ServletA?" + Bug54178ServletA.PARAM_NAME + "=bar", body, null);
    } catch (IOException ioe) {
        // This may happen if test fails. Output the exception in case it is
        // useful and let asserts handle the failure
        ioe.printStackTrace();
    }
    assertEquals(HttpServletResponse.SC_OK, rc);
    body.recycle();
    rc = getUrl("http://localhost:" + getPort() + "/bug54178ServletB", 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) IOException(java.io.IOException) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 18 with Wrapper

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

the class TestAsyncContextImpl method testForbiddenDispatching.

@Test
public void testForbiddenDispatching() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    NonAsyncServlet nonAsyncServlet = new NonAsyncServlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "nonAsyncServlet", nonAsyncServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/target", "nonAsyncServlet");
    DispatchingGenericServlet forbiddenDispatchingServlet = new DispatchingGenericServlet();
    Wrapper wrapper1 = Tomcat.addServlet(ctx, "forbiddenDispatchingServlet", forbiddenDispatchingServlet);
    wrapper1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/forbiddenDispatchingServlet", "forbiddenDispatchingServlet");
    tomcat.start();
    try {
        getUrl("http://localhost:" + getPort() + "/forbiddenDispatchingServlet");
    } catch (IOException ioe) {
        // This may happen if test fails. Output the exception in case it is
        // useful and let asserts handle the failure
        ioe.printStackTrace();
    }
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = "OKNonAsyncServletGet-";
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    Assert.assertEquals(expectedTrack, getTrack());
}
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) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 19 with Wrapper

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

the class TestCoyoteOutputStream method doNonBlockingTest.

private void doNonBlockingTest(int asyncWriteTarget, int syncWriteTarget, boolean useContainerThreadToSetListener) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "nbWrite", new NonBlockingWriteServlet(asyncWriteTarget, useContainerThreadToSetListener));
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/nbWrite", "nbWrite");
    Tomcat.addServlet(root, "write", new BlockingWriteServlet(asyncWriteTarget, syncWriteTarget));
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/write", "write");
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    // Extend timeout to 5 mins for debugging
    int rc = getUrl("http://localhost:" + getPort() + "/nbWrite", bc, 300000, null, null);
    int totalCount = asyncWriteTarget + syncWriteTarget;
    StringBuilder sb = new StringBuilder(totalCount * 16);
    for (int i = 0; i < totalCount; i++) {
        sb.append("OK - " + i + System.lineSeparator());
    }
    String expected = null;
    if (sb.length() > 0) {
        expected = sb.toString();
    }
    Assert.assertEquals(200, rc);
    Assert.assertEquals(expected, bc.toString());
}
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)

Example 20 with Wrapper

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

the class TestHttp11Processor method testRequestBodySwallowing.

/*
     * Tests what happens if a request is completed during a dispatch but the
     * request body has not been fully read.
     */
@Test
public void testRequestBodySwallowing() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    DispatchingServlet servlet = new DispatchingServlet();
    Wrapper w = Tomcat.addServlet(ctx, "Test", servlet);
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/test", "Test");
    tomcat.start();
    // Hand-craft the client so we have complete control over the timing
    SocketAddress addr = new InetSocketAddress("localhost", getPort());
    Socket socket = new Socket();
    socket.setSoTimeout(300000);
    socket.connect(addr, 300000);
    OutputStream os = socket.getOutputStream();
    Writer writer = new OutputStreamWriter(os, "ISO-8859-1");
    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is, "ISO-8859-1");
    BufferedReader reader = new BufferedReader(r);
    // Write the headers
    writer.write("POST /test HTTP/1.1\r\n");
    writer.write("Host: localhost:8080\r\n");
    writer.write("Transfer-Encoding: chunked\r\n");
    writer.write("\r\n");
    writer.flush();
    validateResponse(reader);
    // Write the request body
    writer.write("2\r\n");
    writer.write("AB\r\n");
    writer.write("0\r\n");
    writer.write("\r\n");
    writer.flush();
    // Write the 2nd request
    writer.write("POST /test HTTP/1.1\r\n");
    writer.write("Host: localhost:8080\r\n");
    writer.write("Transfer-Encoding: chunked\r\n");
    writer.write("\r\n");
    writer.flush();
    // Read the 2nd response
    validateResponse(reader);
    // Write the 2nd request body
    writer.write("2\r\n");
    writer.write("AB\r\n");
    writer.write("0\r\n");
    writer.write("\r\n");
    writer.flush();
    // Done
    socket.close();
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Wrapper (org.apache.catalina.Wrapper)87 Context (org.apache.catalina.Context)53 Tomcat (org.apache.catalina.startup.Tomcat)46 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)14 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)14 File (java.io.File)11 Container (org.apache.catalina.Container)11 StandardWrapper (org.apache.catalina.core.StandardWrapper)10 StandardContext (org.apache.catalina.core.StandardContext)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)6 ServletException (javax.servlet.ServletException)5 ArrayList (java.util.ArrayList)4 ServletContext (javax.servlet.ServletContext)4