Created
July 17, 2015 17:38
-
-
Save jonvuri/cf6118f784bb115f6846 to your computer and use it in GitHub Desktop.
Plain text error handler for Jetty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
context.setErrorHandler(new ErrorHandler() { | |
@Override | |
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { | |
baseRequest.setHandled(true); | |
String method = request.getMethod(); | |
if (!method.equals(HttpMethod.GET) && !method.equals(HttpMethod.POST) && !method.equals(HttpMethod.HEAD)) { | |
return; | |
} | |
response.setContentType(MimeTypes.Type.TEXT_PLAIN_8859_1.toString()); | |
response.setHeader(HttpHeader.CACHE_CONTROL.toString(), "must-revalidate,no-cache,no-store"); | |
ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096); | |
handleErrorPage(request, writer, baseRequest.getResponse().getStatus(), baseRequest.getResponse().getReason()); | |
writer.flush(); | |
response.setContentLength(writer.size()); | |
writer.writeTo(response.getOutputStream()); | |
writer.destroy(); | |
} | |
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException { | |
writer.write("Problem accessing "); | |
writer.write(request.getRequestURI()); | |
writer.write("\nReason: "); | |
writer.write(message != null ? message : HttpStatus.getMessage(code)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment