Java Code Examples for org.wso2.carbon.identity.core.util.IdentityDatabaseUtil#closeConnection()

The following examples show how to use org.wso2.carbon.identity.core.util.IdentityDatabaseUtil#closeConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenIDUserRPDAO.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Remove the entry from the database.
 *
 * @param opdo
 */
public void delete(OpenIDUserRPDO opdo, int tenantId) {

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;

    try {

        if (isUserRPExist(connection, opdo, tenantId)) {
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.REMOVE_USER_RP);
            prepStmt.setString(1, opdo.getUserName());
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, opdo.getRpUrl());
            prepStmt.execute();
            connection.commit();
        }

    } catch (SQLException e) {
        log.error("Failed to remove RP: " + opdo.getRpUrl() + " of user: " + opdo.getUserName(), e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 2
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Get permission id for a given permission path
 *
 * @param permission Permission path
 * @return Permission id
 * @throws SQLException
 */
private int getPermissionId(String permission) throws SQLException {
    PreparedStatement loadPermissionsPrepStmt = null;
    ResultSet resultSet = null;
    Connection connection = null;
    int id = -1;
    try {

        connection = IdentityDatabaseUtil.getUserDBConnection();
        loadPermissionsPrepStmt = connection.prepareStatement(ApplicationMgtDBQueries.LOAD_UM_PERMISSIONS_W);
        loadPermissionsPrepStmt.setString(1, permission.toLowerCase());
        resultSet = loadPermissionsPrepStmt.executeQuery();
        if (resultSet.next()) {
            id = resultSet.getInt(1);
        }
    } finally {
        IdentityDatabaseUtil.closeResultSet(resultSet);
        IdentityDatabaseUtil.closeStatement(loadPermissionsPrepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
    return id;
}
 
Example 3
Source File: JDBCUserRecoveryDataStore.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Stores identity data set.
 *
 * @throws IdentityException
 */
@Override
public void store(UserRecoveryDataDO[] recoveryDataDOs) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        connection.setAutoCommit(false);
        prepStmt = connection.prepareStatement(SQLQuery.STORE_META_DATA);
        for (UserRecoveryDataDO dataDO : recoveryDataDOs) {
            prepStmt.setString(1, dataDO.getUserName());
            prepStmt.setInt(2, dataDO.getTenantId());
            prepStmt.setString(3, dataDO.getCode());
            prepStmt.setString(4, dataDO.getSecret());
            prepStmt.addBatch();
        }
        prepStmt.executeBatch();
        connection.commit();
    } catch (SQLException e) {
        throw IdentityException.error("Error while storing user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 4
Source File: IdPManagementDAO.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * @param idPName
 * @param tenantId
 * @param tenantDomain
 * @throws IdentityProviderManagementException
 */
public void deleteIdP(String idPName, int tenantId, String tenantDomain)
        throws IdentityProviderManagementException {

    Connection dbConnection = IdentityDatabaseUtil.getDBConnection();
    try {
        IdentityProvider identityProvider = getIdPByName(dbConnection, idPName, tenantId,
                tenantDomain);
        if (identityProvider == null) {
            String msg = "Trying to delete non-existent Identity Provider for tenant "
                    + tenantDomain;
            log.error(msg);
            return;
        }
        deleteIdP(dbConnection, tenantId, idPName);
        dbConnection.commit();
    } catch (SQLException e) {
        IdentityApplicationManagementUtil.rollBack(dbConnection);
        throw new IdentityProviderManagementException("Error occurred while deleting Identity Provider of tenant "
                + tenantDomain, e);
    } finally {
        IdentityDatabaseUtil.closeConnection(dbConnection);
    }
}
 
Example 5
Source File: OpenIDUserRPDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Remove the entry from the database.
 *
 * @param opdo
 */
public void delete(OpenIDUserRPDO opdo, int tenantId) {

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;

    try {

        if (isUserRPExist(connection, opdo, tenantId)) {
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.REMOVE_USER_RP);
            prepStmt.setString(1, opdo.getUserName());
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, opdo.getRpUrl());
            prepStmt.execute();
            IdentityDatabaseUtil.commitTransaction(connection);
        }

    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        log.error("Failed to remove RP: " + opdo.getRpUrl() + " of user: " + opdo.getUserName(), e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 6
Source File: JDBCUserRecoveryDataStore.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * @param userId
 * @param tenant
 * @throws IdentityException
 */
@Override
public void invalidate(String userId, int tenant) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.INVALIDATE_METADATA);
        prepStmt.setString(1, userId);
        prepStmt.setInt(2, tenant);
        connection.commit();
    } catch (SQLException e) {
        throw IdentityException.error("Error while invalidating user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }

}
 
Example 7
Source File: ClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public void removeClaim(String claimDialectURI, String localClaimURI, int tenantId) throws
        ClaimMetadataException {

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;

    String query = SQLConstants.REMOVE_CLAIM;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, claimDialectURI);
        prepStmt.setInt(2, tenantId);
        prepStmt.setString(3, localClaimURI);
        prepStmt.setInt(4, tenantId);
        prepStmt.executeUpdate();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw new ClaimMetadataException("Error while deleting claim " + localClaimURI + " from dialect" +
                claimDialectURI, e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 8
Source File: UserIdentityMetadataStore.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Can be used to return primary security questions etc
 *
 * @param userName
 * @param tenantId
 * @param metadataType
 * @return
 * @throws IdentityException
 */
public IdentityMetadataDO[] loadMetadata(String userName, int tenantId, String metadataType)
        throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement prepStmt = null;
    ResultSet results = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.LOAD_TENANT_METADATA);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, metadataType);
        results = prepStmt.executeQuery();
        List<IdentityMetadataDO> metada = new ArrayList<IdentityMetadataDO>();
        while (results.next()) {
            metada.add(new IdentityMetadataDO(results.getString(1), results.getInt(2),
                    results.getString(3), results.getString(4),
                    Boolean.parseBoolean(results.getString(5))));
        }
        IdentityMetadataDO[] resultMetadata = new IdentityMetadataDO[metada.size()];
        return metada.toArray(resultMetadata);
    } catch (SQLException e) {
        throw IdentityException.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(results);
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 9
Source File: ApplicationDAOImpl.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Read application role permissions for a given application name
 *
 * @param applicationName Application name
 * @return Map of key value pairs. key is UM table id and value is permission
 * @throws SQLException
 */
private Map<String, String> readApplicationPermissions(String applicationName) throws SQLException {
    PreparedStatement readPermissionsPrepStmt = null;
    ResultSet resultSet = null;
    Connection connection = null;
    Map<String, String> permissions = new HashMap<>();
    try {

        connection = IdentityDatabaseUtil.getUserDBConnection();
        readPermissionsPrepStmt = connection.prepareStatement(ApplicationMgtDBQueries.LOAD_UM_PERMISSIONS);
        readPermissionsPrepStmt.setString(1, "%" + ApplicationMgtUtil.getApplicationPermissionPath() + "%");
        resultSet = readPermissionsPrepStmt.executeQuery();
        while (resultSet.next()) {
            String UM_ID = resultSet.getString(1);
            String permission = resultSet.getString(2);
            if (permission.contains(ApplicationMgtUtil.getApplicationPermissionPath() +
                    ApplicationMgtUtil.PATH_CONSTANT + applicationName.toLowerCase())) {
                permissions.put(UM_ID, permission);
            }
        }
    } finally {
        IdentityDatabaseUtil.closeResultSet(resultSet);
        IdentityDatabaseUtil.closeStatement(readPermissionsPrepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
    return permissions;
}
 
Example 10
Source File: JDBCIdentityDataStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(String userName, UserStoreManager userStoreManager) throws IdentityException {

    super.remove(userName, userStoreManager);
    String domainName = ((org.wso2.carbon.user.core.UserStoreManager) userStoreManager).
            getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
    userName = UserCoreUtil.addDomainToName(userName, domainName);
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        int tenantId = userStoreManager.getTenantId();
        boolean isUsernameCaseSensitive = IdentityUtil.isUserStoreInUsernameCaseSensitive(userName, tenantId);
        String query;
        if (isUsernameCaseSensitive) {
            query = SQLQuery.DELETE_USER_DATA;
        } else {
            query = SQLQuery.DELETE_USER_DATA_CASE_INSENSITIVE;
        }
        prepStmt = connection.prepareStatement(query);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, userName);
        prepStmt.execute();
        connection.commit();
    } catch (SQLException | UserStoreException e) {
        throw IdentityException.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 11
Source File: UserIdentityMetadataStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * This method should return only one result. An exception will be thrown if
 * duplicate entries found.
 * This can be used to check if the given metada exist in the database or to
 * check the validity.
 *
 * @param userName
 * @param tenantId
 * @param metadataType
 * @param metadata
 * @return
 * @throws IdentityException
 */
public IdentityMetadataDO loadMetadata(String userName, int tenantId, String metadataType,
                                       String metadata) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    ResultSet results = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.LOAD_META_DATA);
        prepStmt.setString(1, userName);
        prepStmt.setInt(2, tenantId);
        prepStmt.setString(3, metadataType);
        prepStmt.setString(4, metadata);
        results = prepStmt.executeQuery();
        connection.commit();
        if (results.next()) {
            return new IdentityMetadataDO(results.getString(1), results.getInt(2),
                    results.getString(3), results.getString(4),
                    Boolean.parseBoolean(results.getString(5)));
        }
        if (results.next()) {
            throw IdentityException.error("Duplicate entry found for " + metadataType);
        }
        return null;
    } catch (SQLException e) {
        throw IdentityException.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(results);
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 12
Source File: OpenIDRememberMeTokenDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the remember me token
 *
 * @param rememberMe
 * @throws Exception
 */
public void updateTokenData(OpenIDRememberMeDO rememberMe) throws IdentityProviderException {

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        if (isTokenExist(connection, rememberMe)) {
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.UPDATE_REMEMBER_ME_TOKEN);
            prepStmt.setString(2, rememberMe.getUserName());
            prepStmt.setInt(3, IdentityTenantUtil.getTenantIdOfUser(rememberMe.getUserName()));
            prepStmt.setString(1, rememberMe.getToken());
            prepStmt.execute();
            connection.commit();
            if(log.isDebugEnabled()) {
                log.debug("RememberMe token of " + rememberMe.getUserName() + " successfully updated in the database.");
            }
        } else {
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.STORE_REMEMBER_ME_TOKEN);
            prepStmt.setString(1, rememberMe.getUserName());
            prepStmt.setInt(2, IdentityTenantUtil.getTenantIdOfUser(rememberMe.getUserName()));
            prepStmt.setString(3, rememberMe.getToken());
            prepStmt.execute();
            connection.commit();
            if(log.isDebugEnabled()) {
                log.debug("RememberMe token of " + rememberMe.getUserName() + " successfully stored in the database.");
            }
        }

    } catch (SQLException e) {
        throw new IdentityProviderException("Unable to update the token for " + rememberMe.getUserName(), e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 13
Source File: JDBCUserRecoveryDataStore.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * @param userName
 * @param tenantId
 * @return
 * @throws IdentityException
 */
@Override
@Deprecated
public UserRecoveryDataDO[] load(String userName, int tenantId) throws IdentityException {

    Connection connection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement prepStmt = null;
    ResultSet results = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.LOAD_USER_METADATA);
        prepStmt.setString(1, userName.toLowerCase());
        prepStmt.setInt(2, IdentityTenantUtil.getTenantIdOfUser(userName));

        results = prepStmt.executeQuery();
        List<UserRecoveryDataDO> metada = new ArrayList<UserRecoveryDataDO>();
        while (results.next()) {
            metada.add(new UserRecoveryDataDO(results.getString(1), results.getInt(2),
                    results.getString(3), results.getString(4)));
        }
        UserRecoveryDataDO[] resultMetadata = new UserRecoveryDataDO[metada.size()];
        return metada.toArray(resultMetadata);
    } catch (SQLException e) {
        throw IdentityException.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(results);
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 14
Source File: OpenIDUserRPDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the default user profile corresponding to the given user name and
 * the RP URL.
 *
 * @param userName Unique user name
 * @param rpUrl    Relying party URL
 * @return Default user profile
 */
public String getOpenIDDefaultUserProfile(String userName, String rpUrl, int tenantId) {

    Connection connection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement prepStmt = null;

    OpenIDUserRPDO rpdo = new OpenIDUserRPDO();
    rpdo.setUserName(userName);
    rpdo.setRpUrl(rpUrl);

    try {

        if (isUserRPExist(connection, rpdo, tenantId)) {
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.LOAD_USER_RP_DEFAULT_PROFILE);
            prepStmt.setString(1, userName);
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, rpUrl);
            return prepStmt.executeQuery().getString(7);
        } else {
            if(log.isDebugEnabled()) {
                log.debug("RP: " + rpUrl + " of user: " + userName + " not found in the database");
            }
        }
    } catch (SQLException e) {
        log.error("Failed to load RP: " + rpUrl + " for user: " + userName, e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
    return null;
}
 
Example 15
Source File: JDBCUserRecoveryDataStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param userName
 * @param tenantId
 * @return
 * @throws IdentityException
 */
@Override
public UserRecoveryDataDO[] load(String userName, int tenantId) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    ResultSet results = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.LOAD_USER_METADATA);
        prepStmt.setString(1, userName);
        prepStmt.setInt(2, IdentityTenantUtil.getTenantIdOfUser(userName));

        results = prepStmt.executeQuery();
        List<UserRecoveryDataDO> metada = new ArrayList<UserRecoveryDataDO>();
        while (results.next()) {
            metada.add(new UserRecoveryDataDO(results.getString(1), results.getInt(2),
                    results.getString(3), results.getString(4)));
        }
        UserRecoveryDataDO[] resultMetadata = new UserRecoveryDataDO[metada.size()];
        connection.commit();
        return metada.toArray(resultMetadata);
    } catch (SQLException e) {
        throw IdentityException.error("Error while reading user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(results);
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 16
Source File: LocalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public void addLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetadataException {

        Connection connection = IdentityDatabaseUtil.getDBConnection();
        PreparedStatement prepStmt = null;

        String localClaimURI = localClaim.getClaimURI();

        try {
            // Start transaction
            connection.setAutoCommit(false);

            int localClaimId = addClaim(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId);

            // Some JDBC Drivers returns this in the result, some don't
            if (localClaimId == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("JDBC Driver did not return the claimId, executing Select operation");
                }
                localClaimId = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, localClaimURI, tenantId);
                // TODO : Handle invalid local claim URI

            }

            addClaimAttributeMappings(connection, localClaimId, localClaim.getMappedAttributes(), tenantId);
            addClaimProperties(connection, localClaimId, localClaim.getClaimProperties(), tenantId);

            // End transaction
            connection.commit();
        } catch (SQLException e) {
            rollbackTransaction(connection);
            throw new ClaimMetadataException("Error while adding local claim " + localClaimURI, e);
        } finally {
            IdentityDatabaseUtil.closeConnection(connection);
        }
    }
 
Example 17
Source File: OpenIDUserRPDAO.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a Relying Party and associates it with the User.
 * If the entry exist, then update with the new data
 *
 * @param rpdo
 */
public void createOrUpdate(OpenIDUserRPDO rpdo, int tenantId) {

    // first we try to get DO from the database. Return null if no data
    OpenIDUserRPDO existingdo = getOpenIDUserRP(rpdo.getUserName(), rpdo.getRpUrl(), tenantId);

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;

    try {

        if (existingdo != null) { // data found in the database
            // we should update the entry
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.UPDATE_USER_RP);

            prepStmt.setString(5, rpdo.getUserName());
            prepStmt.setInt(6, tenantId);
            prepStmt.setString(7, rpdo.getRpUrl());
            prepStmt.setString(1, rpdo.isTrustedAlways() ? "TRUE" : "FALSE");

            // we set the new current date
            prepStmt.setDate(2, new java.sql.Date(new Date().getTime()));
            // we increment the value which is in the database
            prepStmt.setInt(3, existingdo.getVisitCount() + 1); // increase visit count

            prepStmt.setString(4, rpdo.getDefaultProfileName());

            prepStmt.execute();
            connection.commit();
        } else {
            // data not found, we should create the entry
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.STORE_USER_RP);

            prepStmt.setString(1, rpdo.getUserName());
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, rpdo.getRpUrl());
            prepStmt.setString(4, rpdo.isTrustedAlways() ? "TRUE" : "FALSE");

            // we set the current date
            prepStmt.setDate(5, new java.sql.Date(new Date().getTime()));
            // ok, this is the first visit
            prepStmt.setInt(6, 1);

            prepStmt.setString(7, rpdo.getDefaultProfileName());

            prepStmt.execute();
            IdentityDatabaseUtil.commitTransaction(connection);
        }
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        log.error("Failed to store RP:  " + rpdo.getRpUrl() + " for user: " +
                rpdo.getUserName() + " Error while accessing the database", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 18
Source File: OpenIDUserRPDAO.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a Relying Party and associates it with the User.
 * If the entry exist, then update with the new data
 *
 * @param rpdo
 */
public void createOrUpdate(OpenIDUserRPDO rpdo, int tenantId) {

    // first we try to get DO from the database. Return null if no data
    OpenIDUserRPDO existingdo = getOpenIDUserRP(rpdo.getUserName(), rpdo.getRpUrl(), tenantId);

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;

    try {

        if (existingdo != null) { // data found in the database
            // we should update the entry
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.UPDATE_USER_RP);

            prepStmt.setString(5, rpdo.getUserName());
            prepStmt.setInt(6, tenantId);
            prepStmt.setString(7, rpdo.getRpUrl());
            prepStmt.setString(1, rpdo.isTrustedAlways() ? "TRUE" : "FALSE");

            // we set the new current date
            prepStmt.setDate(2, new java.sql.Date(new Date().getTime()));
            // we increment the value which is in the database
            prepStmt.setInt(3, existingdo.getVisitCount() + 1); // increase visit count

            prepStmt.setString(4, rpdo.getDefaultProfileName());

            prepStmt.execute();
            connection.commit();
        } else {
            // data not found, we should create the entry
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.STORE_USER_RP);

            prepStmt.setString(1, rpdo.getUserName());
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, rpdo.getRpUrl());
            prepStmt.setString(4, rpdo.isTrustedAlways() ? "TRUE" : "FALSE");

            // we set the current date
            prepStmt.setDate(5, new java.sql.Date(new Date().getTime()));
            // ok, this is the first visit
            prepStmt.setInt(6, 1);

            prepStmt.setString(7, rpdo.getDefaultProfileName());

            prepStmt.execute();
            connection.commit();
        }
    } catch (SQLException e) {
        log.error("Failed to store RP:  " + rpdo.getRpUrl() + " for user: " +
                rpdo.getUserName() + " Error while accessing the database", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 19
Source File: ExternalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 3 votes vote down vote up
public List<ExternalClaim> getExternalClaims(String externalDialectURI, int tenantId) throws
        ClaimMetadataException {

    List<ExternalClaim> externalClaims = new ArrayList<>();

    Connection connection = IdentityDatabaseUtil.getDBConnection(false);

    try {

        Map<Integer, Claim> externalClaimMap = getClaims(connection, externalDialectURI, tenantId);

        for (Map.Entry<Integer, Claim> claimEntry : externalClaimMap.entrySet()) {
            int externalClaimId = claimEntry.getKey();
            Claim claim = claimEntry.getValue();

            String mappedLocalClaimURI = getClaimMapping(connection, externalClaimId, tenantId);

            Map<String, String> claimProperties = getClaimProperties(connection, externalClaimId, tenantId);

            ExternalClaim externalClaim = new ExternalClaim(claim.getClaimDialectURI(), claim.getClaimURI(),
                    mappedLocalClaimURI, claimProperties);

            externalClaims.add(externalClaim);
        }
    } finally {
        IdentityDatabaseUtil.closeConnection(connection);
    }

    return externalClaims;
}
 
Example 20
Source File: IdentityApplicationManagementUtil.java    From carbon-identity-framework with Apache License 2.0 2 votes vote down vote up
/**
 * Utility method to close a database connection
 *
 * @param dbConnection Database <code>Connection</code> object
 */
public static void closeConnection(Connection dbConnection) {

    IdentityDatabaseUtil.closeConnection(dbConnection);
}