Search in sources :

Example 71 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class ClaimDAO method getClaimId.

public int getClaimId(Connection connection, String claimDialectURI, String claimURI, int tenantId) throws ClaimMetadataException {
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    int claimId = 0;
    String query = SQLConstants.GET_CLAIM_ID;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, claimDialectURI);
        prepStmt.setInt(2, tenantId);
        prepStmt.setString(3, claimURI);
        prepStmt.setInt(4, tenantId);
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            claimId = rs.getInt(SQLConstants.ID_COLUMN);
        }
    } catch (SQLException e) {
        throw new ClaimMetadataException("Error while retrieving ID for claim " + claimURI + " in dialect " + claimDialectURI, e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(rs);
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
    if (claimId == 0) {
        // TODO : Throw runtime exception?
        throw new ClaimMetadataClientException(ERROR_CODE_MAPPED_TO_INVALID_LOCAL_CLAIM_URI.getCode(), String.format(ERROR_CODE_MAPPED_TO_INVALID_LOCAL_CLAIM_URI.getMessage(), claimURI, claimDialectURI));
    }
    return claimId;
}
Also used : ClaimMetadataClientException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataClientException) ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 72 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class ClaimDAO method getClaimProperties.

public Map<String, String> getClaimProperties(Connection connection, int claimId, int tenantId) throws ClaimMetadataException {
    Map<String, String> claimProperties = new HashMap<>();
    String query = SQLConstants.GET_CLAIM_PROPERTIES;
    try (PreparedStatement prepStmt = connection.prepareStatement(query)) {
        prepStmt.setInt(1, claimId);
        prepStmt.setInt(2, tenantId);
        try (ResultSet rs = prepStmt.executeQuery()) {
            while (rs.next()) {
                String claimPropertyName = rs.getString(SQLConstants.PROPERTY_NAME_COLUMN);
                String claimPropertyValue = rs.getString(SQLConstants.PROPERTY_VALUE_COLUMN);
                claimProperties.put(claimPropertyName, claimPropertyValue);
            }
        }
    } catch (SQLException e) {
        throw new ClaimMetadataException("Error while retrieving claim properties", e);
    }
    return claimProperties;
}
Also used : ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 73 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class ClaimDAO method addClaimProperties.

public void addClaimProperties(Connection connection, int claimId, Map<String, String> claimProperties, int tenantId) throws ClaimMetadataException {
    if (claimId > 0 && claimProperties != null) {
        String query = SQLConstants.ADD_CLAIM_PROPERTY;
        try (PreparedStatement prepStmt = connection.prepareStatement(query)) {
            for (Map.Entry<String, String> property : claimProperties.entrySet()) {
                prepStmt.setInt(1, claimId);
                prepStmt.setString(2, property.getKey());
                prepStmt.setString(3, property.getValue());
                prepStmt.setInt(4, tenantId);
                prepStmt.addBatch();
            }
            prepStmt.executeBatch();
        } catch (SQLException e) {
            throw new ClaimMetadataException("Error while adding claim properties", e);
        }
    }
}
Also used : ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 74 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class ClaimDAO method getClaims.

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);
        // TODO : Get the logic reviewed : using executeQuery in a transaction.
        rs = prepStmt.executeQuery();
        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;
}
Also used : ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Claim(org.wso2.carbon.identity.claim.metadata.mgt.model.Claim)

Example 75 with Claim

use of org.wso2.carbon.identity.application.common.model.xsd.Claim in project carbon-identity-framework by wso2.

the class ClaimDialectDAO method renameClaimDialect.

public void renameClaimDialect(ClaimDialect oldClaimDialect, ClaimDialect newClaimDialect, int tenantId) throws ClaimMetadataException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    String query = SQLConstants.UPDATE_CLAIM_DIALECT;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, newClaimDialect.getClaimDialectURI());
        prepStmt.setString(2, oldClaimDialect.getClaimDialectURI());
        prepStmt.setInt(3, tenantId);
        prepStmt.executeUpdate();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw new ClaimMetadataException("Error while renaming claim dialect " + oldClaimDialect.getClaimDialectURI(), e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
Also used : ClaimMetadataException(org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

HashMap (java.util.HashMap)112 ArrayList (java.util.ArrayList)89 ClaimMapping (org.wso2.carbon.identity.application.common.model.ClaimMapping)66 UserStoreException (org.wso2.carbon.user.api.UserStoreException)65 Test (org.testng.annotations.Test)63 ClaimMetadataException (org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException)55 Map (java.util.Map)49 PreparedStatement (java.sql.PreparedStatement)48 SQLException (java.sql.SQLException)43 LocalClaim (org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim)34 RealmService (org.wso2.carbon.user.core.service.RealmService)30 UserRealm (org.wso2.carbon.user.core.UserRealm)29 Claim (org.wso2.carbon.user.api.Claim)28 UserStoreException (org.wso2.carbon.user.core.UserStoreException)28 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)28 ResultSet (java.sql.ResultSet)27 Connection (java.sql.Connection)25 ClaimConfig (org.wso2.carbon.identity.application.common.model.ClaimConfig)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 Claim (org.wso2.carbon.identity.application.common.model.Claim)24