private void addJettyErrorPages(ErrorHandler errorHandler, Collection<ErrorPage> errorPages) { if (errorHandler instanceof ErrorPageErrorHandler) { ErrorPageErrorHandler handler = (ErrorPageErrorHandler) errorHandler; for (ErrorPage errorPage : errorPages) { if (errorPage.isGlobal()) { handler.addErrorPage(ErrorPageErrorHandler.GLOBAL_ERROR_PAGE, errorPage.getPath()); } else { if (errorPage.getExceptionName() != null) { handler.addErrorPage(errorPage.getExceptionName(), errorPage.getPath()); } else { handler.addErrorPage(errorPage.getStatusCode(), errorPage.getPath()); } } } } }
@Before public void setUp() throws Exception { TemplateHelper templateHelper = new TemplateHelperImpl(new ApplicationVersionSupport() { @Override public String getEdition() { return "Test"; } }, new VelocityEngine()); ServletContextHandler context = new ServletContextHandler(); context.addServlet(new ServletHolder(new ErrorPageServlet(templateHelper)), "/error.html"); context.addServlet(new ServletHolder(new BadServlet()), "/bad/*"); ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler(); errorHandler.addErrorPage(GLOBAL_ERROR_PAGE, "/error.html"); context.setErrorHandler(errorHandler); BaseUrlHolder.set("http://127.0.0.1"); server = new Server(0); server.setHandler(context); server.start(); port = ((ServerConnector) server.getConnectors()[0]).getLocalPort(); }
private void setupErrorPages(WebAppContext context, List<ErrorHandler> errorHandlers) { if (errorHandlers != null) { ErrorPageErrorHandler errorHandler = (ErrorPageErrorHandler)context.getErrorHandler(); for (ErrorHandler handler : errorHandlers) { String page = handler.getFile(); if (!StringUtils.startsWith(page, "/")) { page = "/" + page; } if (handler.getErrorCode() > 0) { errorHandler.addErrorPage(handler.getErrorCode(), page); } else { errorHandler.addErrorPage(Integer.MIN_VALUE, Integer.MAX_VALUE, page); } } } }
protected Handler createHandler(Set<MappedServlet> servlets, Set<MappedFilter> filters, Set<MappedListener> listeners) { int options = 0; if (sessions) { options |= ServletContextHandler.SESSIONS; } ServletContextHandler handler = new ServletContextHandler(options); handler.setContextPath(context); if (params != null) { params.forEach((k, v) -> handler.setInitParameter(k, v)); } if (staticResourceBase != null) { handler.setResourceBase(staticResourceBase.getUrl().toExternalForm()); } if (compression) { handler.setGzipHandler(createGzipHandler()); } if (errorPages != null) { ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler(); errorPages.forEach((statusCode, location) -> errorHandler.addErrorPage(statusCode, location)); handler.setErrorHandler(errorHandler); } installListeners(handler, listeners); installServlets(handler, servlets); installFilters(handler, filters); return handler; }
private static ErrorHandler generateErrorHandler() { ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler(); errorHandler.addErrorPage(Throwable.class, "/error"); return errorHandler; }