fix(kinesisanalytics): paginate ListApplications (#6813)

* fix(kinesisanalytics): paginate ListApplications

* test(kinesisanalytics): cover default ListApplications page size

* fix(kinesisanalytics): resume list pages after deleted applications

* fix: share pagination bound validation
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-28 16:58:30 -04:00
1 parent 6abd9d7ffb
commit 3d4fdfe7ca
6 files changed
+197 -8

No files matched your search

@@ -128,14 +128,38 @@ public final class Pagination {
Integer maxResults, String nextToken,
int defaultPageSize, int maxResultsLimit, int maxTokenLength,
String errorCode, String namespace) {
validatePageSizeAndToken(maxResults, nextToken, maxResultsLimit, maxTokenLength, errorCode,
false, "Pagination token must not exceed " + maxTokenLength + " characters.");
return paginate(all, cursorOf, maxResults == null ? defaultPageSize : maxResults, nextToken, namespace,
token -> new AwsException(errorCode, "Invalid pagination token.", 400));
}
/**
* Paginates a list whose cursor is itself a valid API token. Unlike the namespaced overloads,
* this leaves the sorted cursor unencoded in the response, so an API whose resource name is
* also its token can resume after that resource has been removed.
*/
public static <T> PaginatedResult<T> paginateUnencoded(List<T> all, Function<T, String> cursorOf,
Integer maxResults, String nextToken,
int defaultPageSize, int maxResultsLimit,
int maxTokenLength, String errorCode) {
validatePageSizeAndToken(maxResults, nextToken, maxResultsLimit, maxTokenLength, errorCode,
true, "Pagination token must be between 1 and " + maxTokenLength + " characters.");
int limit = maxResults == null ? defaultPageSize : maxResults;
return slice(all, cursorOf, limit, nextToken, "", Function.identity());
}
private static void validatePageSizeAndToken(Integer maxResults, String nextToken,
int maxResultsLimit, int maxTokenLength,
String errorCode, boolean tokenMustBeNonEmpty,
String tokenErrorMessage) {
if (maxResults != null && (maxResults < 1 || maxResults > maxResultsLimit)) {
throw new AwsException(errorCode, "Page size must be between 1 and " + maxResultsLimit + ".", 400);
}
if (nextToken != null && nextToken.length() > maxTokenLength) {
throw new AwsException(errorCode, "Pagination token must not exceed " + maxTokenLength + " characters.", 400);
if (nextToken != null
&& ((tokenMustBeNonEmpty && nextToken.isEmpty()) || nextToken.length() > maxTokenLength)) {
throw new AwsException(errorCode, tokenErrorMessage, 400);
}
return paginate(all, cursorOf, maxResults == null ? defaultPageSize : maxResults, nextToken, namespace,
token -> new AwsException(errorCode, "Invalid pagination token.", 400));
}
/** Reads an optional JSON page size without coercing strings, fractions, or overflowing integers. */
@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication;
import io.github.hectorvent.floci.services.kinesisanalytics.model.Snapshot;
import jakarta.enterprise.context.ApplicationScoped;
@@ -114,9 +116,12 @@ public class KinesisAnalyticsV2JsonHandler {
}
private Response handleListApplications(JsonNode request, String region) {
Integer limit = Pagination.parseMaxResults(request, "Limit", "InvalidRequestException");
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidRequestException");
PaginatedResult<FlinkApplication> page = service.listApplications(region, limit, nextToken);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode summaries = response.putArray("ApplicationSummaries");
for (FlinkApplication app : service.listApplications(region)) {
for (FlinkApplication app : page.items()) {
ObjectNode summary = summaries.addObject();
summary.put("ApplicationName", app.getApplicationName());
summary.put("ApplicationARN", app.getApplicationArn());
@@ -127,6 +132,9 @@ public class KinesisAnalyticsV2JsonHandler {
summary.put("ApplicationMode", app.getApplicationMode());
}
}
if (page.nextToken() != null) {
response.put("NextToken", page.nextToken());
}
return Response.ok(response).build();
}
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
@@ -245,6 +247,14 @@ public class KinesisAnalyticsV2Service {
return storage.scan(key -> key.startsWith(prefix));
}
public PaginatedResult<FlinkApplication> listApplications(String region, Integer limit, String nextToken) {
if (nextToken != null && !nextToken.isEmpty() && !APPLICATION_NAME.matcher(nextToken).matches()) {
throw new AwsException("InvalidRequestException", "Invalid NextToken.", 400);
}
return Pagination.paginateUnencoded(listApplications(region), FlinkApplication::getApplicationName,
limit, nextToken, 50, 50, 128, "InvalidRequestException");
}
public FlinkApplication startApplication(String applicationName) {
return startApplication(applicationName, config.defaultRegion());
}