package i5.las2peer.restMapper;

import java.util.logging.Level;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import i5.las2peer.api.execution.ResourceNotFoundException;
import i5.las2peer.api.execution.ServiceAccessDeniedException;
import i5.las2peer.api.execution.InvocationBadArgumentException;
import i5.las2peer.api.execution.ServiceInvocationException;
import i5.las2peer.logging.L2pLogger;

@Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {

	private final L2pLogger logger = L2pLogger.getInstance(GenericExceptionMapper.class);

	@Override
	public Response toResponse(Throwable e) {
		// FIXME use format from WebConnector#sendUnexpectedErrorResponse
		logger.log(Level.INFO, "Request failed: " + e.toString(), e);
		int code = Status.INTERNAL_SERVER_ERROR.getStatusCode();
		// map ServiceInvocationExceptions to WebApplicationExceptions
		if (e instanceof ServiceInvocationException) {
			if (e instanceof InvocationBadArgumentException) {
				e = new BadRequestException(e.getMessage(), e.getCause());
			} else if (e instanceof ResourceNotFoundException) {
				e = new NotFoundException(e.getMessage(), e.getCause());
			} else if (e instanceof ServiceAccessDeniedException) {
				e = new ForbiddenException(e.getMessage(), e.getCause());
			} else {
				e = new InternalServerErrorException("Exception during RMI call", e);
			}
		}
		if (e instanceof WebApplicationException) {
			WebApplicationException webEx = (WebApplicationException) e;
			code = webEx.getResponse().getStatus();
		}
		return Response.status(code).entity(new ExceptionEntity(code, e)).type(MediaType.APPLICATION_JSON_TYPE).build();
	}

}

@@ -179,32 +179,75 @@
 ErrorException;%0A
+import javax.ws.rs.NotAuthorizedException;%0A
 import javax.ws.
@@ -260,32 +260,32 @@
 FoundException;%0A
-
 import javax.ws.
@@ -492,24 +492,89 @@
 .Provider;%0A%0A
+import i5.las2peer.api.execution.InvocationBadArgumentException;%0A
 import i5.la
@@ -709,32 +709,39 @@
 r.api.execution.
+Service
 InvocationBadArg
@@ -730,35 +730,24 @@
 ceInvocation
-BadArgument
 Exception;%0Ai
@@ -781,34 +781,37 @@
 tion.Service
-Invocation
+NotAuthorized
 Exception;%0Ai
@@ -1608,32 +1608,158 @@
  e.getCause());%0A
+%09%09%09%7D else if (e instanceof ServiceNotAuthorizedException) %7B%0A%09%09%09%09e = new NotAuthorizedException(e.getMessage(), e.getCause());%0A
 %09%09%09%7D else if (e 

add ServiceNotAuthorizedException to GenericExceptionMapper