-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
268 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
implementation/src/main/java/io/smallrye/config/FallbackConfigSourceInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.smallrye.config; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import javax.annotation.Priority; | ||
|
||
@Priority(300) | ||
public class FallbackConfigSourceInterceptor implements ConfigSourceInterceptor { | ||
private final Function<String, String> mapping; | ||
|
||
public FallbackConfigSourceInterceptor(final Function<String, String> mapping) { | ||
this.mapping = mapping != null ? mapping : Function.identity(); | ||
} | ||
|
||
public FallbackConfigSourceInterceptor(final Map<String, String> 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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
implementation/src/main/java/io/smallrye/config/RelocateConfigSourceInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.smallrye.config; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import javax.annotation.Priority; | ||
|
||
@Priority(400) | ||
public class RelocateConfigSourceInterceptor implements ConfigSourceInterceptor { | ||
private final Function<String, String> mapping; | ||
|
||
public RelocateConfigSourceInterceptor(final Function<String, String> mapping) { | ||
this.mapping = mapping != null ? mapping : Function.identity(); | ||
} | ||
|
||
public RelocateConfigSourceInterceptor(final Map<String, String> mappings) { | ||
this(name -> mappings.getOrDefault(name, name)); | ||
} | ||
|
||
@Override | ||
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { | ||
final String map = mapping.apply(name); | ||
ConfigValue configValue = context.proceed(map); | ||
if (configValue == null && !name.equals(map)) { | ||
configValue = context.proceed(name); | ||
} | ||
return configValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
implementation/src/test/java/io/smallrye/config/InterceptorChainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.smallrye.config; | ||
|
||
import static io.smallrye.config.ProfileConfigSourceInterceptor.SMALLRYE_PROFILE; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.OptionalInt; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.junit.Test; | ||
|
||
public class InterceptorChainTest { | ||
@Test | ||
public void chain() { | ||
final Config config = buildConfig( | ||
"my.prop", "1", // original property | ||
"%prof.my.prop", "${%prof.my.prop.profile}", // profile property with expansion | ||
"%prof.my.prop.profile", "2", | ||
"my.prop.relocate", "3", // relocation | ||
"%prof.my.prop.relocate", "4", // profile with relocation | ||
SMALLRYE_PROFILE, "prof" // profile to use | ||
); | ||
assertEquals("4", config.getValue("my.prop", String.class)); | ||
} | ||
|
||
private static Config buildConfig(String... keyValues) { | ||
return new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.addDefaultInterceptors() | ||
.withSources(KeyValuesConfigSource.config(keyValues)) | ||
.withInterceptors( | ||
new RelocateConfigSourceInterceptor(s -> { | ||
if (s.contains("my.prop.profile")) { | ||
return "my.prop.relocate"; | ||
} | ||
return s; | ||
})) | ||
// Add the Profile Interceptor again because relocation may require a new search in the profiles | ||
.withInterceptorFactories( | ||
new ConfigSourceInterceptorFactory() { | ||
@Override | ||
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) { | ||
return new ProfileConfigSourceInterceptor(context); | ||
} | ||
|
||
@Override | ||
public OptionalInt getPriority() { | ||
return OptionalInt.of(399); | ||
} | ||
}) | ||
.build(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
implementation/src/test/java/io/smallrye/config/MappingConfigSourceInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String>() { | ||
{ | ||
put("smallrye.jwt.token.header", "mp.jwt.token.header"); | ||
} | ||
}), | ||
new FallbackConfigSourceInterceptor( | ||
new HashMap<String, String>() { | ||
{ | ||
put("smallrye.jwt.token.header", "mp.jwt.token.header"); | ||
put("smallrye.jwt.token.cookie", "mp.jwt.token.cookie"); | ||
} | ||
})) | ||
.build(); | ||
} | ||
} |