Search in sources :

Example 11 with IdentityRuntimeException

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);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 12 with IdentityRuntimeException

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;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) Date(java.util.Date) AccountSuspensionNotificationException(org.wso2.carbon.identity.account.suspension.notification.task.exception.AccountSuspensionNotificationException) ClaimManager(org.wso2.carbon.user.core.claim.ClaimManager) NotificationReceiver(org.wso2.carbon.identity.account.suspension.notification.task.util.NotificationReceiver) RealmService(org.wso2.carbon.user.core.service.RealmService) ResultSet(java.sql.ResultSet) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 13 with IdentityRuntimeException

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;
}
Also used : SessionSerializerException(org.wso2.carbon.identity.application.authentication.framework.exception.SessionSerializerException) SQLException(java.sql.SQLException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ResultSet(java.sql.ResultSet) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 14 with IdentityRuntimeException

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);
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) SQLException(java.sql.SQLException) SessionSerializerException(org.wso2.carbon.identity.application.authentication.framework.exception.SessionSerializerException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) IOException(java.io.IOException) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Example 15 with IdentityRuntimeException

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;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) FunctionLibrary(org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary) PreparedStatement(java.sql.PreparedStatement) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException)

Aggregations

IdentityRuntimeException (org.wso2.carbon.identity.base.IdentityRuntimeException)17 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)13 IOException (java.io.IOException)8 ResultSet (java.sql.ResultSet)5 SessionSerializerException (org.wso2.carbon.identity.application.authentication.framework.exception.SessionSerializerException)4 Registry (org.wso2.carbon.registry.core.Registry)4 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)4 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)3 ArrayList (java.util.ArrayList)2 IdentityEventException (org.wso2.carbon.identity.event.IdentityEventException)2 IdentityEventMessageContext (org.wso2.carbon.identity.event.bean.IdentityEventMessageContext)2 Event (org.wso2.carbon.identity.event.event.Event)2 FunctionLibrary (org.wso2.carbon.identity.functions.library.mgt.model.FunctionLibrary)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1