| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis; |
|---|
| 2 | 2 | |
|---|
| 3 | +import javax.servlet.http.HttpServletRequest; |
|---|
| 3 | 4 | import javax.ws.rs.ForbiddenException; |
|---|
| 5 | +import javax.ws.rs.core.Context; |
|---|
| 4 | 6 | import javax.ws.rs.core.MediaType; |
|---|
| 5 | 7 | import javax.ws.rs.core.Response; |
|---|
| 6 | 8 | import javax.ws.rs.core.Response.Status; |
|---|
| 9 | +import javax.ws.rs.core.SecurityContext; |
|---|
| 7 | 10 | import javax.ws.rs.ext.ExceptionMapper; |
|---|
| 8 | 11 | import javax.ws.rs.ext.Provider; |
|---|
| 9 | 12 | |
|---|
| 13 | +import org.slf4j.Logger; |
|---|
| 14 | +import org.slf4j.LoggerFactory; |
|---|
| 15 | + |
|---|
| 10 | 16 | @Provider |
|---|
| 11 | 17 | public class DefaultExceptionHandler implements ExceptionMapper<Exception> { |
|---|
| 18 | + private static final Logger log = LoggerFactory.getLogger(DefaultExceptionHandler.class); |
|---|
| 12 | 19 | |
|---|
| 13 | 20 | public static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR"; |
|---|
| 14 | 21 | |
|---|
| 15 | 22 | public DefaultExceptionHandler() { |
|---|
| 16 | | - MainApp.log.info("Creating DefaultExceptionHandler "); |
|---|
| 23 | + log.info("Creating DefaultExceptionHandler "); |
|---|
| 17 | 24 | } |
|---|
| 25 | + |
|---|
| 26 | + @Context |
|---|
| 27 | + HttpServletRequest request; |
|---|
| 28 | + @Context |
|---|
| 29 | + SecurityContext bsc; |
|---|
| 18 | 30 | |
|---|
| 19 | 31 | @Override |
|---|
| 20 | 32 | public Response toResponse(Exception e) { |
|---|
| 21 | 33 | // log.info("Creating DefaultExceptionHandler "); |
|---|
| 22 | 34 | // e.printStackTrace(); |
|---|
| 23 | | - if (e instanceof ForbiddenException) |
|---|
| 35 | + if (e instanceof ForbiddenException) { |
|---|
| 36 | + log.warn("Unauthorized access to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 24 | 37 | return Response.status(Status.UNAUTHORIZED).header(ERROR_MESSAGE_HEADER, "Unathorized access to the application").type(MediaType.APPLICATION_JSON).build(); |
|---|
| 38 | + } |
|---|
| 25 | 39 | |
|---|
| 40 | + log.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 41 | + log.error("Request sent from {}, with User-Agent: {}", request.getRemoteHost(), request.getHeader("User-Agent")); |
|---|
| 42 | + log.error("Request url: " + request.getRequestURL(), e); |
|---|
| 26 | 43 | return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build(); |
|---|
| 27 | 44 | } |
|---|
| 28 | 45 | } |
|---|