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

The following examples show how to use org.wso2.carbon.identity.core.util.IdentityDatabaseUtil#closeStatement() . 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: 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 2
Source File: UserIdentityMetadataStore.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Stores identity data.
 *
 * @param metadata
 * @throws IdentityException
 */
public void storeMetadata(IdentityMetadataDO metadata) throws IdentityException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.STORE_META_DATA);
        prepStmt.setString(1, metadata.getUserName());
        prepStmt.setInt(2, metadata.getTenantId());
        prepStmt.setString(3, metadata.getMetadataType());
        prepStmt.setString(4, metadata.getMetadata());
        prepStmt.setString(5, Boolean.toString(metadata.isValid()));
        prepStmt.execute();
        connection.commit();
    } catch (SQLException e) {
        throw IdentityException.error("Error while storing user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 3
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 4
Source File: IdPManagementDAO.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Add Identity provider properties
 *
 * @param dbConnection
 * @param idpId
 * @param properties
 * @throws SQLException
 */
private void addIdentityProviderProperties(Connection dbConnection, int idpId,
        List<IdentityProviderProperty> properties, int tenantId)
        throws SQLException {
    String sqlStmt = IdPManagementConstants.SQLQueries.ADD_IDP_METADATA;
    PreparedStatement prepStmt = null;
    try {
        prepStmt = dbConnection.prepareStatement(sqlStmt);

        for (IdentityProviderProperty property : properties) {
            prepStmt.setInt(1, idpId);
            prepStmt.setString(2, property.getName());
            prepStmt.setString(3, property.getValue());
            prepStmt.setString(4, property.getDisplayName());
            prepStmt.setInt(5, tenantId);
            prepStmt.addBatch();
        }
        prepStmt.executeBatch();

    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
 
Example 5
Source File: TokenMgtDAO.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
private void updateTokenIdIfAutzCodeGrantType(String oldAccessTokenId, String newAccessTokenId, Connection
        connection) throws IdentityOAuth2Exception {
    PreparedStatement prepStmt = null;
    try {
        String updateNewTokenAgaintAuthzCodeSql;
        if (connection.getMetaData().getDriverName().contains("MySQL")){
            updateNewTokenAgaintAuthzCodeSql = SQLQueries.UPDATE_NEW_TOKEN_AGAINST_AUTHZ_CODE_MYSQL;
        }else{
            updateNewTokenAgaintAuthzCodeSql = SQLQueries.UPDATE_NEW_TOKEN_AGAINST_AUTHZ_CODE;
        }
        prepStmt = connection.prepareStatement(updateNewTokenAgaintAuthzCodeSql);
        prepStmt.setString(1, newAccessTokenId);
        prepStmt.setString(2, oldAccessTokenId);
        prepStmt.executeUpdate();
    } catch (SQLException e) {
        throw new IdentityOAuth2Exception("Error while updating Access Token against authorization code for " +
                                          "access token with ID : " + oldAccessTokenId, e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
 
Example 6
Source File: ClaimDialectDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public void removeClaimDialect(ClaimDialect claimDialect, int tenantId) throws ClaimMetadataException {

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

        String query = SQLConstants.REMOVE_CLAIM_DIALECT;
        try {
            prepStmt = connection.prepareStatement(query);
            prepStmt.setString(1, claimDialect.getClaimDialectURI());
            prepStmt.setInt(2, tenantId);
            prepStmt.executeUpdate();
            IdentityDatabaseUtil.commitTransaction(connection);
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw new ClaimMetadataException("Error while deleting claim dialect " + claimDialect
                    .getClaimDialectURI(), e);
        } finally {
            IdentityDatabaseUtil.closeStatement(prepStmt);
            IdentityDatabaseUtil.closeConnection(connection);
        }
    }
 
Example 7
Source File: ExternalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private String getClaimMapping(Connection connection, int externalClaimId, int tenantId)
        throws ClaimMetadataException {

    String mappedLocalClaimURI = null;

    PreparedStatement prepStmt = null;
    ResultSet rs = null;

    String query = SQLConstants.GET_CLAIM_MAPPING;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setInt(1, externalClaimId);
        prepStmt.setInt(2, tenantId);
        prepStmt.setInt(3, tenantId);
        rs = prepStmt.executeQuery();

        while (rs.next()) {
            mappedLocalClaimURI = rs.getString(SQLConstants.CLAIM_URI_COLUMN);
        }
    } catch (SQLException e) {
        throw new ClaimMetadataException("Error while retrieving claim mapping", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(rs);
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }

    if (StringUtils.isBlank(mappedLocalClaimURI)) {
        throw new ClaimMetadataException("Invalid external claim URI. Claim mapping cannot be empty.");
    }

    return mappedLocalClaimURI;
}
 
Example 8
Source File: OpenIDUserRPDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Returns relying party user settings corresponding to a given user name.
 *
 * @param userName Unique user name
 * @param rpUrl    Relying party urlupdateOpenIDUserRPInfo
 * @return A set of OpenIDUserRPDO, corresponding to the provided user name
 * and RP url.
 */
public OpenIDUserRPDO getOpenIDUserRP(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);
            prepStmt.setString(1, userName);
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, rpUrl);
            OpenIDUserRPDO openIDUserRPDO = buildUserRPDO(prepStmt.executeQuery(), userName);
            return openIDUserRPDO;
        } 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 9
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 10
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 11
Source File: IdPManagementDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Add Identity provider properties
 *
 * @param dbConnection
 * @param idpId
 * @param properties
 * @throws SQLException
 */
private void addIdentityProviderProperties(Connection dbConnection, int idpId,
                                           List<IdentityProviderProperty> properties, int tenantId)
        throws SQLException {

    String sqlStmt = IdPManagementConstants.SQLQueries.ADD_IDP_METADATA;
    PreparedStatement prepStmt = null;
    try {
        prepStmt = dbConnection.prepareStatement(sqlStmt);

        for (IdentityProviderProperty property : properties) {
            if (property.getValue() != null) {
                prepStmt.setInt(1, idpId);
                prepStmt.setString(2, property.getName());
                prepStmt.setString(3, property.getValue());
                prepStmt.setString(4, property.getDisplayName());
                prepStmt.setInt(5, tenantId);
                prepStmt.addBatch();
            } else {
                if (log.isDebugEnabled()) {
                    String msg = "IDP property '%s' of IDP with id:%d of tenantId:%d is null. " +
                            "Not adding the property to 'IDP_METADATA' table.";
                    log.debug(String.format(msg, property.getName(), idpId, tenantId));
                }
            }
        }
        prepStmt.executeBatch();

    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
 
Example 12
Source File: OpenIDUserRPDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the Relying Party if exists, if not, then creates a new Relying
 * Party
 *
 * @param rpdo
 */
public void update(OpenIDUserRPDO rpdo, int tenantId) {

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

    try {
        if (isUserRPExist(connection, rpdo, tenantId)) {
            // we should update the entry
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.UPDATE_USER_RP);

            prepStmt.setString(1, rpdo.getUserName());
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, rpdo.getRpUrl());
            prepStmt.setString(4, rpdo.isTrustedAlways() ? "TRUE" : "FALSE");
            prepStmt.setDate(5, new java.sql.Date(rpdo.getLastVisit().getTime()));
            prepStmt.setInt(6, rpdo.getVisitCount() + 1);
            prepStmt.setString(7, rpdo.getDefaultProfileName());
            prepStmt.execute();
            connection.commit();
        } else {
            // we should create the entry
            if(log.isDebugEnabled()) {
                log.debug("Failed to update RP: " + rpdo.getRpUrl() + " for user: " + rpdo.getUserName() + ". " +
                        "Entry does not exist in the database.");
            }
        }
    } catch (SQLException e) {
        log.error("Failed to update RP:  " + rpdo.getRpUrl() + " for user: " +
                rpdo.getUserName() + " Error while accessing the database", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 13
Source File: ClaimDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public Map<Integer, Claim> getClaims(Connection connection, String claimDialectURI, int tenantId) throws
        ClaimMetadataException {

    Map<Integer, Claim> claimMap = new HashMap<>();

    PreparedStatement prepStmt = null;
    ResultSet rs = null;

    String query = SQLConstants.GET_CLAIMS_BY_DIALECT;

    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, claimDialectURI);
        prepStmt.setInt(2, tenantId);
        prepStmt.setInt(3, tenantId);
        rs = prepStmt.executeQuery(); // TODO : Get the logic reviewed : using executeQuery in a transaction.

        while (rs.next()) {
            String claimURI = rs.getString(SQLConstants.CLAIM_URI_COLUMN);
            int claimId = rs.getInt(SQLConstants.ID_COLUMN);
            claimMap.put(claimId, new Claim(claimDialectURI, claimURI));
        }

    } catch (SQLException e) {
        throw new ClaimMetadataException("Error while listing claims for dialect " + claimDialectURI, e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(rs);
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }

    return claimMap;
}
 
Example 14
Source File: JDBCIdentityDataStore.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private boolean isExistingUserDataValue(String userName, int tenantId, String key) throws SQLException {

        Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement prepStmt = null;
        ResultSet results;
        boolean isUsernameCaseSensitive = IdentityUtil.isUserStoreInUsernameCaseSensitive(userName, tenantId);
        try {
            String query;
            if (isUsernameCaseSensitive) {
                query = SQLQuery.CHECK_EXIST_USER_DATA;
            } else {
                query = SQLQuery.CHECK_EXIST_USER_DATA_CASE_INSENSITIVE;
            }
            prepStmt = connection.prepareStatement(query);
            prepStmt.setInt(1, tenantId);
            prepStmt.setString(2, userName);
            prepStmt.setString(3, key);
            results = prepStmt.executeQuery();
            if (results.next()) {
                return true;
            }
        } finally {
            IdentityDatabaseUtil.closeStatement(prepStmt);
            IdentityDatabaseUtil.closeConnection(connection);
        }
        return false;
    }
 
Example 15
Source File: IdPManagementDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param conn
 * @param idPId
 * @param idpRoleNames
 * @throws SQLException
 */
private void addIdPRoles(Connection conn, int idPId, int tenantId, String[] idpRoleNames)
        throws SQLException {

    PreparedStatement prepStmt = null;
    // SP_IDP_ID, SP_IDP_ROLE
    String sqlStmt = IdPManagementConstants.SQLQueries.ADD_IDP_ROLES_SQL;

    if (idpRoleNames == null || idpRoleNames.length == 0) {
        return;
    }

    try {
        prepStmt = conn.prepareStatement(sqlStmt);

        for (String idpRole : idpRoleNames) {
            prepStmt.setInt(1, idPId);
            prepStmt.setInt(2, tenantId);
            prepStmt.setString(3, idpRole);
            prepStmt.addBatch();
            prepStmt.clearParameters();
        }

        prepStmt.executeBatch();

    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
 
Example 16
Source File: OpenIDAssociationDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to remove the association from the database. But if the entry
 * doesn't exist, then this method throws an exception.
 *
 * @param handle
 */
public void removeAssociation(String handle) {

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

    try {

        if (isAssociationExist(connection, handle)) {
            prepStmt = connection.prepareStatement(OpenIDSQLQueries.REMOVE_ASSOCIATION);
            prepStmt.setString(1, handle);
            prepStmt.execute();
            connection.commit();
            if(log.isDebugEnabled()) {
                log.debug("Association " + handle + " successfully removed from the database");
            }
        } else {
            if(log.isDebugEnabled()) {
                log.debug("Association " + handle + " does not exist in the database");
            }
        }

    } catch (SQLException e) {
        log.error("Failed to remove the association " + handle, e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 17
Source File: JDBCUserRecoveryDataStore.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Stores identity data set.
 *
 * @throws IdentityException
 */
@Override
@Deprecated
public void store(UserRecoveryDataDO[] recoveryDataDOs) throws IdentityException {

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    try {
        prepStmt = connection.prepareStatement(SQLQuery.STORE_META_DATA);
        for (UserRecoveryDataDO dataDO : recoveryDataDOs) {
            prepStmt.setString(1, dataDO.getUserName().toLowerCase());
            prepStmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
            prepStmt.setString(3, dataDO.getCode().toLowerCase());
            prepStmt.setString(4, dataDO.getSecret());
            prepStmt.setString(5, dataDO.getExpireTime());
            prepStmt.addBatch();
        }
        prepStmt.executeBatch();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw IdentityException.error("Error while storing user identity data", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example 18
Source File: OAuthConsumerDAO.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public void issueAccessToken(String consumerKey, String accessToken, String accessTokenSecret,
                             String requestToken, String authorizedUser, String scope) throws IdentityOAuthAdminException {

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement removeReqTokStmt = null;
    PreparedStatement issueAccessTokStmt = null;

    try {
        removeReqTokStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.REMOVE_REQUEST_TOKEN);
        removeReqTokStmt.setString(1, requestToken);
        removeReqTokStmt.execute();

        issueAccessTokStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.ADD_ACCESS_TOKEN);
        issueAccessTokStmt.setString(1, accessToken);
        issueAccessTokStmt.setString(2, accessTokenSecret);
        issueAccessTokStmt.setString(3, consumerKey);
        issueAccessTokStmt.setString(4, scope);
        issueAccessTokStmt.setString(5, authorizedUser);
        issueAccessTokStmt.execute();

        connection.commit();

    } catch (SQLException e) {
        log.error(e.getMessage(), e);
        throw new IdentityOAuthAdminException("Error when creating the request token for consumer : " + consumerKey);
    } finally {
        IdentityDatabaseUtil.closeStatement(issueAccessTokStmt);
        IdentityDatabaseUtil.closeAllConnections(connection, null, removeReqTokStmt);
    }

}
 
Example 19
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 20
Source File: IdPManagementDAO.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
public void addFederatedAuthenticatorConfig(FederatedAuthenticatorConfig authnConfig,
                                            Connection dbConnection, int idpId, int tenantId)
        throws IdentityProviderManagementException, SQLException {

    PreparedStatement prepStmt1 = null;
    PreparedStatement prepStmt2 = null;
    String sqlStmt = IdPManagementConstants.SQLQueries.ADD_IDP_AUTH_SQL;

    try {
        prepStmt1 = dbConnection.prepareStatement(sqlStmt);
        prepStmt1.setInt(1, idpId);
        prepStmt1.setInt(2, tenantId);
        if (authnConfig.isEnabled()) {
            prepStmt1.setString(3, IdPManagementConstants.IS_TRUE_VALUE);
        } else {
            prepStmt1.setString(3, IdPManagementConstants.IS_FALSE_VALUE);
        }
        prepStmt1.setString(4, authnConfig.getName());
        prepStmt1.setString(5, authnConfig.getDisplayName());
        prepStmt1.execute();

        int authnId = getAuthenticatorIdentifier(dbConnection, idpId, authnConfig.getName());

        sqlStmt = IdPManagementConstants.SQLQueries.ADD_IDP_AUTH_PROP_SQL;

        if (authnConfig.getProperties() == null) {
            authnConfig.setProperties(new Property[0]);
        }
        for (Property property : authnConfig.getProperties()) {

            prepStmt2 = dbConnection.prepareStatement(sqlStmt);
            prepStmt2.setInt(1, authnId);
            prepStmt2.setInt(2, tenantId);
            prepStmt2.setString(3, property.getName());
            prepStmt2.setString(4, property.getValue());
            if (property.isConfidential()) {
                prepStmt2.setString(5, IdPManagementConstants.IS_TRUE_VALUE);
            } else {
                prepStmt2.setString(5, IdPManagementConstants.IS_FALSE_VALUE);
            }
            prepStmt2.executeUpdate();
        }
    } finally {

        IdentityDatabaseUtil.closeStatement(prepStmt2);
        IdentityDatabaseUtil.closeStatement(prepStmt1);
    }
}