mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(core): extract shared pagination helper, reject maxResults=0 (#2343)
Closes #2339, #2340. ListResult<T> and its paginate()/encode()/decode() helpers existed as five near-identical private copies (BedrockAgentCoreGatewayService, BedrockAgentCoreIdentityService, BedrockAgentCoreMemoryService, BedrockAgentCoreControlService's three list methods, and MskService), not the two originally scoped in #2340 - the ListResult<T> record was already shared within bedrockagentcorecontrol, which is what earlier scoping missed. Extracts core.common.Pagination + PaginatedResult<T> and migrates every one of them, deleting both now-unused per-service ListResult model classes. Every duplicate collapsed "maxResults omitted" and "maxResults=0" onto the same primitive int, defaulting both to the full page instead of rejecting the explicit zero the way AWS's MaxResults min:1 constraint requires (confirmed against botocore's real kafka and verified live: botocore itself refuses to send MaxResults=0 client-side). Pagination.paginate/parseMaxResults take a boxed Integer so the two cases are distinguishable, and every migrated service now rejects 0 with its own existing error code (ValidationException for the bedrockagentcorecontrol services, BadRequestException for MSK) - the fix lands once, not five times. Also carries over the MskController fix from #2336: an Integer-typed @QueryParam converts before the resource method runs, so a non-numeric maxResults was a bare 404 instead of an AWS-shaped 400. Every migrated REST controller now binds maxResults as a String and calls Pagination.parseMaxResults, closing the same gap in BedrockAgentCoreGatewayController/ControlController/MemoryController that MSK already had fixed.
This commit is contained in:
1 parent
1cc97d57c1
commit
25b46e28ad
18 files changed
+215
-338
No files matched your search
@@ -0,0 +1,10 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** A page of list results plus an optional pagination token. */
|
||||
@RegisterForReflection
|
||||
public record PaginatedResult<T>(List<T> items, String nextToken) {
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Opaque-cursor pagination shared by list endpoints that model AWS's {@code maxResults}/
|
||||
* {@code nextToken} pair. The cursor is the last returned item's sort key, base64
|
||||
* URL-encoded - not a raw offset, so a page stays resumable even if items are inserted or
|
||||
* removed elsewhere in the collection between requests.
|
||||
*
|
||||
* <p>{@code maxResults} is a boxed {@link Integer} deliberately: {@code null} means the
|
||||
* caller omitted it (falls back to {@code maxPage}), while {@code 0} is a real, out-of-range
|
||||
* value a caller explicitly sent and must be rejected the same as a value above {@code maxPage}
|
||||
* - AWS's own models declare {@code MaxResults} with a minimum of 1. Collapsing "omitted" and
|
||||
* "explicitly zero" onto the same primitive {@code int} sentinel is what let that distinction
|
||||
* get lost in every service that inlined this logic before this class existed.
|
||||
*/
|
||||
public final class Pagination {
|
||||
|
||||
private Pagination() {}
|
||||
|
||||
public static <T> PaginatedResult<T> paginate(List<T> all, Function<T, String> cursorOf,
|
||||
Integer maxResults, String nextToken,
|
||||
int maxPage, String errorCode) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > maxPage)) {
|
||||
throw new AwsException(errorCode, "maxResults must be between 1 and " + maxPage, 400);
|
||||
}
|
||||
int limit = maxResults != null ? maxResults : maxPage;
|
||||
|
||||
List<T> sorted = all.stream().sorted(Comparator.comparing(cursorOf)).collect(Collectors.toList());
|
||||
String after = decodeToken(nextToken, errorCode);
|
||||
int start = 0;
|
||||
if (after != null) {
|
||||
for (int i = 0; i < sorted.size(); i++) {
|
||||
if (cursorOf.apply(sorted.get(i)).compareTo(after) > 0) {
|
||||
start = i;
|
||||
break;
|
||||
}
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
List<T> page = sorted.stream().skip(start).limit(limit).collect(Collectors.toList());
|
||||
String token = null;
|
||||
if (start + limit < sorted.size() && !page.isEmpty()) {
|
||||
token = encodeToken(cursorOf.apply(page.get(page.size() - 1)));
|
||||
}
|
||||
return new PaginatedResult<>(page, token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a raw {@code maxResults} query-string value. Bind the JAX-RS {@code @QueryParam}
|
||||
* as {@code String}, not {@code Integer}: RESTEasy Reactive's default handling of a
|
||||
* non-numeric value for an {@code Integer}-typed {@code @QueryParam} is a 404 (the request
|
||||
* never reaches the resource method at all), not the AWS-shaped 400 a malformed parameter
|
||||
* should produce.
|
||||
*/
|
||||
public static Integer parseMaxResults(String value, String errorCode) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException(errorCode, "maxResults must be an integer.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static String encodeToken(String cursor) {
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(cursor.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String decodeToken(String token, String errorCode) {
|
||||
if (token == null || token.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException(errorCode, "Invalid nextToken.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-10
@@ -6,11 +6,12 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntime;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntimeEndpoint;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntimeVersion;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.ListResult;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
@@ -97,12 +98,12 @@ public class BedrockAgentCoreControlController {
|
||||
@POST
|
||||
@Path("/runtimes/")
|
||||
public Response listAgentRuntimes(@Context HttpHeaders headers,
|
||||
@QueryParam("maxResults") Integer maxResults,
|
||||
@QueryParam("maxResults") String maxResultsParam,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
ListResult<AgentRuntime> result =
|
||||
service.listAgentRuntimes(maxResults != null ? maxResults : 0, nextToken, region);
|
||||
Integer maxResults = Pagination.parseMaxResults(maxResultsParam, "ValidationException");
|
||||
PaginatedResult<AgentRuntime> result = service.listAgentRuntimes(maxResults, nextToken, region);
|
||||
ObjectNode out = objectMapper.createObjectNode();
|
||||
ArrayNode arr = out.putArray("agentRuntimes");
|
||||
for (AgentRuntime runtime : result.items()) {
|
||||
@@ -202,13 +203,14 @@ public class BedrockAgentCoreControlController {
|
||||
@Path("/runtimes/{agentRuntimeId}/versions/")
|
||||
public Response listAgentRuntimeVersions(@Context HttpHeaders headers,
|
||||
@PathParam("agentRuntimeId") String id,
|
||||
@QueryParam("maxResults") Integer maxResults,
|
||||
@QueryParam("maxResults") String maxResultsParam,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
Integer maxResults = Pagination.parseMaxResults(maxResultsParam, "ValidationException");
|
||||
AgentRuntime runtime = service.getAgentRuntime(id, region);
|
||||
ListResult<AgentRuntimeVersion> result =
|
||||
service.listAgentRuntimeVersions(id, maxResults != null ? maxResults : 0, nextToken, region);
|
||||
PaginatedResult<AgentRuntimeVersion> result =
|
||||
service.listAgentRuntimeVersions(id, maxResults, nextToken, region);
|
||||
ObjectNode out = objectMapper.createObjectNode();
|
||||
ArrayNode arr = out.putArray("agentRuntimes");
|
||||
for (AgentRuntimeVersion snap : result.items()) {
|
||||
@@ -315,13 +317,14 @@ public class BedrockAgentCoreControlController {
|
||||
@Path("/runtimes/{agentRuntimeId}/runtime-endpoints/")
|
||||
public Response listEndpoints(@Context HttpHeaders headers,
|
||||
@PathParam("agentRuntimeId") String id,
|
||||
@QueryParam("maxResults") Integer maxResults,
|
||||
@QueryParam("maxResults") String maxResultsParam,
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
try {
|
||||
Integer maxResults = Pagination.parseMaxResults(maxResultsParam, "ValidationException");
|
||||
AgentRuntime runtime = service.getAgentRuntime(id, region);
|
||||
ListResult<AgentRuntimeEndpoint> result =
|
||||
service.listEndpoints(id, maxResults != null ? maxResults : 0, nextToken, region);
|
||||
PaginatedResult<AgentRuntimeEndpoint> result =
|
||||
service.listEndpoints(id, maxResults, nextToken, region);
|
||||
ObjectNode out = objectMapper.createObjectNode();
|
||||
ArrayNode arr = out.putArray("runtimeEndpoints");
|
||||
for (AgentRuntimeEndpoint endpoint : result.items()) {
|
||||
|
||||
+13
-63
@@ -3,21 +3,19 @@ package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntime;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntimeEndpoint;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntimeVersion;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.ListResult;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -26,9 +24,7 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Business logic for the Amazon Bedrock AgentCore control plane (runtime registry).
|
||||
@@ -48,7 +44,6 @@ public class BedrockAgentCoreControlService {
|
||||
private static final String ID_ALPHABET =
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final int MAX_PAGE = 100;
|
||||
private static final int DEFAULT_PAGE = 100;
|
||||
|
||||
private final StorageBackend<String, AgentRuntime> storage;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -220,20 +215,20 @@ public class BedrockAgentCoreControlService {
|
||||
return region + " | ||||