/*******************************************************************************
 * Copyright 2015 InfinitiesSoft Solutions Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *******************************************************************************/
package com.infinities.nova;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.ext.ExceptionMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.infinities.nova.exception.SafeException;

public class FaultWrapper implements ExceptionMapper<Exception> {

	private final static Logger logger = LoggerFactory.getLogger(FaultWrapper.class);


	@Override
	public Response toResponse(Exception inner) {
		logger.error("Cauht Error", inner);

		boolean safe = false;
		Map<String, List<Object>> headers = null;
		int status = 500;

		if (inner instanceof SafeException) {
			SafeException safeException = (SafeException) inner;
			safe = safeException.isSafe();
			headers = safeException.getHeaders();
			status = safeException.getCode();
		} else if (inner instanceof WebApplicationException) {
			WebApplicationException webapplicationExcpetion = (WebApplicationException) inner;
			headers = webapplicationExcpetion.getResponse().getHeaders();
			status = webapplicationExcpetion.getResponse().getStatus();
		}

		ResponseBuilder res = Response.status(status);
		if (headers != null) {
			for (Entry<String, List<Object>> entry : headers.entrySet()) {
				for (Object obj : entry.getValue()) {
					res.header(entry.getKey(), obj);
				}
			}
		}
		logger.debug("inner:{}", inner.getMessage());

		WebApplicationException outer = null;

		if (safe) {
			outer =
					new WebApplicationException(String.format("%s:%s", inner.getClass().getName(), inner.getMessage()),
							res.build());
		} else {
			Response response = res.build();
			String message =
					Strings.isNullOrEmpty(inner.getMessage()) ? response.getStatusInfo().getReasonPhrase() : inner
							.getMessage();
			outer = new WebApplicationException(message, response);
		}

		return new FaultException(outer);
	}
}

@@ -1133,16 +1133,58 @@
 trings;%0A
+import com.google.common.base.Throwables;%0A
 import c
@@ -2285,16 +2285,80 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09String rootMsg = Throwables.getRootCause(inner).getMessage();%0A
 %09%09logger
@@ -2376,34 +2376,23 @@
 er:%7B%7D%22, 
-inner.getMessage()
+rootMsg
 );%0A%0A%09%09We
@@ -2452,22 +2452,17 @@
 %09outer =
-%0A%09%09%09%09%09
+ 
 new WebA
@@ -2536,36 +2536,18 @@
 (), 
-inner.getMessage()),%0A%09%09%09%09%09%09%09
+rootMsg), 
 res.
@@ -2721,34 +2721,15 @@
 ) : 
-inner%0A%09%09%09%09%09%09%09.getMessage()
+rootMsg
 ;%0A%09%09

update FaultWrapper