Search in sources :

Example 1 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.

the class UserSessionDAOImpl method getSession.

public UserSession getSession(String sessionId) throws SessionManagementServerException {
    HashMap<String, String> propertiesMap = new HashMap<>();
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        List<Application> applicationList = getApplicationsForSessionID(sessionId);
        generateApplicationFromAppID(applicationList);
        String sqlStmt = isH2DB() ? SQLQueries.SQL_GET_PROPERTIES_FROM_SESSION_META_DATA_H2 : SQLQueries.SQL_GET_PROPERTIES_FROM_SESSION_META_DATA;
        jdbcTemplate.executeQuery(sqlStmt, ((resultSet, rowNumber) -> propertiesMap.put(resultSet.getString(1), resultSet.getString(2))), preparedStatement -> preparedStatement.setString(1, sessionId));
        UserSession userSession = new UserSession();
        userSession.setSessionId(sessionId);
        propertiesMap.forEach((key, value) -> {
            switch(key) {
                case SessionMgtConstants.USER_AGENT:
                    userSession.setUserAgent(value);
                    break;
                case SessionMgtConstants.IP_ADDRESS:
                    userSession.setIp(value);
                    break;
                case SessionMgtConstants.LAST_ACCESS_TIME:
                    userSession.setLastAccessTime(value);
                    break;
                case SessionMgtConstants.LOGIN_TIME:
                    userSession.setLoginTime(value);
                    break;
            }
        });
        if (!applicationList.isEmpty()) {
            userSession.setApplications(applicationList);
            return userSession;
        }
    } catch (DataAccessException e) {
        throw new SessionManagementServerException(SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_SESSION, SessionMgtConstants.ErrorMessages.ERROR_CODE_UNABLE_TO_GET_SESSION.getDescription(), e);
    }
    return null;
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) UserSession(org.wso2.carbon.identity.application.authentication.framework.model.UserSession) UserSessionDAO(org.wso2.carbon.identity.application.authentication.framework.dao.UserSessionDAO) HashMap(java.util.HashMap) SQLQueries(org.wso2.carbon.identity.application.authentication.framework.store.SQLQueries) PreparedStatement(java.sql.PreparedStatement) SessionMgtConstants(org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants) Collectors(java.util.stream.Collectors) SQLException(java.sql.SQLException) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) SessionManagementServerException(org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException) JdbcUtils(org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils) ResultSet(java.sql.ResultSet) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) Map(java.util.Map) Application(org.wso2.carbon.identity.application.authentication.framework.model.Application) Collections(java.util.Collections) SessionManagementServerException(org.wso2.carbon.identity.application.authentication.framework.exception.session.mgt.SessionManagementServerException) HashMap(java.util.HashMap) UserSession(org.wso2.carbon.identity.application.authentication.framework.model.UserSession) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Application(org.wso2.carbon.identity.application.authentication.framework.model.Application) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 2 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.

the class UserSessionStore method storeSessionMetaData.

/**
 * Method to store session meta data as a batch
 *
 * @param sessionId id of the authenticated session
 * @param metaData  map of metadata type and value of the session
 * @throws UserSessionException while storing session meta data
 */
public void storeSessionMetaData(String sessionId, Map<String, String> metaData) throws UserSessionException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        String sqlStmt = isH2DB() ? SQLQueries.SQL_INSERT_SESSION_META_DATA_H2 : SQLQueries.SQL_INSERT_SESSION_META_DATA;
        jdbcTemplate.executeBatchInsert(sqlStmt, (preparedStatement -> {
            for (Map.Entry<String, String> entry : metaData.entrySet()) {
                preparedStatement.setString(1, sessionId);
                preparedStatement.setString(2, entry.getKey());
                preparedStatement.setString(3, entry.getValue());
                preparedStatement.addBatch();
            }
        }), sessionId);
        if (log.isDebugEnabled()) {
            log.debug("Inserted metadata for session id: " + sessionId);
        }
    } catch (DataAccessException e) {
        throw new UserSessionException("Error while storing metadata of session:" + sessionId + " in table " + IDN_AUTH_SESSION_META_DATA_TABLE + ".", e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SessionMgtConstants(org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DuplicatedAuthUserException(org.wso2.carbon.identity.application.authentication.framework.exception.DuplicatedAuthUserException) SQLException(java.sql.SQLException) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) JdbcUtils(org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils) ResultSet(java.sql.ResultSet) Map(java.util.Map) IdentityTenantUtil(org.wso2.carbon.identity.core.util.IdentityTenantUtil) User(org.wso2.carbon.identity.application.common.model.User) IdPManagementUtil(org.wso2.carbon.idp.mgt.util.IdPManagementUtil) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) FrameworkUtils(org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 3 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.

the class UserSessionStore method storeAppSessionDataIfNotExist.

/**
 * Method to store app session data if the particular app session is not already exists in the database.
 *
 * @param sessionId   Id of the authenticated session.
 * @param subject     Username in application.
 * @param appID       Id of the application.
 * @param inboundAuth Protocol used in the app.
 * @throws DataAccessException if an error occurs when storing the authenticated user details to the database.
 * @deprecated Please use storeAppSessionData method instead.
 */
@Deprecated
public void storeAppSessionDataIfNotExist(String sessionId, String subject, int appID, String inboundAuth) throws DataAccessException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        jdbcTemplate.withTransaction(template -> {
            Integer recordCount = template.fetchSingleRecord(SQLQueries.SQL_CHECK_IDN_AUTH_SESSION_APP_INFO, (resultSet, rowNumber) -> resultSet.getInt(1), preparedStatement -> {
                preparedStatement.setString(1, sessionId);
                preparedStatement.setString(2, subject);
                preparedStatement.setInt(3, appID);
                preparedStatement.setString(4, inboundAuth);
            });
            if (recordCount == null) {
                storeAppSessionData(sessionId, subject, appID, inboundAuth);
            }
            return null;
        });
    } catch (TransactionException e) {
        throw new DataAccessException("Error while storing application data of session id: " + sessionId + ", subject: " + subject + ", app Id: " + appID + ", protocol: " + inboundAuth + ".", e);
    }
}
Also used : TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 4 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.

the class UserSessionStore method isExistingAppSession.

/**
 * Method to check whether the particular app session is already exists in the database.
 *
 * @param sessionId   id of the authenticated session
 * @param subject     user name of app
 * @param appID       id of application
 * @param inboundAuth protocol used in app
 * @return whether the app session is already available or not
 * @throws UserSessionException while retrieving existing session data
 */
public boolean isExistingAppSession(String sessionId, String subject, int appID, String inboundAuth) throws UserSessionException {
    Integer recordCount;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        recordCount = jdbcTemplate.fetchSingleRecord(SQLQueries.SQL_CHECK_IDN_AUTH_SESSION_APP_INFO, (resultSet, rowNumber) -> resultSet.getInt(1), preparedStatement -> {
            preparedStatement.setString(1, sessionId);
            preparedStatement.setString(2, subject);
            preparedStatement.setInt(3, appID);
            preparedStatement.setString(4, inboundAuth);
        });
    } catch (DataAccessException e) {
        throw new UserSessionException("Error while retrieving application data of session id: " + sessionId + ", subject: " + subject + ", app Id: " + appID + ", protocol: " + inboundAuth + ".", e);
    }
    return recordCount != null;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Connection(java.sql.Connection) IdentityDatabaseUtil(org.wso2.carbon.identity.core.util.IdentityDatabaseUtil) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SessionMgtConstants(org.wso2.carbon.identity.application.authentication.framework.util.SessionMgtConstants) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DuplicatedAuthUserException(org.wso2.carbon.identity.application.authentication.framework.exception.DuplicatedAuthUserException) SQLException(java.sql.SQLException) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) JdbcUtils(org.wso2.carbon.identity.application.authentication.framework.util.JdbcUtils) ResultSet(java.sql.ResultSet) Map(java.util.Map) IdentityTenantUtil(org.wso2.carbon.identity.core.util.IdentityTenantUtil) User(org.wso2.carbon.identity.application.common.model.User) IdPManagementUtil(org.wso2.carbon.idp.mgt.util.IdPManagementUtil) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) IdentityUtil(org.wso2.carbon.identity.core.util.IdentityUtil) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) AuthHistory(org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory) FrameworkUtils(org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 5 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException in project carbon-identity-framework by wso2.

the class UserSessionStore method updateSessionMetaData.

/**
 * Update session meta data.
 *
 * @param sessionId    id of the authenticated session
 * @param propertyType type of the meta data
 * @param value        value of the meta data
 * @throws UserSessionException if the meta data update in the database fails.
 */
public void updateSessionMetaData(String sessionId, String propertyType, String value) throws UserSessionException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(JdbcUtils.Database.SESSION);
    try {
        String sqlStmt = isH2DB() ? SQLQueries.SQL_UPDATE_SESSION_META_DATA_H2 : SQLQueries.SQL_UPDATE_SESSION_META_DATA;
        jdbcTemplate.executeUpdate(sqlStmt, preparedStatement -> {
            preparedStatement.setString(1, value);
            preparedStatement.setString(2, sessionId);
            preparedStatement.setString(3, propertyType);
        });
    } catch (DataAccessException e) {
        throw new UserSessionException("Error while updating " + propertyType + " of session:" + sessionId + " in table " + IDN_AUTH_SESSION_META_DATA_TABLE + ".", e);
    }
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserSessionException(org.wso2.carbon.identity.application.authentication.framework.exception.UserSessionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Aggregations

DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)79 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)58 PreparedStatement (java.sql.PreparedStatement)33 SQLException (java.sql.SQLException)33 List (java.util.List)31 Log (org.apache.commons.logging.Log)29 LogFactory (org.apache.commons.logging.LogFactory)29 TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)28 ArrayList (java.util.ArrayList)26 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)25 Map (java.util.Map)25 HashMap (java.util.HashMap)24 HashSet (java.util.HashSet)22 Set (java.util.Set)22 StringUtils (org.apache.commons.lang.StringUtils)22 Timestamp (java.sql.Timestamp)21 IdentityTenantUtil (org.wso2.carbon.identity.core.util.IdentityTenantUtil)21 Date (java.util.Date)19 Calendar (java.util.Calendar)18 JdbcUtils.isH2DB (org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB)18