Skip to content

Commit 3931ea4

Browse files
committed
GH-1104 Add support for case insensitive routing header names
Resolves #1104
1 parent d5b7cb0 commit 3931ea4

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/RoutingFunction.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.cloud.function.context.config;
1818

1919
import java.util.Map;
20+
import java.util.Map.Entry;
2021
import java.util.function.Function;
2122

2223
import org.apache.commons.logging.Log;
@@ -133,14 +134,8 @@ private Object route(Object input, boolean originalInputIsPublisher) {
133134
}
134135
}
135136
if (function == null) {
136-
if (StringUtils.hasText((String) message.getHeaders().get(FunctionProperties.FUNCTION_DEFINITION))) {
137-
function = functionFromDefinition((String) message.getHeaders().get(FunctionProperties.FUNCTION_DEFINITION));
138-
if (function.isInputTypePublisher()) {
139-
this.assertOriginalInputIsNotPublisher(originalInputIsPublisher);
140-
}
141-
}
142-
else if (StringUtils.hasText((String) message.getHeaders().get(FunctionProperties.ROUTING_EXPRESSION))) {
143-
function = this.functionFromExpression((String) message.getHeaders().get(FunctionProperties.ROUTING_EXPRESSION), message, true);
137+
function = this.locateFunctionFromDefinitionOrExpression(message);
138+
if (function != null) {
144139
if (function.isInputTypePublisher()) {
145140
this.assertOriginalInputIsNotPublisher(originalInputIsPublisher);
146141
}
@@ -196,6 +191,18 @@ else if (StringUtils.hasText(functionProperties.getRoutingExpression())) {
196191
return function.apply(input);
197192
}
198193

194+
private FunctionInvocationWrapper locateFunctionFromDefinitionOrExpression(Message<?> message) {
195+
for (Entry<String, Object> headerEntry : message.getHeaders().entrySet()) {
196+
if (headerEntry.getKey().equalsIgnoreCase(FunctionProperties.FUNCTION_DEFINITION)) {
197+
return functionFromDefinition((String) headerEntry.getValue());
198+
}
199+
else if (headerEntry.getKey().equalsIgnoreCase(FunctionProperties.ROUTING_EXPRESSION)) {
200+
return this.functionFromExpression((String) headerEntry.getValue(), message, true);
201+
}
202+
}
203+
return null;
204+
}
205+
199206
private void assertOriginalInputIsNotPublisher(boolean originalInputIsPublisher) {
200207
Assert.isTrue(!originalInputIsPublisher, "Routing input of type Publisher is not supported per individual "
201208
+ "values (e.g., message header or POJO). Instead you should use 'spring.cloud.function.definition' or "

spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/config/RoutingFunctionTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ public void testMultipleRouters() {
266266
assertThat(function.apply(message)).isEqualTo("olleh");
267267
}
268268

269+
@SuppressWarnings({ "rawtypes", "unchecked" })
270+
@Test
271+
public void testMultipleRoutersCaseInsensitiveKeys() {
272+
FunctionCatalog functionCatalog = this.configureCatalog(MultipleRouterConfiguration.class);
273+
Function function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME);
274+
assertThat(function).isNotNull();
275+
Message<String> message = MessageBuilder.withPayload("hello").setHeader(FunctionProperties.PREFIX + ".DeFiNition", "uppercase").build();
276+
assertThat(function.apply(message)).isEqualTo("HELLO");
277+
}
278+
269279
@EnableAutoConfiguration
270280
@Configuration
271281
protected static class RoutingFunctionConfiguration {

0 commit comments

Comments
 (0)