| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis; |
|---|
| 2 | 2 | |
|---|
| 3 | +import javax.persistence.EntityManager; |
|---|
| 3 | 4 | import javax.servlet.http.HttpServletRequest; |
|---|
| 4 | 5 | import javax.ws.rs.ForbiddenException; |
|---|
| 5 | 6 | import javax.ws.rs.core.Context; |
|---|
| .. | .. |
|---|
| 32 | 33 | HttpServletRequest request; |
|---|
| 33 | 34 | @Context |
|---|
| 34 | 35 | SecurityContext bsc; |
|---|
| 36 | + @Context |
|---|
| 37 | + EntityManager em; |
|---|
| 35 | 38 | |
|---|
| 36 | 39 | @Override |
|---|
| 37 | 40 | public Response toResponse(Exception e) { |
|---|
| 41 | + releaseEntityManager(); |
|---|
| 38 | 42 | if (e instanceof ForbiddenException) { |
|---|
| 39 | 43 | LOG.warn("Unauthorized access to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 40 | | - return Response.status(Status.UNAUTHORIZED) |
|---|
| 41 | | - .header(ERROR_CODE_MESSAGE_HEADER, ErrorCodes.INVALID_CREDENTIALS) |
|---|
| 42 | | - .header(ERROR_MESSAGE_HEADER, "Unathorized access to the application") |
|---|
| 43 | | - .type(MediaType.APPLICATION_JSON).build(); |
|---|
| 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(); |
|---|
| 44 | 46 | } |
|---|
| 45 | 47 | |
|---|
| 46 | 48 | if (e instanceof SeCurisServiceException) { |
|---|
| 47 | 49 | LOG.warn("SeCurisServiceException accessing to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| 48 | | - return Response.status(DEFAULT_APP_ERROR_STATUS_CODE) |
|---|
| 49 | | - .header(ERROR_CODE_MESSAGE_HEADER, ((SeCurisServiceException) e).getStatus()) |
|---|
| 50 | | - .header(ERROR_MESSAGE_HEADER, e.getMessage()) |
|---|
| 51 | | - .type(MediaType.APPLICATION_JSON).build(); |
|---|
| 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 | 52 | } |
|---|
| 53 | 53 | |
|---|
| 54 | 54 | LOG.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal()); |
|---|
| .. | .. |
|---|
| 56 | 56 | LOG.error("Request url: " + request.getRequestURL(), e); |
|---|
| 57 | 57 | return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build(); |
|---|
| 58 | 58 | } |
|---|
| 59 | + |
|---|
| 60 | + private void releaseEntityManager() { |
|---|
| 61 | + if (em != null && em.isOpen()) { |
|---|
| 62 | + LOG.debug("CLOSING EM: {}, trans: {}", em, em.isJoinedToTransaction()); |
|---|
| 63 | + try { |
|---|
| 64 | + if (em.isJoinedToTransaction()) { |
|---|
| 65 | + em.getTransaction().rollback(); |
|---|
| 66 | + LOG.info("ROLLBACK"); |
|---|
| 67 | + } |
|---|
| 68 | + em.close(); |
|---|
| 69 | + } catch (Exception ex) { |
|---|
| 70 | + ex.printStackTrace(); |
|---|
| 71 | + LOG.error("Error closing EM: {}, {}", em, ex); |
|---|
| 72 | + } |
|---|
| 73 | + } |
|---|
| 74 | + } |
|---|
| 59 | 75 | } |
|---|