Search in sources :

Example 11 with Wrapper

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

the class TestDefaultInstanceManager method doClassUnloadingPrep.

private DefaultInstanceManager doClassUnloadingPrep() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // Create the context (don't use addWebapp as we want to modify the
    // JSP Servlet settings).
    File appDir = new File("test/webapp");
    StandardContext ctxt = (StandardContext) tomcat.addContext(null, "/test", appDir.getAbsolutePath());
    ctxt.addServletContainerInitializer(new JasperInitializer(), null);
    // Configure the defaults and then tweak the JSP servlet settings
    // Note: Min value for maxLoadedJsps is 2
    Tomcat.initWebappDefaults(ctxt);
    Wrapper w = (Wrapper) ctxt.findChild("jsp");
    w.addInitParameter("maxLoadedJsps", "2");
    tomcat.start();
    return (DefaultInstanceManager) ctxt.getInstanceManager();
}
Also used : Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) File(java.io.File) JasperInitializer(org.apache.jasper.servlet.JasperInitializer)

Example 12 with Wrapper

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

the class TestCoyoteAdapter method testBug54928.

@Test
public void testBug54928() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncServlet servlet = new AsyncServlet();
    Wrapper w = Tomcat.addServlet(ctx, "async", servlet);
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/async", "async");
    tomcat.start();
    SimpleHttpClient client = new SimpleHttpClient() {

        @Override
        public boolean isResponseBodyOK() {
            return true;
        }
    };
    String request = "GET /async HTTP/1.1" + SimpleHttpClient.CRLF + "Host: a" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    client.setPort(getPort());
    client.setRequest(new String[] { request });
    client.connect();
    client.sendRequest();
    for (int i = 0; i < 10; i++) {
        String line = client.readLine();
        if (line != null && line.length() > 20) {
            log.info(line.subSequence(0, 20) + "...");
        }
    }
    client.disconnect();
    // Wait for server thread to stop
    Thread t = servlet.getThread();
    long startTime = System.nanoTime();
    t.join(5000);
    long endTime = System.nanoTime();
    log.info("Waited for servlet thread to stop for " + (endTime - startTime) / 1000000 + " ms");
    Assert.assertTrue(servlet.isCompleted());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 13 with Wrapper

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

the class TestApplicationContextGetRequestDispatcher method doTestGetRequestDispatcher.

private void doTestGetRequestDispatcher(boolean useEncodedDispatchPaths, String startPath, String startQueryString, String dispatchPath, String targetPath, String expectedBody) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("/test", null);
    ctx.setDispatchersUseEncodedPaths(useEncodedDispatchPaths);
    // Add a default servlet to return 404 for not found resources
    Tomcat.addServlet(ctx, "Default", new Default404Servlet());
    ctx.addServletMappingDecoded("/*", "Default");
    // Add a target servlet to dispatch to
    Tomcat.addServlet(ctx, "target", new TargetServlet());
    ctx.addServletMappingDecoded(UDecoder.URLDecode(targetPath, "UTF-8"), "target");
    if (useAsync) {
        Wrapper w = Tomcat.addServlet(ctx, "rd", new AsyncDispatcherServlet(dispatchPath, useEncodedDispatchPaths));
        w.setAsyncSupported(true);
    } else {
        Tomcat.addServlet(ctx, "rd", new DispatcherServlet(dispatchPath));
    }
    ctx.addServletMappingDecoded(UDecoder.URLDecode(startPath, "UTF-8"), "rd");
    tomcat.start();
    StringBuilder url = new StringBuilder("http://localhost:");
    url.append(getPort());
    url.append("/test");
    url.append(startPath);
    if (startQueryString != null) {
        url.append('?');
        url.append(startQueryString);
    }
    ByteChunk bc = getUrl(url.toString());
    String body = bc.toString();
    Assert.assertEquals(expectedBody, body);
}
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 14 with Wrapper

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

the class TestAsyncContextImpl method testBug49567.

@Test
public void testBug49567() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug49567Servlet servlet = new Bug49567Servlet();
    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, Bug49567Servlet.THREAD_SLEEP_TIME, Bug49567Servlet.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)

Example 15 with Wrapper

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

the class TestAsyncContextImpl method doTestBug51197.

private void doTestBug51197(boolean threaded, boolean customError) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncErrorServlet asyncErrorServlet = new AsyncErrorServlet(HttpServletResponse.SC_BAD_REQUEST, threaded);
    Wrapper wrapper = Tomcat.addServlet(ctx, "asyncErrorServlet", asyncErrorServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/asyncErrorServlet", "asyncErrorServlet");
    if (customError) {
        CustomErrorServlet customErrorServlet = new CustomErrorServlet();
        Tomcat.addServlet(ctx, "customErrorServlet", customErrorServlet);
        ctx.addServletMappingDecoded("/customErrorServlet", "customErrorServlet");
        ErrorPage ep = new ErrorPage();
        ep.setLocation("/customErrorServlet");
        ctx.addErrorPage(ep);
    }
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/asyncErrorServlet");
    ByteChunk res = new ByteChunk();
    int rc = getUrl(url.toString(), res, null);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
    // SRV 10.9.2 - Handling an error is entirely the application's
    // responsibility when an error occurs on an application thread.
    // Calling sendError() followed by complete() and expecting the standard
    // error page mechanism to kick in could be viewed as handling the error
    String responseBody = res.toString();
    Assert.assertNotNull(responseBody);
    assertTrue(responseBody.length() > 0);
    if (customError) {
        assertTrue(responseBody, responseBody.contains(CustomErrorServlet.ERROR_MESSAGE));
    } else {
        assertTrue(responseBody, responseBody.contains(AsyncErrorServlet.ERROR_MESSAGE));
    }
    // Without this test may complete before access log has a chance to log
    // the request
    Thread.sleep(REQUEST_TIME);
    // Check the access log
    alv.validateAccessLog(1, HttpServletResponse.SC_BAD_REQUEST, 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) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

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