Search in sources :

Example 16 with TesterServlet

use of org.apache.catalina.startup.TesterServlet in project tomcat70 by apache.

the class TestDigestAuthenticator method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Configure a context with digest auth and a single protected resource
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctxt = tomcat.addContext(CONTEXT_PATH, null);
    // Add protected servlet
    Tomcat.addServlet(ctxt, "TesterServlet", new TesterServlet());
    ctxt.addServletMapping(URI, "TesterServlet");
    SecurityCollection collection = new SecurityCollection();
    collection.addPattern(URI);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctxt.addConstraint(sc);
    // Configure the Realm
    MapRealm realm = new MapRealm();
    realm.addUser(USER, PWD);
    realm.addUserRole(USER, ROLE);
    ctxt.setRealm(realm);
    // Configure the authenticator
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("DIGEST");
    lc.setRealmName(REALM);
    ctxt.setLoginConfig(lc);
    ctxt.getPipeline().addValve(new DigestAuthenticator());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Tomcat(org.apache.catalina.startup.Tomcat) LoginConfig(org.apache.catalina.deploy.LoginConfig) MapRealm(org.apache.catalina.startup.TestTomcat.MapRealm) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) SecurityCollection(org.apache.catalina.deploy.SecurityCollection)

Example 17 with TesterServlet

use of org.apache.catalina.startup.TesterServlet in project tomcat70 by apache.

the class TestNonLoginAndBasicAuthenticator method setUpNonLogin.

private void setUpNonLogin() throws Exception {
    // No file system docBase required
    nonloginContext = tomcat.addContext(CONTEXT_PATH_NOLOGIN, null);
    nonloginContext.setSessionTimeout(LONG_SESSION_TIMEOUT_MINS);
    // Add protected servlet to the context
    Tomcat.addServlet(nonloginContext, "TesterServlet1", new TesterServlet());
    nonloginContext.addServletMapping(URI_PROTECTED, "TesterServlet1");
    SecurityCollection collection1 = new SecurityCollection();
    collection1.addPattern(URI_PROTECTED);
    SecurityConstraint sc1 = new SecurityConstraint();
    sc1.addAuthRole(ROLE);
    sc1.addCollection(collection1);
    nonloginContext.addConstraint(sc1);
    // Add unprotected servlet to the context
    Tomcat.addServlet(nonloginContext, "TesterServlet2", new TesterServlet());
    nonloginContext.addServletMapping(URI_PUBLIC, "TesterServlet2");
    SecurityCollection collection2 = new SecurityCollection();
    collection2.addPattern(URI_PUBLIC);
    SecurityConstraint sc2 = new SecurityConstraint();
    // do not add a role - which signals access permitted without one
    sc2.addCollection(collection2);
    nonloginContext.addConstraint(sc2);
    // Configure the authenticator and inherit the Realm from Engine
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("NONE");
    nonloginContext.setLoginConfig(lc);
    AuthenticatorBase nonloginAuthenticator = new NonLoginAuthenticator();
    nonloginContext.getPipeline().addValve(nonloginAuthenticator);
}
Also used : LoginConfig(org.apache.catalina.deploy.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) SecurityCollection(org.apache.catalina.deploy.SecurityCollection)

Example 18 with TesterServlet

use of org.apache.catalina.startup.TesterServlet in project tomcat70 by apache.

the class TestNonLoginAndBasicAuthenticator method setUpLogin.

private void setUpLogin() throws Exception {
    // No file system docBase required
    basicContext = tomcat.addContext(CONTEXT_PATH_LOGIN, null);
    basicContext.setSessionTimeout(SHORT_SESSION_TIMEOUT_MINS);
    // Add protected servlet to the context
    Tomcat.addServlet(basicContext, "TesterServlet3", new TesterServlet());
    basicContext.addServletMapping(URI_PROTECTED, "TesterServlet3");
    SecurityCollection collection = new SecurityCollection();
    collection.addPattern(URI_PROTECTED);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    basicContext.addConstraint(sc);
    // Add unprotected servlet to the context
    Tomcat.addServlet(basicContext, "TesterServlet4", new TesterServlet());
    basicContext.addServletMapping(URI_PUBLIC, "TesterServlet4");
    SecurityCollection collection2 = new SecurityCollection();
    collection2.addPattern(URI_PUBLIC);
    SecurityConstraint sc2 = new SecurityConstraint();
    // do not add a role - which signals access permitted without one
    sc2.addCollection(collection2);
    basicContext.addConstraint(sc2);
    // Configure the authenticator and inherit the Realm from Engine
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    basicContext.setLoginConfig(lc);
    AuthenticatorBase basicAuthenticator = new BasicAuthenticator();
    basicContext.getPipeline().addValve(basicAuthenticator);
}
Also used : LoginConfig(org.apache.catalina.deploy.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) SecurityCollection(org.apache.catalina.deploy.SecurityCollection)

Example 19 with TesterServlet

use of org.apache.catalina.startup.TesterServlet in project tomcat70 by apache.

the class TestConnector method testStop.

@Test
public void testStop() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context root = tomcat.addContext("", null);
    Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMapping("/", "tester");
    Connector connector = tomcat.getConnector();
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
    Assert.assertEquals(200, rc);
    Assert.assertEquals("OK", bc.toString());
    rc = -1;
    bc.recycle();
    connector.stop();
    try {
        rc = getUrl("http://localhost:" + getPort() + "/", bc, 1000, null, null);
    } catch (SocketTimeoutException ste) {
        // May also see this with NIO
        // Make sure the test passes if we do
        rc = 503;
    }
    Assert.assertEquals(503, rc);
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) SocketTimeoutException(java.net.SocketTimeoutException) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterServlet(org.apache.catalina.startup.TesterServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 20 with TesterServlet

use of org.apache.catalina.startup.TesterServlet in project tomcat by apache.

the class TestNonLoginAndBasicAuthenticator method setUpNonLogin.

private void setUpNonLogin() throws Exception {
    // Must have a real docBase for webapps - just use temp
    nonloginContext = tomcat.addContext(CONTEXT_PATH_NOLOGIN, System.getProperty("java.io.tmpdir"));
    // Add protected servlet to the context
    Tomcat.addServlet(nonloginContext, "TesterServlet1", new TesterServlet());
    nonloginContext.addServletMappingDecoded(URI_PROTECTED, "TesterServlet1");
    SecurityCollection collection1 = new SecurityCollection();
    collection1.addPatternDecoded(URI_PROTECTED);
    SecurityConstraint sc1 = new SecurityConstraint();
    sc1.addAuthRole(ROLE);
    sc1.addCollection(collection1);
    nonloginContext.addConstraint(sc1);
    // Add unprotected servlet to the context
    Tomcat.addServlet(nonloginContext, "TesterServlet2", new TesterServlet());
    nonloginContext.addServletMappingDecoded(URI_PUBLIC, "TesterServlet2");
    SecurityCollection collection2 = new SecurityCollection();
    collection2.addPatternDecoded(URI_PUBLIC);
    SecurityConstraint sc2 = new SecurityConstraint();
    // do not add a role - which signals access permitted without one
    sc2.addCollection(collection2);
    nonloginContext.addConstraint(sc2);
    // Configure the authenticator and inherit the Realm from Engine
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("NONE");
    nonloginContext.setLoginConfig(lc);
    AuthenticatorBase nonloginAuthenticator = new NonLoginAuthenticator();
    nonloginContext.getPipeline().addValve(nonloginAuthenticator);
}
Also used : LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Aggregations

TesterServlet (org.apache.catalina.startup.TesterServlet)29 Context (org.apache.catalina.Context)25 Tomcat (org.apache.catalina.startup.Tomcat)21 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)14 Test (org.junit.Test)14 SimpleHttpClient (org.apache.catalina.startup.SimpleHttpClient)13 AsyncContext (jakarta.servlet.AsyncContext)12 LoginConfig (org.apache.tomcat.util.descriptor.web.LoginConfig)6 SecurityCollection (org.apache.tomcat.util.descriptor.web.SecurityCollection)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)6 LoginConfig (org.apache.catalina.deploy.LoginConfig)5 SecurityCollection (org.apache.catalina.deploy.SecurityCollection)5 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)5 Wrapper (org.apache.catalina.Wrapper)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 IOException (java.io.IOException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 List (java.util.List)2 AprLifecycleListener (org.apache.catalina.core.AprLifecycleListener)2 StandardServer (org.apache.catalina.core.StandardServer)2