-
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.
Fixes #268. Added interceptor to Relocate configuration properties.
- Loading branch information
Showing
4 changed files
with
106 additions
and
94 deletions.
There are no files selected for viewing
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(500) | ||
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; | ||
} | ||
} |
23 changes: 17 additions & 6 deletions
23
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 |
---|---|---|
@@ -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<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 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; | ||
} | ||
} |
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(); | ||
} | ||
} |
88 changes: 0 additions & 88 deletions
88
implementation/src/test/java/io/smallrye/config/RelocateConfigSourceInterceptorTest.java
This file was deleted.
Oops, something went wrong.