From 198990aecc9bd3b95e1fc1785d6e659a97a47895 Mon Sep 17 00:00:00 2001
From: Thorsten Vitt <thorsten.vitt@uni-wuerzburg.de>
Date: Tue, 11 Sep 2012 14:01:59 +0000
Subject: [PATCH] Added a little special WAE handling

git-svn-id: https://develop.sub.uni-goettingen.de/repos/textgrid/trunk/services/aggregator@13530 7c539038-3410-0410-b1ec-0f2a7bf1c452
---
 .../aggregator/GenericExceptionMapper.java    | 60 +++++++++++--------
 1 file changed, 36 insertions(+), 24 deletions(-)

diff --git a/src/main/java/info/textgrid/services/aggregator/GenericExceptionMapper.java b/src/main/java/info/textgrid/services/aggregator/GenericExceptionMapper.java
index 05dcc03..b71b058 100644
--- a/src/main/java/info/textgrid/services/aggregator/GenericExceptionMapper.java
+++ b/src/main/java/info/textgrid/services/aggregator/GenericExceptionMapper.java
@@ -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();
+
 	}
 
 
-- 
GitLab