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");
}
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
}
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);
}
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);
}
}
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);
}
}
Aggregations