use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project carbon-identity-framework by wso2.
the class CORSOriginDAOImpl method getCORSOriginsByTenantId.
/**
* {@inheritDoc}
*/
@Override
public List<CORSOrigin> getCORSOriginsByTenantId(int tenantId) throws CORSManagementServiceServerException {
String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, GET_CORS_ORIGINS_BY_TENANT_ID)) {
namedPreparedStatement.setInt(1, tenantId);
try (ResultSet resultSet = namedPreparedStatement.executeQuery()) {
List<CORSOrigin> corsOrigins = new ArrayList<>();
while (resultSet.next()) {
CORSOrigin corsOrigin = new CORSOrigin();
corsOrigin.setOrigin(resultSet.getString(ORIGIN));
corsOrigin.setId(resultSet.getString(UNIQUE_ID));
corsOrigins.add(corsOrigin);
}
return corsOrigins;
}
} catch (SQLException e) {
throw handleServerException(ERROR_CODE_CORS_RETRIEVE, e, tenantDomain);
}
}
use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project carbon-identity-framework by wso2.
the class CORSOriginDAOImpl method deleteCORSOrigins.
/**
* {@inheritDoc}
*/
@Override
public void deleteCORSOrigins(int applicationId, List<String> corsOriginIds, int tenantId) throws CORSManagementServiceServerException {
String currentId = null;
try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
try {
for (String corsOriginId : corsOriginIds) {
currentId = corsOriginId;
try (NamedPreparedStatement namedPreparedStatement1 = new NamedPreparedStatement(connection, GET_CORS_ORIGIN_ID_BY_UUID)) {
namedPreparedStatement1.setString(1, corsOriginId);
try (ResultSet resultSet = namedPreparedStatement1.executeQuery()) {
if (resultSet.next()) {
int corsOriginDbId = resultSet.getInt(CORSOriginTableColumns.ID);
// Delete application association.
try (PreparedStatement preparedStatement2 = connection.prepareStatement(DELETE_CORS_APPLICATION_ASSOCIATION)) {
preparedStatement2.setInt(1, corsOriginDbId);
preparedStatement2.setInt(2, applicationId);
preparedStatement2.executeUpdate();
}
} else {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw handleServerException(ERROR_CODE_CORS_DELETE, currentId);
}
}
}
}
// Cleanup dangling origins (origins without any association to an application) is disabled temporary.
// Even the CORS Origins are stored for each application separately, the CORS valve filters them
// based on the tenant level. Because of that there might be other applications which are not configured
// allowed origins but still working as another application has already set is as an allowed origin.
// Related issue: https://github.com/wso2/product-is/issues/11241
// cleanupDanglingOrigins(connection, tenantId);
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(connection);
throw handleServerException(ERROR_CODE_CORS_DELETE, e, currentId);
}
// Commit the transaction as no errors were thrown.
IdentityDatabaseUtil.commitTransaction(connection);
} catch (SQLException e) {
throw handleServerException(ERROR_CODE_CORS_DELETE, e, currentId);
}
}
use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthScopeDAOImpl method getPreparedStatementForGetAllScopesWithPagination.
/**
* Get SQL statement for get all scope with pagination. (including OAuth2 scopes and OIDC scopes).
*
* @param offset Offset.
* @param limit Limit.
* @param tenantID Tenet ID.
* @param conn Database connection.
* @return
* @throws SQLException
*/
private NamedPreparedStatement getPreparedStatementForGetAllScopesWithPagination(Integer offset, Integer limit, int tenantID, Connection conn) throws SQLException {
String query;
String driverName = conn.getMetaData().getDriverName();
if (driverName.contains("MySQL") || driverName.contains("MariaDB") || driverName.contains("H2")) {
query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_MYSQL;
} else if (conn.getMetaData().getDatabaseProductName().contains("DB2")) {
query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_DB2SQL;
} else if (driverName.contains("MS SQL")) {
query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_MSSQL;
} else if (driverName.contains("Microsoft") || driverName.contains("microsoft")) {
query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_MSSQL;
} else if (driverName.contains("PostgreSQL")) {
query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_POSTGRESQL;
} else if (driverName.contains("Informix")) {
// Driver name = "IBM Informix JDBC Driver for IBM Informix Dynamic Server"
query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_INFORMIX;
} else {
query = SQLQueries.RETRIEVE_ALL_SCOPES_WITH_PAGINATION_ORACLE;
}
NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(conn, query);
namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.TENANT_ID, tenantID);
namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.OFFSET, offset);
namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.LIMIT, limit);
return namedPreparedStatement;
}
use of org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthScopeDAOImpl method getScopesWithPagination.
@Override
public Set<Scope> getScopesWithPagination(Integer offset, Integer limit, int tenantID, Boolean includeOIDCScopes) throws IdentityOAuth2ScopeServerException {
if (log.isDebugEnabled()) {
log.debug("Get all scopes with pagination for tenantId :" + tenantID + " including OIDC scope: " + includeOIDCScopes);
}
Set<Scope> scopes = new HashSet<>();
Map<Integer, Scope> scopeMap = new HashMap<>();
try (Connection conn = IdentityDatabaseUtil.getDBConnection(false)) {
NamedPreparedStatement namedPreparedStatement;
if (includeOIDCScopes) {
namedPreparedStatement = getPreparedStatementForGetAllScopesWithPagination(offset, limit, tenantID, conn);
} else {
namedPreparedStatement = getPreparedStatementForGetScopesWithPagination(offset, limit, tenantID, conn);
}
try (PreparedStatement preparedStatement = namedPreparedStatement.getPreparedStatement()) {
try (ResultSet rs = preparedStatement.executeQuery()) {
while (rs.next()) {
int scopeID = rs.getInt(1);
String name = rs.getString(2);
String displayName = rs.getString(3);
String description = rs.getString(4);
final String binding = rs.getString(5);
if (scopeMap.containsKey(scopeID) && scopeMap.get(scopeID) != null) {
scopeMap.get(scopeID).setName(name);
scopeMap.get(scopeID).setDescription(description);
scopeMap.get(scopeID).setDisplayName(displayName);
if (binding != null) {
if (scopeMap.get(scopeID).getBindings() != null) {
scopeMap.get(scopeID).addBinding(binding);
} else {
scopeMap.get(scopeID).setBindings(new ArrayList<String>() {
{
add(binding);
}
});
}
}
} else {
scopeMap.put(scopeID, new Scope(name, displayName, description, new ArrayList<String>()));
if (binding != null) {
scopeMap.get(scopeID).addBinding(binding);
}
}
}
}
}
for (Map.Entry<Integer, Scope> entry : scopeMap.entrySet()) {
scopes.add(entry.getValue());
}
return scopes;
} catch (SQLException e) {
String msg = "Error occurred while getting all scopes with pagination ";
throw new IdentityOAuth2ScopeServerException(msg, e);
}
}
Aggregations