Search in sources :

Example 21 with Wrapper

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

the class TestStreamProcessor method testAsyncDispatch.

@Test
public void testAsyncDispatch() throws Exception {
    enableHttp2();
    Tomcat tomcat = getTomcatInstance();
    // Map the async servlet to /simple so we can re-use the HTTP/2 handling
    // logic from the super class.
    Context ctxt = tomcat.addContext("", null);
    Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
    ctxt.addServletMappingDecoded("/simple", "simple");
    Wrapper w = Tomcat.addServlet(ctxt, "async", new AsyncDispatch());
    w.setAsyncSupported(true);
    ctxt.addServletMappingDecoded("/async", "async");
    tomcat.start();
    openClientConnection();
    doHttpUpgrade();
    sendClientPreface();
    validateHttp2InitialResponse();
    byte[] frameHeader = new byte[9];
    ByteBuffer headersPayload = ByteBuffer.allocate(128);
    buildGetRequest(frameHeader, headersPayload, null, 3, "/async");
    writeFrame(frameHeader, headersPayload);
    readSimpleGetResponse();
    Assert.assertEquals(getSimpleResponseTrace(3), output.getTrace());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 22 with Wrapper

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

the class TestStreamProcessor method testAsyncComplete.

@Test
public void testAsyncComplete() throws Exception {
    enableHttp2();
    Tomcat tomcat = getTomcatInstance();
    // Map the async servlet to /simple so we can re-use the HTTP/2 handling
    // logic from the super class.
    Context ctxt = tomcat.addContext("", null);
    Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
    ctxt.addServletMappingDecoded("/simple", "simple");
    Wrapper w = Tomcat.addServlet(ctxt, "async", new AsyncComplete());
    w.setAsyncSupported(true);
    ctxt.addServletMappingDecoded("/async", "async");
    tomcat.start();
    openClientConnection();
    doHttpUpgrade();
    sendClientPreface();
    validateHttp2InitialResponse();
    byte[] frameHeader = new byte[9];
    ByteBuffer headersPayload = ByteBuffer.allocate(128);
    buildGetRequest(frameHeader, headersPayload, null, 3, "/async");
    writeFrame(frameHeader, headersPayload);
    readSimpleGetResponse();
    // Flush before startAsync means body is written in two packets so an
    // additional frame needs to be read
    parser.readFrame(true);
    Assert.assertEquals("3-HeadersStart\n" + "3-Header-[:status]-[200]\n" + "3-Header-[content-type]-[text/plain;charset=UTF-8]\n" + "3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "3-HeadersEnd\n" + "3-Body-17\n" + "3-Body-8\n" + "3-EndOfStream\n", output.getTrace());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 23 with Wrapper

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

the class TestSsl method testRenegotiateWorks.

@Test
public void testRenegotiateWorks() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Assume.assumeTrue("SSL renegotiation has to be supported for this test", TesterSupport.isClientRenegotiationSupported(getTomcatInstance()));
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/", "tester");
    TesterSupport.initSsl(tomcat);
    tomcat.start();
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(null, TesterSupport.getTrustManagers(), null);
    SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", getPort());
    OutputStream os = socket.getOutputStream();
    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is);
    doRequest(os, r);
    TesterHandshakeListener listener = new TesterHandshakeListener();
    socket.addHandshakeCompletedListener(listener);
    socket.startHandshake();
    // One request should be sufficient
    int requestCount = 0;
    int listenerComplete = 0;
    try {
        while (requestCount < 10) {
            requestCount++;
            doRequest(os, r);
            if (listener.isComplete() && listenerComplete == 0) {
                listenerComplete = requestCount;
            }
        }
    } catch (AssertionError | IOException e) {
        String message = "Failed on request number " + requestCount + " after startHandshake(). " + e.getMessage();
        log.error(message, e);
        Assert.fail(message);
    }
    Assert.assertTrue(listener.isComplete());
    System.out.println("Renegotiation completed after " + listenerComplete + " requests");
}
Also used : SSLContext(javax.net.ssl.SSLContext) Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) TesterServlet(org.apache.catalina.startup.TesterServlet) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 24 with Wrapper

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

the class ContainerBase method getMBeanKeyProperties.

@Override
public String getMBeanKeyProperties() {
    Container c = this;
    StringBuilder keyProperties = new StringBuilder();
    int containerCount = 0;
    // each container
    while (!(c instanceof Engine)) {
        if (c instanceof Wrapper) {
            keyProperties.insert(0, ",servlet=");
            keyProperties.insert(9, c.getName());
        } else if (c instanceof Context) {
            keyProperties.insert(0, ",context=");
            ContextName cn = new ContextName(c.getName(), false);
            keyProperties.insert(9, cn.getDisplayName());
        } else if (c instanceof Host) {
            keyProperties.insert(0, ",host=");
            keyProperties.insert(6, c.getName());
        } else if (c == null) {
            // May happen in unit testing and/or some embedding scenarios
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append("=null");
            break;
        } else {
            // Should never happen...
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append('=');
            keyProperties.append(c.getName());
        }
        c = c.getParent();
    }
    return keyProperties.toString();
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Container(org.apache.catalina.Container) Host(org.apache.catalina.Host) Engine(org.apache.catalina.Engine) ContextName(org.apache.catalina.util.ContextName)

Example 25 with Wrapper

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

the class StandardContext method removeServletMapping.

/**
     * Remove any servlet mapping for the specified pattern, if it exists;
     * otherwise, no action is taken.
     *
     * @param pattern URL pattern of the mapping to remove
     */
@Override
public void removeServletMapping(String pattern) {
    String name = null;
    synchronized (servletMappingsLock) {
        name = servletMappings.remove(pattern);
    }
    Wrapper wrapper = (Wrapper) findChild(name);
    if (wrapper != null) {
        wrapper.removeMapping(pattern);
    }
    fireContainerEvent("removeServletMapping", pattern);
}
Also used : Wrapper(org.apache.catalina.Wrapper)

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