GH-1104 Add support for case insensitive routing header names

Resolves #1104
This commit is contained in:
Oleg Zhurakousky
2024-01-31 13:53:24 +01:00
parent d5b7cb0d5a
commit 3931ea4f14
2 changed files with 25 additions and 8 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.function.context.config;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import org.apache.commons.logging.Log;
@@ -133,14 +134,8 @@ public class RoutingFunction implements Function<Object, Object> {
}
}
if (function == null) {
if (StringUtils.hasText((String) message.getHeaders().get(FunctionProperties.FUNCTION_DEFINITION))) {
function = functionFromDefinition((String) message.getHeaders().get(FunctionProperties.FUNCTION_DEFINITION));
if (function.isInputTypePublisher()) {
this.assertOriginalInputIsNotPublisher(originalInputIsPublisher);
}
}
else if (StringUtils.hasText((String) message.getHeaders().get(FunctionProperties.ROUTING_EXPRESSION))) {
function = this.functionFromExpression((String) message.getHeaders().get(FunctionProperties.ROUTING_EXPRESSION), message, true);
function = this.locateFunctionFromDefinitionOrExpression(message);
if (function != null) {
if (function.isInputTypePublisher()) {
this.assertOriginalInputIsNotPublisher(originalInputIsPublisher);
}
@@ -196,6 +191,18 @@ public class RoutingFunction implements Function<Object, Object> {
return function.apply(input);
}
private FunctionInvocationWrapper locateFunctionFromDefinitionOrExpression(Message<?> message) {
for (Entry<String, Object> headerEntry : message.getHeaders().entrySet()) {
if (headerEntry.getKey().equalsIgnoreCase(FunctionProperties.FUNCTION_DEFINITION)) {
return functionFromDefinition((String) headerEntry.getValue());
}
else if (headerEntry.getKey().equalsIgnoreCase(FunctionProperties.ROUTING_EXPRESSION)) {
return this.functionFromExpression((String) headerEntry.getValue(), message, true);
}
}
return null;
}
private void assertOriginalInputIsNotPublisher(boolean originalInputIsPublisher) {
Assert.isTrue(!originalInputIsPublisher, "Routing input of type Publisher is not supported per individual "
+ "values (e.g., message header or POJO). Instead you should use 'spring.cloud.function.definition' or "

View File

@@ -266,6 +266,16 @@ public class RoutingFunctionTests {
assertThat(function.apply(message)).isEqualTo("olleh");
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testMultipleRoutersCaseInsensitiveKeys() {
FunctionCatalog functionCatalog = this.configureCatalog(MultipleRouterConfiguration.class);
Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME);
assertThat(function).isNotNull();
Message<String> message = MessageBuilder.withPayload("hello").setHeader(FunctionProperties.PREFIX + ".DeFiNition", "uppercase").build();
assertThat(function.apply(message)).isEqualTo("HELLO");
}
@EnableAutoConfiguration
@Configuration
protected static class RoutingFunctionConfiguration {