Search in sources :

Example 6 with ErrorPage

use of org.apache.tomcat.util.descriptor.web.ErrorPage in project tomcat by apache.

the class TestStandardContextValve method testBug51653a.

@Test
public void testBug51653a() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Traces order of events across multiple components
    StringBuilder trace = new StringBuilder();
    //Add the error page
    Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
    ctx.addServletMappingDecoded("/error", "errorPage");
    // And the handling for 404 responses
    ErrorPage errorPage = new ErrorPage();
    errorPage.setErrorCode(Response.SC_NOT_FOUND);
    errorPage.setLocation("/error");
    ctx.addErrorPage(errorPage);
    // Add the request listener
    Bug51653RequestListener reqListener = new Bug51653RequestListener(trace);
    ((StandardContext) ctx).addApplicationEventListener(reqListener);
    tomcat.start();
    // Request a page that does not exist
    int rc = getUrl("http://localhost:" + getPort() + "/invalid", new ByteChunk(), null);
    // Need to allow time (but not too long in case the test fails) for
    // ServletRequestListener to complete
    int i = 20;
    while (i > 0) {
        if (trace.toString().endsWith("Destroy")) {
            break;
        }
        Thread.sleep(250);
        i--;
    }
    assertEquals(Response.SC_NOT_FOUND, rc);
    assertEquals("InitErrorDestroy", trace.toString());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with ErrorPage

use of org.apache.tomcat.util.descriptor.web.ErrorPage in project tomcat by apache.

the class TestStandardHostValve method testInvalidErrorPage.

@Test(expected = IllegalArgumentException.class)
public void testInvalidErrorPage() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add a broken error page configuration
    ErrorPage errorPage500 = new ErrorPage();
    errorPage500.setErrorCode("java.lang.Exception");
    errorPage500.setLocation("/report/500");
    ctx.addErrorPage(errorPage500);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 8 with ErrorPage

use of org.apache.tomcat.util.descriptor.web.ErrorPage in project tomcat by apache.

the class ContextConfig method configureStop.

/**
     * Process a "stop" event for this Context.
     */
protected synchronized void configureStop() {
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("contextConfig.stop"));
    }
    int i;
    // Removing children
    Container[] children = context.findChildren();
    for (i = 0; i < children.length; i++) {
        context.removeChild(children[i]);
    }
    // Removing application parameters
    /*
        ApplicationParameter[] applicationParameters =
            context.findApplicationParameters();
        for (i = 0; i < applicationParameters.length; i++) {
            context.removeApplicationParameter
                (applicationParameters[i].getName());
        }
        */
    // Removing security constraints
    SecurityConstraint[] securityConstraints = context.findConstraints();
    for (i = 0; i < securityConstraints.length; i++) {
        context.removeConstraint(securityConstraints[i]);
    }
    // Removing Ejbs
    /*
        ContextEjb[] contextEjbs = context.findEjbs();
        for (i = 0; i < contextEjbs.length; i++) {
            context.removeEjb(contextEjbs[i].getName());
        }
        */
    // Removing environments
    /*
        ContextEnvironment[] contextEnvironments = context.findEnvironments();
        for (i = 0; i < contextEnvironments.length; i++) {
            context.removeEnvironment(contextEnvironments[i].getName());
        }
        */
    // Removing errors pages
    ErrorPage[] errorPages = context.findErrorPages();
    for (i = 0; i < errorPages.length; i++) {
        context.removeErrorPage(errorPages[i]);
    }
    // Removing filter defs
    FilterDef[] filterDefs = context.findFilterDefs();
    for (i = 0; i < filterDefs.length; i++) {
        context.removeFilterDef(filterDefs[i]);
    }
    // Removing filter maps
    FilterMap[] filterMaps = context.findFilterMaps();
    for (i = 0; i < filterMaps.length; i++) {
        context.removeFilterMap(filterMaps[i]);
    }
    // Removing local ejbs
    /*
        ContextLocalEjb[] contextLocalEjbs = context.findLocalEjbs();
        for (i = 0; i < contextLocalEjbs.length; i++) {
            context.removeLocalEjb(contextLocalEjbs[i].getName());
        }
        */
    // Removing Mime mappings
    String[] mimeMappings = context.findMimeMappings();
    for (i = 0; i < mimeMappings.length; i++) {
        context.removeMimeMapping(mimeMappings[i]);
    }
    // Removing parameters
    String[] parameters = context.findParameters();
    for (i = 0; i < parameters.length; i++) {
        context.removeParameter(parameters[i]);
    }
    // Removing resource env refs
    /*
        String[] resourceEnvRefs = context.findResourceEnvRefs();
        for (i = 0; i < resourceEnvRefs.length; i++) {
            context.removeResourceEnvRef(resourceEnvRefs[i]);
        }
        */
    // Removing resource links
    /*
        ContextResourceLink[] contextResourceLinks =
            context.findResourceLinks();
        for (i = 0; i < contextResourceLinks.length; i++) {
            context.removeResourceLink(contextResourceLinks[i].getName());
        }
        */
    // Removing resources
    /*
        ContextResource[] contextResources = context.findResources();
        for (i = 0; i < contextResources.length; i++) {
            context.removeResource(contextResources[i].getName());
        }
        */
    // Removing security role
    String[] securityRoles = context.findSecurityRoles();
    for (i = 0; i < securityRoles.length; i++) {
        context.removeSecurityRole(securityRoles[i]);
    }
    // Removing servlet mappings
    String[] servletMappings = context.findServletMappings();
    for (i = 0; i < servletMappings.length; i++) {
        context.removeServletMapping(servletMappings[i]);
    }
    // FIXME : Removing status pages
    // Removing welcome files
    String[] welcomeFiles = context.findWelcomeFiles();
    for (i = 0; i < welcomeFiles.length; i++) {
        context.removeWelcomeFile(welcomeFiles[i]);
    }
    // Removing wrapper lifecycles
    String[] wrapperLifecycles = context.findWrapperLifecycles();
    for (i = 0; i < wrapperLifecycles.length; i++) {
        context.removeWrapperLifecycle(wrapperLifecycles[i]);
    }
    // Removing wrapper listeners
    String[] wrapperListeners = context.findWrapperListeners();
    for (i = 0; i < wrapperListeners.length; i++) {
        context.removeWrapperListener(wrapperListeners[i]);
    }
    // Remove (partially) folders and files created by antiLocking
    if (antiLockingDocBase != null) {
        // No need to log failure - it is expected in this case
        ExpandWar.delete(antiLockingDocBase, false);
    }
    // Reset ServletContextInitializer scanning
    initializerClassMap.clear();
    typeInitializerMap.clear();
    ok = true;
}
Also used : ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) Container(org.apache.catalina.Container)

Example 9 with ErrorPage

use of org.apache.tomcat.util.descriptor.web.ErrorPage in project tomcat by apache.

the class ContextMBean method findErrorPages.

/**
     * Return the set of defined error pages for all specified error codes
     * and exception types.
     * @return a string array with a representation of each error page
     * @throws MBeanException propagated from the managed resource access
     */
public String[] findErrorPages() throws MBeanException {
    Context context = doGetManagedResource();
    ErrorPage[] pages = context.findErrorPages();
    String[] stringPages = new String[pages.length];
    for (int counter = 0; counter < pages.length; counter++) {
        stringPages[counter] = pages[counter].toString();
    }
    return stringPages;
}
Also used : Context(org.apache.catalina.Context) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 10 with ErrorPage

use of org.apache.tomcat.util.descriptor.web.ErrorPage in project tomcat by apache.

the class TestStandardContextValve method testBug51653b.

@Test
public void testBug51653b() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Traces order of events across multiple components
    StringBuilder trace = new StringBuilder();
    // Add the page that generates the error
    Tomcat.addServlet(ctx, "test", new Bug51653ErrorTrigger());
    ctx.addServletMappingDecoded("/test", "test");
    // Add the error page
    Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
    ctx.addServletMappingDecoded("/error", "errorPage");
    // And the handling for 404 responses
    ErrorPage errorPage = new ErrorPage();
    errorPage.setErrorCode(Response.SC_NOT_FOUND);
    errorPage.setLocation("/error");
    ctx.addErrorPage(errorPage);
    // Add the request listener
    Bug51653RequestListener reqListener = new Bug51653RequestListener(trace);
    ((StandardContext) ctx).addApplicationEventListener(reqListener);
    tomcat.start();
    // Request a page that does not exist
    int rc = getUrl("http://localhost:" + getPort() + "/test", new ByteChunk(), null);
    // Need to allow time (but not too long in case the test fails) for
    // ServletRequestListener to complete
    int i = 20;
    while (i > 0) {
        if (trace.toString().endsWith("Destroy")) {
            break;
        }
        Thread.sleep(250);
        i--;
    }
    assertEquals(Response.SC_NOT_FOUND, rc);
    assertEquals("InitErrorDestroy", trace.toString());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ErrorPage (org.apache.tomcat.util.descriptor.web.ErrorPage)15 Context (org.apache.catalina.Context)11 Tomcat (org.apache.catalina.startup.Tomcat)8 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)6 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)6 Test (org.junit.Test)6 Wrapper (org.apache.catalina.Wrapper)5 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)4 IOException (java.io.IOException)3 AsyncContext (javax.servlet.AsyncContext)3 TesterContext (org.apache.tomcat.unittest.TesterContext)3 ServletContext (javax.servlet.ServletContext)2 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)2 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)2 ClientAbortException (org.apache.catalina.connector.ClientAbortException)2 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)2 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)2 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)2 File (java.io.File)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1