GH-676 Relax SpEL evaluation failures for input header enrichment
Add documentation Resolves #676
This commit is contained in:
@@ -20,6 +20,8 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.expression.MapAccessor;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.expression.Expression;
|
||||
@@ -37,7 +39,9 @@ import org.springframework.util.Assert;
|
||||
* @since 3.1.3
|
||||
*
|
||||
*/
|
||||
public class InputEnricher implements Function<Object, Object> {
|
||||
class InputEnricher implements Function<Object, Object> {
|
||||
|
||||
protected Log logger = LogFactory.getLog(InputEnricher.class);
|
||||
|
||||
private final Map<String, Map<String, String>> headerExpressions;
|
||||
|
||||
@@ -62,8 +66,19 @@ public class InputEnricher implements Function<Object, Object> {
|
||||
Map<String, String> mappings = this.headerExpressions.get("0");
|
||||
for (Entry<String, String> keyValueExpressionEntry : mappings.entrySet()) {
|
||||
Expression expression = this.spelParser.parseExpression(keyValueExpressionEntry.getValue());
|
||||
Object value = expression.getValue(this.evalContext, input, Object.class);
|
||||
messageBuilder.setHeader(keyValueExpressionEntry.getKey(), value);
|
||||
try {
|
||||
Object value = expression.getValue(this.evalContext, input, Object.class);
|
||||
messageBuilder.setHeader(keyValueExpressionEntry.getKey(), value);
|
||||
}
|
||||
catch (Exception e) {
|
||||
String message = "Failed while evaluating expression \"" + keyValueExpressionEntry.getValue() + "\" on incoming message";
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.warn(message + ": " + input, e);
|
||||
}
|
||||
else {
|
||||
logger.warn(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
input = messageBuilder.build();
|
||||
}
|
||||
|
||||
@@ -34,7 +34,23 @@ import org.springframework.messaging.support.MessageBuilder;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
//NOTE!!! assertions for all tests are in 'echo' function since we're validating what's coming into it.
|
||||
public class FunctionPropertiesTests {
|
||||
public class InputHeaderMappingTests {
|
||||
|
||||
@Test
|
||||
public void testErrorWarnAndContinue() throws Exception {
|
||||
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
|
||||
SampleFunctionConfiguration.class).web(WebApplicationType.NONE).run(
|
||||
"--logging.level.org.springframework.cloud.function=DEBUG",
|
||||
"--spring.main.lazy-initialization=true",
|
||||
"--spring.cloud.function.configuration.echoFail.input-header-mapping-expression[0].key1=hello1",
|
||||
"--spring.cloud.function.configuration.echoFail.input-header-mapping-expression[0].key2='hello2'",
|
||||
"--spring.cloud.function.configuration.echoFail.input-header-mapping-expression[0].foo=headers.contentType")) {
|
||||
|
||||
FunctionCatalog functionCatalog = context.getBean(FunctionCatalog.class);
|
||||
FunctionInvocationWrapper function = functionCatalog.lookup("echoFail");
|
||||
function.apply(MessageBuilder.withPayload("helo").build());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInputHeaderMappingPropertyWithIndex() throws Exception {
|
||||
@@ -137,6 +153,16 @@ public class FunctionPropertiesTests {
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Message<?>, Message<?>> echoFail() {
|
||||
return m -> {
|
||||
assertThat(m.getHeaders().containsKey("key1")).isFalse();
|
||||
assertThat(m.getHeaders().get("key2")).isEqualTo("hello2");
|
||||
assertThat(m.getHeaders().containsKey("foo")).isFalse();
|
||||
return m;
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Function<Message<?>, Message<?>> split() {
|
||||
return m -> {
|
||||
Reference in New Issue
Block a user