Java Code Examples for org.apache.catalina.Context#addServletContainerInitializer()

The following examples show how to use org.apache.catalina.Context#addServletContainerInitializer() . 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: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestBug51376(boolean loadOnStartUp) throws Exception {
    // Test that for a servlet that was added programmatically its
    // loadOnStartup property is honored and its init() and destroy()
    // methods are called.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

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

    // Add ServletContainerInitializer
    Bug51376SCI sci = new Bug51376SCI(loadOnStartUp);
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    // Make sure that init() and destroy() were called correctly
    Assert.assertTrue(sci.getServlet().isOk());
    Assert.assertTrue(loadOnStartUp == sci.getServlet().isInitCalled());
}
 
Example 2
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testTldListener() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();

    File docBase = new File("test/webapp-3.0");
    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
    ctx.addServletContainerInitializer(new JasperInitializer(), null);

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    String log = TesterTldListener.getLog();
    Assert.assertTrue(log, log.contains("PASS-01"));
    Assert.assertTrue(log, log.contains("PASS-02"));
    Assert.assertFalse(log, log.contains("FAIL"));
}
 
Example 3
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testUncoveredMethods() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

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

    ServletContainerInitializer sci = new SCI();
    ctx.addServletContainerInitializer(sci, null);

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc;

    rc = getUrl("http://localhost:" + getPort() + "/test/foo", bc, false);

    Assert.assertEquals(403, rc);
}
 
Example 4
Source File: ProxyServletSetup.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(final Context context) {
    final Configuration config = instance.getConfiguration().getExtension(Configuration.class);
    if (config.skip) {
        return;
    }
    context.addServletContainerInitializer((c, ctx) -> {
        final ServletRegistration.Dynamic servlet = ctx.addServlet("meecrowave-proxy-servlet", CDIProxyServlet.class);
        servlet.setLoadOnStartup(1);
        servlet.setAsyncSupported(true);
        servlet.addMapping(config.mapping);
        if (config.multipart) {
            servlet.setMultipartConfig(new MultipartConfigElement(
                    config.multipartLocation,
                    config.multipartMaxFileSize,
                    config.multipartMaxRequestSize,
                    config.multipartFileSizeThreshold));
        }
        servlet.setInitParameter("mapping", config.mapping);
        servlet.setInitParameter("configuration", config.configuration);
    }, null);
}
 
Example 5
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void doTestBug51376(boolean loadOnStartUp) throws Exception {
    // Test that for a servlet that was added programmatically its
    // loadOnStartup property is honored and its init() and destroy()
    // methods are called.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

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

    // Add ServletContainerInitializer
    Bug51376SCI sci = new Bug51376SCI(loadOnStartUp);
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    // Make sure that init() and destroy() were called correctly
    assertTrue(sci.getServlet().isOk());
    assertTrue(loadOnStartUp == sci.getServlet().isInitCalled());
}
 
Example 6
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void doTestBug51376(boolean loadOnStartUp) throws Exception {
    // Test that for a servlet that was added programmatically its
    // loadOnStartup property is honored and its init() and destroy()
    // methods are called.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

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

    // Add ServletContainerInitializer
    Bug51376SCI sci = new Bug51376SCI(loadOnStartUp);
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Stop the context
    ctx.stop();

    // Make sure that init() and destroy() were called correctly
    assertTrue(sci.getServlet().isOk());
    assertTrue(loadOnStartUp == sci.getServlet().isInitCalled());
}
 
Example 7
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug50015() throws Exception {
    // Test that configuring servlet security constraints programmatically
    // does work.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

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

    // Setup realm
    MapRealm realm = new MapRealm();
    realm.addUser("tomcat", "tomcat");
    realm.addUserRole("tomcat", "tomcat");
    ctx.setRealm(realm);

    // Configure app for BASIC auth
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    ctx.setLoginConfig(lc);
    ctx.getPipeline().addValve(new BasicAuthenticator());

    // Add ServletContainerInitializer
    ServletContainerInitializer sci = new Bug50015SCI();
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Request the first servlet
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/bug50015",
            bc, null);

    // Check for a 401
    assertNotSame("OK", bc.toString());
    assertEquals(401, rc);
}
 
Example 8
Source File: WebConfigurer.java    From springboot-angular-atmosphere-quickstart with Apache License 2.0 5 votes vote down vote up
@Bean
public TomcatContextCustomizer tomcatContextCustomizer() {
  return new TomcatContextCustomizer() {
    @Override
    public void customize(Context context) {
      context.addServletContainerInitializer(new WsSci(), null);
    }
  };
}
 
Example 9
Source File: JsonRpcConfiguration.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Bean
public TomcatContextCustomizer tomcatContextCustomizer() {
  return new TomcatContextCustomizer() {
    @Override
    public void customize(Context context) {
      context.addServletContainerInitializer(new WsSci(), null);
    }
  };
}
 
Example 10
Source File: TestStandardWrapper.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void doTestSecurityAnnotationsAddServlet(boolean useCreateServlet)
        throws Exception {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

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

    Servlet s = new DenyAllServlet();
    ServletContainerInitializer sci = new SCI(s, useCreateServlet);
    ctx.addServletContainerInitializer(sci, null);

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);

    if (useCreateServlet) {
        assertTrue(bc.getLength() > 0);
        assertEquals(403, rc);
    } else {
        assertEquals("OK", bc.toString());
        assertEquals(200, rc);
    }
}
 
Example 11
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug50015() throws Exception {
    // Test that configuring servlet security constraints programmatically
    // does work.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

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

    // Setup realm
    MapRealm realm = new MapRealm();
    realm.addUser("tomcat", "tomcat");
    realm.addUserRole("tomcat", "tomcat");
    ctx.setRealm(realm);

    // Configure app for BASIC auth
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    ctx.setLoginConfig(lc);
    ctx.getPipeline().addValve(new BasicAuthenticator());

    // Add ServletContainerInitializer
    ServletContainerInitializer sci = new Bug50015SCI();
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Request the first servlet
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/bug50015",
            bc, null);

    // Check for a 401
    assertNotSame("OK", bc.toString());
    assertEquals(401, rc);
}
 
Example 12
Source File: TestListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Check that a ServletContainerInitializer can install a
 * {@link ServletContextListener} and that it gets initialized.
 * @throws Exception
 */
@Test
public void testServletContainerInitializer() throws Exception {
    Tomcat tomcat = getTomcatInstance();

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

    context.addServletContainerInitializer(new SCI(), null);
    tomcat.start();
    assertTrue(SCL.initialized);
}
 
Example 13
Source File: TestStandardWrapper.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void doTestSecurityAnnotationsAddServlet(boolean useCreateServlet)
        throws Exception {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

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

    Servlet s = new DenyAllServlet();
    ServletContainerInitializer sci = new SCI(s, useCreateServlet);
    ctx.addServletContainerInitializer(sci, null);

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);

    if (useCreateServlet) {
        assertTrue(bc.getLength() > 0);
        assertEquals(403, rc);
    } else {
        assertEquals("OK", bc.toString());
        assertEquals(200, rc);
    }
}
 
Example 14
Source File: TestListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testServletContainerInitializer() throws Exception {
    Tomcat tomcat = getTomcatInstance();

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

    context.addServletContainerInitializer(new SCI(), null);
    tomcat.start();
    Assert.assertTrue(SCL.initialized);
}
 
Example 15
Source File: TestListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Check that a ServletContainerInitializer can install a
 * {@link ServletContextListener} and that it gets initialized.
 * @throws Exception
 */
@Test
public void testServletContainerInitializer() throws Exception {
    Tomcat tomcat = getTomcatInstance();

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

    context.addServletContainerInitializer(new SCI(), null);
    tomcat.start();
    assertTrue(SCL.initialized);
}
 
Example 16
Source File: TestStandardWrapper.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doTestSecurityAnnotationsAddServlet(boolean useCreateServlet)
        throws Exception {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

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

    Servlet s = new DenyAllServlet();
    ServletContainerInitializer sci = new SCI(s, useCreateServlet);
    ctx.addServletContainerInitializer(sci, null);

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);

    if (useCreateServlet) {
        Assert.assertTrue(bc.getLength() > 0);
        Assert.assertEquals(403, rc);
    } else {
        Assert.assertEquals("OK", bc.toString());
        Assert.assertEquals(200, rc);
    }
}
 
Example 17
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug50015() throws Exception {
    // Test that configuring servlet security constraints programmatically
    // does work.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

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

    // Setup realm
    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser("tomcat", "tomcat");
    realm.addUserRole("tomcat", "tomcat");
    ctx.setRealm(realm);

    // Configure app for BASIC auth
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    ctx.setLoginConfig(lc);
    ctx.getPipeline().addValve(new BasicAuthenticator());

    // Add ServletContainerInitializer
    ServletContainerInitializer sci = new Bug50015SCI();
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Request the first servlet
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/bug50015",
            bc, null);

    // Check for a 401
    Assert.assertNotSame("OK", bc.toString());
    Assert.assertEquals(401, rc);
}
 
Example 18
Source File: TestServletSecurityMappings.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void doTestSecurityAnnotationsAddServlet() throws Exception {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

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

    ServletContainerInitializer sci = new SCI(secureRoot, secureDefault, secureFoo);
    ctx.addServletContainerInitializer(sci, null);

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc;

    // Foo
    rc = getUrl("http://localhost:" + getPort() + "/test/foo", bc, false);
    if (secureFoo || secureDefault) {
        Assert.assertEquals(403, rc);
    } else {
        Assert.assertEquals(200, rc);
    }
    bc.recycle();

    // Default
    rc = getUrl("http://localhost:" + getPort() + "/test/something", bc, false);
    if (secureDefault) {
        Assert.assertEquals(403, rc);
    } else {
        Assert.assertEquals(200, rc);
    }
    bc.recycle();

    // Root
    rc = getUrl("http://localhost:" + getPort() + "/test", bc, false);
    if (redirectContextRoot) {
       Assert.assertEquals(302, rc);
    } else {
        if (secureRoot || secureDefault) {
            Assert.assertEquals(403, rc);
        } else {
            Assert.assertEquals(200, rc);
        }
    }
}
 
Example 19
Source File: TestStandardContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void doTestDenyUncoveredHttpMethodsSCI(boolean enableDeny)
        throws Exception {
    // Test that denying uncovered HTTP methods when adding servlet security
    // constraints programmatically does work.

    // Set up a container
    Tomcat tomcat = getTomcatInstance();

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

    ctx.setDenyUncoveredHttpMethods(enableDeny);

    // Setup realm
    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser("tomcat", "tomcat");
    realm.addUserRole("tomcat", "tomcat");
    ctx.setRealm(realm);

    // Configure app for BASIC auth
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    ctx.setLoginConfig(lc);
    ctx.getPipeline().addValve(new BasicAuthenticator());

    // Add ServletContainerInitializer
    ServletContainerInitializer sci = new DenyUncoveredHttpMethodsSCI();
    ctx.addServletContainerInitializer(sci, null);

    // Start the context
    tomcat.start();

    // Request the first servlet
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/test",
            bc, null);

    // Check for a 401
    if (enableDeny) {
        // Should be default error page
        Assert.assertTrue(bc.toString().contains("403"));
        Assert.assertEquals(403, rc);
    } else {
        Assert.assertEquals("OK", bc.toString());
        Assert.assertEquals(200, rc);
    }
}