fix(transfer): omit pagination token on final page (#7081)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-31 13:13:29 -04:00
1 parent 7cf516a14d
commit 25b0f234f9
3 files changed
+187 -43

No files matched your search

@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.services.transfer.model.HomeDirectoryMapping;
import io.github.hectorvent.floci.services.transfer.model.Server;
import io.github.hectorvent.floci.services.transfer.model.SshPublicKey;
@@ -98,15 +99,16 @@ public class TransferHandler {
private Response listServers(JsonNode req) {
String nextToken = textOrNull(req, "NextToken");
int maxResults = req.path("MaxResults").asInt(100);
List<Server> servers = service.listServers(nextToken, maxResults);
requirePageSize(maxResults);
PaginatedResult<Server> page = service.listServers(nextToken, maxResults);
ObjectNode resp = objectMapper.createObjectNode();
ArrayNode arr = resp.putArray("Servers");
for (Server s : servers) {
for (Server s : page.items()) {
arr.add(buildServerListEntry(s));
}
if (servers.size() == maxResults) {
resp.put("NextToken", servers.get(servers.size() - 1).getServerId());
if (page.nextToken() != null) {
resp.put("NextToken", page.nextToken());
}
return Response.ok(resp).build();
}
@@ -185,20 +187,27 @@ public class TransferHandler {
String serverId = req.path("ServerId").asText();
String nextToken = textOrNull(req, "NextToken");
int maxResults = req.path("MaxResults").asInt(100);
List<User> users = service.listUsers(serverId, nextToken, maxResults);
requirePageSize(maxResults);
PaginatedResult<User> page = service.listUsers(serverId, nextToken, maxResults);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", serverId);
ArrayNode arr = resp.putArray("Users");
for (User u : users) {
for (User u : page.items()) {
arr.add(buildUserListEntry(u));
}
if (users.size() == maxResults) {
resp.put("NextToken", users.get(users.size() - 1).getUserName());
if (page.nextToken() != null) {
resp.put("NextToken", page.nextToken());
}
return Response.ok(resp).build();
}
private static void requirePageSize(int maxResults) {
if (maxResults < 1 || maxResults > 1000) {
throw new AwsException("InvalidRequestException", "MaxResults must be between 1 and 1000", 400);
}
}
private Response updateUser(JsonNode req) {
String serverId = req.path("ServerId").asText();
String userName = req.path("UserName").asText();
@@ -4,7 +4,10 @@ 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.SignedMarkers;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.transfer.model.HomeDirectoryMapping;
@@ -31,6 +34,7 @@ public class TransferService {
private final AccountAwareStorageBackend<User> userStore;
private final AccountAwareStorageBackend<Map<String, String>> tagStore;
private final RegionResolver regionResolver;
private final SignedMarkers listMarkers = new SignedMarkers();
@Inject
public TransferService(StorageFactory factory, EmulatorConfig config, RegionResolver regionResolver) {
@@ -110,25 +114,13 @@ public class TransferService {
}
}
public List<Server> listServers(String nextToken, int maxResults) {
public PaginatedResult<Server> listServers(String nextToken, int maxResults) {
migrateLegacyServers(currentAccount(), currentRegion());
List<Server> all = new ArrayList<>(serverStore.scanForAccount(currentAccount(),
k -> k.startsWith(currentRegion() + "/")));
all.sort((a, b) -> a.getServerId().compareTo(b.getServerId()));
if (nextToken != null && !nextToken.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getServerId().equals(nextToken)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxResults > 0 && all.size() > maxResults) {
return all.subList(0, maxResults);
}
return all;
String scope = "transfer:servers:" + currentAccount() + ":" + currentRegion() + ":";
return Pagination.paginate(all, Server::getServerId, maxResults, nextToken, listMarkers,
scope, token -> new AwsException("InvalidNextTokenException", "Invalid NextToken.", 400));
}
public Server startServer(String serverId) {
@@ -247,26 +239,14 @@ public class TransferService {
deleteUserRecord(getUser(serverId, userName));
}
public List<User> listUsers(String serverId, String nextToken, int maxResults) {
public PaginatedResult<User> listUsers(String serverId, String nextToken, int maxResults) {
getServer(serverId);
migrateLegacyUsers(currentAccount(), currentRegion(), serverId);
List<User> all = new ArrayList<>(userStore.scanForAccount(currentAccount(),
k -> k.startsWith(scopedKey(currentRegion(), serverId) + "/")));
all.sort((a, b) -> a.getUserName().compareTo(b.getUserName()));
if (nextToken != null && !nextToken.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getUserName().equals(nextToken)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxResults > 0 && all.size() > maxResults) {
return all.subList(0, maxResults);
}
return all;
String scope = "transfer:users:" + currentAccount() + ":" + currentRegion() + ":" + serverId + ":";
return Pagination.paginate(all, User::getUserName, maxResults, nextToken, listMarkers,
scope, token -> new AwsException("InvalidNextTokenException", "Invalid NextToken.", 400));
}
public User updateUser(String serverId, String userName, String role,