Search in sources :

Example 1 with NamedPreparedStatement

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

the class CORSOriginDAOImpl method getCORSOriginsByApplicationId.

/**
 * {@inheritDoc}
 */
@Override
public List<CORSOrigin> getCORSOriginsByApplicationId(int applicationId, int tenantId) throws CORSManagementServiceServerException {
    String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, GET_CORS_ORIGINS_BY_APPLICATION_ID)) {
        namedPreparedStatement.setInt(1, tenantId);
        namedPreparedStatement.setInt(2, applicationId);
        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);
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList)

Example 2 with NamedPreparedStatement

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

the class CORSOriginDAOImpl method setCORSOrigins.

/**
 * {@inheritDoc}
 */
@Override
public void setCORSOrigins(int applicationId, List<CORSOrigin> corsOrigins, int tenantId) throws CORSManagementServiceServerException {
    String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try (NamedPreparedStatement namedPreparedStatement1 = new NamedPreparedStatement(connection, GET_CORS_ORIGINS_BY_TENANT_ID)) {
            // Delete existing application associations.
            namedPreparedStatement1.setInt(1, tenantId);
            try (ResultSet resultSet = namedPreparedStatement1.executeQuery()) {
                while (resultSet.next()) {
                    try (PreparedStatement preparedStatement = connection.prepareStatement(DELETE_CORS_APPLICATION_ASSOCIATION)) {
                        preparedStatement.setInt(1, resultSet.getInt("ID"));
                        preparedStatement.setInt(2, applicationId);
                        preparedStatement.executeUpdate();
                    }
                }
            }
            for (CORSOrigin corsOrigin : corsOrigins) {
                // Check if the origins is there.
                try (NamedPreparedStatement namedPreparedStatement2 = new NamedPreparedStatement(connection, GET_CORS_ORIGIN_ID)) {
                    namedPreparedStatement2.setInt(TENANT_ID, tenantId);
                    namedPreparedStatement2.setString(ORIGIN, corsOrigin.getOrigin());
                    try (ResultSet resultSet1 = namedPreparedStatement2.executeQuery()) {
                        int corsOriginId = -1;
                        if (!resultSet1.next()) {
                            try (NamedPreparedStatement namedPreparedStatement3 = new NamedPreparedStatement(connection, INSERT_CORS_ORIGIN)) {
                                // Origin is not present. Therefore add an origin.
                                namedPreparedStatement3.setInt(TENANT_ID, tenantId);
                                namedPreparedStatement3.setString(ORIGIN, corsOrigin.getOrigin());
                                namedPreparedStatement3.setString(UNIQUE_ID, UUID.randomUUID().toString());
                                namedPreparedStatement3.executeUpdate();
                                // Get origin id.
                                try (ResultSet resultSet2 = namedPreparedStatement3.getGeneratedKeys()) {
                                    if (resultSet2.next()) {
                                        corsOriginId = resultSet2.getInt(1);
                                    }
                                }
                            }
                        } else {
                            // Get origin id.
                            corsOriginId = resultSet1.getInt(CORSOriginTableColumns.ID);
                        }
                        // Add application associations.
                        try (PreparedStatement preparedStatement4 = connection.prepareStatement(INSERT_CORS_ASSOCIATION)) {
                            preparedStatement4.setInt(1, corsOriginId);
                            preparedStatement4.setInt(2, applicationId);
                            preparedStatement4.executeUpdate();
                        }
                    }
                }
            }
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw handleServerException(ERROR_CODE_CORS_ADD, e, tenantDomain);
        }
        // Commit the transaction as no errors were thrown.
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        throw handleServerException(ERROR_CODE_CORS_ADD, e, tenantDomain);
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement)

Example 3 with NamedPreparedStatement

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

the class CORSOriginDAOImpl method cleanupDanglingOrigins.

private void cleanupDanglingOrigins(Connection connection, int tenantId) throws SQLException {
    // Get tenant CORS origins.
    try (NamedPreparedStatement namedPreparedStatement1 = new NamedPreparedStatement(connection, GET_CORS_ORIGINS_BY_TENANT_ID)) {
        namedPreparedStatement1.setInt(1, tenantId);
        try (ResultSet resultSet1 = namedPreparedStatement1.executeQuery()) {
            while (resultSet1.next()) {
                int corsOriginId = resultSet1.getInt("ID");
                try (NamedPreparedStatement namedPreparedStatement2 = new NamedPreparedStatement(connection, GET_CORS_APPLICATION_IDS_BY_CORS_ORIGIN_ID)) {
                    namedPreparedStatement2.setInt(1, corsOriginId);
                    try (ResultSet resultSet2 = namedPreparedStatement2.executeQuery()) {
                        // Get the associated applications.
                        if (!resultSet2.next()) {
                            try (NamedPreparedStatement namedPreparedStatement3 = new NamedPreparedStatement(connection, DELETE_ORIGIN)) {
                                namedPreparedStatement3.setInt(1, corsOriginId);
                                namedPreparedStatement3.executeUpdate();
                            }
                        }
                    }
                }
            }
        }
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw e;
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 4 with NamedPreparedStatement

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

the class CORSOriginDAOImpl method getCORSOriginApplications.

/**
 * {@inheritDoc}
 */
@Override
public List<CORSApplication> getCORSOriginApplications(String corsOriginId) throws CORSManagementServiceServerException {
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false);
        NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, GET_CORS_APPLICATIONS_BY_CORS_ORIGIN_ID)) {
        namedPreparedStatement.setString(1, corsOriginId);
        try (ResultSet resultSet = namedPreparedStatement.executeQuery()) {
            List<CORSApplication> corsApplications = new ArrayList<>();
            while (resultSet.next()) {
                CORSApplication corsApplication = new CORSApplication(resultSet.getString(UNIQUE_ID), resultSet.getString("APP_NAME"));
                corsApplications.add(corsApplication);
            }
            return corsApplications;
        }
    } catch (SQLException e) {
        throw handleServerException(ERROR_CODE_CORS_APPLICATIONS_RETRIEVE, e, String.valueOf(corsOriginId));
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) CORSApplication(org.wso2.carbon.identity.cors.mgt.core.model.CORSApplication)

Example 5 with NamedPreparedStatement

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

the class CORSOriginDAOImpl method addCORSOrigins.

/**
 * {@inheritDoc}
 */
@Override
public void addCORSOrigins(int applicationId, List<CORSOrigin> corsOrigins, int tenantId) throws CORSManagementServiceServerException {
    String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantId);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try {
            for (CORSOrigin corsOrigin : corsOrigins) {
                // Check if the origins is there.
                try (NamedPreparedStatement namedPreparedStatement1 = new NamedPreparedStatement(connection, GET_CORS_ORIGIN_ID)) {
                    namedPreparedStatement1.setInt(1, tenantId);
                    namedPreparedStatement1.setString(2, corsOrigin.getOrigin());
                    try (ResultSet resultSet1 = namedPreparedStatement1.executeQuery()) {
                        if (!resultSet1.next()) {
                            // Origin is not present. Therefore add an origin without the tenant association.
                            try (NamedPreparedStatement namedPreparedStatement2 = new NamedPreparedStatement(connection, INSERT_CORS_ORIGIN)) {
                                namedPreparedStatement2.setInt(TENANT_ID, tenantId);
                                namedPreparedStatement2.setString(ORIGIN, corsOrigin.getOrigin());
                                namedPreparedStatement2.setString(UNIQUE_ID, UUID.randomUUID().toString());
                                namedPreparedStatement2.executeUpdate();
                            }
                        }
                    }
                }
                try (NamedPreparedStatement namedPreparedStatement3 = new NamedPreparedStatement(connection, GET_CORS_ORIGIN_ID)) {
                    // Get origin id.
                    namedPreparedStatement3.setInt(TENANT_ID, tenantId);
                    namedPreparedStatement3.setString(ORIGIN, corsOrigin.getOrigin());
                    try (ResultSet resultSet2 = namedPreparedStatement3.executeQuery()) {
                        if (resultSet2.next()) {
                            int corsOriginId = resultSet2.getInt("ID");
                            // Add application associations.
                            try (PreparedStatement preparedStatement4 = connection.prepareStatement(INSERT_CORS_ASSOCIATION)) {
                                preparedStatement4.setInt(1, corsOriginId);
                                preparedStatement4.setInt(2, applicationId);
                                preparedStatement4.executeUpdate();
                            }
                        } else {
                            IdentityDatabaseUtil.rollbackTransaction(connection);
                            throw handleServerException(ERROR_CODE_CORS_ADD, tenantDomain);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw handleServerException(ERROR_CODE_CORS_ADD, e, tenantDomain);
        }
        // Commit the transaction as no errors were thrown.
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        throw handleServerException(ERROR_CODE_CORS_ADD, e, tenantDomain);
    }
}
Also used : CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement)

Aggregations

NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)45 SQLException (java.sql.SQLException)40 Connection (java.sql.Connection)39 ResultSet (java.sql.ResultSet)33 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)17 ArrayList (java.util.ArrayList)12 IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)12 PreparedStatement (java.sql.PreparedStatement)7 CORSOrigin (org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.testng.annotations.Test)6 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)6 HashMap (java.util.HashMap)5 IdentityApplicationManagementServerException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException)4 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)4 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)4 NamedPreparedStatement (org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)3 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)3 UserRealm (org.wso2.carbon.user.api.UserRealm)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3