rsanchez
2016-12-05 1ed7bc42993b3d23d92dfc38dfd34026a4619ae7
securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
....@@ -11,65 +11,65 @@
1111 import javax.ws.rs.ext.ExceptionMapper;
1212 import javax.ws.rs.ext.Provider;
1313
14
-import net.curisit.securis.services.exception.SeCurisServiceException;
15
-import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
16
-
1714 import org.apache.logging.log4j.LogManager;
1815 import org.apache.logging.log4j.Logger;
1916
17
+import net.curisit.securis.services.exception.SeCurisServiceException;
18
+import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
19
+
2020 @Provider
2121 public class DefaultExceptionHandler implements ExceptionMapper<Exception> {
22
- private static final Logger LOG = LogManager.getLogger(DefaultExceptionHandler.class);
22
+ private static final Logger LOG = LogManager.getLogger(DefaultExceptionHandler.class);
2323
24
- public static final int DEFAULT_APP_ERROR_STATUS_CODE = 418;
25
- public static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
26
- public static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
24
+ public static final int DEFAULT_APP_ERROR_STATUS_CODE = 418;
25
+ public static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
26
+ public static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
2727
28
- public DefaultExceptionHandler() {
29
- LOG.info("Creating DefaultExceptionHandler ");
30
- }
28
+ public DefaultExceptionHandler() {
29
+ LOG.info("Creating DefaultExceptionHandler ");
30
+ }
3131
32
- @Context
33
- HttpServletRequest request;
34
- @Context
35
- SecurityContext bsc;
36
- @Context
37
- EntityManager em;
32
+ @Context
33
+ HttpServletRequest request;
34
+ @Context
35
+ SecurityContext bsc;
36
+ @Context
37
+ EntityManager em;
3838
39
- @Override
40
- public Response toResponse(Exception e) {
41
- releaseEntityManager();
42
- if (e instanceof ForbiddenException) {
43
- LOG.warn("ForbiddenException: {}", e);
44
- return Response.status(Status.UNAUTHORIZED).header(ERROR_CODE_MESSAGE_HEADER, ErrorCodes.INVALID_CREDENTIALS)
45
- .header(ERROR_MESSAGE_HEADER, "Unathorized access to the application").type(MediaType.APPLICATION_JSON).build();
46
- }
39
+ @Override
40
+ public Response toResponse(Exception e) {
41
+ releaseEntityManager();
42
+ if (e instanceof ForbiddenException) {
43
+ LOG.warn("ForbiddenException: {}", e.toString());
44
+ return Response.status(Status.UNAUTHORIZED).header(ERROR_CODE_MESSAGE_HEADER, ErrorCodes.INVALID_CREDENTIALS)
45
+ .header(ERROR_MESSAGE_HEADER, "Unathorized access to the application").type(MediaType.APPLICATION_JSON).build();
46
+ }
4747
48
- if (e instanceof SeCurisServiceException) {
49
- LOG.warn("SeCurisServiceException: {}", e);
50
- return Response.status(DEFAULT_APP_ERROR_STATUS_CODE).header(ERROR_CODE_MESSAGE_HEADER, ((SeCurisServiceException) e).getStatus())
51
- .header(ERROR_MESSAGE_HEADER, e.getMessage()).type(MediaType.APPLICATION_JSON).build();
52
- }
48
+ if (e instanceof SeCurisServiceException) {
49
+ LOG.warn("SeCurisServiceException: {}", e.toString());
50
+ return Response.status(DEFAULT_APP_ERROR_STATUS_CODE).header(ERROR_CODE_MESSAGE_HEADER, ((SeCurisServiceException) e).getStatus())
51
+ .header(ERROR_MESSAGE_HEADER, e.getMessage()).type(MediaType.APPLICATION_JSON).build();
52
+ }
5353
54
- LOG.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal());
55
- LOG.error("Request sent from {}, with User-Agent: {}", request.getRemoteHost(), request.getHeader("User-Agent"));
56
- LOG.error("Request url: " + request.getRequestURL(), e);
57
- return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build();
58
- }
54
+ LOG.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal());
55
+ LOG.error("Request sent from {}, with User-Agent: {}", request.getRemoteHost(), request.getHeader("User-Agent"));
56
+ LOG.error("Request url: " + request.getRequestURL(), e);
57
+ return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build();
58
+ }
5959
60
- private void releaseEntityManager() {
61
- try {
62
- if (em != null && em.isOpen()) {
63
- LOG.debug("CLOSING EM: {}, trans: {}", em, em.isJoinedToTransaction());
64
- if (em.isJoinedToTransaction()) {
65
- em.getTransaction().rollback();
66
- LOG.info("ROLLBACK");
67
- }
68
- em.close();
69
- }
70
- } catch (Exception ex) {
71
- ex.printStackTrace();
72
- LOG.error("Error closing EM: {}, {}", em, ex);
73
- }
74
- }
60
+ private void releaseEntityManager() {
61
+ try {
62
+ if (em != null && em.isOpen()) {
63
+ LOG.debug("CLOSING EM: {}, trans: {}", em, em.isJoinedToTransaction());
64
+ if (em.isJoinedToTransaction()) {
65
+ em.getTransaction().rollback();
66
+ LOG.info("ROLLBACK");
67
+ }
68
+ em.close();
69
+ }
70
+ } catch (Exception ex) {
71
+ ex.printStackTrace();
72
+ LOG.error("Error closing EM: {}, {}", em, ex);
73
+ }
74
+ }
7575 }