use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.
the class AsyncListenerTest method test_StartAsync_Throw_OnError_SendError_CustomErrorPage.
@Test
public void test_StartAsync_Throw_OnError_SendError_CustomErrorPage() throws Exception {
test_StartAsync_Throw_OnError(event -> {
HttpServletResponse response = (HttpServletResponse) event.getAsyncContext().getResponse();
response.sendError(HttpStatus.BAD_GATEWAY_502);
});
// Add a custom error page.
ErrorHandler errorHandler = new ErrorHandler() {
@Override
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri) throws IOException {
writer.write("CUSTOM\n");
super.writeErrorPageMessage(request, writer, code, message, uri);
}
};
server.setErrorHandler(errorHandler);
String httpResponse = connector.getResponse("" + "GET /ctx/path HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n", 10, TimeUnit.MINUTES);
assertThat(httpResponse, containsString("HTTP/1.1 502 "));
assertThat(httpResponse, containsString("CUSTOM"));
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project spring-boot by spring-projects.
the class JettyServletWebServerFactory method getErrorPageConfiguration.
/**
* Create a configuration object that adds error handlers.
* @return a configuration object for adding error pages
*/
private Configuration getErrorPageConfiguration() {
return new AbstractConfiguration() {
@Override
public void configure(WebAppContext context) throws Exception {
ErrorHandler errorHandler = context.getErrorHandler();
context.setErrorHandler(new JettyEmbeddedErrorHandler(errorHandler));
addJettyErrorPages(errorHandler, getErrorPages());
}
};
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project cxf by apache.
the class JettyHTTPServerEngine method createServer.
private Server createServer() {
Server s = null;
if (connector != null && connector.getServer() != null) {
s = connector.getServer();
}
if (threadPool != null) {
try {
if (s == null) {
s = new Server(threadPool);
} else {
s.addBean(threadPool);
}
} catch (Exception e) {
// ignore
}
}
if (s == null) {
s = new Server();
}
// need an error handler that won't leak information about the exception
// back to the client.
ErrorHandler eh = new ErrorHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String msg = (String) request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
if (StringUtils.isEmpty(msg) || msg.contains("org.apache.cxf.interceptor.Fault")) {
msg = HttpStatus.getMessage(response.getStatus());
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
}
if (response instanceof Response) {
((Response) response).setStatusWithReason(response.getStatus(), msg);
}
super.handle(target, baseRequest, request, response);
}
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
super.writeErrorPage(request, writer, code, message, false);
}
};
s.addBean(eh);
return s;
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project yacy_grid_mcp by yacy.
the class APIServer method open.
private static void open(int port, String htmlPath) throws IOException {
try {
QueuedThreadPool pool = new QueuedThreadPool();
pool.setMaxThreads(500);
server = new Server(pool);
ServerConnector connector = new ServerConnector(server);
HttpConfiguration http_config = new HttpConfiguration();
http_config.setRequestHeaderSize(65536);
http_config.setResponseHeaderSize(65536);
connector.addConnectionFactory(new HttpConnectionFactory(http_config));
connector.setPort(port);
connector.setName("httpd:" + port);
// timout in ms when no bytes send / received
connector.setIdleTimeout(20000);
server.addConnector(connector);
server.setStopAtShutdown(true);
// add services
ServletContextHandler servletHandler = new ServletContextHandler();
for (Class<? extends Servlet> service : services) try {
servletHandler.addServlet(service, ((APIHandler) (service.getConstructor().newInstance())).getAPIPath());
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Data.logger.warn(service.getName() + " instantiation error", e);
}
ErrorHandler errorHandler = new ErrorHandler();
errorHandler.setShowStacks(true);
servletHandler.setErrorHandler(errorHandler);
HandlerList handlerlist2 = new HandlerList();
if (htmlPath == null) {
handlerlist2.setHandlers(new Handler[] { servletHandler, new DefaultHandler() });
} else {
FileHandler fileHandler = new FileHandler();
fileHandler.setDirectoriesListed(true);
fileHandler.setWelcomeFiles(new String[] { "index.html" });
fileHandler.setResourceBase(htmlPath);
handlerlist2.setHandlers(new Handler[] { fileHandler, servletHandler, new DefaultHandler() });
}
server.setHandler(handlerlist2);
server.start();
} catch (Throwable e) {
throw new IOException(e.getMessage());
}
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project dropwizard by dropwizard.
the class AbstractServerFactory method buildServer.
protected Server buildServer(LifecycleEnvironment lifecycle, ThreadPool threadPool) {
final Server server = new Server(threadPool);
server.addLifeCycleListener(buildSetUIDListener());
lifecycle.attach(server);
final ErrorHandler errorHandler = new ErrorHandler();
errorHandler.setServer(server);
errorHandler.setShowStacks(false);
server.addBean(errorHandler);
server.setStopAtShutdown(true);
server.setStopTimeout(shutdownGracePeriod.toMilliseconds());
server.setDumpAfterStart(dumpAfterStart);
server.setDumpBeforeStop(dumpBeforeStop);
return server;
}
Aggregations