From fc256f48aa7e1a378f540fc1fdbde46739ff903c Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Wed, 28 Jan 2015 23:12:42 +0000
Subject: [PATCH] #0 fix - Fixed Application modification service

---
 securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java |   32 ++++++++++++++++++++++++--------
 1 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java b/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
index fc29955..9a087cf 100644
--- a/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
+++ b/securis/src/main/java/net/curisit/securis/DefaultExceptionHandler.java
@@ -1,5 +1,6 @@
 package net.curisit.securis;
 
+import javax.persistence.EntityManager;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.ForbiddenException;
 import javax.ws.rs.core.Context;
@@ -32,23 +33,22 @@
     HttpServletRequest request;
     @Context
     SecurityContext bsc;
+    @Context
+    EntityManager em;
 
     @Override
     public Response toResponse(Exception e) {
+        releaseEntityManager();
         if (e instanceof ForbiddenException) {
             LOG.warn("Unauthorized access to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal());
-            return Response.status(Status.UNAUTHORIZED)
-                    .header(ERROR_CODE_MESSAGE_HEADER, ErrorCodes.INVALID_CREDENTIALS)
-                    .header(ERROR_MESSAGE_HEADER, "Unathorized access to the application")
-                    .type(MediaType.APPLICATION_JSON).build();
+            return Response.status(Status.UNAUTHORIZED).header(ERROR_CODE_MESSAGE_HEADER, ErrorCodes.INVALID_CREDENTIALS)
+                    .header(ERROR_MESSAGE_HEADER, "Unathorized access to the application").type(MediaType.APPLICATION_JSON).build();
         }
 
         if (e instanceof SeCurisServiceException) {
             LOG.warn("SeCurisServiceException accessing to {}, user: {}", request.getPathInfo(), bsc.getUserPrincipal());
-            return Response.status(DEFAULT_APP_ERROR_STATUS_CODE)
-                    .header(ERROR_CODE_MESSAGE_HEADER, ((SeCurisServiceException) e).getStatus())
-                    .header(ERROR_MESSAGE_HEADER, e.getMessage())
-                    .type(MediaType.APPLICATION_JSON).build();
+            return Response.status(DEFAULT_APP_ERROR_STATUS_CODE).header(ERROR_CODE_MESSAGE_HEADER, ((SeCurisServiceException) e).getStatus())
+                    .header(ERROR_MESSAGE_HEADER, e.getMessage()).type(MediaType.APPLICATION_JSON).build();
         }
 
         LOG.error("Unexpected error accesing to '{}' by user: {}", request.getPathInfo(), bsc.getUserPrincipal());
@@ -56,4 +56,20 @@
         LOG.error("Request url: " + request.getRequestURL(), e);
         return Response.serverError().header(ERROR_MESSAGE_HEADER, "Unexpected error: " + e.toString()).type(MediaType.APPLICATION_JSON).build();
     }
+
+    private void releaseEntityManager() {
+        if (em != null && em.isOpen()) {
+            LOG.debug("CLOSING EM: {}, trans: {}", em, em.isJoinedToTransaction());
+            try {
+                if (em.isJoinedToTransaction()) {
+                    em.getTransaction().rollback();
+                    LOG.info("ROLLBACK");
+                }
+                em.close();
+            } catch (Exception ex) {
+                ex.printStackTrace();
+                LOG.error("Error closing EM: {}, {}", em, ex);
+            }
+        }
+    }
 }

--
Gitblit v1.3.2