Search in sources :

Example 11 with Context

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

the class StandardContextSF method store.

/**
     * Store a Context as Separate file as configFile value from context exists.
     * filename can be relative to catalina.base.
     *
     * @see org.apache.catalina.storeconfig.IStoreFactory#store(java.io.PrintWriter,
     *      int, java.lang.Object)
     */
@Override
public void store(PrintWriter aWriter, int indent, Object aContext) throws Exception {
    if (aContext instanceof StandardContext) {
        StoreDescription desc = getRegistry().findDescription(aContext.getClass());
        if (desc.isStoreSeparate()) {
            URL configFile = ((StandardContext) aContext).getConfigFile();
            if (configFile != null) {
                if (desc.isExternalAllowed()) {
                    if (desc.isBackup())
                        storeWithBackup((StandardContext) aContext);
                    else
                        storeContextSeparate(aWriter, indent, (StandardContext) aContext);
                    return;
                }
            } else if (desc.isExternalOnly()) {
                // Set a configFile so that the configuration is actually saved
                Context context = ((StandardContext) aContext);
                Host host = (Host) context.getParent();
                File configBase = host.getConfigBaseFile();
                ContextName cn = new ContextName(context.getName(), false);
                String baseName = cn.getBaseName();
                File xml = new File(configBase, baseName + ".xml");
                context.setConfigFile(xml.toURI().toURL());
                if (desc.isBackup())
                    storeWithBackup((StandardContext) aContext);
                else
                    storeContextSeparate(aWriter, indent, (StandardContext) aContext);
                return;
            }
        }
    }
    super.store(aWriter, indent, aContext);
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext) Host(org.apache.catalina.Host) File(java.io.File) URL(java.net.URL) ContextName(org.apache.catalina.util.ContextName)

Example 12 with Context

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

the class Tomcat method addContext.

/**
     * @param host The host in which the context will be deployed
     * @param contextPath The context mapping to use, "" for root context.
     * @param contextName The context name
     * @param dir Base directory for the context, for static files.
     *  Must exist, relative to the server home
     * @return the deployed context
     * @see #addContext(String, String)
     */
public Context addContext(Host host, String contextPath, String contextName, String dir) {
    silence(host, contextName);
    Context ctx = createContext(host, contextPath);
    ctx.setName(contextName);
    ctx.setPath(contextPath);
    ctx.setDocBase(dir);
    ctx.addLifecycleListener(new FixContextListener());
    if (host == null) {
        getHost().addChild(ctx);
    } else {
        host.addChild(ctx);
    }
    return ctx;
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext)

Example 13 with Context

use of org.apache.catalina.Context in project tomcat 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.addServletMappingDecoded(URI, "TesterServlet");
    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded(URI);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctxt.addConstraint(sc);
    // Configure the Realm
    TesterMapRealm realm = new TesterMapRealm();
    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 : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Tomcat(org.apache.catalina.startup.Tomcat) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) 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)

Example 14 with Context

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

the class TestFormAuthenticator method testTimeoutWithoutCookies.

@Test
public void testTimeoutWithoutCookies() throws Exception {
    String protectedUri = doTest("GET", "GET", NO_100_CONTINUE, CLIENT_NO_COOKIES, SERVER_USE_COOKIES, SERVER_FREEZE_SESSID);
    // Force session to expire one second from now
    Context context = (Context) getTomcatInstance().getHost().findChildren()[0];
    forceSessionMaxInactiveInterval(context, SHORT_SESSION_TIMEOUT_SECS);
    // wait long enough for my session to expire
    Thread.sleep(TIMEOUT_DELAY_MSECS);
    // then try to continue using the expired session to get the
    // protected resource once more.
    // should get login challenge or timeout status 408
    doTestProtected("GET", protectedUri, NO_100_CONTINUE, FormAuthClient.LOGIN_REQUIRED, 1);
}
Also used : Context(org.apache.catalina.Context) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 15 with Context

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

the class TestAsyncContextImpl method testCommitOnComplete.

@Test
public void testCommitOnComplete() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncStatusServlet asyncStatusServlet = new AsyncStatusServlet(HttpServletResponse.SC_BAD_REQUEST);
    Wrapper wrapper = Tomcat.addServlet(ctx, "asyncStatusServlet", asyncStatusServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/asyncStatusServlet", "asyncStatusServlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/asyncStatusServlet");
    int rc = getUrl(url.toString(), new ByteChunk(), null);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
    // 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) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24