Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(s3): dedupe Filter grammar between IntelligentTiering and Metrics (#3836)
Priamo Germosen18a2f36eda62
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/S3FilterGrammar.java b/src/main/java/io/github/hectorvent/floci/core/common/S3FilterGrammar.javanew file mode 100644index 0000000000..29eb9c20e4--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/S3FilterGrammar.java@@ -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;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3IntelligentTieringConfiguration.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3IntelligentTieringConfiguration.javaindex 5e1d3b2416..1083d509e8 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3IntelligentTieringConfiguration.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3IntelligentTieringConfiguration.java@@ -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;- }}diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3MetricsConfiguration.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3MetricsConfiguration.javaindex 3fb6bb4807..348d72ef29 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3MetricsConfiguration.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3MetricsConfiguration.java@@ -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;- }}