org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException Java Examples

The following examples show how to use org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException. 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: LocalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private void deleteClaimAttributeMappings(Connection connection, int localClaimId, int tenantId) throws
        ClaimMetadataException {

    PreparedStatement prepStmt = null;
    try {
        String query = SQLConstants.DELETE_CLAIM_MAPPED_ATTRIBUTE;
        prepStmt = connection.prepareStatement(query);

        prepStmt.setInt(1, localClaimId);
        prepStmt.setInt(2, tenantId);
        prepStmt.execute();
    } catch (SQLException e) {
        throw new ClaimMetadataException("Error while deleting attribute mappings", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
 
Example #2
Source File: ServerClaimManagementService.java    From identity-api-server with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve all claims belonging to the local dialect.
 *
 * @param attributes attributes filter (optional).
 * @param limit      limit (optional).
 * @param offset     offset (optional).
 * @param filter     filter (optional).
 * @param sort       sort (optional).
 * @return List of local claims.
 */
public List<LocalClaimResDTO> getLocalClaims(String attributes, Integer limit, Integer offset,
                                             String filter, String sort) {

    handleNotImplementedCapabilities(attributes, limit, offset, filter, sort);

    try {
        List<LocalClaim> localClaimList = getClaimMetadataManagementService().getLocalClaims(
                ContextLoader.getTenantDomainFromContext());

        return getLocalClaimResDTOs(localClaimList);

    } catch (ClaimMetadataException e) {
        throw handleClaimManagementException(e, ERROR_CODE_ERROR_RETRIEVING_LOCAL_CLAIMS);
    }
}
 
Example #3
Source File: ClaimMetadataManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void addLocalClaim(LocalClaim localClaim, String tenantDomain) throws ClaimMetadataException {

    if (localClaim == null || StringUtils.isBlank(localClaim.getClaimURI())) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_LOCAL_CLAIM_URI);
    } else if (localClaim.getMappedAttributes().isEmpty()) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_MAPPED_ATTRIBUTES_IN_LOCAL_CLAIM.getCode(),
                String.format(ERROR_CODE_EMPTY_MAPPED_ATTRIBUTES_IN_LOCAL_CLAIM.getMessage(), localClaim
                        .getClaimDialectURI(), localClaim.getClaimURI()));
    }

    // TODO : validate tenant domain?
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    if (isExistingLocalClaimURI(localClaim.getClaimURI(), tenantId)) {
        throw new ClaimMetadataClientException(ERROR_CODE_EXISTING_LOCAL_CLAIM_URI.getCode(),
                String.format(ERROR_CODE_EXISTING_LOCAL_CLAIM_URI.getMessage(), localClaim.getClaimURI()));
    }

    // Add listener

    this.localClaimDAO.addLocalClaim(localClaim, tenantId);

    // Add listener
}
 
Example #4
Source File: ServerClaimManagementService.java    From identity-api-server with Apache License 2.0 6 votes vote down vote up
/**
 * Add an external claim.
 *
 * @param dialectId           dialectId.
 * @param externalClaimReqDTO externalClaimReqDTO.
 * @return Resource identifier.
 */
public String addExternalClaim(String dialectId, ExternalClaimReqDTO externalClaimReqDTO) {

    try {
        if (!isDialectExists(dialectId)) {
            throw handleClaimManagementClientError(ERROR_CODE_INVALID_DIALECT_ID, NOT_FOUND, dialectId);
        }

        getClaimMetadataManagementService().addExternalClaim(
                createExternalClaim(dialectId, externalClaimReqDTO),
                ContextLoader.getTenantDomainFromContext());
    } catch (ClaimMetadataException e) {
        throw handleClaimManagementException(e, ERROR_CODE_ERROR_ADDING_EXTERNAL_CLAIM,
                externalClaimReqDTO.getClaimURI());
    }

    return getResourceId(externalClaimReqDTO.getClaimURI());
}
 
Example #5
Source File: ServerClaimManagementService.java    From identity-api-server with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve an external claim.
 *
 * @param dialectId dialectId.
 * @param claimId   claimId.
 * @return Local claim.
 */
public ExternalClaimResDTO getExternalClaim(String dialectId, String claimId) {

    try {
        List<ExternalClaim> externalClaimList = getClaimMetadataManagementService().getExternalClaims(
                base64DecodeId(dialectId),
                ContextLoader.getTenantDomainFromContext());

        if (CollectionUtils.isEmpty(externalClaimList)) {
            throw handleClaimManagementClientError(ERROR_CODE_CLAIMS_NOT_FOUND_FOR_DIALECT, NOT_FOUND, dialectId);
        }

        ExternalClaim externalClaim = extractExternalClaimFromClaimList(base64DecodeId(claimId), externalClaimList);

        if (externalClaim == null) {
            throw handleClaimManagementClientError(ERROR_CODE_EXTERNAL_CLAIM_NOT_FOUND, NOT_FOUND, claimId,
                    dialectId);
        }

        return getExternalClaimResDTO(externalClaim);

    } catch (ClaimMetadataException e) {
        throw handleClaimManagementException(e, ERROR_CODE_ERROR_RETRIEVING_EXTERNAL_CLAIM, claimId, dialectId);
    }
}
 
Example #6
Source File: ServerClaimManagementService.java    From identity-api-server with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve all claims belonging to an external dialect.
 *
 * @param dialectId dialectId.
 * @param limit     limit (optional).
 * @param offset    offset (optional).
 * @param filter    filter (optional).
 * @param sort      sort (optional).
 * @return List of external claims.
 */
public List<ExternalClaimResDTO> getExternalClaims(String dialectId, Integer limit, Integer offset,
                                                   String filter, String sort) {

    handleNotImplementedCapabilities(limit, offset, filter, sort);

    try {
        List<ClaimDialect> claimDialectList = getClaimMetadataManagementService().getClaimDialects(
                ContextLoader.getTenantDomainFromContext());
        String decodedDialectId = base64DecodeId(dialectId);
        ClaimDialect claimDialect = extractDialectFromDialectList(decodedDialectId, claimDialectList);

        if (claimDialect == null) {
            throw handleClaimManagementClientError(ERROR_CODE_DIALECT_NOT_FOUND, NOT_FOUND, dialectId);
        }

        List<ExternalClaim> externalClaimList = getClaimMetadataManagementService().getExternalClaims(
                base64DecodeId(dialectId),
                ContextLoader.getTenantDomainFromContext());
        return getExternalClaimResDTOs(externalClaimList);

    } catch (ClaimMetadataException e) {
        throw handleClaimManagementException(e, ERROR_CODE_ERROR_RETRIEVING_EXTERNAL_CLAIMS, dialectId);
    }
}
 
Example #7
Source File: ClaimMetadataManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void removeClaimDialect(ClaimDialect claimDialect, String tenantDomain) throws ClaimMetadataException {

    if (claimDialect == null || StringUtils.isBlank(claimDialect.getClaimDialectURI())) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_CLAIM_DIALECT.getCode(),
                "Claim dialect URI cannot be empty");
    }

    // TODO : validate claim dialect already exists?

    // TODO : validate tenant domain?
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    // Add listener

    this.claimDialectDAO.removeClaimDialect(claimDialect, tenantId);
    // When deleting a claim dialect the relevant external claim deletion is handled by the DB through
    // ON DELETE CASCADE. Here we are removing the relevant cache entry.
    externalClaimDAO.removeExternalClaimCache(claimDialect.getClaimDialectURI(), tenantId);
    // Add listener

}
 
Example #8
Source File: ServerClaimManagementService.java    From identity-api-server with Apache License 2.0 6 votes vote down vote up
/**
 * Delete a local claim.
 *
 * @param claimId claimId.
 */
public void deleteLocalClaim(String claimId) {


    String claimURI;
    try {
        claimURI = base64DecodeId(claimId);
    } catch (Exception ignored) {
        // Ignoring the delete operation and return 204 response code, since the resource does not exist.
        return;
    }
    try {
        getClaimMetadataManagementService().removeLocalClaim(
                claimURI,
                ContextLoader.getTenantDomainFromContext());
    } catch (ClaimMetadataException e) {
        throw handleClaimManagementException(e, ERROR_CODE_ERROR_DELETING_LOCAL_CLAIM, claimId);
    }

}
 
Example #9
Source File: ServerClaimManagementService.java    From identity-api-server with Apache License 2.0 6 votes vote down vote up
/**
 * Update claim dialect.
 * Due to the current implementation of the resource identifier of a new resource,
 * the resource id of the claim dialect changes when an update occurs.
 *
 * @param dialectId          dialectId.
 * @param claimDialectReqDTO claimDialectReqDTO.
 * @return Resource identifier.
 */
public String updateClaimDialect(String dialectId, ClaimDialectReqDTO claimDialectReqDTO) {

    try {
        // If the old and new dialect uri is the same we don't need to do a db update.
        if (!StringUtils.equals(base64DecodeId(dialectId), claimDialectReqDTO.getDialectURI())) {
            getClaimMetadataManagementService().renameClaimDialect(
                    createClaimDialect(base64DecodeId(dialectId)),
                    createClaimDialect(claimDialectReqDTO),
                    ContextLoader.getTenantDomainFromContext());
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Skipping db update as the old dialectURI and the new dialectURI is the same. " +
                        "DialectURI: " + claimDialectReqDTO.getDialectURI());
            }
        }
    } catch (ClaimMetadataException e) {
        throw handleClaimManagementException(e, ERROR_CODE_ERROR_UPDATING_DIALECT, dialectId);
    }

    // Since the dialects identifier has changed we have to send the new identifier in the location header.
    return getResourceId(claimDialectReqDTO.getDialectURI());
}
 
Example #10
Source File: DefaultClaimHandler.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * @param otherDialect
 * @param keySet
 * @param tenantDomain
 * @param useLocalDialectAsKey
 * @return
 * @throws FrameworkException
 */
private Map<String, String> getClaimMappings(String otherDialect, Set<String> keySet,
                                             String tenantDomain, boolean useLocalDialectAsKey)
        throws FrameworkException {

    Map<String, String> claimMapping = null;
    try {
        claimMapping = ClaimMetadataHandler.getInstance()
                .getMappingsMapFromOtherDialectToCarbon(otherDialect, keySet, tenantDomain,
                                                        useLocalDialectAsKey);
    } catch (ClaimMetadataException e) {
        throw new FrameworkException("Error while loading mappings.", e);
    }

    if (claimMapping == null) {
        claimMapping = new HashMap<>();
    }

    return claimMapping;
}
 
Example #11
Source File: ClaimMetadataHandler.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * @param otherDialectURI
 * @param otherClaimURIs
 * @param tenantDomain
 * @param useCarbonDialectAsKey
 * @return
 * @throws ClaimMetadataException
 */
public Map<String, String> getMappingsMapFromOtherDialectToCarbon(String otherDialectURI, Set<String>
        otherClaimURIs, String tenantDomain, boolean useCarbonDialectAsKey) throws ClaimMetadataException {

    Map<String, String> returnMap = new HashMap<>();
    Set<ExternalClaim> mappings = getMappingsFromOtherDialectToCarbon(
            otherDialectURI, otherClaimURIs, tenantDomain);
    for (ExternalClaim externalClaim : mappings) {
        if (useCarbonDialectAsKey) {
            returnMap.put(externalClaim.getMappedLocalClaim(), externalClaim.getClaimURI());
        } else {
            returnMap.put(externalClaim.getClaimURI(), externalClaim.getMappedLocalClaim());
        }
    }
    return returnMap;
}
 
Example #12
Source File: ClaimDialectDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Remove all claim dialects of a given tenant.
 *
 * @param tenantId Id of the tenant
 * @throws ClaimMetadataException
 */
public void removeAllClaimDialects(int tenantId) throws ClaimMetadataException {

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

    String query = SQLConstants.REMOVE_CLAIM_DIALECTS_BY_TENANT_ID;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setInt(1, tenantId);
        prepStmt.executeUpdate();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw new ClaimMetadataException("Error while deleting claim dialects of tenant: " + tenantId, e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
        IdentityDatabaseUtil.closeConnection(connection);
    }
}
 
Example #13
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 #14
Source File: ClaimMetadataManagementServiceImpl.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void renameClaimDialect(ClaimDialect oldClaimDialect, ClaimDialect newClaimDialect, String tenantDomain)
        throws ClaimMetadataException {

    if (oldClaimDialect == null || StringUtils.isBlank(oldClaimDialect.getClaimDialectURI())
            || newClaimDialect == null || StringUtils.isBlank(newClaimDialect.getClaimDialectURI())) {
        throw new ClaimMetadataClientException(ERROR_CODE_EMPTY_CLAIM_DIALECT);
    }

    // TODO : Validate oldClaimDialectURI is valid????

    // TODO : validate tenant domain?
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

    // Add listener

    this.claimDialectDAO.renameClaimDialect(oldClaimDialect, newClaimDialect, tenantId);
    externalClaimDAO.removeExternalClaimCache(oldClaimDialect.getClaimDialectURI(), tenantId);
    // Add listener

}
 
Example #15
Source File: DefaultClaimMetadataStore.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Override
@Deprecated
public ClaimMapping[] getAllSupportClaimMappingsByDefault() throws UserStoreException {

    try {
        List<LocalClaim> localClaims = localClaimDAO.getLocalClaims(this.tenantId);

        List<ClaimMapping> claimMappings = new ArrayList<>();

        for (LocalClaim localClaim : localClaims) {
            ClaimMapping claimMapping = ClaimMetadataUtils.convertLocalClaimToClaimMapping(localClaim, this
                    .tenantId);

            if (claimMapping.getClaim().isSupportedByDefault()) {
                claimMappings.add(claimMapping);
            }
        }

        return claimMappings.toArray(new ClaimMapping[0]);
    } catch (ClaimMetadataException e) {
        throw new UserStoreException(e.getMessage(), e);
    }
}
 
Example #16
Source File: CacheBackedLocalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public List<LocalClaim> getLocalClaims(int tenantId) throws ClaimMetadataException {

        List<LocalClaim> localClaimList = localClaimInvalidationCache.getValueFromCache(tenantId);

        if (localClaimList == null) {
            if (log.isDebugEnabled()) {
                log.debug("Cache miss for local claim list for tenant: " + tenantId);
            }
            localClaimList = localClaimDAO.getLocalClaims(tenantId);
            localClaimInvalidationCache.addToCache(tenantId, new ArrayList<>(localClaimList));
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Cache hit for local claim list for tenant: " + tenantId);
            }
        }

        return localClaimList;
    }
 
Example #17
Source File: ExternalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private void addClaimMapping(Connection connection, int externalClaimId, int localClaimId, int tenantId)
        throws ClaimMetadataException {

    PreparedStatement prepStmt = null;

    String query = SQLConstants.ADD_CLAIM_MAPPING;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setInt(1, localClaimId);
        prepStmt.setInt(2, externalClaimId);
        prepStmt.setInt(3, tenantId);
        prepStmt.executeUpdate();
    } catch (SQLException e) {
        throw new ClaimMetadataException("Error while adding claim mapping", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
 
Example #18
Source File: ExternalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private void updateClaimMapping(Connection connection, int externalClaimId, int localClaimId, int tenantId)
        throws ClaimMetadataException {

    PreparedStatement prepStmt = null;

    String query = SQLConstants.UPDATE_CLAIM_MAPPING;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setInt(1, localClaimId);
        prepStmt.setInt(2, externalClaimId);
        prepStmt.setInt(3, tenantId);
        prepStmt.executeUpdate();
    } catch (SQLException e) {
        throw new ClaimMetadataException("Error while updating claim mapping", e);
    } finally {
        IdentityDatabaseUtil.closeStatement(prepStmt);
    }
}
 
Example #19
Source File: CacheBackedClaimDialectDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public List<ClaimDialect> getClaimDialects(int tenantId) throws ClaimMetadataException {

        List<ClaimDialect> claimDialectList = claimDialectCache.getClaimDialects(tenantId);
        if (claimDialectList != null && !claimDialectList.isEmpty()) {
            if (log.isDebugEnabled()) {
                log.debug("Cache hit for claim dialect list for tenant: " + tenantId + ". Claim dialect list size: "
                        + claimDialectList.size());
            }
            return claimDialectList;
        }

        claimDialectList = super.getClaimDialects(tenantId);
        claimDialectCache.putClaimDialects(tenantId, claimDialectList);

        if (log.isDebugEnabled()) {
            log.debug("Cache miss for claim dialect list for tenant: " + tenantId + ". Updated cache with claim " +
                    "dialect list retrieved from database. Claim dialect list size: " + claimDialectList.size());
        }
        return claimDialectList;
    }
 
Example #20
Source File: CacheBackedExternalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public List<ExternalClaim> getExternalClaims(String externalDialectURI, int tenantId) throws
        ClaimMetadataException {

    ExternalClaimCacheKey cacheKey = new ExternalClaimCacheKey(externalDialectURI, tenantId);
    List<ExternalClaim> externalClaimList = externalClaimCache.getValueFromCache(cacheKey);

    if (externalClaimList == null) {
        if (log.isDebugEnabled()) {
            log.debug("Cache miss for external claim list for dialect: " + externalDialectURI + " in tenant: " +
                    tenantId);
        }
        externalClaimList = externalClaimDAO.getExternalClaims(externalDialectURI, tenantId);
        externalClaimCache.addToCache(cacheKey, new ArrayList<>(externalClaimList));
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Cache hit for external claim list for dialect: " + externalDialectURI + " in tenant: " +
                    tenantId);
        }
    }

    return externalClaimList;
}
 
Example #21
Source File: ClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example #22
Source File: ClaimDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #23
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 #24
Source File: ClaimDialectDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public void addClaimDialect(ClaimDialect claimDialect, int tenantId) throws ClaimMetadataException {

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

        String query = SQLConstants.ADD_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 adding claim dialect " + claimDialect
                    .getClaimDialectURI(), e);
        } finally {
            IdentityDatabaseUtil.closeStatement(prepStmt);
            IdentityDatabaseUtil.closeConnection(connection);
        }
    }
 
Example #25
Source File: ClaimDialectDAO.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #26
Source File: ExternalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
public void updateExternalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException {

        Connection connection = IdentityDatabaseUtil.getDBConnection();

        String externalClaimURI = externalClaim.getClaimURI();
        String externalClaimDialectURI = externalClaim.getClaimDialectURI();
        String mappedLocalClaimURI = externalClaim.getMappedLocalClaim();

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

            int externalClaimId = getClaimId(connection, externalClaimDialectURI, externalClaimURI, tenantId);
            // TODO : Handle invalid external claim URI

            int localClaimId = getClaimId(connection, ClaimConstants.LOCAL_CLAIM_DIALECT_URI, mappedLocalClaimURI,
                    tenantId);
            // TODO : Handle invalid local claim URI

            updateClaimMapping(connection, externalClaimId, localClaimId, tenantId);

            deleteClaimProperties(connection, externalClaimId, tenantId);
            addClaimProperties(connection, externalClaimId, externalClaim.getClaimProperties(), tenantId);

            // End transaction
            connection.commit();
        } catch (SQLException e) {
            rollbackTransaction(connection);
            throw new ClaimMetadataException("Error while updating external claim " + externalClaimURI + " in " +
                    "dialect " + externalClaimDialectURI, e);
        } finally {
            IdentityDatabaseUtil.closeAllConnections(connection, null, null);
        }
    }
 
Example #27
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 #28
Source File: ClaimMetadataManagementAdminService.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public void removeExternalClaim(String externalClaimDialectURI, String externalClaimURI) throws
        ClaimMetadataException {

    try {
        String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();

        IdentityClaimManagementServiceDataHolder.getInstance().getClaimManagementService()
                .removeExternalClaim(externalClaimDialectURI, externalClaimURI, tenantDomain);
    } catch (Throwable e) {
        log.error(e.getMessage(), e);
        throw new ClaimMetadataException(e.getMessage(), e);
    }
}
 
Example #29
Source File: LocalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private Map<Integer, Map<String, String>> getClaimPropertiesOfDialect(Connection connection, String claimDialectURI,
                                                                      int tenantId) throws ClaimMetadataException {

    Map<Integer, Map<String, String>> claimPropertyMap = new HashMap<>();
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    try {
        preparedStatement = connection.prepareStatement(SQLConstants.GET_CLAIMS_PROPERTIES_QUERY);
        preparedStatement.setString(1, claimDialectURI);
        preparedStatement.setInt(2, tenantId);
        preparedStatement.setInt(3, tenantId);

        resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            String propertyName = resultSet.getString(SQLConstants.PROPERTY_NAME_COLUMN);
            String propertyValue = resultSet.getString(SQLConstants.PROPERTY_VALUE_COLUMN);
            int localClaimId = resultSet.getInt(SQLConstants.LOCAL_CLAIM_ID_COLUMN);

            Map<String, String> existingAttributeMap = claimPropertyMap.get(localClaimId);

            if (existingAttributeMap == null) {
                existingAttributeMap = new HashMap<>();
            }

            existingAttributeMap.put(propertyName, propertyValue);
            claimPropertyMap.put(localClaimId, existingAttributeMap);
        }
    } catch (SQLException e) {
        throw new ClaimMetadataException("Error occurred while retrieving attribute mappings.", e);
    } finally {
        IdentityDatabaseUtil.closeResultSet(resultSet);
        IdentityDatabaseUtil.closeStatement(preparedStatement);
    }
    return claimPropertyMap;
}
 
Example #30
Source File: LocalClaimDAO.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private void addClaimAttributeMappings(Connection connection, int localClaimId, List<AttributeMapping>
        attributeMappings, int tenantId) throws ClaimMetadataException {

    PreparedStatement prepStmt = null;
    if (localClaimId > 0 && attributeMappings != null) {
        try {
            String query = SQLConstants.ADD_CLAIM_MAPPED_ATTRIBUTE;
            prepStmt = connection.prepareStatement(query);
            for (AttributeMapping attributeMapping : attributeMappings) {
                if (StringUtils.isBlank(attributeMapping.getUserStoreDomain())) {
                    throw new ClaimMetadataException("User store domain of mapped Attribute cannot be empty for " +
                            "the local claim id : " + localClaimId);
                } else if (StringUtils.isBlank(attributeMapping.getAttributeName())) {
                    throw new ClaimMetadataException("Mapped attribute of the local claim id : " + localClaimId +
                            " cannot be empty");
                }
                prepStmt.setInt(1, localClaimId);
                prepStmt.setString(2, attributeMapping.getUserStoreDomain());
                prepStmt.setString(3, attributeMapping.getAttributeName());
                prepStmt.setInt(4, tenantId);
                prepStmt.addBatch();
            }

            prepStmt.executeBatch();
        } catch (SQLException e) {
            throw new ClaimMetadataException("Error while adding attribute mappings", e);
        } finally {
            IdentityDatabaseUtil.closeStatement(prepStmt);
        }
    }
}