refactor(cloudfront): extract repeated marker/maxItems pagination into the existing paginate helper (#3586)

CloudFrontService.java already has a private generic paginate(List<T>, marker, maxItems, keyFn)
helper, used by seven of its list* methods (functions, continuous deployment policies, public
keys, key groups, realtime log configs, field-level encryption configs/profiles). Six older
list* methods (listDistributions, listInvalidations, listCachePolicies,
listOriginRequestPolicies, listOriginAccessControls, listCloudFrontOriginAccessIdentities)
were never migrated to it and kept their own inline copy of the same 18-line marker-lookup and
maxItems-truncation block, the duplication CPD flagged.

Wired all six onto the existing helper. listResponseHeadersPolicies keeps its own inline
version untouched: unlike the others, it treats an unmatched marker as InvalidArgument instead
of silently starting from the beginning, a genuine behavioral difference from paginate() that
this issue does not ask to change.

No behavior change for the six migrated methods. Full CloudFront and CloudFront CloudFormation
test suites pass unchanged.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-14 21:55:25 -04:00
1 parent bd13c7f9ce
commit efdef71527
1 file changed
+6 -84
@@ -266,20 +266,7 @@ public class CloudFrontService {
public List<Distribution> listDistributions(String marker, int maxItems) {
List<Distribution> all = new ArrayList<>(distStore.scan(k -> true));
all.sort((a, b) -> a.getId().compareTo(b.getId()));
if (marker != null && !marker.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getId().equals(marker)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
}
return all;
return paginate(all, marker, maxItems, Distribution::getId);
}
public synchronized void associateAlias(String targetDistributionId, String alias) {
@@ -443,20 +430,7 @@ public class CloudFrontService {
getDistribution(distributionId);
List<Invalidation> all = new ArrayList<>(
invalidationStore.get(distributionId).orElse(List.of()));
if (marker != null && !marker.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getId().equals(marker)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
}
return all;
return paginate(all, marker, maxItems, Invalidation::getId);
}
// ── Cache Policies ────────────────────────────────────────────────────────
@@ -505,20 +479,7 @@ public class CloudFrontService {
List<CachePolicy> all = new ArrayList<>(cachePolicyStore.scan(k -> true));
all.sort((a, b) -> a.getName() != null && b.getName() != null
? a.getName().compareTo(b.getName()) : a.getId().compareTo(b.getId()));
if (marker != null && !marker.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getId().equals(marker)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
}
return all;
return paginate(all, marker, maxItems, CachePolicy::getId);
}
// ── Origin Request Policies ───────────────────────────────────────────────
@@ -569,20 +530,7 @@ public class CloudFrontService {
List<OriginRequestPolicy> all = new ArrayList<>(orpStore.scan(k -> true));
all.sort((a, b) -> a.getName() != null && b.getName() != null
? a.getName().compareTo(b.getName()) : a.getId().compareTo(b.getId()));
if (marker != null && !marker.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getId().equals(marker)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
}
return all;
return paginate(all, marker, maxItems, OriginRequestPolicy::getId);
}
// ── Response Headers Policies ─────────────────────────────────────────────
@@ -933,20 +881,7 @@ public class CloudFrontService {
List<OriginAccessControl> all = new ArrayList<>(oacStore.scan(k -> true));
all.sort((a, b) -> a.getName() != null && b.getName() != null
? a.getName().compareTo(b.getName()) : a.getId().compareTo(b.getId()));
if (marker != null && !marker.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getId().equals(marker)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
}
return all;
return paginate(all, marker, maxItems, OriginAccessControl::getId);
}
// ── Origin Access Identity (OAI) ──────────────────────────────────────────
@@ -1003,20 +938,7 @@ public class CloudFrontService {
public List<CloudFrontOriginAccessIdentity> listCloudFrontOriginAccessIdentities(
String marker, int maxItems) {
List<CloudFrontOriginAccessIdentity> all = new ArrayList<>(oaiStore.scan(k -> true));
if (marker != null && !marker.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getId().equals(marker)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
}
return all;
return paginate(all, marker, maxItems, CloudFrontOriginAccessIdentity::getId);
}
private static void validateOriginAccessControl(OriginAccessControl oac) {