use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate 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;
}
use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate 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);
}
}
use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate 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);
}
}
use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate 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;
}
use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate 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);
}
}
Aggregations