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);
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations