/*******************************************************************************
 * 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<SafeException> {

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


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

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

		safe = inner.isSafe();
		headers = inner.getHeaders();
		status = inner.getCode();

		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);
	}
}

@@ -1239,20 +1239,16 @@
 nMapper%3C
-Safe
 Exceptio
@@ -1377,20 +1377,16 @@
 esponse(
-Safe
 Exceptio
@@ -1530,20 +1530,125 @@
 %0A%0A%09%09
-safe = inner
+if (inner instanceof SafeException) %7B%0A%09%09%09SafeException safeException = (SafeException) inner;%0A%09%09%09safe = safeException
 .isS
@@ -1654,16 +1654,17 @@
 Safe();%0A
+%09
 %09%09header
@@ -1663,29 +1663,37 @@
 %09%09headers = 
-inner
+safeException
 .getHeaders(
@@ -1697,16 +1697,17 @@
 rs();%0A%09%09
+%09
 status =
@@ -1711,24 +1711,307 @@
 s = 
-inner.getCode();
+safeException.getCode();%0A%09%09%7D else if (inner instanceof WebApplicationException) %7B%0A%09%09%09WebApplicationException webapplicationExcpetion = (WebApplicationException) inner;%0A%09%09%09headers = webapplicationExcpetion.getResponse().getHeaders();%0A%09%09%09status = webapplicationExcpetion.getResponse().getStatus();%0A%09%09%7D
 %0A%0A%09%09
@@ -2349,33 +2349,38 @@
 fe) %7B%0A%09%09%09outer =
- 
+%0A%09%09%09%09%09
 new WebApplicati
@@ -2463,16 +2463,18 @@
 age()),%0A
+%09%09
 %09%09%09%09%09res
@@ -2550,17 +2550,22 @@
 essage =
- 
+%0A%09%09%09%09%09
 Strings.
@@ -2651,16 +2651,18 @@
 : inner%0A
+%09%09
 %09%09%09%09%09.ge

update FaultWrapper