From aff398542bf962dcc6e7028c2011d8231e702d33 Mon Sep 17 00:00:00 2001
From: Joaquín Reñé <jrene@curisit.net>
Date: Fri, 17 Apr 2026 08:54:41 +0000
Subject: [PATCH] #4479 - upgrade SecurisServer to Java 21
---
securis/src/main/java/net/curisit/securis/ioc/EntityManagerProducer.java | 41 ++++++++++++++++++++
securis/src/main/java/net/curisit/securis/ioc/RequestsInterceptor.java | 57 +++++++++++-----------------
securis/src/main/webapp/WEB-INF/web.xml | 2
3 files changed, 65 insertions(+), 35 deletions(-)
diff --git a/securis/src/main/java/net/curisit/securis/ioc/EntityManagerProducer.java b/securis/src/main/java/net/curisit/securis/ioc/EntityManagerProducer.java
new file mode 100644
index 0000000..fe4612d
--- /dev/null
+++ b/securis/src/main/java/net/curisit/securis/ioc/EntityManagerProducer.java
@@ -0,0 +1,41 @@
+/*
+* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
+*/
+package net.curisit.securis.ioc;
+
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.enterprise.inject.Disposes;
+import jakarta.enterprise.inject.Produces;
+import jakarta.inject.Inject;
+import jakarta.persistence.EntityManager;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ * EntityManagerProducer<p>
+ * Producer for the entity manager, this is used to expose the entity manager as a CDI bean
+ */
+@RequestScoped
+public class EntityManagerProducer {
+
+ private static final Logger LOG = LogManager.getLogger(EntityManagerProducer.class);
+
+ @Inject
+ private EntityManagerProvider emProvider;
+
+ @Produces
+ @RequestScoped
+ public EntityManager produceEntityManager() {
+ EntityManager em = emProvider.getEntityManager();
+ LOG.info("Produced EntityManager: {}", em);
+ return em;
+ }
+
+ public void closeEntityManager(@Disposes EntityManager em) {
+ if (em != null && em.isOpen()) {
+ LOG.info("Closing produced EntityManager: {}", em);
+ em.close();
+ }
+ }
+}
diff --git a/securis/src/main/java/net/curisit/securis/ioc/RequestsInterceptor.java b/securis/src/main/java/net/curisit/securis/ioc/RequestsInterceptor.java
index dcc89b9..4c55831 100644
--- a/securis/src/main/java/net/curisit/securis/ioc/RequestsInterceptor.java
+++ b/securis/src/main/java/net/curisit/securis/ioc/RequestsInterceptor.java
@@ -56,11 +56,11 @@
@Priority(Priorities.AUTHENTICATION)
public class RequestsInterceptor implements ContainerRequestFilter, WriterInterceptor {
- private static final Logger LOG = LogManager.getLogger(RequestsInterceptor.class);
+ private static final Logger log = LogManager.getLogger(RequestsInterceptor.class);
@Inject private CacheTTL cache;
@Inject private TokenHelper tokenHelper;
- @Inject private EntityManagerProvider emProvider;
+ @Inject private EntityManager em;
@Context private HttpServletResponse servletResponse;
@Context private HttpServletRequest servletRequest;
@@ -84,7 +84,7 @@
Method method = resourceInfo != null ? resourceInfo.getResourceMethod() : null;
if (method == null) {
- LOG.warn("RequestsInterceptor: resource method is null");
+ log.warn("RequestsInterceptor: resource method is null");
return;
}
@@ -93,11 +93,11 @@
// Only require injected helpers when the endpoint actually needs them
if (securable) {
- if (tokenHelper == null || cache == null || emProvider == null) {
- LOG.error(
+ if (tokenHelper == null || cache == null || em == null) {
+ log.error(
"RequestsInterceptor is not fully initialized for secured endpoint '{}'. " +
- "tokenHelper={}, cache={}, emProvider={}",
- method.getName(), tokenHelper, cache, emProvider
+ "tokenHelper={}, cache={}, em={}",
+ method.getName(), tokenHelper, cache, em
);
requestContext.abortWith(
Response.status(Status.INTERNAL_SERVER_ERROR)
@@ -116,7 +116,7 @@
if (ensureTransaction || securable) {
EntityManager em = getEntityManagerSafely();
if (em == null) {
- LOG.error("No EntityManager available for method '{}'", method.getName());
+ log.error("No EntityManager available for method '{}'", method.getName());
requestContext.abortWith(
Response.status(Status.INTERNAL_SERVER_ERROR)
.entity("Persistence infrastructure not initialized")
@@ -125,17 +125,17 @@
return;
}
- LOG.debug("GETTING EM: {}", em);
+ log.debug("GETTING EM: {}", em);
requestContext.setProperty(EM_CONTEXT_PROPERTY, em);
if (ensureTransaction) {
try {
if (!em.getTransaction().isActive()) {
- LOG.debug("Beginning transaction");
+ log.debug("Beginning transaction");
em.getTransaction().begin();
}
} catch (Exception e) {
- LOG.error("Error beginning transaction for method '{}'", method.getName(), e);
+ log.error("Error beginning transaction for method '{}'", method.getName(), e);
requestContext.abortWith(
Response.status(Status.INTERNAL_SERVER_ERROR)
.entity("Could not begin transaction")
@@ -162,7 +162,7 @@
String token = servletRequest != null ? servletRequest.getHeader(TokenHelper.TOKEN_HEADER_PÀRAM) : null;
if (token == null || !tokenHelper.isTokenValid(token)) {
- LOG.warn("Access denied, invalid token");
+ log.warn("Access denied, invalid token");
ctx.abortWith(Response.status(Status.UNAUTHORIZED).build());
return false;
}
@@ -172,7 +172,7 @@
Securable securable = method.getAnnotation(Securable.class);
if (securable.roles() != 0 && (securable.roles() & roles) == 0) {
- LOG.warn("User {} lacks required roles for method {}", username, method.getName());
+ log.warn("User {} lacks required roles for method {}", username, method.getName());
ctx.abortWith(Response.status(Status.UNAUTHORIZED).build());
return false;
}
@@ -193,12 +193,9 @@
*/
private EntityManager getEntityManagerSafely() {
try {
- if (emProvider == null) {
- return null;
- }
- return emProvider.getEntityManager();
+ return em;
} catch (Exception e) {
- LOG.error("Error obtaining EntityManager from provider", e);
+ log.error("Error obtaining injected EntityManager", e);
return null;
}
}
@@ -226,7 +223,7 @@
EntityManager em = getEntityManagerSafely();
if (em == null) {
- LOG.error("Cannot resolve user roles: EntityManager is not available");
+ log.error("Cannot resolve user roles: EntityManager is not available");
return 0;
}
@@ -265,7 +262,7 @@
EntityManager em = getEntityManagerSafely();
if (em == null) {
- LOG.error("Cannot resolve user organizations: EntityManager is not available");
+ log.error("Cannot resolve user organizations: EntityManager is not available");
return Set.of();
}
@@ -298,7 +295,7 @@
EntityManager em = getEntityManagerSafely();
if (em == null) {
- LOG.error("Cannot resolve user applications: EntityManager is not available");
+ log.error("Cannot resolve user applications: EntityManager is not available");
return Set.of();
}
@@ -338,30 +335,22 @@
int status = servletResponse != null ? servletResponse.getStatus() : Status.INTERNAL_SERVER_ERROR.getStatusCode();
if (status >= 200 && status < 300) {
em.getTransaction().commit();
- LOG.debug("Transaction committed");
+ log.debug("Transaction committed");
} else {
em.getTransaction().rollback();
- LOG.debug("Transaction rolled back");
+ log.debug("Transaction rolled back");
}
}
} catch (Exception e) {
- LOG.error("Error finalizing transaction", e);
+ log.error("Error finalizing transaction", e);
try {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} catch (Exception rollbackEx) {
- LOG.error("Error rolling back transaction", rollbackEx);
+ log.error("Error rolling back transaction", rollbackEx);
}
- } finally {
- try {
- if (em.isOpen()) {
- em.close();
- }
- } catch (Exception e) {
- LOG.error("Error closing EntityManager", e);
- }
- }
+ }
}
}
diff --git a/securis/src/main/webapp/WEB-INF/web.xml b/securis/src/main/webapp/WEB-INF/web.xml
index be1f5fa..61cb287 100644
--- a/securis/src/main/webapp/WEB-INF/web.xml
+++ b/securis/src/main/webapp/WEB-INF/web.xml
@@ -108,7 +108,7 @@
<resource-ref>
<description>SeCuris DataSource</description>
<res-ref-name>SeCurisDS</res-ref-name>
- <res-type>jakarta.sql.DataSource</res-type>
+ <res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
--
Gitblit v1.3.2