Search in sources :

Example 21 with TransactionException

use of org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException in project carbon-identity-framework by wso2.

the class SecretDAOImpl method addSecret.

@Override
public void addSecret(Secret secret) throws SecretManagementException {
    SecretType secretType = getSecretTypeByName(secret.getSecretType());
    Timestamp currentTime = new java.sql.Timestamp(new Date().getTime());
    NamedJdbcTemplate jdbcTemplate = getNewTemplate();
    try {
        jdbcTemplate.withTransaction(template -> {
            // Insert secret metadata.
            template.executeInsert(INSERT_SECRET, preparedStatement -> {
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ID, secret.getSecretId());
                preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SECRET_NAME, secret.getSecretName());
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SECRET_VALUE, secret.getSecretValue());
                preparedStatement.setTimeStamp(DB_SCHEMA_COLUMN_NAME_CREATED_TIME, currentTime, calendar);
                preparedStatement.setTimeStamp(DB_SCHEMA_COLUMN_NAME_LAST_MODIFIED, currentTime, calendar);
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_TYPE, secretType.getId());
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_DESCRIPTION, secret.getDescription());
            }, secret, false);
            return null;
        });
        secret.setLastModified(currentTime.toInstant().toString());
        secret.setCreatedTime(currentTime.toInstant().toString());
        secret.setSecretType(secretType.getName());
    } catch (TransactionException e) {
        throw handleServerException(ERROR_CODE_ADD_SECRET, secret.getSecretName(), e);
    }
}
Also used : TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) SecretType(org.wso2.carbon.identity.secret.mgt.core.model.SecretType) NamedJdbcTemplate(org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 22 with TransactionException

use of org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeClaimMappingDAOImpl method insertClaims.

private void insertClaims(int tenantId, int scopeId, Set<String> claimsList) throws IdentityOAuth2Exception {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    int scopeClaimMappingId = -1;
    try {
        jdbcTemplate.withTransaction(template -> {
            template.executeBatchInsert(SQLQueries.STORE_IDN_OIDC_CLAIMS, (preparedStatement -> {
                if (CollectionUtils.isNotEmpty(claimsList)) {
                    for (String claim : claimsList) {
                        preparedStatement.setInt(1, scopeId);
                        preparedStatement.setString(2, claim);
                        preparedStatement.setInt(3, tenantId);
                        preparedStatement.addBatch();
                        if (log.isDebugEnabled()) {
                            log.debug("Claim value :" + claim + " is added to the batch.");
                        }
                    }
                }
            }), scopeClaimMappingId);
            return null;
        });
    } catch (TransactionException e) {
        String errorMessage = String.format("Error when storing oidc claims for scope ID: %s for tenant: %s", scopeId, tenantId);
        throw new IdentityOAuth2Exception(errorMessage, e);
    }
}
Also used : IdentityOAuth2ClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ClientException) Arrays(java.util.Arrays) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) LambdaExceptionUtils.rethrowConsumer(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer) Oauth2ScopeConstants(org.wso2.carbon.identity.oauth2.Oauth2ScopeConstants) Set(java.util.Set) HashMap(java.util.HashMap) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) JdbcUtils(org.wso2.carbon.identity.oauth2.util.JdbcUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) ScopeDTO(org.wso2.carbon.identity.oauth.dto.ScopeDTO) Map(java.util.Map) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) Log(org.apache.commons.logging.Log) RowMapper(org.wso2.carbon.database.utils.jdbc.RowMapper) LogFactory(org.apache.commons.logging.LogFactory) ArrayUtils(org.apache.commons.lang.ArrayUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate)

Example 23 with TransactionException

use of org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeClaimMappingDAOImpl method hasScopesPopulated.

public boolean hasScopesPopulated(int tenantId) throws IdentityOAuth2Exception {
    Integer id;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        id = jdbcTemplate.withTransaction(template -> template.fetchSingleRecord(SQLQueries.GET_ALL_IDN_OIDC_SCOPES, (resultSet, rowNumber) -> resultSet.getInt(1), preparedStatement -> {
            preparedStatement.setInt(1, tenantId);
            preparedStatement.setString(2, Oauth2ScopeConstants.SCOPE_TYPE_OIDC);
        }));
        if (id == 0) {
            return false;
        }
        if (log.isDebugEnabled()) {
            log.debug("Scope id: " + id + "is returned for the tenant: " + tenantId);
        }
    } catch (TransactionException e) {
        String errorMessage = "Error while loading the top scope id for the tenant: " + tenantId;
        throw new IdentityOAuth2Exception(errorMessage, e);
    }
    return true;
}
Also used : IdentityOAuth2ClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ClientException) Arrays(java.util.Arrays) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) LambdaExceptionUtils.rethrowConsumer(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer) Oauth2ScopeConstants(org.wso2.carbon.identity.oauth2.Oauth2ScopeConstants) Set(java.util.Set) HashMap(java.util.HashMap) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) JdbcUtils(org.wso2.carbon.identity.oauth2.util.JdbcUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) ScopeDTO(org.wso2.carbon.identity.oauth.dto.ScopeDTO) Map(java.util.Map) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) Log(org.apache.commons.logging.Log) RowMapper(org.wso2.carbon.database.utils.jdbc.RowMapper) LogFactory(org.apache.commons.logging.LogFactory) ArrayUtils(org.apache.commons.lang.ArrayUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate)

Example 24 with TransactionException

use of org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeClaimMappingDAOImpl method loadOIDCClaimId.

private int loadOIDCClaimId(String claim, int tenantId) throws IdentityOAuth2Exception {
    Integer oidcClaimId;
    try {
        JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
        oidcClaimId = jdbcTemplate.withTransaction(template -> template.fetchSingleRecord(SQLQueries.GET_OIDC_CLAIM_ID, (resultSet, rowNumber) -> resultSet.getInt(1), preparedStatement -> {
            preparedStatement.setString(1, claim);
            preparedStatement.setInt(2, tenantId);
            preparedStatement.setString(3, OIDC_DIALECT_URI);
            preparedStatement.setInt(4, tenantId);
        }));
        if (oidcClaimId == null) {
            oidcClaimId = -1;
        }
        if (log.isDebugEnabled()) {
            log.debug("Claim id: " + oidcClaimId + "is returned.");
        }
    } catch (TransactionException e) {
        String errorMessage = "Error fetching data for oidc scope: " + claim;
        throw new IdentityOAuth2Exception(errorMessage, e);
    }
    return oidcClaimId;
}
Also used : IdentityOAuth2ClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ClientException) Arrays(java.util.Arrays) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) LambdaExceptionUtils.rethrowConsumer(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer) Oauth2ScopeConstants(org.wso2.carbon.identity.oauth2.Oauth2ScopeConstants) Set(java.util.Set) HashMap(java.util.HashMap) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) JdbcUtils(org.wso2.carbon.identity.oauth2.util.JdbcUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) ScopeDTO(org.wso2.carbon.identity.oauth.dto.ScopeDTO) Map(java.util.Map) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) Log(org.apache.commons.logging.Log) RowMapper(org.wso2.carbon.database.utils.jdbc.RowMapper) LogFactory(org.apache.commons.logging.LogFactory) ArrayUtils(org.apache.commons.lang.ArrayUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate)

Example 25 with TransactionException

use of org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeClaimMappingDAOImpl method getScopeIdWithoutScopeType.

/**
 * Obtain scope ID for proivded scope name regardless of scope type.
 *
 * @param scope    Scope name.
 * @param tenantId Tenant ID.
 * @return Scope ID.
 * @throws IdentityOAuth2Exception
 */
private int getScopeIdWithoutScopeType(String scope, int tenantId) throws IdentityOAuth2Exception {
    Integer scopeId;
    try {
        JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
        scopeId = jdbcTemplate.withTransaction(template -> template.fetchSingleRecord(SQLQueries.GET_IDN_OIDC_SCOPE_ID_WITHOUT_SCOPE_TYPE, (resultSet, rowNumber) -> resultSet.getInt(1), preparedStatement -> {
            preparedStatement.setString(1, scope);
            preparedStatement.setInt(2, tenantId);
        }));
        if (scopeId == null) {
            scopeId = Oauth2ScopeConstants.INVALID_SCOPE_ID;
        }
        if (log.isDebugEnabled()) {
            log.debug("Scope id: " + scopeId + "is returned for the tenant: " + tenantId + "and scope: " + scope);
        }
    } catch (TransactionException e) {
        String errorMessage = "Error while obtaining ID of scope: " + scope;
        throw new IdentityOAuth2Exception(errorMessage, e);
    }
    return scopeId;
}
Also used : IdentityOAuth2ClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ClientException) Arrays(java.util.Arrays) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) LambdaExceptionUtils.rethrowConsumer(org.wso2.carbon.identity.core.util.LambdaExceptionUtils.rethrowConsumer) Oauth2ScopeConstants(org.wso2.carbon.identity.oauth2.Oauth2ScopeConstants) Set(java.util.Set) HashMap(java.util.HashMap) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) JdbcUtils(org.wso2.carbon.identity.oauth2.util.JdbcUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) ScopeDTO(org.wso2.carbon.identity.oauth.dto.ScopeDTO) Map(java.util.Map) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) Log(org.apache.commons.logging.Log) RowMapper(org.wso2.carbon.database.utils.jdbc.RowMapper) LogFactory(org.apache.commons.logging.LogFactory) ArrayUtils(org.apache.commons.lang.ArrayUtils) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate)

Aggregations

TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)25 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)23 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)18 Timestamp (java.sql.Timestamp)15 Date (java.util.Date)15 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)13 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)13 List (java.util.List)13 Map (java.util.Map)13 Set (java.util.Set)13 Log (org.apache.commons.logging.Log)13 LogFactory (org.apache.commons.logging.LogFactory)13 InputStream (java.io.InputStream)8 Blob (java.sql.Blob)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 UTC (java.time.ZoneOffset.UTC)8 Calendar (java.util.Calendar)8