Skip to content

Commit

Permalink
Modified link attribute's href in get all applications response.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanthamara committed Oct 12, 2023
1 parent 65457d1 commit 18f07ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public ApplicationListResponse getAllApplications(Integer limit, Integer offset,
.count(resultsInCurrentPage)
.applications(getApplicationListItems(serviceProviderList, requestedAttributeList))
.links(Util.buildPaginationLinks(limit, offset, totalResults,
APPLICATION_MANAGEMENT_PATH_COMPONENT)
APPLICATION_MANAGEMENT_PATH_COMPONENT, requiredAttributes, filter)
.entrySet()
.stream()
.map(link -> new Link().rel(link.getKey()).href(link.getValue()))
Expand All @@ -291,7 +291,7 @@ public ApplicationListResponse getAllApplications(Integer limit, Integer offset,
.count(resultsInCurrentPage)
.applications(getApplicationListItems(filteredAppList))
.links(Util.buildPaginationLinks(limit, offset, totalResults,
APPLICATION_MANAGEMENT_PATH_COMPONENT)
APPLICATION_MANAGEMENT_PATH_COMPONENT, requiredAttributes, filter)
.entrySet()
.stream()
.map(link -> new Link().rel(link.getKey()).href(link.getValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.wso2.carbon.identity.api.server.common;

import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.recovery.ChallengeQuestionManager;
Expand Down Expand Up @@ -100,22 +101,32 @@ public static String base64URLDecode(String value) {

/**
* Build 'next' and 'previous' pagination links.
* @param limit Value of the 'limit' parameter.
* @param currentOffset Value of the 'currentOffset' parameter.
*
* @param limit Value of the 'limit' parameter.
* @param currentOffset Value of the 'currentOffset' parameter.
* @param totalResultsFromSearch Value of the 'totalResultsFromSearch' parameter.
* @param servicePathComponent API service path. E.g: applications/
* @param servicePathComponent API service path. E.g: applications/
* @param requiredAttributes Value of the 'attributes' parameter.
* @param filter Value of the 'filter' parameter.
* @return A map containing pagination link key-value pairs.
*/
public static Map<String, String> buildPaginationLinks(int limit, int currentOffset, int totalResultsFromSearch,
String servicePathComponent) {
String servicePathComponent, String requiredAttributes, String filter) {

Map<String, String> links = new HashMap<>();

StringBuilder otherParams = new StringBuilder();
if (!StringUtils.isEmpty(requiredAttributes)) {
otherParams.append("&attributes=").append(requiredAttributes);
}
if (!StringUtils.isEmpty(filter)) {
otherParams.append("&filter=").append(filter.replace(" ", "+"));
}

// Next link.
if ((currentOffset + limit) < totalResultsFromSearch) {
links.put(PAGE_LINK_REL_NEXT, ContextLoader.buildURIForBody
(String.format(PAGINATION_LINK_FORMAT, servicePathComponent, (currentOffset + limit), limit))
.toString());
links.put(PAGE_LINK_REL_NEXT, ContextLoader.buildURIForBody(String.format(PAGINATION_LINK_FORMAT,
servicePathComponent, (currentOffset + limit), limit) + otherParams).toString());
}

/*
Expand All @@ -126,11 +137,12 @@ public static Map<String, String> buildPaginationLinks(int limit, int currentOff
if ((currentOffset - limit) >= 0) { // A previous page of size 'limit' exists.
links.put(PAGE_LINK_REL_PREVIOUS, ContextLoader.buildURIForBody
(String.format(PAGINATION_LINK_FORMAT, servicePathComponent,
calculateOffsetForPreviousLink(currentOffset, limit, totalResultsFromSearch), limit))
.toString());
calculateOffsetForPreviousLink(currentOffset, limit, totalResultsFromSearch), limit) +
otherParams).toString());
} else { // A previous page exists but it's size is less than the specified limit.
links.put(PAGE_LINK_REL_PREVIOUS, ContextLoader.buildURIForBody
(String.format(PAGINATION_LINK_FORMAT, servicePathComponent, 0, currentOffset)).toString());
(String.format(PAGINATION_LINK_FORMAT, servicePathComponent, 0, currentOffset) +
otherParams).toString());
}
}

Expand Down

0 comments on commit 18f07ce

Please sign in to comment.