Search in sources :

Example 16 with ErrorPage

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

the class TestStandardHostValve method testErrorPageHandling.

@Test
public void testErrorPageHandling() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add the error page
    Tomcat.addServlet(ctx, "error", new ErrorServlet());
    ctx.addServletMapping("/error", "error");
    // Add the error handling page
    Tomcat.addServlet(ctx, "report", new ReportServlet());
    ctx.addServletMapping("/report/*", "report");
    // And the handling for 500 responses
    ErrorPage errorPage500 = new ErrorPage();
    errorPage500.setErrorCode(Response.SC_INTERNAL_SERVER_ERROR);
    errorPage500.setLocation("/report/500");
    ctx.addErrorPage(errorPage500);
    // And the default error handling
    ErrorPage errorPageDefault = new ErrorPage();
    errorPageDefault.setLocation("/report/default");
    ctx.addErrorPage(errorPageDefault);
    tomcat.start();
    doTestErrorPageHandling(500, "/500");
    doTestErrorPageHandling(501, "/default");
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ErrorPage(org.apache.catalina.deploy.ErrorPage) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 17 with ErrorPage

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

the class StandardHostValve method status.

/**
 * Handle the HTTP status code (and corresponding message) generated
 * while processing the specified Request to produce the specified
 * Response.  Any exceptions that occur during generation of the error
 * report are logged and swallowed.
 *
 * @param request The request being processed
 * @param response The response being generated
 */
protected void status(Request request, Response response) {
    // Handle a custom error page for this status code
    Context context = request.getContext();
    if (context == null)
        return;
    /*
         * Only look for error pages when isError() is set.
         * isError() is set when response.sendError() is invoked.
         */
    if (!response.isError()) {
        return;
    }
    int statusCode = ((HttpResponse) response).getStatus();
    ErrorPage errorPage = context.findErrorPage(statusCode);
    if (errorPage != null) {
        if (errorPage.getLocation() != null) {
            File file = new File(context.getDocBase(), errorPage.getLocation());
            if (!file.exists()) {
                File file2 = new File(errorPage.getLocation());
                if (!file2.exists()) {
                    boolean fileExists = false;
                    for (String servletMapping : ((StandardHost) getContainer()).findDeployedApp(context.getPath()).findServletMappings()) {
                        if (URLPattern.match(servletMapping, errorPage.getLocation())) {
                            fileExists = true;
                            break;
                        }
                    }
                    if (!fileExists) {
                        for (FilterMap mapping : ((StandardHost) getContainer()).findDeployedApp(context.getPath()).findFilterMaps()) {
                            if (mapping.getDispatcherTypes().contains(DispatcherType.ERROR) && URLPattern.match(mapping.getURLPattern(), errorPage.getLocation())) {
                                fileExists = true;
                                break;
                            }
                        }
                        if (!fileExists) {
                            log.log(Level.WARNING, LogFacade.ERROR_PAGE_NOT_EXIST, new Object[] { file.getAbsolutePath(), file2.getAbsolutePath() });
                        }
                    }
                }
            }
        }
        setErrorPageContentType(response, errorPage.getLocation(), context);
        dispatchToErrorPage(request, response, errorPage, null, null, statusCode);
    } else if (statusCode >= 400 && statusCode < 600 && context.getDefaultErrorPage() != null) {
        dispatchToErrorPage(request, response, context.getDefaultErrorPage(), null, null, statusCode);
    } else // START SJSAS 6324911
    {
        errorPage = ((StandardHost) getContainer()).findErrorPage(statusCode);
        if (errorPage != null) {
            if (errorPage.getLocation() != null) {
                File file = new File(context.getDocBase(), errorPage.getLocation());
                if (!file.exists()) {
                    File file2 = new File(errorPage.getLocation());
                    if (!file2.exists()) {
                        log.log(Level.WARNING, LogFacade.ERROR_PAGE_NOT_EXIST, new Object[] { file.getAbsolutePath(), file2.getAbsolutePath() });
                    }
                }
            }
            try {
                setErrorPageContentType(response, errorPage.getLocation(), context);
                handleHostErrorPage(response, errorPage, statusCode);
            } catch (Exception e) {
                log("Exception processing " + errorPage, e);
            }
        }
    }
// END SJSAS 6324911
}
Also used : ErrorPage(org.apache.catalina.deploy.ErrorPage) FilterMap(org.apache.catalina.deploy.FilterMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException)

Example 18 with ErrorPage

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

the class StandardHostValve method findErrorPage.

/**
 * Find and return the ErrorPage instance for the specified exception's
 * class, or an ErrorPage instance for the closest superclass for which
 * there is such a definition.  If no associated ErrorPage instance is
 * found, return <code>null</code>.
 *
 * @param context The Context in which to search
 * @param exception The exception for which to find an ErrorPage
 */
protected static ErrorPage findErrorPage(Context context, Throwable exception) {
    if (exception == null)
        return (null);
    Class<?> clazz = exception.getClass();
    String name = clazz.getName();
    while (!Object.class.equals(clazz)) {
        ErrorPage errorPage = context.findErrorPage(name);
        if (errorPage != null)
            return (errorPage);
        clazz = clazz.getSuperclass();
        if (clazz == null)
            break;
        name = clazz.getName();
    }
    return (null);
}
Also used : ErrorPage(org.apache.catalina.deploy.ErrorPage)

Example 19 with ErrorPage

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

the class RequestFilterValve method handleError.

private void handleError(Request request, Response response, int statusCode) throws IOException {
    ServletRequest sreq = request.getRequest();
    ServletResponse sres = response.getResponse();
    HttpServletResponse hres = (HttpServletResponse) sres;
    ErrorPage errorPage = null;
    if (getContainer() instanceof StandardHost) {
        errorPage = ((StandardHost) getContainer()).findErrorPage(statusCode);
    } else if (getContainer() instanceof StandardContext) {
        errorPage = ((StandardContext) getContainer()).findErrorPage(statusCode);
    }
    if (errorPage != null) {
        try {
            hres.setStatus(statusCode);
            ServletContext servletContext = request.getContext().getServletContext();
            ApplicationDispatcher dispatcher = (ApplicationDispatcher) servletContext.getRequestDispatcher(errorPage.getLocation());
            if (hres.isCommitted()) {
                // Response is committed - including the error page is the
                // best we can do
                dispatcher.include(sreq, sres);
            } else {
                // Reset the response (keeping the real error code and message)
                response.resetBuffer(true);
                dispatcher.dispatch(sreq, sres, DispatcherType.ERROR);
                // If we forward, the response is suspended again
                response.setSuspended(false);
            }
            sres.flushBuffer();
        } catch (Throwable t) {
            if (log.isLoggable(Level.INFO)) {
                String msg = MessageFormat.format(rb.getString(LogFacade.CANNOT_PROCESS_ERROR_PAGE_INFO), errorPage.getLocation());
                log.log(Level.INFO, msg, t);
            }
        }
    } else {
        hres.sendError(statusCode);
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ApplicationDispatcher(org.apache.catalina.core.ApplicationDispatcher) ErrorPage(org.apache.catalina.deploy.ErrorPage) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext)

Example 20 with ErrorPage

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

the class VirtualServer method configureErrorPage.

/**
 * Configures this VirtualServer with its send-error properties.
 */
void configureErrorPage() {
    ErrorPage errorPage = null;
    List<Property> props = vsBean.getProperty();
    if (props == null) {
        return;
    }
    for (Property prop : props) {
        String propName = prop.getName();
        String propValue = prop.getValue();
        if (propName == null || propValue == null) {
            _logger.log(Level.WARNING, LogFacade.NULL_VIRTUAL_SERVER_PROPERTY, getID());
            continue;
        }
        if (!propName.startsWith("send-error_")) {
            continue;
        }
        /*
             * Validate the prop value
             */
        String path = null;
        String reason = null;
        String status = null;
        String[] errorParams = propValue.split(" ");
        for (String errorParam : errorParams) {
            if (errorParam.startsWith("path=")) {
                if (path != null) {
                    _logger.log(Level.WARNING, LogFacade.SEND_ERROR_MULTIPLE_ELEMENT, new Object[] { propValue, getID(), "path" });
                }
                path = errorParam.substring("path=".length());
            }
            if (errorParam.startsWith("reason=")) {
                if (reason != null) {
                    _logger.log(Level.WARNING, LogFacade.SEND_ERROR_MULTIPLE_ELEMENT, new Object[] { propValue, getID(), "reason" });
                }
                reason = errorParam.substring("reason=".length());
            }
            if (errorParam.startsWith("code=")) {
                if (status != null) {
                    _logger.log(Level.WARNING, LogFacade.SEND_ERROR_MULTIPLE_ELEMENT, new Object[] { propValue, getID(), "code" });
                }
                status = errorParam.substring("code=".length());
            }
        }
        if (path == null || path.length() == 0) {
            _logger.log(Level.WARNING, LogFacade.SEND_ERROR_MISSING_PATH, new Object[] { propValue, getID() });
        }
        errorPage = new ErrorPage();
        errorPage.setLocation(path);
        errorPage.setErrorCode(status);
        errorPage.setReason(reason);
        addErrorPage(errorPage);
    }
}
Also used : ErrorPage(org.apache.catalina.deploy.ErrorPage) Property(org.jvnet.hk2.config.types.Property)

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