Search in sources :

Example 1 with CORSManagementServiceServerException

use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceServerException in project carbon-identity-framework by wso2.

the class CORSConfigurationDAOImpl method setCORSConfigurationByTenantDomain.

/**
 * {@inheritDoc}
 */
@Override
public void setCORSConfigurationByTenantDomain(CORSConfiguration corsConfiguration, String tenantDomain) throws CORSManagementServiceServerException {
    try {
        FrameworkUtils.startTenantFlow(tenantDomain);
        ResourceAdd resourceAdd = new CORSConfigurationToResourceAdd().apply(corsConfiguration);
        getConfigurationManager().replaceResource(CORS_CONFIGURATION_RESOURCE_TYPE_NAME, resourceAdd);
    } catch (ConfigurationManagementException e) {
        throw handleServerException(ERROR_CODE_CORS_CONFIG_SET, e, tenantDomain);
    } finally {
        FrameworkUtils.endTenantFlow();
    }
}
Also used : CORSConfigurationToResourceAdd(org.wso2.carbon.identity.cors.mgt.core.internal.function.CORSConfigurationToResourceAdd) ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) CORSConfigurationToResourceAdd(org.wso2.carbon.identity.cors.mgt.core.internal.function.CORSConfigurationToResourceAdd) ResourceAdd(org.wso2.carbon.identity.configuration.mgt.core.model.ResourceAdd)

Example 2 with CORSManagementServiceServerException

use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceServerException 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 3 with CORSManagementServiceServerException

use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceServerException 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 4 with CORSManagementServiceServerException

use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceServerException 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 CORSManagementServiceServerException

use of org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceServerException 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

Connection (java.sql.Connection)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)6 CORSOrigin (org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin)4 PreparedStatement (java.sql.PreparedStatement)3 ArrayList (java.util.ArrayList)3 Response (javax.ws.rs.core.Response)2 APIError (org.wso2.carbon.identity.api.server.common.error.APIError)2 ErrorResponse (org.wso2.carbon.identity.api.server.common.error.ErrorResponse)2 ConfigurationManagementException (org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException)2 CORSManagementServiceClientException (org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceClientException)2 CORSManagementServiceServerException (org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceServerException)2 CORSConfiguration (org.wso2.carbon.identity.cors.mgt.core.model.CORSConfiguration)2 Resource (org.wso2.carbon.identity.configuration.mgt.core.model.Resource)1 ResourceAdd (org.wso2.carbon.identity.configuration.mgt.core.model.ResourceAdd)1 CORSConfigurationToResourceAdd (org.wso2.carbon.identity.cors.mgt.core.internal.function.CORSConfigurationToResourceAdd)1 ResourceToCORSConfiguration (org.wso2.carbon.identity.cors.mgt.core.internal.function.ResourceToCORSConfiguration)1 CORSApplication (org.wso2.carbon.identity.cors.mgt.core.model.CORSApplication)1