Java Code Examples for org.apache.catalina.Wrapper#addMapping()

The following examples show how to use org.apache.catalina.Wrapper#addMapping() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TomcatTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
protected int createServer() throws Exception
{
    String baseDir = "target/webapp-runner";
    tomcat = new Tomcat();
    int port = super.getPort();
    tomcat.setPort(port);
    File base = new File(baseDir);
    if (!base.exists())
    {
        base.mkdirs();
    }
    tomcat.setBaseDir(baseDir);
    Context ctx = tomcat.addContext("/",base.getAbsolutePath());
    StandardContext standardContext = (StandardContext)ctx;
    standardContext.addApplicationListener(CdiServletContextListener.class.getName());

    Wrapper wrapper = Tomcat.addServlet(ctx,"RequestServlet",RequestServlet.class.getName());
    wrapper.addMapping("/*");
    tomcat.start();
    return port;
}
 
Example 2
Source File: TestStuckThreadDetectionValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testDetection() throws Exception {
    // second, we test the actual effect of the flag on the startup
    StickingServlet stickingServlet = new StickingServlet(8000L);
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            stickingServlet);
    servlet.addMapping("/myservlet");

    StuckThreadDetectionValve valve = new StuckThreadDetectionValve();
    valve.setThreshold(2);
    context.addValve(valve);
    context.setBackgroundProcessorDelay(1);
    tomcat.start();

    Assert.assertEquals(0, valve.getStuckThreadIds().length);

    final ByteChunk result = new ByteChunk();
    Thread asyncThread = new Thread() {
        @Override
        public void run() {
            try {
                getUrl("http://localhost:" + getPort() + "/myservlet",
                        result, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };
    asyncThread.start();
    try {
        Thread.sleep(500L);
        Assert.assertEquals(0, valve.getStuckThreadIds().length);

        Thread.sleep(5000L);
        Assert.assertEquals(1, valve.getStuckThreadIds().length);
    } finally {
        asyncThread.join(20000);
        // check that we did not reach the join timeout
        Assert.assertFalse(asyncThread.isAlive());
    }
    Assert.assertFalse(stickingServlet.wasInterrupted);
    Assert.assertTrue(result.toString().startsWith("OK"));
}
 
Example 3
Source File: TestStuckThreadDetectionValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testInterruption() throws Exception {
    // second, we test the actual effect of the flag on the startup
    StickingServlet stickingServlet = new StickingServlet(
            TimeUnit.SECONDS.toMillis(20L));
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            stickingServlet);
    servlet.addMapping("/myservlet");

    StuckThreadDetectionValve valve = new StuckThreadDetectionValve();
    valve.setThreshold(2);
    valve.setInterruptThreadThreshold(5);
    context.addValve(valve);
    context.setBackgroundProcessorDelay(1);
    tomcat.start();

    Assert.assertEquals(0, valve.getStuckThreadIds().length);

    final ByteChunk result = new ByteChunk();
    Thread asyncThread = new Thread() {
        @Override
        public void run() {
            try {
                getUrl("http://localhost:" + getPort() + "/myservlet",
                        result, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };
    asyncThread.start();
    try {
        Thread.sleep(4000L);
        Assert.assertEquals(1, valve.getStuckThreadIds().length);

    } finally {
        asyncThread.join(20000);
        // check that we did not reach the join timeout
        Assert.assertFalse(asyncThread.isAlive());
    }
    Assert.assertTrue(stickingServlet.wasInterrupted);
    Assert.assertEquals(0, valve.getStuckThreadIds().length);
    Assert.assertTrue(result.toString().startsWith("OK"));
}
 
Example 4
Source File: TestStuckThreadDetectionValve.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetection() throws Exception {
    // second, we test the actual effect of the flag on the startup
    StuckingServlet stuckingServlet = new StuckingServlet(8000L);
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            stuckingServlet);
    servlet.addMapping("/myservlet");

    StuckThreadDetectionValve valve = new StuckThreadDetectionValve();
    valve.setThreshold(2);
    context.addValve(valve);
    context.setBackgroundProcessorDelay(1);
    tomcat.start();

    Assert.assertEquals(0, valve.getStuckThreadIds().length);

    final ByteChunk result = new ByteChunk();
    Thread asyncThread = new Thread() {
        @Override
        public void run() {
            try {
                getUrl("http://localhost:" + getPort() + "/myservlet",
                        result, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };
    asyncThread.start();
    try {
        Thread.sleep(500L);
        Assert.assertEquals(0, valve.getStuckThreadIds().length);

        Thread.sleep(5000L);
        Assert.assertEquals(1, valve.getStuckThreadIds().length);
    } finally {
        asyncThread.join(20000);
        // check that we did not reach the join timeout
        Assert.assertFalse(asyncThread.isAlive());
    }
    Assert.assertFalse(stuckingServlet.wasInterrupted);
    Assert.assertTrue(result.toString().startsWith("OK"));
}
 
Example 5
Source File: TestStuckThreadDetectionValve.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testInterruption() throws Exception {
    // second, we test the actual effect of the flag on the startup
    StuckingServlet stuckingServlet = new StuckingServlet(
            TimeUnit.SECONDS.toMillis(20L));
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            stuckingServlet);
    servlet.addMapping("/myservlet");

    StuckThreadDetectionValve valve = new StuckThreadDetectionValve();
    valve.setThreshold(2);
    valve.setInterruptThreadThreshold(5);
    context.addValve(valve);
    context.setBackgroundProcessorDelay(1);
    tomcat.start();

    Assert.assertEquals(0, valve.getStuckThreadIds().length);

    final ByteChunk result = new ByteChunk();
    Thread asyncThread = new Thread() {
        @Override
        public void run() {
            try {
                getUrl("http://localhost:" + getPort() + "/myservlet",
                        result, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };
    asyncThread.start();
    try {
        Thread.sleep(4000L);
        Assert.assertEquals(1, valve.getStuckThreadIds().length);

    } finally {
        asyncThread.join(20000);
        // check that we did not reach the join timeout
        Assert.assertFalse(asyncThread.isAlive());
    }
    Assert.assertTrue(stuckingServlet.wasInterrupted);
    Assert.assertEquals(0, valve.getStuckThreadIds().length);
    Assert.assertTrue(result.toString().startsWith("OK"));
}
 
Example 6
Source File: TestStuckThreadDetectionValve.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testDetection() throws Exception {
    // second, we test the actual effect of the flag on the startup
    StuckingServlet stuckingServlet = new StuckingServlet(8000L);
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            stuckingServlet);
    servlet.addMapping("/myservlet");

    StuckThreadDetectionValve valve = new StuckThreadDetectionValve();
    valve.setThreshold(2);
    context.addValve(valve);
    context.setBackgroundProcessorDelay(1);
    tomcat.start();

    Assert.assertEquals(0, valve.getStuckThreadIds().length);

    final ByteChunk result = new ByteChunk();
    Thread asyncThread = new Thread() {
        @Override
        public void run() {
            try {
                getUrl("http://localhost:" + getPort() + "/myservlet",
                        result, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };
    asyncThread.start();
    try {
        Thread.sleep(500L);
        Assert.assertEquals(0, valve.getStuckThreadIds().length);

        Thread.sleep(5000L);
        Assert.assertEquals(1, valve.getStuckThreadIds().length);
    } finally {
        asyncThread.join(20000);
        // check that we did not reach the join timeout
        Assert.assertFalse(asyncThread.isAlive());
    }
    Assert.assertFalse(stuckingServlet.wasInterrupted);
    Assert.assertTrue(result.toString().startsWith("OK"));
}
 
Example 7
Source File: TestStuckThreadDetectionValve.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testInterruption() throws Exception {
    // second, we test the actual effect of the flag on the startup
    StuckingServlet stuckingServlet = new StuckingServlet(
            TimeUnit.SECONDS.toMillis(20L));
    Wrapper servlet = Tomcat.addServlet(context, "myservlet",
            stuckingServlet);
    servlet.addMapping("/myservlet");

    StuckThreadDetectionValve valve = new StuckThreadDetectionValve();
    valve.setThreshold(2);
    valve.setInterruptThreadThreshold(5);
    context.addValve(valve);
    context.setBackgroundProcessorDelay(1);
    tomcat.start();

    Assert.assertEquals(0, valve.getStuckThreadIds().length);

    final ByteChunk result = new ByteChunk();
    Thread asyncThread = new Thread() {
        @Override
        public void run() {
            try {
                getUrl("http://localhost:" + getPort() + "/myservlet",
                        result, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    };
    asyncThread.start();
    try {
        Thread.sleep(4000L);
        Assert.assertEquals(1, valve.getStuckThreadIds().length);

    } finally {
        asyncThread.join(20000);
        // check that we did not reach the join timeout
        Assert.assertFalse(asyncThread.isAlive());
    }
    Assert.assertTrue(stuckingServlet.wasInterrupted);
    Assert.assertEquals(0, valve.getStuckThreadIds().length);
    Assert.assertTrue(result.toString().startsWith("OK"));
}
 
Example 8
Source File: ServingLayer.java    From oryx with Apache License 2.0 4 votes vote down vote up
private void makeContext(Tomcat tomcat, Path noSuchBaseDir) throws IOException {
  Path contextPath = noSuchBaseDir.resolve("context");
  Files.createDirectories(contextPath);

  context = tomcat.addContext(contextPathURIBase, contextPath.toAbsolutePath().toString());

  context.setWebappVersion("3.1");
  context.setName("Oryx");

  context.addWelcomeFile("index.html");
  addErrorPages(context);

  // OryxApplication only needs one config value, so just pass it
  context.addParameter(OryxApplication.class.getName() + ".packages", appResourcesPackages);
  // ModelManagerListener will need whole config
  String serializedConfig = ConfigUtils.serialize(config);
  context.addParameter(ConfigUtils.class.getName() + ".serialized", serializedConfig);

  Wrapper wrapper =
      Tomcat.addServlet(context, "Jersey", "org.glassfish.jersey.servlet.ServletContainer");
  wrapper.addInitParameter("javax.ws.rs.Application", OryxApplication.class.getName());
  //wrapper.addInitParameter(OryxApplication.class.getName() + ".packages", appResourcesPackage);
  wrapper.addMapping("/*");
  wrapper.setLoadOnStartup(1);
  wrapper.setMultipartConfigElement(new MultipartConfigElement(""));

  if (!doNotInitTopics) { // Only for tests
    context.addApplicationListener(ModelManagerListener.class.getName());
  }

  // Better way to configure JASPIC?
  AuthConfigFactory.setFactory(new AuthConfigFactoryImpl());

  boolean needHTTPS = keystoreFile != null;
  boolean needAuthentication = userName != null;

  if (needHTTPS || needAuthentication) {

    SecurityCollection securityCollection = new SecurityCollection();
    securityCollection.addPattern("/*");
    SecurityConstraint securityConstraint = new SecurityConstraint();
    securityConstraint.addCollection(securityCollection);

    if (needHTTPS) {
      securityConstraint.setUserConstraint("CONFIDENTIAL");
    }

    if (needAuthentication) {

      LoginConfig loginConfig = new LoginConfig();
      loginConfig.setAuthMethod("DIGEST");
      loginConfig.setRealmName(InMemoryRealm.NAME);
      context.setLoginConfig(loginConfig);

      securityConstraint.addAuthRole(InMemoryRealm.AUTH_ROLE);

      context.addSecurityRole(InMemoryRealm.AUTH_ROLE);
      DigestAuthenticator authenticator = new DigestAuthenticator();
      authenticator.setNonceValidity(10 * 1000L); // Shorten from 5 minutes to 10 seconds
      authenticator.setNonceCacheSize(20000); // Increase from 1000 to 20000
      context.getPipeline().addValve(authenticator);
    }

    context.addConstraint(securityConstraint);
  }

  context.setCookies(false);
}