Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaminduR committed Aug 21, 2023
1 parent a686c6a commit e3d0db5
Show file tree
Hide file tree
Showing 12 changed files with 1,153 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<artifactId>powermock-module-testng-common</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.testutil</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ APIResourceSearchResult getAPIResources(String after, String before, Integer lim
String tenantDomain)
throws APIResourceMgtException;


/**
* Get API resources total count.
*
* @param filter Filter expression.
* @param tenantDomain Tenant domain.
* @return API resource total count.
* @throws APIResourceMgtException If an error occurs while retrieving API resources total count.
*/
int getAPIResourceTotalCount(String filter, String tenantDomain) throws APIResourceMgtException;

/**
* Get API resource by id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public APIResourceSearchResult getAPIResources(String after, String before, Inte
return result;
}

@Override
public int getAPIResourceTotalCount(String filter, String tenantDomain) throws APIResourceMgtException {
return 0;
}

@Override
public APIResource getAPIResourceById(String apiResourceId, String tenantDomain)
throws APIResourceMgtException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,11 @@ public class SQLConstants {
public static final String SCOPE_UNIQUE_CONSTRAINT = "scope_unique";
public static final String GET_API_RESOURCES = "SELECT ID, CURSOR_KEY, NAME, IDENTIFIER, DESCRIPTION, TENANT_ID," +
" TYPE, REQUIRES_AUTHORIZATION FROM API_RESOURCE WHERE ";
public static final String GET_API_RESOURCES_TAIL = " TENANT_ID = %d ORDER BY CURSOR_KEY %s LIMIT %d";
public static final String GET_API_RESOURCES_TAIL_ORACLE =
" TENANT_ID = %d ORDER BY CURSOR_KEY %s FETCH NEXT %d ROWS ONLY";
public static final String GET_API_RESOURCES_TAIL_MSSQL =
" TENANT_ID = %d ORDER BY CURSOR_KEY %s FETCH NEXT %d ROWS ONLY";
public static final String GET_API_RESOURCES_TAIL_H2 =
" TENANT_ID = %d ORDER BY CURSOR_KEY %s FETCH NEXT %d ROWS ONLY";
public static final String GET_API_RESOURCES_TAIL_DB2 =
// public static final String GET_API_RESOURCES_TAIL = " TENANT_ID = %d ORDER BY CURSOR_KEY %s LIMIT %d";
public static final String GET_API_RESOURCES_TAIL =
" TENANT_ID = %d ORDER BY CURSOR_KEY %s FETCH NEXT %d ROWS ONLY";
public static final String GET_API_RESOURCES_COUNT = "SELECT COUNT(DISTINCT(ID)) FROM API_RESOURCE WHERE ";
public static final String GET_API_RESOURCES_COUNT_TAIL = " TENANT_ID = ?";
public static final String GET_API_RESOURCES_COUNT_TAIL_ORACLE = " TENANT_ID = ?";
public static final String GET_API_RESOURCES_COUNT_TAIL_MSSQL = " TENANT_ID = ?";
public static final String GET_API_RESOURCES_COUNT_TAIL_H2 = " TENANT_ID = ?";
public static final String GET_API_RESOURCES_COUNT_TAIL_DB2 = " TENANT_ID = ?";
public static final String GET_API_RESOURCE_BY_ID = "SELECT" +
" AR.ID AS API_RESOURCE_ID," +
" AR.NAME AS API_RESOURCE_NAME," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ public Integer getAPIResourcesCount(Integer tenantId, List<ExpressionNode> expre
Map<Integer, String> filterAttributeValue = filterQueryBuilder.getFilterAttributeValue();
String sqlStmt;
String getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_TAIL;
if (APIResourceManagementUtil.isH2() || APIResourceManagementUtil.isMySQL()
|| APIResourceManagementUtil.isMariaDB()) {
getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_TAIL_H2;
} else if (APIResourceManagementUtil.isOracleDB()) {
getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_TAIL_ORACLE;
} else if (APIResourceManagementUtil.isMSSqlDB()) {
getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_TAIL_MSSQL;
} else if (APIResourceManagementUtil.isDB2()) {
getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_TAIL_DB2;
}

sqlStmt = SQLConstants.GET_API_RESOURCES_COUNT + filterQueryBuilder.getFilterQuery() +
getAPIResourcesCountSqlStmtTail;
Expand Down Expand Up @@ -138,8 +128,10 @@ public APIResource addAPIResource(APIResource apiResource, Integer tenantId) thr
prepStmt.executeUpdate();
prepStmt.clearParameters();

// Add scopes.
addScopes(dbConnection, generatedAPIId, apiResource.getScopes(), tenantId);
if (CollectionUtils.isNotEmpty(apiResource.getScopes())) {
// Add scopes.
addScopes(dbConnection, generatedAPIId, apiResource.getScopes(), tenantId);
}
IdentityDatabaseUtil.commitTransaction(dbConnection);

return getAPIResourceById(generatedAPIId, tenantId);
Expand Down Expand Up @@ -324,8 +316,10 @@ public void updateAPIResource(APIResource apiResource, List<Scope> addedScopes,
preparedStatement.setString(3, apiResource.getId());
preparedStatement.executeUpdate();

// Add Scopes.
addScopes(dbConnection, apiResource.getId(), addedScopes, tenantId);
if (CollectionUtils.isNotEmpty(addedScopes)) {
// Add Scopes.
addScopes(dbConnection, apiResource.getId(), addedScopes, tenantId);
}

IdentityDatabaseUtil.commitTransaction(dbConnection);
} catch (SQLException e) {
Expand Down Expand Up @@ -496,6 +490,7 @@ public void deleteAllScopes(String apiId, Integer tenantId) throws APIResourceMg

Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true);
deleteScopeByAPIId(dbConnection, apiId, tenantId);
IdentityDatabaseUtil.commitTransaction(dbConnection);
IdentityDatabaseUtil.closeConnection(dbConnection);
}

Expand All @@ -504,6 +499,7 @@ public void deleteScope(String apiId, String scopeId, Integer tenantId) throws A

Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true);
deleteScopeById(dbConnection, scopeId, tenantId);
IdentityDatabaseUtil.commitTransaction(dbConnection);
IdentityDatabaseUtil.closeConnection(dbConnection);
}

Expand Down Expand Up @@ -668,24 +664,12 @@ private Connection getConnection(boolean applyTransaction) {
* @param sortOrder Sort order.
* @param limit Limit.
* @return SQL statement to retrieve API resources.
* @throws APIResourceMgtServerException If an error occurs while building the SQL statement.
*/
private String buildAPIResourcesSqlStatement(Integer tenantId, String filterQuery, String sortOrder, Integer limit)
throws APIResourceMgtServerException {
String sqlStmtTail;
private String buildAPIResourcesSqlStatement(Integer tenantId, String filterQuery, String sortOrder,
Integer limit) {

if (APIResourceManagementUtil.isH2() || APIResourceManagementUtil.isMySQL()
|| APIResourceManagementUtil.isMariaDB()) {
sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL_H2;
} else if (APIResourceManagementUtil.isOracleDB()) {
sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL_ORACLE;
} else if (APIResourceManagementUtil.isMSSqlDB()) {
sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL_MSSQL;
} else if (APIResourceManagementUtil.isDB2()) {
sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL_DB2;
} else {
sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL;
}
String sqlStmtTail;
sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL;

return SQLConstants.GET_API_RESOURCES + filterQuery + String.format(sqlStmtTail, tenantId, sortOrder, limit);
}
Expand Down Expand Up @@ -766,6 +750,9 @@ private void addScopes(Connection dbConnection, String apiId, List<Scope> scopes
throws APIResourceMgtException {


if (CollectionUtils.isEmpty(scopes)) {
return;
}
//Throw client error if scope list contains scopes that starts with internal_*, console:*
for (Scope scope : scopes) {
for (String restrictedScope : APIResourceManagementConstants.RESTRICTED_SCOPE_REGEXES) {
Expand Down
Loading

0 comments on commit e3d0db5

Please sign in to comment.