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.");
}
}
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);
}
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;
}
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);
}
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;
}
Aggregations