Search in sources :

Example 21 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method updateBasicApplicationData.

/**
 * @param serviceProvider
 * @param connection
 * @throws SQLException
 * @throws UserStoreException
 * @throws IdentityApplicationManagementException
 */
private void updateBasicApplicationData(ServiceProvider serviceProvider, Connection connection) throws SQLException, UserStoreException, IdentityApplicationManagementException {
    int applicationId = serviceProvider.getApplicationID();
    String applicationName = serviceProvider.getApplicationName();
    String description = serviceProvider.getDescription();
    boolean isSaasApp = serviceProvider.isSaasApp();
    boolean isDiscoverable = serviceProvider.isDiscoverable();
    int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    String storedAppName = null;
    if (applicationName == null) {
        // check for required attributes.
        throw new IdentityApplicationManagementException("Application Name is required.");
    }
    if (log.isDebugEnabled()) {
        log.debug("Updating Application with id: " + applicationId);
    }
    // reads back the Application Name. This is to check if the Application
    // has been renamed
    storedAppName = getApplicationName(applicationId, connection);
    if (log.isDebugEnabled()) {
        log.debug("Stored application name for id: " + applicationId + " is " + storedAppName);
    }
    boolean validateRoles = ApplicationMgtUtil.validateRoles();
    // only if the application has been renamed TODO: move to OSGi layer
    if (!StringUtils.equals(applicationName, storedAppName) && validateRoles) {
        String applicationNameforRole = IdentityUtil.addDomainToName(applicationName, ApplicationConstants.APPLICATION_DOMAIN);
        String storedAppNameforRole = IdentityUtil.addDomainToName(storedAppName, ApplicationConstants.APPLICATION_DOMAIN);
        // rename the role
        ApplicationMgtUtil.renameRole(storedAppNameforRole, applicationNameforRole);
        if (log.isDebugEnabled()) {
            log.debug("Renaming application role from " + storedAppName + " to " + applicationName);
        }
        Map<String, String> applicationPermissions = readApplicationPermissions(storedAppName);
        for (Map.Entry<String, String> entry : applicationPermissions.entrySet()) {
            updatePermissionPath(entry.getKey(), entry.getValue().replace(storedAppName.toLowerCase(), applicationName.toLowerCase()));
        }
    }
    boolean isValidUserForOwnerUpdate = ApplicationMgtUtil.isValidApplicationOwner(serviceProvider);
    String sql;
    if (isValidUserForOwnerUpdate) {
        sql = UPDATE_BASIC_APPINFO_WITH_OWNER_UPDATE;
    } else {
        sql = UPDATE_BASIC_APPINFO;
    }
    try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, sql)) {
        statement.setString(ApplicationTableColumns.APP_NAME, applicationName);
        statement.setString(ApplicationTableColumns.DESCRIPTION, description);
        statement.setString(ApplicationTableColumns.IS_SAAS_APP, isSaasApp ? "1" : "0");
        statement.setString(ApplicationTableColumns.IS_DISCOVERABLE, isDiscoverable ? "1" : "0");
        statement.setString(ApplicationTableColumns.IMAGE_URL, serviceProvider.getImageUrl());
        statement.setString(ApplicationTableColumns.ACCESS_URL, serviceProvider.getAccessUrl());
        if (isValidUserForOwnerUpdate) {
            User owner = serviceProvider.getOwner();
            statement.setString(ApplicationTableColumns.USERNAME, owner.getUserName());
            statement.setString(ApplicationTableColumns.USER_STORE, owner.getUserStoreDomain());
        }
        statement.setInt(ApplicationTableColumns.TENANT_ID, tenantID);
        statement.setInt(ApplicationTableColumns.ID, applicationId);
        statement.executeUpdate();
    }
    if (log.isDebugEnabled()) {
        String tenantDomain = IdentityTenantUtil.getTenantDomain(tenantID);
        log.debug("Application with name: " + applicationName + " , id: " + applicationId + " in tenantDomain: " + tenantDomain + " updated successfully.");
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) User(org.wso2.carbon.identity.application.common.model.User) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method getDiscoverableApplicationBasicInfo.

private List<ApplicationBasicInfo> getDiscoverableApplicationBasicInfo(int limit, int offset, String tenantDomain) throws IdentityApplicationManagementException {
    List<ApplicationBasicInfo> applicationBasicInfoList = new ArrayList<>();
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        String databaseVendorType = connection.getMetaData().getDatabaseProductName();
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, getDBVendorSpecificDiscoverableAppRetrievalQuery(databaseVendorType))) {
            statement.setInt(ApplicationTableColumns.TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
            statement.setInt(ApplicationConstants.OFFSET, offset);
            statement.setInt(ApplicationConstants.LIMIT, limit);
            statement.setInt(ApplicationConstants.ZERO_BASED_START_INDEX, offset);
            statement.setInt(ApplicationConstants.ONE_BASED_START_INDEX, offset + 1);
            statement.setInt(ApplicationConstants.END_INDEX, offset + limit);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    applicationBasicInfoList.add(buildApplicationBasicInfo(resultSet));
                }
            }
        }
    } catch (SQLException e) {
        throw new IdentityApplicationManagementServerException("Error while getting application basic information" + " for discoverable applications in tenantDomain: " + tenantDomain, e);
    }
    return Collections.unmodifiableList(applicationBasicInfoList);
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IdentityApplicationManagementServerException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)

Example 23 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method getAppIdUsingResourceId.

/**
 * Returns the internal application id for a given resourceId in a tenant.
 *
 * @param resourceId
 * @param tenantDomain
 * @return
 * @throws IdentityApplicationManagementException
 */
private int getAppIdUsingResourceId(String resourceId, String tenantDomain) throws IdentityApplicationManagementException {
    int applicationId = 0;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, LOAD_APP_ID_BY_UUID)) {
            statement.setString(ApplicationTableColumns.UUID, resourceId);
            statement.setInt(ApplicationTableColumns.TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    applicationId = resultSet.getInt(ApplicationTableColumns.ID);
                }
            }
        }
    } catch (SQLException e) {
        String msg = "Error while retrieving the application id for resourceId: %s in tenantDomain:  %s";
        throw new IdentityApplicationManagementException(String.format(msg, resourceId, tenantDomain), e);
    }
    return applicationId;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 24 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class CORSManagementServiceTests method testAddCORSOrigins.

@Test
public void testAddCORSOrigins() throws CORSManagementServiceException {
    corsManagementService.addCORSOrigins(SampleApp1.UUID, SAMPLE_ORIGIN_LIST_1, SUPER_TENANT_DOMAIN_NAME);
    List<CORSOrigin> retrievedCORSOrigins = new ArrayList<>();
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(connection, GET_CORS_ORIGINS_BY_APPLICATION_ID)) {
            namedPreparedStatement.setInt(1, SUPER_TENANT_ID);
            namedPreparedStatement.setInt(2, SampleApp1.ID);
            try (ResultSet resultSet = namedPreparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    CORSOrigin corsOrigin = new CORSOrigin();
                    corsOrigin.setId(resultSet.getString(CORSOriginTableColumns.ID));
                    corsOrigin.setOrigin(resultSet.getString(CORSOriginTableColumns.ORIGIN));
                    retrievedCORSOrigins.add(corsOrigin);
                }
            }
        }
    } catch (SQLException e) {
        throw handleServerException(ERROR_CODE_CORS_RETRIEVE, e, SUPER_TENANT_DOMAIN_NAME);
    }
    assertEquals(retrievedCORSOrigins.stream().map(CORSOrigin::getOrigin).collect(Collectors.toList()), SAMPLE_ORIGIN_LIST_1);
}
Also used : CORSOrigin(org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthScopeDAOImpl method getPreparedStatementForGetScopesWithPagination.

/**
 * Get SQL statement for get OAuth2 scope with pagination.
 *
 * @param offset   Offset.
 * @param limit    Limit.
 * @param tenantID Tenet ID.
 * @param conn     Database connection.
 * @return
 * @throws SQLException
 */
private NamedPreparedStatement getPreparedStatementForGetScopesWithPagination(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_SCOPES_WITH_PAGINATION_MYSQL;
    } else if (conn.getMetaData().getDatabaseProductName().contains("DB2")) {
        query = SQLQueries.RETRIEVE_SCOPES_WITH_PAGINATION_DB2SQL;
    } else if (driverName.contains("MS SQL")) {
        query = SQLQueries.RETRIEVE_SCOPES_WITH_PAGINATION_MSSQL;
    } else if (driverName.contains("Microsoft") || driverName.contains("microsoft")) {
        query = SQLQueries.RETRIEVE_SCOPES_WITH_PAGINATION_MSSQL;
    } else if (driverName.contains("PostgreSQL")) {
        query = SQLQueries.RETRIEVE_SCOPES_WITH_PAGINATION_POSTGRESQL;
    } else if (driverName.contains("Informix")) {
        // Driver name = "IBM Informix JDBC Driver for IBM Informix Dynamic Server"
        query = SQLQueries.RETRIEVE_SCOPES_WITH_PAGINATION_INFORMIX;
    } else {
        query = SQLQueries.RETRIEVE_SCOPES_WITH_PAGINATION_ORACLE;
    }
    NamedPreparedStatement namedPreparedStatement = new NamedPreparedStatement(conn, query);
    namedPreparedStatement.setString(Oauth2ScopeConstants.SQLPlaceholders.SCOPE_TYPE, Oauth2ScopeConstants.SCOPE_TYPE_OAUTH2);
    namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.TENANT_ID, tenantID);
    namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.OFFSET, offset);
    namedPreparedStatement.setInt(Oauth2ScopeConstants.SQLPlaceholders.LIMIT, limit);
    return namedPreparedStatement;
}
Also used : NamedPreparedStatement(org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)

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