diff --git a/README.md b/README.md index 073471b..0ef5461 100644 --- a/README.md +++ b/README.md @@ -93,3 +93,19 @@ Data types supported - String +## Custom ## + +Sanitizes against a custom class + +```java +@CustomSanitizer(className = "MyCleanser") +``` + +Required +- className - [String] Package and class name of custom sanitation class + +**Note:** Custom validation class must implement the Cleanser interface + +## TODO ## + +Handle exception from CustomSanitizer \ No newline at end of file diff --git a/src/main/java/com/ljunggren/sanitizer/Sanitizer.java b/src/main/java/com/ljunggren/sanitizer/Sanitizer.java index 0f77d22..fb53ac5 100644 --- a/src/main/java/com/ljunggren/sanitizer/Sanitizer.java +++ b/src/main/java/com/ljunggren/sanitizer/Sanitizer.java @@ -10,6 +10,7 @@ import com.ljunggren.sanitizer.sanitation.CatchAllSanitation; import com.ljunggren.sanitizer.sanitation.ConcurrentSpaceSanitation; +import com.ljunggren.sanitizer.sanitation.CustomSanitation; import com.ljunggren.sanitizer.sanitation.LowercaseSanitation; import com.ljunggren.sanitizer.sanitation.RemoveSanitation; import com.ljunggren.sanitizer.sanitation.TrimSanitation; @@ -70,8 +71,9 @@ .nextChain(new RemoveSanitation() .nextChain(new TrimSanitation() .nextChain(new UppercaseSanitation() + .nextChain(new CustomSanitation() .nextChain(new CatchAllSanitation() - ))))).sanitize(annotation, item); + )))))).sanitize(annotation, item); } } diff --git a/src/main/java/com/ljunggren/sanitizer/annotation/CustomSanitizer.java b/src/main/java/com/ljunggren/sanitizer/annotation/CustomSanitizer.java new file mode 100644 index 0000000..9b157fe --- /dev/null +++ b/src/main/java/com/ljunggren/sanitizer/annotation/CustomSanitizer.java @@ -0,0 +1,14 @@ +package com.ljunggren.sanitizer.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface CustomSanitizer { + + String className(); + +} diff --git a/src/main/java/com/ljunggren/sanitizer/cleanser/Cleanser.java b/src/main/java/com/ljunggren/sanitizer/cleanser/Cleanser.java index f79a965..fef4dd8 100644 --- a/src/main/java/com/ljunggren/sanitizer/cleanser/Cleanser.java +++ b/src/main/java/com/ljunggren/sanitizer/cleanser/Cleanser.java @@ -4,4 +4,5 @@ String cleanse(T value); String description(); + } diff --git a/src/main/java/com/ljunggren/sanitizer/sanitation/CustomSanitation.java b/src/main/java/com/ljunggren/sanitizer/sanitation/CustomSanitation.java new file mode 100644 index 0000000..fdb788f --- /dev/null +++ b/src/main/java/com/ljunggren/sanitizer/sanitation/CustomSanitation.java @@ -0,0 +1,39 @@ +package com.ljunggren.sanitizer.sanitation; + +import java.lang.annotation.Annotation; + +import com.ljunggren.sanitizer.Item; +import com.ljunggren.sanitizer.annotation.CustomSanitizer; +import com.ljunggren.sanitizer.cleanser.Cleanser; + +public class CustomSanitation extends SanitationChain { + + @Override + public void sanitize(Annotation annotation, Item item) { + if (annotation.annotationType() == CustomSanitizer.class) { + String className = ((CustomSanitizer) annotation).className(); + try { + Cleanser cleanser = instantiateCleanser(className); + item.setValue(cleanser.cleanse(item.getValue().toString())); + } catch (Exception e) { + // do something with exception + } + return; + } + nextChain.sanitize(annotation, item); + } + + @SuppressWarnings("unchecked") + private Cleanser instantiateCleanser(String className) throws Exception { + try { + Class clazz = Class.forName(className); + if (Cleanser.class.isAssignableFrom(clazz)) { + return (Cleanser) clazz.newInstance(); + } + throw new Exception("CustomSanitizer className does not implement Cleanser"); + } catch (ClassNotFoundException e) { + throw new Exception("CustomSanitizer class not found " + e.getMessage()); + } + } + +} diff --git a/src/test/java/com/ljunggren/sanitizer/SanitizerTest.java b/src/test/java/com/ljunggren/sanitizer/SanitizerTest.java index 79c0a5b..1079086 100644 --- a/src/test/java/com/ljunggren/sanitizer/SanitizerTest.java +++ b/src/test/java/com/ljunggren/sanitizer/SanitizerTest.java @@ -19,7 +19,7 @@ } @Test - public void validateTest() { + public void sanitizeTest() { TestPojo pojo = new TestPojo("Alex"); new Sanitizer(pojo).sanitize(); assertEquals("alex", pojo.getName()); diff --git a/src/test/java/com/ljunggren/sanitizer/sanitation/CatchAllSanitationTest.java b/src/test/java/com/ljunggren/sanitizer/sanitation/CatchAllSanitationTest.java index 3e3afc7..7034739 100644 --- a/src/test/java/com/ljunggren/sanitizer/sanitation/CatchAllSanitationTest.java +++ b/src/test/java/com/ljunggren/sanitizer/sanitation/CatchAllSanitationTest.java @@ -20,7 +20,7 @@ } @Test - public void test() { + public void sanitizeTest() { Pojo pojo = new Pojo(1234); new Sanitizer(pojo).sanitize(); assertEquals(1234, pojo.getNumber()); diff --git a/src/test/java/com/ljunggren/sanitizer/sanitation/CustomSanitationTest.java b/src/test/java/com/ljunggren/sanitizer/sanitation/CustomSanitationTest.java new file mode 100644 index 0000000..d582815 --- /dev/null +++ b/src/test/java/com/ljunggren/sanitizer/sanitation/CustomSanitationTest.java @@ -0,0 +1,43 @@ +package com.ljunggren.sanitizer.sanitation; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.ljunggren.sanitizer.Sanitizer; +import com.ljunggren.sanitizer.annotation.CustomSanitizer; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +public class CustomSanitationTest { + + @AllArgsConstructor + @Getter + private class CustomPojo { + @CustomSanitizer(className = "com.ljunggren.sanitizer.cleanser.LowercaseCleanser") + private String name; + } + + @Test + public void sanitizeTest() { + CustomPojo pojo = new CustomPojo("ALEX"); + new Sanitizer(pojo).sanitize(); + assertEquals("alex", pojo.getName()); + } + + @AllArgsConstructor + @Getter + private class InvalidCustomPojo { + @CustomSanitizer(className = "not.real.class") + private String name; + } + + @Test + public void sanitizeInvalidClassTest() { + InvalidCustomPojo pojo = new InvalidCustomPojo("ALEX"); + new Sanitizer(pojo).sanitize(); + assertEquals("ALEX", pojo.getName()); + } + +}