diff --git a/README.md b/README.md index 6137dcd..ddba26a 100644 --- a/README.md +++ b/README.md @@ -129,13 +129,38 @@ ``` Required -- value - Class that implements `Cleanser` +- value - Class that implements `Cleanser` or `ObjectAwareCleanser` -**Note:** The class supplied to `@CustomSanitizer` must implement the -`Cleanser` interface and expose a public no-argument constructor. If the -class does not implement `Cleanser`, the field is silently skipped. If the -class cannot be instantiated reflectively, an `IllegalStateException` is -thrown. +**Note:** The class supplied to `@CustomSanitizer` must implement either the +`Cleanser` or `ObjectAwareCleanser` interface and expose a public no-argument +constructor. If the class implements neither interface, the field is +silently skipped. If the class cannot be instantiated reflectively, an +`IllegalStateException` is thrown. + +### Object-aware custom sanitizers ### + +`Cleanser` only sees the annotated field's own value. For rules that depend +on a sibling field on the same object, implement `ObjectAwareCleanser` +instead: + +```java +public class DiscountCodeCleanser implements ObjectAwareCleanser { + + @Override + public String cleanse(Object owner, String value) { + Order order = (Order) owner; + return "VIP".equals(order.getTier()) ? value.toUpperCase() : value; + } + + @Override + public String description() { + return "Normalizes discount codes for VIP tier orders"; + } +} +``` + +`owner` is the object instance that owns the annotated field, so the +cleanser can read sibling fields to decide how to cleanse the value. ## Error handling ## diff --git a/src/main/java/io/ljunggren/sanitizer/cleanser/ObjectAwareCleanser.java b/src/main/java/io/ljunggren/sanitizer/cleanser/ObjectAwareCleanser.java new file mode 100644 index 0000000..9855f08 --- /dev/null +++ b/src/main/java/io/ljunggren/sanitizer/cleanser/ObjectAwareCleanser.java @@ -0,0 +1,27 @@ +package io.ljunggren.sanitizer.cleanser; + +/** + * Strategy interface for cleansing a field's value with visibility into the + * object that owns the field, for rules that depend on sibling fields (e.g. + * a postal code cleanser that behaves differently depending on a country + * field on the same object). + * + * @param the type of value the cleanser operates on + */ +public interface ObjectAwareCleanser { + + /** + * Apply the cleansing rule to the given value. + * + * @param owner the object that owns the annotated field + * @param value input value (implementations should accept {@code null}) + * @return the cleansed value + */ + String cleanse(Object owner, T value); + + /** + * @return human-readable description of what this cleanser does + */ + String description(); + +} diff --git a/src/main/java/io/ljunggren/sanitizer/sanitation/CustomSanitation.java b/src/main/java/io/ljunggren/sanitizer/sanitation/CustomSanitation.java index 18a0dd4..2777918 100644 --- a/src/main/java/io/ljunggren/sanitizer/sanitation/CustomSanitation.java +++ b/src/main/java/io/ljunggren/sanitizer/sanitation/CustomSanitation.java @@ -6,6 +6,7 @@ import io.ljunggren.sanitizer.Item; import io.ljunggren.sanitizer.annotation.CustomSanitizer; import io.ljunggren.sanitizer.cleanser.Cleanser; +import io.ljunggren.sanitizer.cleanser.ObjectAwareCleanser; public class CustomSanitation extends SanitationChain { @@ -13,13 +14,19 @@ public void sanitize(Annotation annotation, Item item) { if (annotation.annotationType() == CustomSanitizer.class) { Class clazz = ((CustomSanitizer) annotation).value(); - if (!Cleanser.class.isAssignableFrom(clazz)) { - // The configured class does not implement Cleanser; skip rather than fail. - return; - } try { - Cleanser cleanser = instantiateCleanser(clazz); - item.setValue(cleanser.cleanse(item.getValue())); + if (ObjectAwareCleanser.class.isAssignableFrom(clazz)) { + ObjectAwareCleanser cleanser = instantiate(clazz); + item.setValue(cleanser.cleanse(item.getObject(), item.getValue())); + return; + } + if (Cleanser.class.isAssignableFrom(clazz)) { + Cleanser cleanser = instantiate(clazz); + item.setValue(cleanser.cleanse(item.getValue())); + return; + } + // The configured class does not implement Cleanser or + // ObjectAwareCleanser; skip rather than fail. } catch (ReflectiveOperationException e) { throw new IllegalStateException( "Failed to instantiate CustomSanitizer " + clazz.getName(), e); @@ -30,10 +37,10 @@ } @SuppressWarnings("unchecked") - private Cleanser instantiateCleanser(Class clazz) + private C instantiate(Class clazz) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { - return (Cleanser) clazz.getDeclaredConstructor().newInstance(); + return (C) clazz.getDeclaredConstructor().newInstance(); } } diff --git a/src/test/java/io/ljunggren/sanitizer/sanitation/CustomSanitationExtraTest.java b/src/test/java/io/ljunggren/sanitizer/sanitation/CustomSanitationExtraTest.java index 45f4bee..95caa43 100755 --- a/src/test/java/io/ljunggren/sanitizer/sanitation/CustomSanitationExtraTest.java +++ b/src/test/java/io/ljunggren/sanitizer/sanitation/CustomSanitationExtraTest.java @@ -8,6 +8,7 @@ import io.ljunggren.sanitizer.Sanitizer; import io.ljunggren.sanitizer.annotation.CustomSanitizer; import io.ljunggren.sanitizer.cleanser.Cleanser; +import io.ljunggren.sanitizer.cleanser.ObjectAwareCleanser; import lombok.AllArgsConstructor; import lombok.Getter; @@ -74,4 +75,42 @@ new Sanitizer(pojo).sanitize(); assertEquals("alex!", pojo.getName()); } + + /** An ObjectAwareCleanser that uses a sibling field on the owning object. */ + public static class SuffixFromFlagCleanser implements ObjectAwareCleanser { + @Override + public String cleanse(Object owner, Object value) { + if (value == null) { + return null; + } + boolean flag = owner instanceof FlaggedPojo && ((FlaggedPojo) owner).isFlag(); + return flag ? value + "-flagged" : value.toString(); + } + @Override + public String description() { + return "append -flagged when sibling flag is true"; + } + } + + @AllArgsConstructor + @Getter + private static class FlaggedPojo { + private boolean flag; + @CustomSanitizer(SuffixFromFlagCleanser.class) + private String name; + } + + @Test + public void appliesObjectAwareCustomCleanserUsingSiblingFieldTest() { + FlaggedPojo pojo = new FlaggedPojo(true, "alex"); + new Sanitizer(pojo).sanitize(); + assertEquals("alex-flagged", pojo.getName()); + } + + @Test + public void objectAwareCustomCleanserNoOpWhenSiblingFlagFalseTest() { + FlaggedPojo pojo = new FlaggedPojo(false, "alex"); + new Sanitizer(pojo).sanitize(); + assertEquals("alex", pojo.getName()); + } }