Search in sources :

Example 1 with CORSApplication

use of org.wso2.carbon.identity.cors.mgt.core.model.CORSApplication 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 2 with CORSApplication

use of org.wso2.carbon.identity.cors.mgt.core.model.CORSApplication in project carbon-identity-framework by wso2.

the class CORSManagementServiceTests method testGetCORSApplicationsByCORSOriginId.

@Test
public void testGetCORSApplicationsByCORSOriginId() throws CORSManagementServiceException {
    String originUUID = UUID.randomUUID().toString();
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        try {
            // Add the application.
            try (PreparedStatement preparedStatement1 = connection.prepareStatement(INSERT_APPLICATION)) {
                // Add application 1.
                preparedStatement1.setInt(1, SampleApp1.ID);
                preparedStatement1.setInt(2, SampleTenant.ID);
                preparedStatement1.setString(3, SampleApp1.NAME);
                preparedStatement1.setString(4, SampleApp1.UUID);
                preparedStatement1.executeUpdate();
            }
            // Add the application.
            try (PreparedStatement preparedStatement2 = connection.prepareStatement(INSERT_APPLICATION)) {
                // Add application 2.
                preparedStatement2.setInt(1, SampleApp2.ID);
                preparedStatement2.setInt(2, SampleApp2.ID);
                preparedStatement2.setString(3, SampleApp2.NAME);
                preparedStatement2.setString(4, SampleApp2.UUID);
                preparedStatement2.executeUpdate();
            }
            String origin = SAMPLE_ORIGIN_LIST_1.get(0);
            try (NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, INSERT_CORS_ORIGIN)) {
                // Origin is not present. Therefore add an origin.
                namedPreparedStatement.setInt(1, SampleTenant.ID);
                namedPreparedStatement.setString(2, origin);
                namedPreparedStatement.setString(3, originUUID);
                namedPreparedStatement.executeUpdate();
                // Get origin id.
                int corsOriginId;
                try (ResultSet resultSet = namedPreparedStatement.getGeneratedKeys()) {
                    if (resultSet.next()) {
                        corsOriginId = resultSet.getInt(1);
                    } else {
                        IdentityDatabaseUtil.rollbackTransaction(connection);
                        throw handleServerException(ERROR_CODE_CORS_ADD, SampleTenant.DOMAIN_NAME);
                    }
                }
                try (PreparedStatement preparedStatement4 = connection.prepareStatement(INSERT_CORS_ASSOCIATION)) {
                    // Add application associations.
                    preparedStatement4.setInt(1, corsOriginId);
                    preparedStatement4.setInt(2, SampleApp1.ID);
                    preparedStatement4.executeUpdate();
                }
                try (PreparedStatement preparedStatement5 = connection.prepareStatement(INSERT_CORS_ASSOCIATION)) {
                    // Add application associations.
                    preparedStatement5.setInt(1, corsOriginId);
                    preparedStatement5.setInt(2, SampleApp2.ID);
                    preparedStatement5.executeUpdate();
                }
            }
        } catch (SQLException e) {
            IdentityDatabaseUtil.rollbackTransaction(connection);
            throw handleServerException(ERROR_CODE_CORS_ADD, e, IdentityTenantUtil.getTenantDomain(SampleTenant.ID));
        }
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        throw handleServerException(ERROR_CODE_CORS_ADD, e, SampleTenant.DOMAIN_NAME);
    }
    List<String> retrievedCORSOriginApplicationIds = corsManagementService.getCORSApplicationsByCORSOriginId(originUUID, SampleTenant.DOMAIN_NAME).stream().map(CORSApplication::getId).collect(Collectors.toList());
    List<String> expectedApplicationIds = new ArrayList<>();
    expectedApplicationIds.add(SampleApp1.UUID);
    expectedApplicationIds.add(SampleApp2.UUID);
    assertEquals(retrievedCORSOriginApplicationIds, expectedApplicationIds);
}
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) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with CORSApplication

use of org.wso2.carbon.identity.cors.mgt.core.model.CORSApplication in project identity-api-server by wso2.

the class CORSApplicationToCORSApplicationObject method apply.

@Override
public CORSApplicationObject apply(CORSApplication corsApplication) {
    CORSApplicationObject corsApplicationObject = new CORSApplicationObject();
    corsApplicationObject.setId(corsApplication.getId());
    corsApplicationObject.setName(corsApplication.getName());
    return corsApplicationObject;
}
Also used : CORSApplicationObject(org.wso2.carbon.identity.api.server.cors.v1.model.CORSApplicationObject)

Aggregations

Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)2 PreparedStatement (java.sql.PreparedStatement)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Test (org.testng.annotations.Test)1 CORSApplicationObject (org.wso2.carbon.identity.api.server.cors.v1.model.CORSApplicationObject)1 CORSApplication (org.wso2.carbon.identity.cors.mgt.core.model.CORSApplication)1