use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.
the class HttpChannel method onBadMessage.
public void onBadMessage(int status, String reason) {
if (status < 400 || status > 599)
status = HttpStatus.BAD_REQUEST_400;
Action action;
try {
action = _state.handling();
} catch (IllegalStateException e) {
// The bad message cannot be handled in the current state, so throw
// to hopefull somebody that can handle
abort(e);
throw new BadMessageException(status, reason);
}
try {
if (action == Action.DISPATCH) {
ByteBuffer content = null;
HttpFields fields = new HttpFields();
ErrorHandler handler = getServer().getBean(ErrorHandler.class);
if (handler != null)
content = handler.badMessageError(status, reason, fields);
sendResponse(new MetaData.Response(HttpVersion.HTTP_1_1, status, reason, fields, BufferUtil.length(content)), content, true);
}
} catch (IOException e) {
LOG.debug(e);
} finally {
// TODO: review whether it's the right state to check.
if (_state.unhandle() == Action.COMPLETE)
_state.onComplete();
else
// TODO: don't throw from finally blocks !
throw new IllegalStateException();
onCompleted();
}
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.
the class AsyncListenerTest method test_StartAsync_OnTimeout_SendError_CustomErrorPage.
@Test
public void test_StartAsync_OnTimeout_SendError_CustomErrorPage() throws Exception {
test_StartAsync_OnTimeout(500, event -> {
AsyncContext asyncContext = event.getAsyncContext();
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
response.sendError(HttpStatus.BAD_GATEWAY_502);
asyncContext.complete();
});
// 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);
}
};
errorHandler.setServer(server);
server.setErrorHandler(errorHandler);
String httpResponse = connector.getResponse("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n");
assertThat(httpResponse, containsString("HTTP/1.1 502 "));
assertThat(httpResponse, containsString("CUSTOM"));
}
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 jetty.project by eclipse.
the class HttpConnectionTest method init.
@Before
public void init() throws Exception {
server = new Server();
HttpConfiguration config = new HttpConfiguration();
config.setRequestHeaderSize(1024);
config.setResponseHeaderSize(1024);
config.setSendDateHeader(true);
HttpConnectionFactory http = new HttpConnectionFactory(config);
connector = new LocalConnector(server, http, null);
connector.setIdleTimeout(5000);
server.addConnector(connector);
server.setHandler(new DumpHandler());
ErrorHandler eh = new ErrorHandler();
eh.setServer(server);
server.addBean(eh);
server.start();
}
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());
}
};
}
Aggregations