Joaquín Reñé
9 hours ago aff398542bf962dcc6e7028c2011d8231e702d33
securis/src/main/java/net/curisit/securis/ioc/RequestsInterceptor.java
....@@ -56,11 +56,11 @@
5656 @Priority(Priorities.AUTHENTICATION)
5757 public class RequestsInterceptor implements ContainerRequestFilter, WriterInterceptor {
5858
59
- private static final Logger LOG = LogManager.getLogger(RequestsInterceptor.class);
59
+ private static final Logger log = LogManager.getLogger(RequestsInterceptor.class);
6060
6161 @Inject private CacheTTL cache;
6262 @Inject private TokenHelper tokenHelper;
63
- @Inject private EntityManagerProvider emProvider;
63
+ @Inject private EntityManager em;
6464
6565 @Context private HttpServletResponse servletResponse;
6666 @Context private HttpServletRequest servletRequest;
....@@ -84,7 +84,7 @@
8484
8585 Method method = resourceInfo != null ? resourceInfo.getResourceMethod() : null;
8686 if (method == null) {
87
- LOG.warn("RequestsInterceptor: resource method is null");
87
+ log.warn("RequestsInterceptor: resource method is null");
8888 return;
8989 }
9090
....@@ -93,11 +93,11 @@
9393
9494 // Only require injected helpers when the endpoint actually needs them
9595 if (securable) {
96
- if (tokenHelper == null || cache == null || emProvider == null) {
97
- LOG.error(
96
+ if (tokenHelper == null || cache == null || em == null) {
97
+ log.error(
9898 "RequestsInterceptor is not fully initialized for secured endpoint '{}'. " +
99
- "tokenHelper={}, cache={}, emProvider={}",
100
- method.getName(), tokenHelper, cache, emProvider
99
+ "tokenHelper={}, cache={}, em={}",
100
+ method.getName(), tokenHelper, cache, em
101101 );
102102 requestContext.abortWith(
103103 Response.status(Status.INTERNAL_SERVER_ERROR)
....@@ -116,7 +116,7 @@
116116 if (ensureTransaction || securable) {
117117 EntityManager em = getEntityManagerSafely();
118118 if (em == null) {
119
- LOG.error("No EntityManager available for method '{}'", method.getName());
119
+ log.error("No EntityManager available for method '{}'", method.getName());
120120 requestContext.abortWith(
121121 Response.status(Status.INTERNAL_SERVER_ERROR)
122122 .entity("Persistence infrastructure not initialized")
....@@ -125,17 +125,17 @@
125125 return;
126126 }
127127
128
- LOG.debug("GETTING EM: {}", em);
128
+ log.debug("GETTING EM: {}", em);
129129 requestContext.setProperty(EM_CONTEXT_PROPERTY, em);
130130
131131 if (ensureTransaction) {
132132 try {
133133 if (!em.getTransaction().isActive()) {
134
- LOG.debug("Beginning transaction");
134
+ log.debug("Beginning transaction");
135135 em.getTransaction().begin();
136136 }
137137 } catch (Exception e) {
138
- LOG.error("Error beginning transaction for method '{}'", method.getName(), e);
138
+ log.error("Error beginning transaction for method '{}'", method.getName(), e);
139139 requestContext.abortWith(
140140 Response.status(Status.INTERNAL_SERVER_ERROR)
141141 .entity("Could not begin transaction")
....@@ -162,7 +162,7 @@
162162
163163 String token = servletRequest != null ? servletRequest.getHeader(TokenHelper.TOKEN_HEADER_PÀRAM) : null;
164164 if (token == null || !tokenHelper.isTokenValid(token)) {
165
- LOG.warn("Access denied, invalid token");
165
+ log.warn("Access denied, invalid token");
166166 ctx.abortWith(Response.status(Status.UNAUTHORIZED).build());
167167 return false;
168168 }
....@@ -172,7 +172,7 @@
172172 Securable securable = method.getAnnotation(Securable.class);
173173
174174 if (securable.roles() != 0 && (securable.roles() & roles) == 0) {
175
- LOG.warn("User {} lacks required roles for method {}", username, method.getName());
175
+ log.warn("User {} lacks required roles for method {}", username, method.getName());
176176 ctx.abortWith(Response.status(Status.UNAUTHORIZED).build());
177177 return false;
178178 }
....@@ -193,12 +193,9 @@
193193 */
194194 private EntityManager getEntityManagerSafely() {
195195 try {
196
- if (emProvider == null) {
197
- return null;
198
- }
199
- return emProvider.getEntityManager();
196
+ return em;
200197 } catch (Exception e) {
201
- LOG.error("Error obtaining EntityManager from provider", e);
198
+ log.error("Error obtaining injected EntityManager", e);
202199 return null;
203200 }
204201 }
....@@ -226,7 +223,7 @@
226223
227224 EntityManager em = getEntityManagerSafely();
228225 if (em == null) {
229
- LOG.error("Cannot resolve user roles: EntityManager is not available");
226
+ log.error("Cannot resolve user roles: EntityManager is not available");
230227 return 0;
231228 }
232229
....@@ -265,7 +262,7 @@
265262
266263 EntityManager em = getEntityManagerSafely();
267264 if (em == null) {
268
- LOG.error("Cannot resolve user organizations: EntityManager is not available");
265
+ log.error("Cannot resolve user organizations: EntityManager is not available");
269266 return Set.of();
270267 }
271268
....@@ -298,7 +295,7 @@
298295
299296 EntityManager em = getEntityManagerSafely();
300297 if (em == null) {
301
- LOG.error("Cannot resolve user applications: EntityManager is not available");
298
+ log.error("Cannot resolve user applications: EntityManager is not available");
302299 return Set.of();
303300 }
304301
....@@ -338,30 +335,22 @@
338335 int status = servletResponse != null ? servletResponse.getStatus() : Status.INTERNAL_SERVER_ERROR.getStatusCode();
339336 if (status >= 200 && status < 300) {
340337 em.getTransaction().commit();
341
- LOG.debug("Transaction committed");
338
+ log.debug("Transaction committed");
342339 } else {
343340 em.getTransaction().rollback();
344
- LOG.debug("Transaction rolled back");
341
+ log.debug("Transaction rolled back");
345342 }
346343 }
347344 } catch (Exception e) {
348
- LOG.error("Error finalizing transaction", e);
345
+ log.error("Error finalizing transaction", e);
349346 try {
350347 if (em.getTransaction() != null && em.getTransaction().isActive()) {
351348 em.getTransaction().rollback();
352349 }
353350 } catch (Exception rollbackEx) {
354
- LOG.error("Error rolling back transaction", rollbackEx);
351
+ log.error("Error rolling back transaction", rollbackEx);
355352 }
356
- } finally {
357
- try {
358
- if (em.isOpen()) {
359
- em.close();
360
- }
361
- } catch (Exception e) {
362
- LOG.error("Error closing EntityManager", e);
363
- }
364
- }
353
+ }
365354 }
366355 }
367356