Search in sources :

Example 1 with CORSOrigin

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

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

use of org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin 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)

Example 4 with CORSOrigin

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

the class CORSManagementServiceImpl method setCORSOrigins.

/**
 * {@inheritDoc}
 */
@Override
public void setCORSOrigins(String applicationId, List<String> origins, String tenantDomain) throws CORSManagementServiceException {
    int tenantId = getTenantId(tenantDomain);
    ApplicationBasicInfo applicationBasicInfo = getApplicationBasicInfo(applicationId, tenantDomain);
    // Check for duplicate entries.
    if (CORSConfigurationUtils.hasDuplicates(origins)) {
        throw handleClientException(ERROR_CODE_DUPLICATE_ORIGINS);
    }
    List<Origin> originList = CORSConfigurationUtils.createOriginList(origins);
    // Set the CORS origins.
    getCORSOriginDAO().setCORSOrigins(applicationBasicInfo.getApplicationId(), originList.stream().map(origin -> {
        // Create the CORS origin.
        CORSOrigin corsOrigin = new CORSOrigin();
        corsOrigin.setOrigin(origin.getValue());
        return corsOrigin;
    }).collect(Collectors.toList()), tenantId);
}
Also used : Origin(org.wso2.carbon.identity.cors.mgt.core.model.Origin) CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)

Example 5 with CORSOrigin

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

the class CORSManagementServiceImpl method addCORSOrigins.

/**
 * {@inheritDoc}
 */
@Override
public void addCORSOrigins(String applicationId, List<String> origins, String tenantDomain) throws CORSManagementServiceException {
    int tenantId = getTenantId(tenantDomain);
    ApplicationBasicInfo applicationBasicInfo = getApplicationBasicInfo(applicationId, tenantDomain);
    List<Origin> originList = CORSConfigurationUtils.createOriginList(origins);
    // Check if the CORS origins are already present.
    List<CORSOrigin> existingCORSOrigins = getCORSOriginDAO().getCORSOriginsByApplicationId(applicationBasicInfo.getApplicationId(), tenantId);
    List<String> corsOriginIdList = existingCORSOrigins.stream().map(CORSOrigin::getId).collect(Collectors.toList());
    for (Origin origin : originList) {
        if (corsOriginIdList.contains(origin.getValue())) {
            // CORS origin is already registered for the application.
            if (log.isDebugEnabled()) {
                log.debug(String.format("Duplicate addition of existing CORS Origin (%s) for the " + "application id: %s, tenant domain: %s", origin, applicationId, tenantDomain));
            }
            throw handleClientException(ERROR_CODE_ORIGIN_PRESENT, tenantDomain, origin.getValue());
        }
    }
    // Add the CORS origins.
    getCORSOriginDAO().addCORSOrigins(applicationBasicInfo.getApplicationId(), originList.stream().map(origin -> {
        // Create the CORS origin.
        CORSOrigin corsOrigin = new CORSOrigin();
        corsOrigin.setOrigin(origin.getValue());
        return corsOrigin;
    }).collect(Collectors.toList()), tenantId);
}
Also used : Origin(org.wso2.carbon.identity.cors.mgt.core.model.Origin) CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)

Aggregations

CORSOrigin (org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin)14 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)9 ResultSet (java.sql.ResultSet)8 ArrayList (java.util.ArrayList)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)5 PreparedStatement (java.sql.PreparedStatement)4 CORSManagementServiceException (org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceException)3 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)2 Origin (org.wso2.carbon.identity.cors.mgt.core.model.Origin)2 IdentityOAuthAdminException (org.wso2.carbon.identity.oauth.IdentityOAuthAdminException)2 OAuthConsumerAppDTO (org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1