From 50520c7fa53a7e6fd92a8c919e01b22e3d495e68 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Wed, 1 Apr 2020 16:10:09 +0100 Subject: [PATCH] Fixes #268. Added interceptor to Relocate configuration properties. --- .../FallbackConfigSourceInterceptor.java | 31 +++++++ .../RelocateConfigSourceInterceptor.java | 23 +++-- .../MappingConfigSourceInterceptorTest.java | 58 ++++++++++++ .../RelocateConfigSourceInterceptorTest.java | 88 ------------------- 4 files changed, 106 insertions(+), 94 deletions(-) create mode 100644 implementation/src/main/java/io/smallrye/config/FallbackConfigSourceInterceptor.java create mode 100644 implementation/src/test/java/io/smallrye/config/MappingConfigSourceInterceptorTest.java delete mode 100644 implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java diff --git a/implementation/src/main/java/io/smallrye/config/FallbackConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/FallbackConfigSourceInterceptor.java new file mode 100644 index 000000000..e065cc2f5 --- /dev/null +++ b/implementation/src/main/java/io/smallrye/config/FallbackConfigSourceInterceptor.java @@ -0,0 +1,31 @@ +package io.smallrye.config; + +import java.util.Map; +import java.util.function.Function; + +import javax.annotation.Priority; + +@Priority(500) +public class FallbackConfigSourceInterceptor implements ConfigSourceInterceptor { + private final Function mapping; + + public FallbackConfigSourceInterceptor(final Function mapping) { + this.mapping = mapping != null ? mapping : Function.identity(); + } + + public FallbackConfigSourceInterceptor(final Map mappings) { + this(name -> mappings.getOrDefault(name, name)); + } + + @Override + public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { + ConfigValue configValue = context.proceed(name); + if (configValue == null) { + final String map = mapping.apply(name); + if (!name.equals(map)) { + configValue = context.proceed(map); + } + } + return configValue; + } +} diff --git a/implementation/src/main/java/io/smallrye/config/RelocateConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/RelocateConfigSourceInterceptor.java index 5da8a0085..790b024a6 100644 --- a/implementation/src/main/java/io/smallrye/config/RelocateConfigSourceInterceptor.java +++ b/implementation/src/main/java/io/smallrye/config/RelocateConfigSourceInterceptor.java @@ -1,18 +1,29 @@ package io.smallrye.config; +import java.util.Map; +import java.util.function.Function; + import javax.annotation.Priority; -@Priority(200) +@Priority(300) public class RelocateConfigSourceInterceptor implements ConfigSourceInterceptor { - private static final String RELOCATE_PREFIX = "%relocate."; + private final Function mapping; + + public RelocateConfigSourceInterceptor(final Function mapping) { + this.mapping = mapping != null ? mapping : Function.identity(); + } + + public RelocateConfigSourceInterceptor(final Map mappings) { + this(name -> mappings.getOrDefault(name, name)); + } @Override public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { - final ConfigValue configValue = context.proceed(name); - if (configValue != null && configValue.getValue().startsWith(RELOCATE_PREFIX)) { - return context.proceed(configValue.getValue().substring(RELOCATE_PREFIX.length())); + final String map = mapping.apply(name); + ConfigValue configValue = context.proceed(map); + if (configValue == null && !name.equals(map)) { + configValue = context.proceed(name); } - return configValue; } } diff --git a/implementation/src/test/java/io/smallrye/config/MappingConfigSourceInterceptorTest.java b/implementation/src/test/java/io/smallrye/config/MappingConfigSourceInterceptorTest.java new file mode 100644 index 000000000..65c497c58 --- /dev/null +++ b/implementation/src/test/java/io/smallrye/config/MappingConfigSourceInterceptorTest.java @@ -0,0 +1,58 @@ +package io.smallrye.config; + +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; + +import org.eclipse.microprofile.config.Config; +import org.junit.Test; + +public class MappingConfigSourceInterceptorTest { + @Test + public void relocateAndFallback() { + final Config config = buildConfig("mp.jwt.token.header", "Authorization", + "mp.jwt.token.cookie", "Bearer"); + + assertEquals("Authorization", config.getValue("smallrye.jwt.token.header", String.class)); + assertEquals("Bearer", config.getValue("smallrye.jwt.token.cookie", String.class)); + } + + @Test + public void relocate() { + final Config config = buildConfig( + "smallrye.jwt.token.header", "Cookie", + "mp.jwt.token.header", "Authorization"); + + assertEquals("Authorization", config.getValue("smallrye.jwt.token.header", String.class)); + } + + @Test + public void fallback() { + final Config config = buildConfig( + "smallrye.jwt.token.cookie", "jwt", + "mp.jwt.token.cookie", "Bearer"); + + assertEquals("jwt", config.getValue("smallrye.jwt.token.cookie", String.class)); + } + + private static Config buildConfig(String... keyValues) { + return new SmallRyeConfigBuilder() + .addDefaultSources() + .withSources(KeyValuesConfigSource.config(keyValues)) + .withInterceptors( + new RelocateConfigSourceInterceptor( + new HashMap() { + { + put("smallrye.jwt.token.header", "mp.jwt.token.header"); + } + }), + new FallbackConfigSourceInterceptor( + new HashMap() { + { + put("smallrye.jwt.token.header", "mp.jwt.token.header"); + put("smallrye.jwt.token.cookie", "mp.jwt.token.cookie"); + } + })) + .build(); + } +} diff --git a/implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java b/implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java deleted file mode 100644 index 47560a209..000000000 --- a/implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package io.smallrye.config; - -import static org.junit.Assert.assertEquals; - -import java.util.HashMap; - -import org.eclipse.microprofile.config.Config; -import org.junit.Test; - -import io.smallrye.config.common.MapBackedConfigSource; - -public class RelocateConfigSourceInterceptorTest { - @Test - public void relocate() { - final Config config = buildConfig("smallrye.jwt.token.header", "%relocate.mp.jwt.token.header", - "mp.jwt.token.header", "Authorization"); - - assertEquals("Authorization", config.getValue("smallrye.jwt.token.header", String.class)); - } - - @Test - public void noRelocatePriority() { - final Config config = new SmallRyeConfigBuilder() - .withSources( - new MapBackedConfigSource("HighPriority", - new HashMap() { - { - put("smallrye.jwt.token.header", "Authorization"); - } - }, 1000) { - }) - .withSources( - new MapBackedConfigSource("LowPriority", - new HashMap() { - { - put("smallrye.jwt.token.header", "%relocate.mp.jwt.token.header"); - put("mp.jwt.token.header", "Cookie"); - } - }, 100) { - }) - .withInterceptors(new RelocateConfigSourceInterceptor()) - .build(); - - assertEquals("Authorization", config.getValue("smallrye.jwt.token.header", String.class)); - } - - @Test - public void relocatePriority() { - final Config config = new SmallRyeConfigBuilder() - .withSources( - new MapBackedConfigSource("HighPriority", - new HashMap() { - { - put("smallrye.jwt.token.header", "Authorization"); - } - }, 100) { - }) - .withSources( - new MapBackedConfigSource("LowPriority", - new HashMap() { - { - put("smallrye.jwt.token.header", "%relocate.mp.jwt.token.header"); - put("mp.jwt.token.header", "Cookie"); - } - }, 1000) { - }) - .withInterceptors(new RelocateConfigSourceInterceptor()) - .build(); - - assertEquals("Cookie", config.getValue("smallrye.jwt.token.header", String.class)); - } - - @Test - public void noLoop() { - final Config config = buildConfig("smallrye.jwt.token.header", "%relocate.mp.jwt.token.header", - "mp.jwt.token.header", "%relocate.smallrye.jwt.token.header"); - - assertEquals("%relocate.smallrye.jwt.token.header", config.getValue("smallrye.jwt.token.header", String.class)); - } - - private static Config buildConfig(String... keyValues) { - return new SmallRyeConfigBuilder() - .addDefaultSources() - .withSources(KeyValuesConfigSource.config(keyValues)) - .withInterceptors(new RelocateConfigSourceInterceptor()) - .build(); - } -}