Skip to content
Snippets Groups Projects
Commit 198990ae authored by Thorsten Vitt's avatar Thorsten Vitt
Browse files

Added a little special WAE handling

parent a4d00203
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@
import java.text.MessageFormat;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
......@@ -26,43 +27,54 @@ public class GenericExceptionMapper implements ExceptionMapper<Exception> {
.put(AuthFault.class, Status.UNAUTHORIZED)
.put(IoFault.class, Status.INTERNAL_SERVER_ERROR)
.put(ProtocolNotImplementedFault.class, Status.BAD_REQUEST).build();
private static final String HTML_TEMPLATE =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE html>\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
" <head>\n" +
" <title>404 Not Found</title>\n" +
" <style type=\"text/css\">'\n"
+
" h1 { border-bottom: 1px solid red; }\n" +
" .details { color: gray; }\n" +
" '</style>\n"
+
" </head>\n" +
" <body>\n" +
" <h1>Not Found</h1>\n" +
" <p class=\"message\">{2}</p>\n" +
" <div class=\"details\">\n" +
" <pre>\n" +
"{3}\n" +
" </pre>\n" +
" </div>\n" +
" </body>\n" +
"</html>";
"<!DOCTYPE html>\n"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+ " <head>\n"
+ " <title>{0} {1}</title>\n"
+ " <style type=\"text/css\">'\n"
+ " h1 { border-bottom: 1px solid red; }\n"
+ " .details { color: gray; }\n"
+ " '</style>\n"
+ " </head>\n"
+ " <body>\n"
+ " <h1>{1}</h1>\n"
+ " <p class=\"message\">{2}</p>\n"
+ " <div class=\"details\">\n"
+ " <pre>\n"
+ "{3}\n"
+ " </pre>\n"
+ " </div>\n"
+ " </body>\n" + "</html>";
@Override
public Response toResponse(final Exception exception) {
Status status = STATUS_MAP.get(exception.getClass());
Status status;
String message;
if (exception instanceof WebApplicationException
&& exception.getCause() != null) {
status = STATUS_MAP.get(exception.getCause().getClass());
if (status == null)
status = Status
.fromStatusCode(((WebApplicationException) exception)
.getResponse().getStatus());
message = exception.getCause().getLocalizedMessage();
} else {
status = STATUS_MAP.get(exception.getClass());
message = exception.getLocalizedMessage();
}
if (status == null)
status = Status.INTERNAL_SERVER_ERROR;
ResponseBuilder builder = Response.status(status);
builder.type(MediaType.APPLICATION_XHTML_XML_TYPE);
builder.entity(MessageFormat.format(HTML_TEMPLATE,
status.getStatusCode(), status.getReasonPhrase(),
exception.getLocalizedMessage(),
message,
Throwables.getStackTraceAsString(exception)));
return builder.build();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment