Search in sources :

Example 1 with ErrorPage

use of org.apache.catalina.deploy.ErrorPage in project Payara by payara.

the class ApplicationDispatcherForward method status.

/*
     * Searches and processes a custom error page for the given status code.
     *
     * This method attempts to map the given status code to an error page,
     * using the mappings of the given context or those of the host on which
     * the given context has been deployed.
     *
     * If a match is found using the context mappings, the request is forwarded
     * to the error page. Otherwise, if a match is found using the host 
     * mappings, the contents of the error page are returned. If no match is
     * found, no action is taken.
     *
     * @return true if a matching error page was found, false otherwise
     */
private static boolean status(HttpServletRequest request, HttpServletResponse response, Context context, Wrapper wrapper, int statusCode) {
    RequestFacadeHelper reqFacHelper = RequestFacadeHelper.getInstance(request);
    /*
         * Attempt error-page mapping only if response.sendError(), as
         * opposed to response.setStatus(), was called.
         */
    if (reqFacHelper == null || !reqFacHelper.isResponseError()) {
        return false;
    }
    boolean matchFound = false;
    ErrorPage errorPage = context.findErrorPage(statusCode);
    if (errorPage != null) {
        matchFound = true;
        // Prevent infinite loop
        String requestPath = (String) request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR);
        if (requestPath == null || !requestPath.equals(errorPage.getLocation())) {
            String message = reqFacHelper.getResponseMessage();
            if (message == null) {
                message = "";
            } else {
                message = HtmlEntityEncoder.encodeXSS(message);
            }
            prepareRequestForDispatch(request, wrapper, errorPage.getLocation(), statusCode, message);
            custom(request, response, errorPage, context);
        }
    } else {
        errorPage = ((StandardHost) context.getParent()).findErrorPage(statusCode);
        if (errorPage != null) {
            matchFound = true;
            try {
                serveErrorPage(response, errorPage, statusCode);
            } catch (Exception e) {
                String msg = MessageFormat.format(rb.getString(LogFacade.EXCEPTION_PROCESSING), errorPage);
                log.log(Level.WARNING, msg, e);
            }
        }
    }
    return matchFound;
}
Also used : ErrorPage(org.apache.catalina.deploy.ErrorPage)

Example 2 with ErrorPage

use of org.apache.catalina.deploy.ErrorPage in project Payara by payara.

the class StandardHostValve method throwable.

// ------------------------------------------------------ Protected Methods
/**
 * Handle the specified Throwable encountered while processing
 * the specified Request to produce the specified Response.  Any
 * exceptions that occur during generation of the exception report are
 * logged and swallowed.
 *
 * @param request The request being processed
 * @param response The response being generated
 * @param throwable The throwable that occurred (which possibly wraps
 *  a root cause exception
 */
protected void throwable(Request request, Response response, Throwable throwable) {
    Context context = request.getContext();
    if (context == null)
        return;
    Throwable realError = throwable;
    if (realError instanceof ServletException) {
        realError = ((ServletException) realError).getRootCause();
        if (realError == null) {
            realError = throwable;
        }
    }
    // If this is an aborted request from a client just log it and return
    if (realError instanceof ClientAbortException) {
        if (log.isLoggable(Level.FINE)) {
            log.log(Level.FINE, LogFacade.REMOTE_CLIENT_ABORTED_EXCEPTION, realError.getCause().getMessage());
        }
        return;
    }
    ErrorPage errorPage = findErrorPage(context, throwable);
    if ((errorPage == null) && (realError != throwable)) {
        errorPage = findErrorPage(context, realError);
    }
    if (errorPage != null) {
        dispatchToErrorPage(request, response, errorPage, throwable, realError, 0);
    } else if (context.getDefaultErrorPage() != null) {
        dispatchToErrorPage(request, response, context.getDefaultErrorPage(), throwable, realError, 0);
    } else {
        // A custom error-page has not been defined for the exception
        // that was thrown during request processing. Check if an
        // error-page for error code 500 was specified and if so,
        // send that page back as the response.
        ServletResponse sresp = (ServletResponse) response;
        /* GlassFish 6386229
            if (sresp instanceof HttpServletResponse) {
                ((HttpServletResponse) sresp).setStatus(
                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                // The response is an error
                response.setError();

                status(request, response);
            }
            */
        // START GlassFish 6386229
        ((HttpServletResponse) sresp).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // The response is an error
        response.setError();
        status(request, response);
    // END GlassFish 6386229
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ErrorPage(org.apache.catalina.deploy.ErrorPage) ClientAbortException(org.apache.catalina.connector.ClientAbortException)

Example 3 with ErrorPage

use of org.apache.catalina.deploy.ErrorPage in project tomcat70 by apache.

the class ContextMBean method findErrorPages.

/**
 * Return the set of defined error pages for all specified error codes
 * and exception types.
 */
public String[] findErrorPages() throws MBeanException {
    Context context;
    try {
        context = (Context) getManagedResource();
    } catch (InstanceNotFoundException e) {
        throw new MBeanException(e);
    } catch (RuntimeOperationsException e) {
        throw new MBeanException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new MBeanException(e);
    }
    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.catalina.deploy.ErrorPage) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 4 with ErrorPage

use of org.apache.catalina.deploy.ErrorPage in project tomcat70 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.catalina.deploy.ErrorPage) FilterDef(org.apache.catalina.deploy.FilterDef) FilterMap(org.apache.catalina.deploy.FilterMap) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) Container(org.apache.catalina.Container)

Example 5 with ErrorPage

use of org.apache.catalina.deploy.ErrorPage in project tomcat70 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.addServletMapping("/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--;
    }
    Assert.assertEquals(Response.SC_NOT_FOUND, rc);
    Assert.assertEquals("InitErrorDestroy", trace.toString());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ErrorPage(org.apache.catalina.deploy.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ErrorPage (org.apache.catalina.deploy.ErrorPage)20 Context (org.apache.catalina.Context)11 Tomcat (org.apache.catalina.startup.Tomcat)8 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)6 Test (org.junit.Test)6 Wrapper (org.apache.catalina.Wrapper)5 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)5 ClientAbortException (org.apache.catalina.connector.ClientAbortException)4 IOException (java.io.IOException)3 AsyncContext (javax.servlet.AsyncContext)3 ServletContext (javax.servlet.ServletContext)3 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)3 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)2 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 FilterMap (org.apache.catalina.deploy.FilterMap)2 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)2 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1