mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
refactor(s3): dedupe Filter grammar between IntelligentTiering and Metrics (#3836)
S3IntelligentTieringConfiguration and S3MetricsConfiguration each independently implemented the S3 Filter sub-resource grammar (Prefix/Tag/And) and shared an identical tagsXml helper, found via CPD as an 18-35 line overlapping block (#3835). MetricsFilter additionally accepts AccessPointArn as a conjunct, which IntelligentTieringFilter does not (verified against botocore's service-2.json: MetricsFilter/MetricsAndOperator carry AccessPointArn, IntelligentTieringFilter/IntelligentTieringAndOperator don't), so the two weren't byte-identical. Extracted the shared skeleton into S3FilterGrammar.filterXml(xml, extraConjuncts) in core.common, parameterized by which extra conjuncts a caller's Filter type supports: IntelligentTiering passes List.of(), Metrics passes List.of("AccessPointArn"). tagsXml and requireText, which were fully identical, moved with it. S3AnalyticsConfiguration and S3InventoryConfiguration also parse a Filter grammar, but through a different XmlElement-based parser, not the XmlParser string-based approach these two share, so they're a different implementation and out of scope here. No behavior change: verified against both configuration types' existing unit and integration test suites, which directly exercise Prefix/Tag/And/AccessPointArn and every malformed-filter case.
This commit is contained in:
1 parent
530d1a54d8
commit
18a2f36eda
3 files changed
+109
-140
No files matched your search
@@ -0,0 +1,105 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The S3 {@code Filter} sub-resource grammar shared by configuration types that scope
|
||||
* themselves to a subset of objects: a filter is exactly one of a prefix, an object tag, one
|
||||
* of the caller's extra conjuncts, or an {@code And} conjunction of two or more of those.
|
||||
* {@code S3IntelligentTieringConfiguration} and {@code S3MetricsConfiguration} each implemented
|
||||
* this independently before this class existed; the two aren't 100% identical, since
|
||||
* {@code MetricsFilter} additionally accepts {@code AccessPointArn} where
|
||||
* {@code IntelligentTieringFilter} does not, which is why {@code extraConjuncts} is a parameter
|
||||
* rather than a hardcoded set.
|
||||
*/
|
||||
public final class S3FilterGrammar {
|
||||
|
||||
private S3FilterGrammar() {
|
||||
}
|
||||
|
||||
/** AWS reports a body that does not match the published schema this way. */
|
||||
private static AwsException malformed() {
|
||||
return new AwsException("MalformedXML",
|
||||
"The XML you provided was not well-formed or did not validate against our published schema", 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the filter. A filter is exactly one of a prefix, an object tag, one of
|
||||
* {@code extraConjuncts}, or an And conjunction: AWS answers MalformedXML for a filter naming
|
||||
* none of those or more than one, and for an And holding fewer than two predicates.
|
||||
*/
|
||||
public static String filterXml(String xml, List<String> extraConjuncts) {
|
||||
List<String> predicates = XmlParser.childElementNames(xml, "Filter");
|
||||
if (predicates.size() != 1) {
|
||||
throw malformed();
|
||||
}
|
||||
|
||||
String predicate = predicates.getFirst();
|
||||
return switch (predicate) {
|
||||
case "Tag" -> tagsXml(xml, 1);
|
||||
case "And" -> andXml(xml, extraConjuncts);
|
||||
default -> {
|
||||
if (!"Prefix".equals(predicate) && !extraConjuncts.contains(predicate)) {
|
||||
throw malformed();
|
||||
}
|
||||
yield new XmlBuilder().elem(predicate, requireText(xml, predicate)).build();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static String andXml(String xml, List<String> extraConjuncts) {
|
||||
List<String> conjuncts = XmlParser.childElementNames(xml, "And");
|
||||
if (conjuncts.size() < 2) {
|
||||
throw malformed();
|
||||
}
|
||||
XmlBuilder and = new XmlBuilder().start("And");
|
||||
if (conjuncts.contains("Prefix")) {
|
||||
and.elem("Prefix", requireText(xml, "Prefix"));
|
||||
}
|
||||
long tagCount = conjuncts.stream().filter("Tag"::equals).count();
|
||||
if (tagCount > 0) {
|
||||
and.raw(tagsXml(xml, (int) tagCount));
|
||||
}
|
||||
long extraCount = 0;
|
||||
for (String extra : extraConjuncts) {
|
||||
if (conjuncts.contains(extra)) {
|
||||
and.elem(extra, requireText(xml, extra));
|
||||
extraCount++;
|
||||
}
|
||||
}
|
||||
// Every conjunct has to be one floci understands, or the filter it stores would
|
||||
// not be the filter that was sent.
|
||||
if (conjuncts.size() != tagCount + (conjuncts.contains("Prefix") ? 1 : 0) + extraCount) {
|
||||
throw malformed();
|
||||
}
|
||||
return and.end("And").build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes {@code expected} tags. A tag carries both a key and a value, so a pair short of
|
||||
* the element count means one of them was missing.
|
||||
*/
|
||||
public static String tagsXml(String xml, int expected) {
|
||||
Map<String, String> tags = XmlParser.extractPairs(xml, "Tag", "Key", "Value");
|
||||
if (tags.size() != expected) {
|
||||
throw malformed();
|
||||
}
|
||||
XmlBuilder out = new XmlBuilder();
|
||||
tags.forEach((key, value) -> {
|
||||
if (key.isBlank() || value == null) {
|
||||
throw malformed();
|
||||
}
|
||||
out.start("Tag").elem("Key", key).elem("Value", value).end("Tag");
|
||||
});
|
||||
return out.build();
|
||||
}
|
||||
|
||||
public static String requireText(String xml, String element) {
|
||||
String value = XmlParser.extractFirst(xml, element, null);
|
||||
if (value == null) {
|
||||
throw malformed();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
+2
-67
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.S3FilterGrammar;
|
||||
import io.github.hectorvent.floci.core.common.XmlBuilder;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
|
||||
@@ -81,7 +82,7 @@ record S3IntelligentTieringConfiguration(String id, String innerXml) {
|
||||
|
||||
XmlBuilder inner = new XmlBuilder().elem("Id", id);
|
||||
if (filters == 1) {
|
||||
inner.start("Filter").raw(filterXml(xml)).end("Filter");
|
||||
inner.start("Filter").raw(S3FilterGrammar.filterXml(xml, List.of())).end("Filter");
|
||||
}
|
||||
inner.elem("Status", status);
|
||||
for (int i = 0; i < tierings; i++) {
|
||||
@@ -90,64 +91,6 @@ record S3IntelligentTieringConfiguration(String id, String innerXml) {
|
||||
return new S3IntelligentTieringConfiguration(id, inner.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the filter. A filter is exactly one of a prefix, an object tag, or an And
|
||||
* conjunction: AWS answers MalformedXML for a filter naming none of them or more than one,
|
||||
* and for an And holding fewer than two predicates.
|
||||
*/
|
||||
private static String filterXml(String xml) {
|
||||
List<String> predicates = XmlParser.childElementNames(xml, "Filter");
|
||||
if (predicates.size() != 1) {
|
||||
throw malformed();
|
||||
}
|
||||
|
||||
String predicate = predicates.getFirst();
|
||||
return switch (predicate) {
|
||||
case "Prefix" -> new XmlBuilder().elem("Prefix", requireText(xml, "Prefix")).build();
|
||||
case "Tag" -> tagsXml(xml, 1);
|
||||
case "And" -> {
|
||||
List<String> conjuncts = XmlParser.childElementNames(xml, "And");
|
||||
if (conjuncts.size() < 2) {
|
||||
throw malformed();
|
||||
}
|
||||
XmlBuilder and = new XmlBuilder().start("And");
|
||||
if (conjuncts.contains("Prefix")) {
|
||||
and.elem("Prefix", requireText(xml, "Prefix"));
|
||||
}
|
||||
long tagCount = conjuncts.stream().filter("Tag"::equals).count();
|
||||
if (tagCount > 0) {
|
||||
and.raw(tagsXml(xml, (int) tagCount));
|
||||
}
|
||||
// Every conjunct has to be one floci understands, or the filter it stores would
|
||||
// not be the filter that was sent.
|
||||
if (conjuncts.size() != tagCount + (conjuncts.contains("Prefix") ? 1 : 0)) {
|
||||
throw malformed();
|
||||
}
|
||||
yield and.end("And").build();
|
||||
}
|
||||
default -> throw malformed();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes {@code expected} tags. A tag carries both a key and a value, so a pair short of
|
||||
* the element count means one of them was missing.
|
||||
*/
|
||||
private static String tagsXml(String xml, int expected) {
|
||||
Map<String, String> tags = XmlParser.extractPairs(xml, "Tag", "Key", "Value");
|
||||
if (tags.size() != expected) {
|
||||
throw malformed();
|
||||
}
|
||||
XmlBuilder out = new XmlBuilder();
|
||||
tags.forEach((key, value) -> {
|
||||
if (key.isBlank() || value == null) {
|
||||
throw malformed();
|
||||
}
|
||||
out.start("Tag").elem("Key", key).elem("Value", value).end("Tag");
|
||||
});
|
||||
return out.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the {@code index}-th {@code Tiering} entry. Each tiering is an access tier and a
|
||||
* positive day count, and both are required. Days must also fall in the tier-specific range
|
||||
@@ -189,12 +132,4 @@ record S3IntelligentTieringConfiguration(String id, String innerXml) {
|
||||
.elem("Days", days)
|
||||
.build();
|
||||
}
|
||||
|
||||
private static String requireText(String xml, String element) {
|
||||
String value = XmlParser.extractFirst(xml, element, null);
|
||||
if (value == null) {
|
||||
throw malformed();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.S3FilterGrammar;
|
||||
import io.github.hectorvent.floci.core.common.XmlBuilder;
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A parsed {@code MetricsConfiguration} request body, reduced to its id and a canonical
|
||||
@@ -48,80 +48,9 @@ record S3MetricsConfiguration(String id, String innerXml) {
|
||||
|
||||
XmlBuilder inner = new XmlBuilder().elem("Id", id);
|
||||
if (hasFilter) {
|
||||
inner.start("Filter").raw(filterXml(xml)).end("Filter");
|
||||
inner.start("Filter").raw(S3FilterGrammar.filterXml(xml, List.of("AccessPointArn"))).end("Filter");
|
||||
}
|
||||
return new S3MetricsConfiguration(id, inner.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the filter. A filter is exactly one of a prefix, an object tag, an access point
|
||||
* ARN, or an And conjunction: AWS answers MalformedXML for a filter naming none of them or
|
||||
* more than one, and for an And holding fewer than two predicates.
|
||||
*/
|
||||
private static String filterXml(String xml) {
|
||||
List<String> predicates = XmlParser.childElementNames(xml, "Filter");
|
||||
if (predicates.size() != 1) {
|
||||
throw malformed();
|
||||
}
|
||||
|
||||
String predicate = predicates.getFirst();
|
||||
return switch (predicate) {
|
||||
case "Prefix", "AccessPointArn" -> new XmlBuilder()
|
||||
.elem(predicate, requireText(xml, predicate))
|
||||
.build();
|
||||
case "Tag" -> tagsXml(xml, 1);
|
||||
case "And" -> {
|
||||
List<String> conjuncts = XmlParser.childElementNames(xml, "And");
|
||||
if (conjuncts.size() < 2) {
|
||||
throw malformed();
|
||||
}
|
||||
XmlBuilder and = new XmlBuilder().start("And");
|
||||
if (conjuncts.contains("Prefix")) {
|
||||
and.elem("Prefix", requireText(xml, "Prefix"));
|
||||
}
|
||||
long tagCount = conjuncts.stream().filter("Tag"::equals).count();
|
||||
if (tagCount > 0) {
|
||||
and.raw(tagsXml(xml, (int) tagCount));
|
||||
}
|
||||
if (conjuncts.contains("AccessPointArn")) {
|
||||
and.elem("AccessPointArn", requireText(xml, "AccessPointArn"));
|
||||
}
|
||||
// Every conjunct has to be one floci understands, or the filter it stores would
|
||||
// not be the filter that was sent.
|
||||
if (conjuncts.size() != tagCount + (conjuncts.contains("Prefix") ? 1 : 0)
|
||||
+ (conjuncts.contains("AccessPointArn") ? 1 : 0)) {
|
||||
throw malformed();
|
||||
}
|
||||
yield and.end("And").build();
|
||||
}
|
||||
default -> throw malformed();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes {@code expected} tags. A tag carries both a key and a value, so a pair short of
|
||||
* the element count means one of them was missing.
|
||||
*/
|
||||
private static String tagsXml(String xml, int expected) {
|
||||
Map<String, String> tags = XmlParser.extractPairs(xml, "Tag", "Key", "Value");
|
||||
if (tags.size() != expected) {
|
||||
throw malformed();
|
||||
}
|
||||
XmlBuilder out = new XmlBuilder();
|
||||
tags.forEach((key, value) -> {
|
||||
if (key.isBlank() || value == null) {
|
||||
throw malformed();
|
||||
}
|
||||
out.start("Tag").elem("Key", key).elem("Value", value).end("Tag");
|
||||
});
|
||||
return out.build();
|
||||
}
|
||||
|
||||
private static String requireText(String xml, String element) {
|
||||
String value = XmlParser.extractFirst(xml, element, null);
|
||||
if (value == null) {
|
||||
throw malformed();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user