use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.
the class FunctionLibraryDAOImpl method updateFunctionLibrary.
/**
* Update an existing function library.
*
* @param oldFunctionLibName Previous name of the function library
* @param functionLibrary Function library
* @param tenantDomain Tenant domain
* @throws FunctionLibraryManagementException
*/
public void updateFunctionLibrary(String oldFunctionLibName, FunctionLibrary functionLibrary, String tenantDomain) throws FunctionLibraryManagementException {
// get logged-in users tenant identifier.
int tenantID = MultitenantConstants.INVALID_TENANT_ID;
if (tenantDomain != null) {
tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
}
if (tenantID != MultitenantConstants.INVALID_TENANT_ID) {
try (Connection connection = IdentityDatabaseUtil.getDBConnection()) {
try (PreparedStatement updateFunctionLibStmt = connection.prepareStatement(FunctionLibMgtDBQueries.UPDATE_FUNCTIONLIB_INFO)) {
updateFunctionLibStmt.setString(1, functionLibrary.getFunctionLibraryName());
updateFunctionLibStmt.setString(2, functionLibrary.getDescription());
setBlobValue(functionLibrary.getFunctionLibraryScript(), updateFunctionLibStmt, 3);
updateFunctionLibStmt.setInt(4, tenantID);
updateFunctionLibStmt.setString(5, oldFunctionLibName);
updateFunctionLibStmt.executeUpdate();
IdentityDatabaseUtil.commitTransaction(connection);
} catch (SQLException e1) {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_UPDATE_SCRIPT_LIBRARY, oldFunctionLibName, e1);
}
} catch (SQLException e) {
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_UPDATE_SCRIPT_LIBRARY, oldFunctionLibName, e);
} catch (IOException e) {
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_PROCESSING_CONTENT_STREAM_SCRIPT_LIBRARY, oldFunctionLibName, e);
} catch (IdentityRuntimeException e) {
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_DATABASE_CONNECTION, e);
}
} else {
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_INVALID_TENANT);
}
}
use of org.wso2.carbon.identity.base.IdentityRuntimeException in project identity-governance by wso2-extensions.
the class JDBCNotificationReceiversRetrieval method getNotificationReceivers.
@Override
public List<NotificationReceiver> getNotificationReceivers(long lookupMin, long lookupMax, long delayForSuspension, String tenantDomain) throws AccountSuspensionNotificationException {
List<NotificationReceiver> users = new ArrayList<>();
RealmService realmService = NotificationTaskDataHolder.getInstance().getRealmService();
try {
ClaimManager claimManager = (ClaimManager) realmService.getTenantUserRealm(IdentityTenantUtil.getTenantId(tenantDomain)).getClaimManager();
String userStoreDomain = realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
if (StringUtils.isBlank(userStoreDomain)) {
userStoreDomain = IdentityUtil.getPrimaryDomainName();
}
String identityClaimForLastLoginTime = IdentityUtil.getProperty(NotificationConstants.USE_IDENTITY_CLAIM_FOR_LAST_LOGIN_TIME);
boolean useIdentityClaimForLastLoginTime = StringUtils.isBlank(identityClaimForLastLoginTime) || Boolean.parseBoolean(identityClaimForLastLoginTime);
if (useIdentityClaimForLastLoginTime) {
if (log.isDebugEnabled()) {
log.debug("Property " + NotificationConstants.USE_IDENTITY_CLAIM_FOR_LAST_LOGIN_TIME + " is enabled in identity.xml file. Hence treating last login time as identity claim.");
}
return NotificationReceiversRetrievalUtil.getNotificationReceiversFromIdentityClaim(lookupMin, lookupMax, delayForSuspension, realmService, tenantDomain, userStoreDomain);
}
String lastLoginClaim = NotificationConstants.LAST_LOGIN_TIME;
String lastLoginTimeAttribute = claimManager.getAttributeName(userStoreDomain, lastLoginClaim);
try (Connection dbConnection = getDBConnection(realmConfiguration)) {
String sqlStmt = NotificationConstants.GET_USERS_FILTERED_BY_LAST_LOGIN_TIME;
try (PreparedStatement prepStmt = dbConnection.prepareStatement(sqlStmt)) {
prepStmt.setString(1, lastLoginTimeAttribute);
prepStmt.setString(2, String.valueOf(lookupMin));
prepStmt.setString(3, String.valueOf(lookupMax));
// As UM_TENANT_ID is integer, this has to be set as an int to work with postgres.
prepStmt.setInt(4, IdentityTenantUtil.getTenantId(tenantDomain));
prepStmt.setInt(5, IdentityTenantUtil.getTenantId(tenantDomain));
try (ResultSet resultSet = prepStmt.executeQuery()) {
while (resultSet.next()) {
String userName = resultSet.getString(1);
if (StringUtils.isNotBlank(userName)) {
String[] claims = new String[3];
claims[0] = NotificationConstants.FIRST_NAME_CLAIM;
claims[1] = NotificationConstants.EMAIL_CLAIM;
claims[2] = lastLoginClaim;
UserStoreManager userStoreManager = (UserStoreManager) realmService.getTenantUserRealm(IdentityTenantUtil.getTenantId(tenantDomain)).getUserStoreManager();
Map<String, String> map = userStoreManager.getUserClaimValues(IdentityUtil.addDomainToName(userName, userStoreDomain), claims, null);
NotificationReceiver receiver = new NotificationReceiver();
receiver.setEmail(map.get(NotificationConstants.EMAIL_CLAIM));
receiver.setUsername(userName);
receiver.setFirstName(map.get(NotificationConstants.FIRST_NAME_CLAIM));
receiver.setUserStoreDomain(userStoreDomain);
long lastLoginTime = Long.parseLong(map.get(lastLoginClaim));
long expireDate = lastLoginTime + TimeUnit.DAYS.toMillis(delayForSuspension);
receiver.setExpireDate(new SimpleDateFormat("dd-MM-yyyy").format(new Date(expireDate)));
users.add(receiver);
}
}
}
dbConnection.commit();
} catch (SQLException e) {
DatabaseUtil.rollBack(dbConnection);
if (log.isDebugEnabled()) {
log.debug("Using sql : " + sqlStmt);
}
throw new AccountSuspensionNotificationException(e.getMessage(), e);
}
}
} catch (IdentityRuntimeException | SQLException | NumberFormatException | UserStoreException e) {
throw new AccountSuspensionNotificationException(e.getMessage(), e);
}
return users;
}
use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.
the class SessionDataStore method getSessionContextData.
public SessionContextDO getSessionContextData(String key, String type) {
if (log.isDebugEnabled()) {
log.debug("Getting SessionContextData from DB. key : " + key + " type : " + type);
}
if (!enablePersist) {
return null;
}
Connection connection = null;
try {
connection = IdentityDatabaseUtil.getSessionDBConnection(false);
} catch (IdentityRuntimeException e) {
log.error(e.getMessage(), e);
return null;
}
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
if (StringUtils.isBlank(sqlSelect)) {
String driverName = connection.getMetaData().getDriverName();
if (driverName.contains(MYSQL_DATABASE) || driverName.contains(MARIA_DATABASE) || driverName.contains(H2_DATABASE)) {
sqlSelect = SQL_DESERIALIZE_OBJECT_MYSQL;
} else if (connection.getMetaData().getDatabaseProductName().contains(DB2_DATABASE)) {
sqlSelect = SQL_DESERIALIZE_OBJECT_DB2SQL;
} else if (driverName.contains(MS_SQL_DATABASE) || driverName.contains(MICROSOFT_DATABASE)) {
sqlSelect = SQL_DESERIALIZE_OBJECT_MSSQL;
} else if (driverName.contains(POSTGRESQL_DATABASE)) {
sqlSelect = SQL_DESERIALIZE_OBJECT_POSTGRESQL;
} else if (driverName.contains(INFORMIX_DATABASE)) {
// Driver name = "IBM Informix JDBC Driver for IBM Informix Dynamic Server"
sqlSelect = SQL_DESERIALIZE_OBJECT_INFORMIX;
} else {
sqlSelect = SQL_DESERIALIZE_OBJECT_ORACLE;
}
}
preparedStatement = connection.prepareStatement(getSessionStoreDBQuery(sqlSelect, type));
preparedStatement.setString(1, key);
preparedStatement.setString(2, type);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
String operation = resultSet.getString(1);
long nanoTime = resultSet.getLong(3);
if ((OPERATION_STORE.equals(operation))) {
return new SessionContextDO(key, type, getBlobObject(resultSet.getBinaryStream(2)), nanoTime);
}
}
} catch (ClassNotFoundException | IOException | SQLException | SessionSerializerException | IdentityApplicationManagementException e) {
log.error("Error while retrieving session data", e);
} finally {
IdentityDatabaseUtil.closeAllConnections(connection, resultSet, preparedStatement);
}
return null;
}
use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.
the class SessionDataStore method removeTempAuthnContextData.
/**
* Removes temporary authn context data from the table if temporary data cleanup is enabled.
*
* @param key
* @param type
*/
public void removeTempAuthnContextData(String key, String type) {
if (!enablePersist) {
return;
}
Connection connection = null;
try {
connection = IdentityDatabaseUtil.getSessionDBConnection(true);
} catch (IdentityRuntimeException e) {
log.error(e.getMessage(), e);
return;
}
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(SQL_DELETE_TEMP_RECORDS);
preparedStatement.setString(1, key);
preparedStatement.setString(2, type);
preparedStatement.executeUpdate();
IdentityDatabaseUtil.commitTransaction(connection);
} catch (Exception e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
log.error("Error while deleting temporary authentication context data", e);
} finally {
IdentityDatabaseUtil.closeAllConnections(connection, null, preparedStatement);
}
}
use of org.wso2.carbon.identity.base.IdentityRuntimeException in project carbon-identity-framework by wso2.
the class FunctionLibraryDAOImpl method listFunctionLibraries.
/**
* Retrieve function library list in the tenant domain.
*
* @param tenantDomain Tenant domain
* @return A list of function libraries
* @throws FunctionLibraryManagementException
*/
public List<FunctionLibrary> listFunctionLibraries(String tenantDomain) throws FunctionLibraryManagementException {
int tenantID = MultitenantConstants.INVALID_TENANT_ID;
if (tenantDomain != null) {
tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
}
List<FunctionLibrary> functionLibraries = new ArrayList<>();
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
try (PreparedStatement getFunctionLibrariesStmt = connection.prepareStatement(FunctionLibMgtDBQueries.LOAD_FUNCTIONLIB_FROM_TENANTID)) {
getFunctionLibrariesStmt.setInt(1, tenantID);
try (ResultSet functionLibsResultSet = getFunctionLibrariesStmt.executeQuery()) {
while (functionLibsResultSet.next()) {
FunctionLibrary functionlib = new FunctionLibrary();
functionlib.setFunctionLibraryName(functionLibsResultSet.getString("NAME"));
functionlib.setDescription(functionLibsResultSet.getString("DESCRIPTION"));
functionLibraries.add(functionlib);
}
}
} catch (SQLException e1) {
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_SCRIPT_LIBRARIES, e1);
}
} catch (SQLException e) {
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_SCRIPT_LIBRARIES, e);
} catch (IdentityRuntimeException e) {
throw FunctionLibraryExceptionManagementUtil.handleServerException(FunctionLibraryManagementConstants.ErrorMessage.ERROR_CODE_DATABASE_CONNECTION, e);
}
return functionLibraries;
}
Aggregations