Skip to content

Commit 86f0ca2

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
1 parent 0d21823 commit 86f0ca2

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,9 +1267,13 @@ private String determineHeaderExpression(Annotation headerAnnotation, MethodPara
12671267
AnnotationAttributes annotationAttributes =
12681268
(AnnotationAttributes) AnnotationUtils.getAnnotationAttributes(headerAnnotation);
12691269
String valueAttribute = annotationAttributes.getString(AnnotationUtils.VALUE);
1270+
int len = valueAttribute == null ? 0 : valueAttribute.length();
12701271
if (!StringUtils.hasText(valueAttribute)) {
12711272
headerName = methodParameter.getParameterName();
12721273
}
1274+
else if (len > 2 && valueAttribute.charAt(0) == '\'' && valueAttribute.charAt(len - 1) == '\'') {
1275+
headerName = valueAttribute.substring(1, len - 1);
1276+
}
12731277
else if (valueAttribute.indexOf('.') != -1) {
12741278
String[] tokens = valueAttribute.split("\\.", 2);
12751279
headerName = tokens[0];

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public Person(String fname, String lname) {
170170
this.name = fname + " " + lname;
171171
}
172172

173+
@Override
173174
public String toString() {
174175
return "Person: " + this.name;
175176
}
@@ -552,7 +553,8 @@ private void optionalAndRequiredWithAnnotatedMethodGuts(MethodInvokingMessagePro
552553
@Test
553554
public void optionalAndRequiredDottedWithAnnotatedMethod() throws Exception {
554555
AnnotatedTestService service = new AnnotatedTestService();
555-
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class);
556+
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class,
557+
String.class);
556558
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
557559
processor.setUseSpelInvoker(true);
558560
optionalAndRequiredDottedWithAnnotatedMethodGuts(processor, false);
@@ -561,7 +563,8 @@ public void optionalAndRequiredDottedWithAnnotatedMethod() throws Exception {
561563
@Test
562564
public void compiledOptionalAndRequiredDottedWithAnnotatedMethod() throws Exception {
563565
AnnotatedTestService service = new AnnotatedTestService();
564-
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class);
566+
Method method = service.getClass().getMethod("optionalAndRequiredDottedHeader", String.class, Integer.class,
567+
String.class);
565568
MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(service, method);
566569
processor.setUseSpelInvoker(true);
567570
DirectFieldAccessor compilerConfigAccessor = compileImmediate(processor);
@@ -577,17 +580,20 @@ private void optionalAndRequiredDottedWithAnnotatedMethodGuts(MethodInvokingMess
577580
processor.setBeanFactory(mock(BeanFactory.class));
578581
Message<String> message = MessageBuilder.withPayload("hello")
579582
.setHeader("dot2", new DotBean())
583+
.setHeader("dotted.literal", "dotted")
580584
.build();
581585
Object result = processor.processMessage(message);
582-
assertEquals("null42", result);
586+
assertEquals("null42dotted", result);
583587
message = MessageBuilder.withPayload("hello")
584588
.setHeader("dot1", new DotBean())
585589
.setHeader("dot2", new DotBean())
590+
.setHeader("dotted.literal", "dotted")
586591
.build();
587592
result = processor.processMessage(message);
588-
assertEquals("bar42", result);
593+
assertEquals("bar42dotted", result);
589594
message = MessageBuilder.withPayload("hello")
590595
.setHeader("dot1", new DotBean())
596+
.setHeader("dotted.literal", "dotted")
591597
.build();
592598
try {
593599
result = processor.processMessage(message);
@@ -1380,8 +1386,8 @@ public String optionalAndRequiredHeader(@Header(required = false) String prop,
13801386
}
13811387

13821388
public String optionalAndRequiredDottedHeader(@Header(name = "dot1.foo", required = false) String prop,
1383-
@Header(name = "dot2.baz") Integer num) {
1384-
return prop + num;
1389+
@Header(name = "dot2.baz") Integer num, @Header("'dotted.literal'") String dotted) {
1390+
return prop + num + dotted;
13851391
}
13861392

13871393
public Properties propertiesMethod(Properties properties) {

0 commit comments

Comments
 (0)