Saviynt unveils its cutting-edge Intelligence Suite products to revolutionize Identity Security!
Click HERE to see how Saviynt Intelligence is transforming the industry.
Saviynt Copilot Icon

Pagination through Java code

Pandit
New Contributor II
New Contributor II

Issue :  We have the Java Jar for display name which actually calling GET USER API request. Given that Saviynt has the limitation on the GET USER API which will only retrieve 500 user records in single API request. As we don’t have Sav to Sav connection hence We want to achieve pagination through Java code. Refer the below sample for the Offset Pagination total CountPath attribute for the Rest connection in sample response that shows the total count of records in the attribute "totalcount" (see below)

Pandit_0-1720087243677.png

 

Pagination we tried:

"pagination": {
"offset": {
"offsetParam": "$skip",
"batchParam": "$top",
"batchSize": 100,
"totalCountPath": "${completeResponseMap.get('@totalcount')}"
}

"pagination": {
"offset": {
"offsetParam": "$skip",
"batchParam": "$top",
"batchSize": 100,
"totalCountPath": "9999999999999999999"
}
}

4 REPLIES 4

NM
Honored Contributor II
Honored Contributor II

Hi @Pandit , we also have to user jar for a use case, could you please let me know how are you generating access token in code?

If you can share code sample .. hiding confidential values 

You can create different thread


Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

rushikeshvartak
All-Star
All-Star

Saviynt will only return top 500 records you need to write pagination code to iterate 


Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.

rushikeshvartak
All-Star
All-Star

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class SaviyntUserFetcher {

public static void main(String[] args) {
try {
// Saviynt API details
String baseUrl = "https://your-saviynt-instance/api/v5/users";
String token = "your_auth_token"; // Obtain this token using the previous login API code
int batchSize = 100;
int offset = 0;
boolean moreRecords = true;

while (moreRecords) {
// Create the request URL with pagination parameters
String requestUrl = baseUrl + "?$skip=" + offset + "&$top=" + batchSize;

// Create URL object
URL url = new URL(requestUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

// Set request method to GET and add authorization header
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Bearer " + token);
con.setRequestProperty("Content-Type", "application/json");

// Read the response
int responseCode = con.getResponseCode();
System.out.println("Response Code: " + responseCode);

if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
StringBuilder response = new StringBuilder();
String inputLine;

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// Parse the JSON response to get the users and total count
String responseBody = response.toString();
int usersFetched = parseAndProcessUsers(responseBody);
int totalCount = extractTotalCount(responseBody);

// Update offset for next batch
offset += batchSize;

// Check if all records have been fetched
if (offset >= totalCount) {
moreRecords = false;
}
} else {
System.out.println("GET request failed");
moreRecords = false;
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

private static int parseAndProcessUsers(String responseBody) {
// Dummy implementation for parsing and processing users
// Replace with actual logic to process user data
System.out.println("Processing users from response: " + responseBody);
return 100; // Example: return number of users fetched in this batch
}

private static int extractTotalCount(String responseBody) {
// Dummy implementation to extract total count
// Replace with actual logic to parse total count from response
System.out.println("Extracting total count from response: " + responseBody);
return 1000; // Example: return total count of users
}
}


Regards,
Rushikesh Vartak
If this helped you move forward, click 'Kudos'. If it solved your query, select 'Accept As Solution'.