| .. | .. |
|---|
| 56 | 56 | @Priority(Priorities.AUTHENTICATION) |
|---|
| 57 | 57 | public class RequestsInterceptor implements ContainerRequestFilter, WriterInterceptor { |
|---|
| 58 | 58 | |
|---|
| 59 | | - private static final Logger LOG = LogManager.getLogger(RequestsInterceptor.class); |
|---|
| 59 | + private static final Logger log = LogManager.getLogger(RequestsInterceptor.class); |
|---|
| 60 | 60 | |
|---|
| 61 | 61 | @Inject private CacheTTL cache; |
|---|
| 62 | 62 | @Inject private TokenHelper tokenHelper; |
|---|
| 63 | | - @Inject private EntityManagerProvider emProvider; |
|---|
| 63 | + @Inject private EntityManager em; |
|---|
| 64 | 64 | |
|---|
| 65 | 65 | @Context private HttpServletResponse servletResponse; |
|---|
| 66 | 66 | @Context private HttpServletRequest servletRequest; |
|---|
| .. | .. |
|---|
| 84 | 84 | |
|---|
| 85 | 85 | Method method = resourceInfo != null ? resourceInfo.getResourceMethod() : null; |
|---|
| 86 | 86 | if (method == null) { |
|---|
| 87 | | - LOG.warn("RequestsInterceptor: resource method is null"); |
|---|
| 87 | + log.warn("RequestsInterceptor: resource method is null"); |
|---|
| 88 | 88 | return; |
|---|
| 89 | 89 | } |
|---|
| 90 | 90 | |
|---|
| .. | .. |
|---|
| 93 | 93 | |
|---|
| 94 | 94 | // Only require injected helpers when the endpoint actually needs them |
|---|
| 95 | 95 | if (securable) { |
|---|
| 96 | | - if (tokenHelper == null || cache == null || emProvider == null) { |
|---|
| 97 | | - LOG.error( |
|---|
| 96 | + if (tokenHelper == null || cache == null || em == null) { |
|---|
| 97 | + log.error( |
|---|
| 98 | 98 | "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 |
|---|
| 101 | 101 | ); |
|---|
| 102 | 102 | requestContext.abortWith( |
|---|
| 103 | 103 | Response.status(Status.INTERNAL_SERVER_ERROR) |
|---|
| .. | .. |
|---|
| 116 | 116 | if (ensureTransaction || securable) { |
|---|
| 117 | 117 | EntityManager em = getEntityManagerSafely(); |
|---|
| 118 | 118 | if (em == null) { |
|---|
| 119 | | - LOG.error("No EntityManager available for method '{}'", method.getName()); |
|---|
| 119 | + log.error("No EntityManager available for method '{}'", method.getName()); |
|---|
| 120 | 120 | requestContext.abortWith( |
|---|
| 121 | 121 | Response.status(Status.INTERNAL_SERVER_ERROR) |
|---|
| 122 | 122 | .entity("Persistence infrastructure not initialized") |
|---|
| .. | .. |
|---|
| 125 | 125 | return; |
|---|
| 126 | 126 | } |
|---|
| 127 | 127 | |
|---|
| 128 | | - LOG.debug("GETTING EM: {}", em); |
|---|
| 128 | + log.debug("GETTING EM: {}", em); |
|---|
| 129 | 129 | requestContext.setProperty(EM_CONTEXT_PROPERTY, em); |
|---|
| 130 | 130 | |
|---|
| 131 | 131 | if (ensureTransaction) { |
|---|
| 132 | 132 | try { |
|---|
| 133 | 133 | if (!em.getTransaction().isActive()) { |
|---|
| 134 | | - LOG.debug("Beginning transaction"); |
|---|
| 134 | + log.debug("Beginning transaction"); |
|---|
| 135 | 135 | em.getTransaction().begin(); |
|---|
| 136 | 136 | } |
|---|
| 137 | 137 | } 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); |
|---|
| 139 | 139 | requestContext.abortWith( |
|---|
| 140 | 140 | Response.status(Status.INTERNAL_SERVER_ERROR) |
|---|
| 141 | 141 | .entity("Could not begin transaction") |
|---|
| .. | .. |
|---|
| 162 | 162 | |
|---|
| 163 | 163 | String token = servletRequest != null ? servletRequest.getHeader(TokenHelper.TOKEN_HEADER_PÀRAM) : null; |
|---|
| 164 | 164 | if (token == null || !tokenHelper.isTokenValid(token)) { |
|---|
| 165 | | - LOG.warn("Access denied, invalid token"); |
|---|
| 165 | + log.warn("Access denied, invalid token"); |
|---|
| 166 | 166 | ctx.abortWith(Response.status(Status.UNAUTHORIZED).build()); |
|---|
| 167 | 167 | return false; |
|---|
| 168 | 168 | } |
|---|
| .. | .. |
|---|
| 172 | 172 | Securable securable = method.getAnnotation(Securable.class); |
|---|
| 173 | 173 | |
|---|
| 174 | 174 | 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()); |
|---|
| 176 | 176 | ctx.abortWith(Response.status(Status.UNAUTHORIZED).build()); |
|---|
| 177 | 177 | return false; |
|---|
| 178 | 178 | } |
|---|
| .. | .. |
|---|
| 193 | 193 | */ |
|---|
| 194 | 194 | private EntityManager getEntityManagerSafely() { |
|---|
| 195 | 195 | try { |
|---|
| 196 | | - if (emProvider == null) { |
|---|
| 197 | | - return null; |
|---|
| 198 | | - } |
|---|
| 199 | | - return emProvider.getEntityManager(); |
|---|
| 196 | + return em; |
|---|
| 200 | 197 | } catch (Exception e) { |
|---|
| 201 | | - LOG.error("Error obtaining EntityManager from provider", e); |
|---|
| 198 | + log.error("Error obtaining injected EntityManager", e); |
|---|
| 202 | 199 | return null; |
|---|
| 203 | 200 | } |
|---|
| 204 | 201 | } |
|---|
| .. | .. |
|---|
| 226 | 223 | |
|---|
| 227 | 224 | EntityManager em = getEntityManagerSafely(); |
|---|
| 228 | 225 | 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"); |
|---|
| 230 | 227 | return 0; |
|---|
| 231 | 228 | } |
|---|
| 232 | 229 | |
|---|
| .. | .. |
|---|
| 265 | 262 | |
|---|
| 266 | 263 | EntityManager em = getEntityManagerSafely(); |
|---|
| 267 | 264 | 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"); |
|---|
| 269 | 266 | return Set.of(); |
|---|
| 270 | 267 | } |
|---|
| 271 | 268 | |
|---|
| .. | .. |
|---|
| 298 | 295 | |
|---|
| 299 | 296 | EntityManager em = getEntityManagerSafely(); |
|---|
| 300 | 297 | 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"); |
|---|
| 302 | 299 | return Set.of(); |
|---|
| 303 | 300 | } |
|---|
| 304 | 301 | |
|---|
| .. | .. |
|---|
| 338 | 335 | int status = servletResponse != null ? servletResponse.getStatus() : Status.INTERNAL_SERVER_ERROR.getStatusCode(); |
|---|
| 339 | 336 | if (status >= 200 && status < 300) { |
|---|
| 340 | 337 | em.getTransaction().commit(); |
|---|
| 341 | | - LOG.debug("Transaction committed"); |
|---|
| 338 | + log.debug("Transaction committed"); |
|---|
| 342 | 339 | } else { |
|---|
| 343 | 340 | em.getTransaction().rollback(); |
|---|
| 344 | | - LOG.debug("Transaction rolled back"); |
|---|
| 341 | + log.debug("Transaction rolled back"); |
|---|
| 345 | 342 | } |
|---|
| 346 | 343 | } |
|---|
| 347 | 344 | } catch (Exception e) { |
|---|
| 348 | | - LOG.error("Error finalizing transaction", e); |
|---|
| 345 | + log.error("Error finalizing transaction", e); |
|---|
| 349 | 346 | try { |
|---|
| 350 | 347 | if (em.getTransaction() != null && em.getTransaction().isActive()) { |
|---|
| 351 | 348 | em.getTransaction().rollback(); |
|---|
| 352 | 349 | } |
|---|
| 353 | 350 | } catch (Exception rollbackEx) { |
|---|
| 354 | | - LOG.error("Error rolling back transaction", rollbackEx); |
|---|
| 351 | + log.error("Error rolling back transaction", rollbackEx); |
|---|
| 355 | 352 | } |
|---|
| 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 | + } |
|---|
| 365 | 354 | } |
|---|
| 366 | 355 | } |
|---|
| 367 | 356 | |
|---|