Skip to content

Commit

Permalink
Fixes #268. Added interceptor to Relocate configuration properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Apr 1, 2020
1 parent 77eedd0 commit 50520c7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 94 deletions.
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;
}
}
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;
}
}
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();
}
}

This file was deleted.

0 comments on commit 50520c7

Please sign in to comment.