@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
KeycloakSessionFactory sessionFactory = (KeycloakSessionFactory) servletRequest.getServletContext().getAttribute(KeycloakSessionFactory.class.getName());
KeycloakSession session = sessionFactory.create();
ResteasyProviderFactory.pushContext(KeycloakSession.class, session);
KeycloakTransaction tx = session.getTransaction();
ResteasyProviderFactory.pushContext(KeycloakTransaction.class, tx);
tx.begin();
try {
filterChain.doFilter(servletRequest, servletResponse);
if (tx.isActive()) {
if (tx.getRollbackOnly()) tx.rollback();
else tx.commit();
}
} catch (IOException ex) {
if (tx.isActive()) tx.rollback();
throw ex;
} catch (ServletException ex) {
if (tx.isActive()) tx.rollback();
throw ex;
}
catch (RuntimeException ex) {
if (tx.isActive()) tx.rollback();
throw new RuntimeException("request path: " + request.getRequestURI(), ex);
} finally {
session.close();
ResteasyProviderFactory.clearContextData();
}
}