public void testListeners() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // Must have a real docBase - just use temp
    File docBase = new File(System.getProperty("java.io.tmpdir"));

    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());

    TrackingServlet tracking = new TrackingServlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "tracking", tracking);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/stage1", "tracking");

    TimeoutServlet timeout = new TimeoutServlet(true, null);
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "timeout", timeout);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMapping("/stage2", "timeout");

    tomcat.start();

    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/stage1");

    ByteChunk res = getUrl(url.toString());

    assertEquals(
        "DispatchingServletGet-DispatchingServletGet-onStartAsync-"
            + "TimeoutServletGet-onStartAsync-onTimeout-onComplete-",
        res.toString());
  }
  public void testAsyncStartNoComplete() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // Minimise pauses during test
    tomcat.getConnector().setAttribute("connectionTimeout", Integer.valueOf(3000));

    // Must have a real docBase - just use temp
    Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir"));

    AsyncStartNoCompleteServlet servlet = new AsyncStartNoCompleteServlet();

    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/", "servlet");

    tomcat.start();

    // Call the servlet the first time
    ByteChunk bc1 = getUrl("http://localhost:" + getPort() + "/?echo=run1");
    assertEquals("OK-run1", bc1.toString());

    // Call the servlet the second time with a request parameter
    ByteChunk bc2 = getUrl("http://localhost:" + getPort() + "/?echo=run2");
    assertEquals("OK-run2", bc2.toString());
  }
  public void testBug49567() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // Must have a real docBase - just use temp
    Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir"));

    Bug49567Servlet servlet = new Bug49567Servlet();

    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/", "servlet");

    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());
  }
  @Test
  public void testBug56042() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Bug56042Servlet bug56042Servlet = new Bug56042Servlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "bug56042Servlet", bug56042Servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/bug56042Servlet", "bug56042Servlet");

    tomcat.start();

    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/bug56042Servlet");

    ByteChunk res = new ByteChunk();
    int rc = getUrl(url.toString(), res, null);

    Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
  }
Example #5
0
  @Override
  public void construct() {
    tomcat = new Tomcat();

    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setURIEncoding("UTF-8");
    connector.setPort(5000);
    tomcat.getService().addConnector(connector);
    tomcat.setConnector(connector);

    try {
      Context ctx;

      if (new File(Configuration.WEBROOT_PATH).exists()) {
        L.i("Static web client found.");

        ctx = tomcat.addContext("", Configuration.WEBROOT_PATH);
        ctx.addWelcomeFile("index.html");

        Wrapper wrapper = tomcat.addServlet(ctx, "DefaultServlet", new DefaultServlet());
        wrapper.setAsyncSupported(true);
        wrapper.addInitParameter("listings", "false");
        wrapper.addMapping("/");
        wrapper.setLoadOnStartup(1);
      } else {
        L.w("No static web client found, web interface will not be available.");
        ctx = tomcat.addContext("/", "/tmp");
      }

      configureMimeMappings(ctx);

      addSessionFilter(ctx);
      addCharacterEncodingFilter(ctx);

      Wrapper wrapper = tomcat.addServlet(ctx, "JerseyServlet", new ServletContainer());
      wrapper.setAsyncSupported(true);
      wrapper.addInitParameter("javax.ws.rs.Application", JerseyApplication.class.getName());
      wrapper.addMapping("/api/*");
      wrapper.setLoadOnStartup(1);

      tomcat.start();
    } catch (Exception e) {
      L.e("Failed to start RestApiModule.", e);
    }
  }
  private void doTestDispatch(int iter, boolean useThread) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // Must have a real docBase - just use temp
    File docBase = new File(System.getProperty("java.io.tmpdir"));

    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());

    DispatchingServlet dispatch = new DispatchingServlet(false, false);
    Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/stage1", "dispatch");

    NonAsyncServlet nonasync = new NonAsyncServlet();
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "nonasync", nonasync);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMapping("/stage2", "nonasync");

    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");
    }
    ByteChunk res = getUrl(url.toString());

    StringBuilder expected = new StringBuilder();
    int loop = iter;
    while (loop > 0) {
      expected.append("DispatchingServletGet-");
      loop--;
    }
    expected.append("NonAsyncServletGet-");
    assertEquals(expected.toString(), res.toString());
  }
  public void testAsyncStartWithComplete() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // Must have a real docBase - just use temp
    Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir"));

    AsyncStartWithCompleteServlet servlet = new AsyncStartWithCompleteServlet();

    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/", "servlet");

    tomcat.start();

    // Call the servlet once
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    assertEquals("OK", bc.toString());
  }
  private void doTestTimeout(boolean completeOnTimeout, String dispatchUrl) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // Must have a real docBase - just use temp
    File docBase = new File(System.getProperty("java.io.tmpdir"));

    // Create the folder that will trigger the redirect
    File foo = new File(docBase, "async");
    if (!foo.exists() && !foo.mkdirs()) {
      fail("Unable to create async directory in docBase");
    }

    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());

    TimeoutServlet timeout = new TimeoutServlet(completeOnTimeout, dispatchUrl);

    Wrapper wrapper = Tomcat.addServlet(ctx, "time", timeout);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/async", "time");

    if (dispatchUrl != null) {
      NonAsyncServlet nonAsync = new NonAsyncServlet();
      Tomcat.addServlet(ctx, "nonasync", nonAsync);
      ctx.addServletMapping(dispatchUrl, "nonasync");
    }

    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/async");
    StringBuilder expected = new StringBuilder();
    expected.append("TimeoutServletGet-onTimeout-");
    if (!completeOnTimeout) {
      expected.append("onError-");
    }
    if (dispatchUrl == null) {
      expected.append("onComplete-");
    } else {
      expected.append("NonAsyncServletGet-");
    }
    assertEquals(expected.toString(), res.toString());
  }
  public void testBug50352() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // Must have a real docBase - just use temp
    File docBase = new File(System.getProperty("java.io.tmpdir"));

    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());

    AsyncStartRunnable servlet = new AsyncStartRunnable();
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMapping("/", "servlet");

    ErrorServlet error = new ErrorServlet();
    Tomcat.addServlet(ctx, "error", error);
    ctx.addServletMapping("/stage2", "error");

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");

    assertEquals("Runnable-onComplete-", res.toString());
  }