From fa7f69cffc2c9b8d89ecc63f2dce72fffe1b671a Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Thu, 21 Apr 2016 08:30:09 +0200 Subject: Added RedisTransaction storage --- .../moa/id/commons/db/dao/session/AssertionStore.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment') diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AssertionStore.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AssertionStore.java index c2f5ec962..d3c7abd1b 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AssertionStore.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AssertionStore.java @@ -35,6 +35,8 @@ import javax.persistence.Table; import org.hibernate.annotations.DynamicUpdate; +import com.fasterxml.jackson.annotation.JsonCreator; + @Entity @@ -46,8 +48,19 @@ import org.hibernate.annotations.DynamicUpdate; }) public class AssertionStore implements Serializable{ + /** + * + */ + private static final long serialVersionUID = 2804964892915004185L; + + + + @JsonCreator + public AssertionStore(){ + + } + - private static final long serialVersionUID = 1L; @Id //@GeneratedValue(strategy = GenerationType.AUTO) -- cgit v1.2.3 From 61661f2ea4978d56691f7a672469c6d43e9dba41 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Thu, 28 Apr 2016 13:35:55 +0200 Subject: Modifiing MOASessionDBUtils --- .../moa/id/commons/db/MOASessionDBUtils.java | 229 +++++++++++---------- 1 file changed, 119 insertions(+), 110 deletions(-) (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment') diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java index ecb13ef34..88de9ceb5 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java @@ -24,62 +24,67 @@ package at.gv.egovernment.moa.id.commons.db; import java.util.Properties; -import org.apache.commons.lang3.StringUtils; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; -import org.hibernate.service.ServiceRegistry; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; import at.gv.egovernment.moa.id.commons.db.ex.MOADatabaseException; import at.gv.egovernment.moa.logging.Logger; -public final class MOASessionDBUtils { +@Transactional +public class MOASessionDBUtils { - private static SessionFactory sessionFactory; - private static ServiceRegistry serviceRegistry; - - @SuppressWarnings("rawtypes") - private static final ThreadLocal THREAD_LOCAL = new ThreadLocal(); - private static boolean automaticSessionHandling = false; - private static final String[] AUTOMATIC_SESSION_HANDLING_VALUES = new String[] { "jta", "thread" }; - private static final String SESSION_HANDLING_KEY = "hibernate.current_session_context_class"; + private SessionFactory sessionFactory; +// private static ServiceRegistry serviceRegistry; - protected MOASessionDBUtils() { } +// @SuppressWarnings("rawtypes") +// private static final ThreadLocal THREAD_LOCAL = new ThreadLocal(); +// private static boolean automaticSessionHandling = false; + +// private static final String[] AUTOMATIC_SESSION_HANDLING_VALUES = new String[] { "jta", "thread" }; +// private static final String SESSION_HANDLING_KEY = "hibernate.current_session_context_class"; + @Autowired + public void setSessionFactory(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } - public static void initHibernate(Configuration config, Properties hibernateProperties) { - - String scm = StringUtils.trimToNull(hibernateProperties.getProperty(SESSION_HANDLING_KEY)); - if (scm != null) { - automaticSessionHandling = scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[0]) != -1 || scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[1]) != -1; - } - Logger.debug("Evaluating hibernate property \"" + SESSION_HANDLING_KEY + "\"."); - if (automaticSessionHandling) { - Logger.info("Hibernate is automatically handling session context management."); - } else { - Logger.info("Hibernate is NOT automatically handling session context management. Using build-in ThreadLocal session handling."); - } - try { - //Create the SessionFactory - Logger.debug("Creating initial MOASession session factory..."); - - config.configure("hibernate_moasession.cfg.xml"); - //serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); - - serviceRegistry = new StandardServiceRegistryBuilder(). - applySettings(config.getProperties()).build(); - - sessionFactory = config.buildSessionFactory(serviceRegistry); - Logger.debug("Initial MOASession session factory successfully created."); - - } catch (Throwable ex) { - Logger.error("Initial MOASession session factory creation failed: " + ex.getMessage()); - throw new ExceptionInInitializerError(ex); - } - } + public void initHibernate(Configuration config, Properties hibernateProperties) { +// +// String scm = StringUtils.trimToNull(hibernateProperties.getProperty(SESSION_HANDLING_KEY)); +// if (scm != null) { +// automaticSessionHandling = scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[0]) != -1 || scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[1]) != -1; +// } +// Logger.debug("Evaluating hibernate property \"" + SESSION_HANDLING_KEY + "\"."); +// if (automaticSessionHandling) { +// Logger.info("Hibernate is automatically handling session context management."); +// } else { +// Logger.info("Hibernate is NOT automatically handling session context management. Using build-in ThreadLocal session handling."); +// } +// try { +// //Create the SessionFactory +// Logger.debug("Creating initial MOASession session factory..."); +// +// config.configure("hibernate_moasession.cfg.xml"); +// //serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); +// +// // serviceRegistry = new StandardServiceRegistryBuilder(). +// // applySettings(config.getProperties()).build(); +// +// //dbSessionSessionFactory = config.buildSessionFactory(serviceRegistry); +// +// Logger.debug("Initial MOASession session factory successfully created."); +// +// } catch (Throwable ex) { +// Logger.error("Initial MOASession session factory creation failed: " + ex.getMessage()); +// throw new ExceptionInInitializerError(ex); +// } + } /** * Checks if a session factory is currently available. If necessary a new @@ -89,37 +94,39 @@ public final class MOASessionDBUtils { * @throws HibernateException * thrown if a hibernate error occurs */ - public static Session getCurrentSession() { - if (automaticSessionHandling) { - return sessionFactory.getCurrentSession(); - } - Session session = (Session) THREAD_LOCAL.get(); - // Open a new Session, if this Thread has none yet - if (session == null || !session.isConnected()) { - session = getNewSession(); - } - return session; + public Session getCurrentSession() { + return sessionFactory.getCurrentSession(); +// if (automaticSessionHandling) { +// return dbSessionSessionFactory.getCurrentSession(); +// } +// Session session = (Session) THREAD_LOCAL.get(); +// // Open a new Session, if this Thread has none yet +// if (session == null || !session.isConnected()) { +// session = getNewSession(); +// } +// return session; } @SuppressWarnings("unchecked") - public static Session getNewSession() { - if (automaticSessionHandling) { - Logger.warn("Session is being automatically handled by hibernate. Therefore this session maybe not being newly created. Use HibernateUtil.getCurrentSession() instead."); - return sessionFactory.getCurrentSession(); - } - Session session = (Session) THREAD_LOCAL.get(); - if (session != null) { - Logger.warn("Previous MOASession session has not been closed; closing session now."); - closeSession(); - } - Logger.debug("Opening new MOASession hibernate session..."); - try { - session = sessionFactory.openSession(); - THREAD_LOCAL.set(session); - } catch (HibernateException hex) { - Logger.error(hex.getMessage()); - } - return session; + public Session getNewSession() { + return sessionFactory.getCurrentSession(); +// if (automaticSessionHandling) { +// Logger.warn("Session is being automatically handled by hibernate. Therefore this session maybe not being newly created. Use HibernateUtil.getCurrentSession() instead."); +// return dbSessionSessionFactory.getCurrentSession(); +// } +// Session session = (Session) THREAD_LOCAL.get(); +// if (session != null) { +// Logger.warn("Previous MOASession session has not been closed; closing session now."); +// closeSession(); +// } +// Logger.debug("Opening new MOASession hibernate session..."); +// try { +// session = dbSessionSessionFactory.openSession(); +// THREAD_LOCAL.set(session); +// } catch (HibernateException hex) { +// Logger.error(hex.getMessage()); +// } +// return session; } /** @@ -130,61 +137,63 @@ public final class MOASessionDBUtils { * occurs. */ @SuppressWarnings("unchecked") - public static void closeSession() { - if (automaticSessionHandling) { - Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand."); - return; - } - Logger.debug("Closing current MOASession hibernate session..."); - Session session = (Session) THREAD_LOCAL.get(); - THREAD_LOCAL.set(null); - if (session != null) { - try { - session.close(); - - } catch (HibernateException hex) { - Logger.error(hex.getMessage()); - } - } + public void closeSession() { + return; +// if (automaticSessionHandling) { +// Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand."); +// return; +// } +// Logger.debug("Closing current MOASession hibernate session..."); +// Session session = (Session) THREAD_LOCAL.get(); +// THREAD_LOCAL.set(null); +// if (session != null) { +// try { +// session.close(); +// +// } catch (HibernateException hex) { +// Logger.error(hex.getMessage()); +// } +// } } - public static boolean saveOrUpdate(Object dbo) throws MOADatabaseException { - Transaction tx = null; + public boolean saveOrUpdate(Object dbo) throws MOADatabaseException { + // Transaction tx = null; try { - Session session = MOASessionDBUtils.getCurrentSession(); - - synchronized (session) { - tx = session.beginTransaction(); - session.saveOrUpdate(dbo); - tx.commit(); - } - return true; + Session session = sessionFactory.getCurrentSession(); + session.saveOrUpdate(dbo); + return true; +// +// synchronized (session) { +// tx = session.beginTransaction(); +// session.saveOrUpdate(dbo); +// tx.commit(); +// } +// return true; } catch(HibernateException e) { Logger.warn("Error during MOASession database saveOrUpdate. Rollback.", e); - if (tx != null) - tx.rollback(); + throw new MOADatabaseException(e); } } - public static boolean delete(Object dbo) { - Transaction tx = null; + public boolean delete(Object dbo) { + //Transaction tx = null; try { - Session session = MOASessionDBUtils.getCurrentSession(); - - synchronized (session) { - tx = session.beginTransaction(); - session.delete(dbo); - tx.commit(); - } + Session session = sessionFactory.getCurrentSession(); + session.delete(dbo); +// synchronized (session) { +// tx = session.beginTransaction(); +// session.delete(dbo); +// tx.commit(); +// } return true; } catch(HibernateException e) { Logger.warn("Error during MOASession database delete. Rollback.", e); - if (tx != null) - tx.rollback(); +// if (tx != null) +// tx.rollback(); return false; } } -- cgit v1.2.3 From 42e40bb58ec9b4c6b7a474e058e79d366548a520 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Tue, 3 May 2016 11:38:51 +0200 Subject: Added Transactional Support to MOASessionDBUtils --- .../moa/id/commons/db/MOASessionDBUtils.java | 119 +-------------------- .../db/dao/session/AuthenticatedSessionStore.java | 4 +- 2 files changed, 8 insertions(+), 115 deletions(-) (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment') diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java index 88de9ceb5..fbbaf9476 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/MOASessionDBUtils.java @@ -41,49 +41,14 @@ public class MOASessionDBUtils { private SessionFactory sessionFactory; -// private static ServiceRegistry serviceRegistry; - -// @SuppressWarnings("rawtypes") -// private static final ThreadLocal THREAD_LOCAL = new ThreadLocal(); -// private static boolean automaticSessionHandling = false; -// private static final String[] AUTOMATIC_SESSION_HANDLING_VALUES = new String[] { "jta", "thread" }; -// private static final String SESSION_HANDLING_KEY = "hibernate.current_session_context_class"; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void initHibernate(Configuration config, Properties hibernateProperties) { -// -// String scm = StringUtils.trimToNull(hibernateProperties.getProperty(SESSION_HANDLING_KEY)); -// if (scm != null) { -// automaticSessionHandling = scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[0]) != -1 || scm.indexOf(AUTOMATIC_SESSION_HANDLING_VALUES[1]) != -1; -// } -// Logger.debug("Evaluating hibernate property \"" + SESSION_HANDLING_KEY + "\"."); -// if (automaticSessionHandling) { -// Logger.info("Hibernate is automatically handling session context management."); -// } else { -// Logger.info("Hibernate is NOT automatically handling session context management. Using build-in ThreadLocal session handling."); -// } -// try { -// //Create the SessionFactory -// Logger.debug("Creating initial MOASession session factory..."); -// -// config.configure("hibernate_moasession.cfg.xml"); -// //serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); -// -// // serviceRegistry = new StandardServiceRegistryBuilder(). -// // applySettings(config.getProperties()).build(); -// -// //dbSessionSessionFactory = config.buildSessionFactory(serviceRegistry); -// -// Logger.debug("Initial MOASession session factory successfully created."); -// -// } catch (Throwable ex) { -// Logger.error("Initial MOASession session factory creation failed: " + ex.getMessage()); -// throw new ExceptionInInitializerError(ex); -// } + } /** @@ -96,104 +61,30 @@ public class MOASessionDBUtils { */ public Session getCurrentSession() { return sessionFactory.getCurrentSession(); -// if (automaticSessionHandling) { -// return dbSessionSessionFactory.getCurrentSession(); -// } -// Session session = (Session) THREAD_LOCAL.get(); -// // Open a new Session, if this Thread has none yet -// if (session == null || !session.isConnected()) { -// session = getNewSession(); -// } -// return session; - } - - @SuppressWarnings("unchecked") - public Session getNewSession() { - return sessionFactory.getCurrentSession(); -// if (automaticSessionHandling) { -// Logger.warn("Session is being automatically handled by hibernate. Therefore this session maybe not being newly created. Use HibernateUtil.getCurrentSession() instead."); -// return dbSessionSessionFactory.getCurrentSession(); -// } -// Session session = (Session) THREAD_LOCAL.get(); -// if (session != null) { -// Logger.warn("Previous MOASession session has not been closed; closing session now."); -// closeSession(); -// } -// Logger.debug("Opening new MOASession hibernate session..."); -// try { -// session = dbSessionSessionFactory.openSession(); -// THREAD_LOCAL.set(session); -// } catch (HibernateException hex) { -// Logger.error(hex.getMessage()); -// } -// return session; - } - - /** - * Closes the current session. - * - * @throws HibernateException - * thrown if session is already closed or a hibernate error - * occurs. - */ - @SuppressWarnings("unchecked") - public void closeSession() { - return; -// if (automaticSessionHandling) { -// Logger.warn("Session is being automatically handled by hibernate. Therefore the current session cannot be closed on demand."); -// return; -// } -// Logger.debug("Closing current MOASession hibernate session..."); -// Session session = (Session) THREAD_LOCAL.get(); -// THREAD_LOCAL.set(null); -// if (session != null) { -// try { -// session.close(); -// -// } catch (HibernateException hex) { -// Logger.error(hex.getMessage()); -// } -// } } public boolean saveOrUpdate(Object dbo) throws MOADatabaseException { - // Transaction tx = null; try { Session session = sessionFactory.getCurrentSession(); - session.saveOrUpdate(dbo); + session.merge(dbo); return true; -// -// synchronized (session) { -// tx = session.beginTransaction(); -// session.saveOrUpdate(dbo); -// tx.commit(); -// } -// return true; } catch(HibernateException e) { - Logger.warn("Error during MOASession database saveOrUpdate. Rollback.", e); + Logger.warn("Error during MOASession database saveOrUpdate.", e); throw new MOADatabaseException(e); } } public boolean delete(Object dbo) { - //Transaction tx = null; + try { Session session = sessionFactory.getCurrentSession(); session.delete(dbo); -// synchronized (session) { -// tx = session.beginTransaction(); -// session.delete(dbo); -// tx.commit(); -// } - return true; } catch(HibernateException e) { - Logger.warn("Error during MOASession database delete. Rollback.", e); -// if (tx != null) -// tx.rollback(); + Logger.warn("Error during MOASession database delete. Rollback."); return false; } } diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java index 6333451b9..f5fc798b0 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/session/AuthenticatedSessionStore.java @@ -57,7 +57,9 @@ import org.hibernate.annotations.DynamicUpdate; @NamedQuery(name="getMOASessionWithNameIDandOAID", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore join fetch authenticatedsessionstore.activeOAsessions activeOAsessions where activeOAsessions.oaurlprefix = :oaID and activeOAsessions.userNameID = :nameID"), @NamedQuery(name="getInterfederatedIDPForAttributeQueryWithSessionID", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore join fetch authenticatedsessionstore.inderfederation inderfederations where inderfederations.attributesRequested is false and authenticatedsessionstore.sessionid = :sessionID"), @NamedQuery(name="getInterfederatedIDPForSSOWithSessionID", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore join fetch authenticatedsessionstore.inderfederation inderfederations where inderfederations.attributesRequested is true and inderfederations.storeSSOInformation is true and authenticatedsessionstore.sessionid = :sessionID order by inderfederations.QAALevel DESC"), - @NamedQuery(name="getInterfederatedIDPForSSOWithSessionIDIDPID", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore join fetch authenticatedsessionstore.inderfederation inderfederations where inderfederations.attributesRequested is true and authenticatedsessionstore.sessionid = :sessionID and inderfederations.idpurlprefix = :idpID") + @NamedQuery(name="getInterfederatedIDPForSSOWithSessionIDIDPID", query = "select authenticatedsessionstore from AuthenticatedSessionStore authenticatedsessionstore join fetch authenticatedsessionstore.inderfederation inderfederations where inderfederations.attributesRequested is true and authenticatedsessionstore.sessionid = :sessionID and inderfederations.idpurlprefix = :idpID"), + @NamedQuery(name="getAllActiveOAsForSessionID", query = "select activeOAsessions from AuthenticatedSessionStore authenticatedsessionstore join authenticatedsessionstore.activeOAsessions activeOAsessions where authenticatedsessionstore.sessionid = :sessionID "), + @NamedQuery(name="getAllActiveIDPsForSessionID", query = "select inderfederation from AuthenticatedSessionStore authenticatedsessionstore join authenticatedsessionstore.inderfederation inderfederation where authenticatedsessionstore.sessionid = :sessionID ") }) public class AuthenticatedSessionStore implements Serializable{ -- cgit v1.2.3 From 9e5fefab3ea53859e31af25c5fd3e68a23fe9883 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Wed, 18 May 2016 13:39:45 +0200 Subject: changed constants to final static hash map --- .../src/main/java/at/gv/egovernment/moa/util/Constants.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment') diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/util/Constants.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/util/Constants.java index 5a5f4edac..260b2ecb1 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/util/Constants.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/util/Constants.java @@ -24,7 +24,10 @@ package at.gv.egovernment.moa.util; +import java.util.Collections; import java.util.HashMap; +import java.util.Map; + /** * Contains various constants used throughout the system. @@ -509,6 +512,14 @@ public interface Constants { /** * A map used to map namespace prefixes to namespace URIs */ - public static HashMap nSMap = new HashMap(5); + public static final Map nSMap = Collections.unmodifiableMap(new HashMap(){ + private static final long serialVersionUID = 3845384324295136490L; + { + put(Constants.SAML_PREFIX, Constants.SAML_NS_URI); + put(Constants.ECDSA_PREFIX, "http://www.w3.org/2001/04/xmldsig-more#"); + put(Constants.DSIG_PREFIX, Constants.DSIG_NS_URI); + } + }); + } -- cgit v1.2.3