Skip to content

Commit

Permalink
Merge pull request #426 from BojithaPiyathilake/returnTypeChange
Browse files Browse the repository at this point in the history
Changing the response type of GET and POST/.Search
  • Loading branch information
AnuradhaSK authored Jul 15, 2022
2 parents e6fcca9 + 40dae25 commit bd07897
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 172 deletions.
2 changes: 1 addition & 1 deletion components/org.wso2.carbon.identity.scim2.common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<groupId>org.wso2.carbon.identity.inbound.provisioning.scim2</groupId>
<artifactId>identity-inbound-provisioning-scim2</artifactId>
<relativePath>../../pom.xml</relativePath>
<version>1.6.20-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.wso2.charon3.core.objects.Group;
import org.wso2.charon3.core.objects.Role;
import org.wso2.charon3.core.objects.User;
import org.wso2.charon3.core.objects.plainobjects.RolesGetResponse;
import org.wso2.charon3.core.utils.codeutils.ExpressionNode;
import org.wso2.charon3.core.utils.codeutils.Node;
import org.wso2.charon3.core.utils.codeutils.OperationNode;
Expand Down Expand Up @@ -175,13 +176,13 @@ public void deleteRole(String roleID) throws CharonException, NotFoundException,
}

@Override
public List<Object> listRolesWithGET(Node rootNode, Integer startIndex, Integer count, String sortBy,
String sortOrder) throws CharonException, NotImplementedException, BadRequestException {
public RolesGetResponse listRolesWithGET(Node rootNode, Integer startIndex, Integer count, String sortBy,
String sortOrder) throws CharonException, NotImplementedException, BadRequestException {

if (sortBy != null || sortOrder != null) {
throw new NotImplementedException("Sorting is not supported.");
} else if (count != null && count == 0) {
return Collections.emptyList();
return new RolesGetResponse(0, Collections.emptyList());
} else if (rootNode != null) {
return filterRoles(rootNode, count, startIndex, sortBy, sortOrder);
} else {
Expand All @@ -200,7 +201,7 @@ public List<Object> listRolesWithGET(Node rootNode, Integer startIndex, Integer
* @return Detailed user list.
* @throws CharonException Error filtering the roles.
*/
private List<Object> filterRoles(Node node, Integer count, Integer startIndex, String sortBy, String sortOrder)
private RolesGetResponse filterRoles(Node node, Integer count, Integer startIndex, String sortBy, String sortOrder)
throws CharonException, NotImplementedException, BadRequestException {

// Handle single attribute search.
Expand All @@ -225,7 +226,7 @@ private List<Object> filterRoles(Node node, Integer count, Integer startIndex, S
* @return Filtered roles.
* @throws CharonException Error filtering the roles.
*/
private List<Object> filterRolesBySingleAttribute(ExpressionNode node, Integer count, Integer startIndex,
private RolesGetResponse filterRolesBySingleAttribute(ExpressionNode node, Integer count, Integer startIndex,
String sortBy, String sortOrder) throws CharonException, BadRequestException {

String attributeName = node.getAttributeValue();
Expand All @@ -241,9 +242,6 @@ private List<Object> filterRolesBySingleAttribute(ExpressionNode node, Integer c
throw new BadRequestException(errorMessage);
}

List<Object> filteredRoles = new ArrayList<>();
// 0th index is to store total number of results.
filteredRoles.add(0);
String searchFilter = getSearchFilter(filterOperation, attributeValue);
if (log.isDebugEnabled()) {
log.debug(String.format("Filtering roleNames from search filter: %s", searchFilter));
Expand All @@ -256,14 +254,8 @@ private List<Object> filterRolesBySingleAttribute(ExpressionNode node, Integer c
String.format("Error occurred while listing roles based on the search filter: %s", searchFilter),
e);
}
List<Object> scimRoles = getScimRolesList(roles);

// Set total number of results to 0th index.
filteredRoles.set(0, scimRoles.size());
// Add the results list.
filteredRoles.addAll(scimRoles);

return filteredRoles;
List<Role> scimRoles = getScimRolesList(roles);
return new RolesGetResponse(scimRoles.size(), scimRoles);
}

/**
Expand Down Expand Up @@ -312,35 +304,32 @@ private String getSearchFilter(String filterOperation, String attributeValue) {
* @return List of roles.
* @throws CharonException Error while listing users
*/
private List<Object> listRoles(Integer count, Integer startIndex, String sortBy, String sortOrder)
private RolesGetResponse listRoles(Integer count, Integer startIndex, String sortBy, String sortOrder)
throws CharonException, BadRequestException {

List<Object> rolesList = new ArrayList<>();
List<Role> rolesList = new ArrayList<>();
int rolesCount;
try {
// 0th index is to store total number of results.
rolesList.add(0);
List<RoleBasicInfo> roles = roleManagementService
.getRoles(count, startIndex, sortBy, sortOrder, tenantDomain);
List<Object> scimRoles = getScimRolesList(roles);
List<Role> scimRoles = getScimRolesList(roles);

int rolesCount = roleManagementService.getRolesCount(tenantDomain);
rolesCount = roleManagementService.getRolesCount(tenantDomain);
// Set total number of results to 0th index.
if (rolesCount == 0) {
rolesList.set(0, scimRoles.size());
} else {
rolesList.set(0, rolesCount);
rolesCount = scimRoles.size();
}
// Add the results list.
rolesList.addAll(scimRoles);
} catch (IdentityRoleManagementException e) {
throw new CharonException("Error occurred while listing roles.", e);
}
return rolesList;
return new RolesGetResponse(rolesCount, rolesList);
}

private List<Object> getScimRolesList(List<RoleBasicInfo> roles) throws BadRequestException, CharonException {
private List<Role> getScimRolesList(List<RoleBasicInfo> roles) throws BadRequestException, CharonException {

List<Object> scimRoles = new ArrayList<>();
List<Role> scimRoles = new ArrayList<>();
for (RoleBasicInfo roleBasicInfo : roles) {
Role scimRole = new Role();
scimRole.setDisplayName(roleBasicInfo.getName());
Expand Down Expand Up @@ -527,7 +516,7 @@ private boolean hasPermissionsChanged(List<String> oldRolePermissions, List<Stri
}

@Override
public List<Object> listRolesWithPost(SearchRequest searchRequest)
public RolesGetResponse listRolesWithPost(SearchRequest searchRequest)
throws NotImplementedException, BadRequestException, CharonException {

return listRolesWithGET(searchRequest.getFilter(), searchRequest.getStartIndex(), searchRequest.getCount(),
Expand Down
Loading

0 comments on commit bd07897

Please sign in to comment.