From 8ea930153ca5949324919cb611b7b9e99b894e6e Mon Sep 17 00:00:00 2001 From: ashanthamara Date: Thu, 12 Oct 2023 12:48:16 +0530 Subject: [PATCH] Deprecated the previous method and introduced a new public method --- .../identity/api/server/common/Util.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.api.server.common/src/main/java/org/wso2/carbon/identity/api/server/common/Util.java b/components/org.wso2.carbon.identity.api.server.common/src/main/java/org/wso2/carbon/identity/api/server/common/Util.java index 79e55aa8a2..f569d7bb4d 100644 --- a/components/org.wso2.carbon.identity.api.server.common/src/main/java/org/wso2/carbon/identity/api/server/common/Util.java +++ b/components/org.wso2.carbon.identity.api.server.common/src/main/java/org/wso2/carbon/identity/api/server/common/Util.java @@ -99,6 +99,48 @@ public static String base64URLDecode(String value) { StandardCharsets.UTF_8); } + /** + * Build 'next' and 'previous' pagination links. + * @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/ + * @return A map containing pagination link key-value pairs. + * @deprecated because this can not build pagination links when filter and attributes params are used in the api. + * Use {@link #buildPaginationLinks(int, int, int, String, String, String)} instead. + */ + @Deprecated + public static Map buildPaginationLinks(int limit, int currentOffset, int totalResultsFromSearch, + String servicePathComponent) { + + Map links = new HashMap<>(); + + // Next link. + if ((currentOffset + limit) < totalResultsFromSearch) { + links.put(PAGE_LINK_REL_NEXT, ContextLoader.buildURIForBody + (String.format(PAGINATION_LINK_FORMAT, servicePathComponent, (currentOffset + limit), limit)) + .toString()); + } + + /* + Previous link. + Previous link matters only if offset is greater than 0. + */ + if (currentOffset > 0) { + 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()); + } 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()); + } + } + + return links; + } + /** * Build 'next' and 'previous' pagination links. * @@ -111,7 +153,8 @@ public static String base64URLDecode(String value) { * @return A map containing pagination link key-value pairs. */ public static Map buildPaginationLinks(int limit, int currentOffset, int totalResultsFromSearch, - String servicePathComponent, String requiredAttributes, String filter) { + String servicePathComponent, String requiredAttributes, + String filter) { Map links = new HashMap<>();