Skip to content

Commit 9c24e16

Browse files
garyrussellartembilan
authored andcommitted
GH-3083: Support @Header with dotted literals
Fixes #3083 `@Header("foo.bar")` means extract property `bar` from header `foo`. Support `@Header("'foo.bar'")`, meaning get the value of header `foo.bar`. **cherry-pick to 5.1.x, 4.3.x** # Conflicts: # spring-integration-core/src/test/java/org/springframework/integration/handler/MethodInvokingMessageProcessorTests.java # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/handler/support/MessagingMethodInvokerHelper.java # spring-integration-core/src/test/java/org/springframework/integration/handler/MethodInvokingMessageProcessorTests.java
1 parent a63f197 commit 9c24e16

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

spring-integration-core/src/main/java/org/springframework/integration/util/MessagingMethodInvokerHelper.java

+4
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,13 @@ private String determineHeaderExpression(Annotation headerAnnotation, MethodPara
830830
AnnotationAttributes annotationAttributes =
831831
(AnnotationAttributes) AnnotationUtils.getAnnotationAttributes(headerAnnotation);
832832
String valueAttribute = annotationAttributes.getString(AnnotationUtils.VALUE);
833+
int len = valueAttribute == null ? 0 : valueAttribute.length();
833834
if (!StringUtils.hasText(valueAttribute)) {
834835
headerName = methodParameter.getParameterName();
835836
}
837+
else if (len > 2 && valueAttribute.charAt(0) == '\'' && valueAttribute.charAt(len - 1) == '\'') {
838+
headerName = valueAttribute.substring(1, len - 1);
839+
}
836840
else if (valueAttribute.indexOf('.') != -1) {
837841
String[] tokens = valueAttribute.split("\\.", 2);
838842
headerName = tokens[0];

spring-integration-core/src/test/java/org/springframework/integration/handler/MethodInvokingMessageProcessorTests.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,17 @@ private void optionalAndRequiredWithAnnotatedMethodGuts(MethodInvokingMessagePro
411411
@Test
412412
public void optionalAndRequiredDottedWithAnnotatedMethod() throws Exception {
413413
AnnotatedTestService service = new AnnotatedTestService();
414-
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class);
414+
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class,
415+
String.class);
415416
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
416417
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor, false);
417418
}
418419

419420
@Test
420421
public void compiledOptionalAndRequiredDottedWithAnnotatedMethod() throws Exception {
421422
AnnotatedTestService service = new AnnotatedTestService();
422-
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class);
423+
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class,
424+
String.class);
423425
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
424426
DirectFieldAccessor compilerConfigAccessor = compileImmediate(processor);
425427
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor, true);
@@ -432,17 +434,20 @@ private void optionalAndRequiredDottedWithAnnotatedMethodGuts(MethodInvokingMess
432434
boolean compiled) {
433435
Message<String> message = MessageBuilder.withPayload("hello")
434436
.setHeader("dot2", new DotBean())
437+
.setHeader("dotted.literal", "dotted")
435438
.build();
436439
Object result = processor.processMessage(message);
437-
assertEquals("null42", result);
440+
assertEquals("null42dotted", result);
438441
message = MessageBuilder.withPayload("hello")
439442
.setHeader("dot1", new DotBean())
440443
.setHeader("dot2", new DotBean())
444+
.setHeader("dotted.literal", "dotted")
441445
.build();
442446
result = processor.processMessage(message);
443-
assertEquals("bar42", result);
447+
assertEquals("bar42dotted", result);
444448
message = MessageBuilder.withPayload("hello")
445449
.setHeader("dot1", new DotBean())
450+
.setHeader("dotted.literal", "dotted")
446451
.build();
447452
try {
448453
result = processor.processMessage(message);
@@ -794,8 +799,8 @@ public String optionalAndRequiredHeader(@Header(required = false) String prop,
794799
}
795800

796801
public String optionalAndRequiredDottedHeader(@Header(name = "dot1.foo", required = false) String prop,
797-
@Header(name = "dot2.baz") Integer num) {
798-
return prop + num;
802+
@Header(name = "dot2.baz") Integer num, @Header("'dotted.literal'") String dotted) {
803+
return prop + num + dotted;
799804
}
800805

801806
public Properties propertiesMethod(Properties properties) {

0 commit comments

Comments
 (0)