Skip to content

Commit 194480d

Browse files
artembilangaryrussell
authored andcommitted
GH-2388: Fix HeaderEnricherSpec for adviceChain
Fixes #2388 During `HeaderEnricherSpec` refactoring the `adviceChain` population has been missed, alongside with many other `AbstractReplyProducingMessageHandler` options from the `ConsumerEndpointSpec` * Refactor `HeaderEnricherSpec` to build `HeaderEnricher` and an appropriate `MessageTransformingHandler` from the ctor to be able to pick up an `adviceChain` automatically in the `ConsumerEndpointSpec.get()` **Cherry-pick to 5.0.x**
1 parent 6718d8d commit 194480d

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/ConsumerEndpointSpec.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,10 +56,6 @@ public abstract class ConsumerEndpointSpec<S extends ConsumerEndpointSpec<S, H>,
5656

5757
protected ConsumerEndpointSpec(H messageHandler) {
5858
super(messageHandler);
59-
this.endpointFactoryBean.setAdviceChain(this.adviceChain);
60-
if (messageHandler instanceof AbstractReplyProducingMessageHandler) {
61-
((AbstractReplyProducingMessageHandler) messageHandler).setAdviceChain(this.adviceChain);
62-
}
6359
}
6460

6561
@Override
@@ -270,6 +266,10 @@ public S notPropagatedHeaders(String... headerPatterns) {
270266

271267
@Override
272268
protected Tuple2<ConsumerEndpointFactoryBean, H> doGet() {
269+
this.endpointFactoryBean.setAdviceChain(this.adviceChain);
270+
if (this.handler instanceof AbstractReplyProducingMessageHandler) {
271+
((AbstractReplyProducingMessageHandler) this.handler).setAdviceChain(this.adviceChain);
272+
}
273273
this.endpointFactoryBean.setHandler(this.handler);
274274
return super.doGet();
275275
}

spring-integration-core/src/main/java/org/springframework/integration/dsl/HeaderEnricherSpec.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,14 +55,11 @@ public class HeaderEnricherSpec extends ConsumerEndpointSpec<HeaderEnricherSpec,
5555

5656
private final Map<String, HeaderValueMessageProcessor<?>> headerToAdd = new HashMap<>();
5757

58-
private boolean defaultOverwrite = false;
59-
60-
private boolean shouldSkipNulls = true;
61-
62-
private MessageProcessor<?> messageProcessor;
58+
private final HeaderEnricher headerEnricher = new HeaderEnricher(this.headerToAdd);
6359

6460
HeaderEnricherSpec() {
6561
super(null);
62+
this.handler = new MessageTransformingHandler(this.headerEnricher);
6663
}
6764

6865
/**
@@ -73,7 +70,7 @@ public class HeaderEnricherSpec extends ConsumerEndpointSpec<HeaderEnricherSpec,
7370
* @see HeaderEnricher#setDefaultOverwrite(boolean)
7471
*/
7572
public HeaderEnricherSpec defaultOverwrite(boolean defaultOverwrite) {
76-
this.defaultOverwrite = defaultOverwrite;
73+
this.headerEnricher.setDefaultOverwrite(defaultOverwrite);
7774
return _this();
7875
}
7976

@@ -83,7 +80,7 @@ public HeaderEnricherSpec defaultOverwrite(boolean defaultOverwrite) {
8380
* @see HeaderEnricher#setShouldSkipNulls(boolean)
8481
*/
8582
public HeaderEnricherSpec shouldSkipNulls(boolean shouldSkipNulls) {
86-
this.shouldSkipNulls = shouldSkipNulls;
83+
this.headerEnricher.setShouldSkipNulls(shouldSkipNulls);
8784
return _this();
8885
}
8986

@@ -97,7 +94,7 @@ public HeaderEnricherSpec shouldSkipNulls(boolean shouldSkipNulls) {
9794
* @see HeaderEnricher#setMessageProcessor(MessageProcessor)
9895
*/
9996
public HeaderEnricherSpec messageProcessor(MessageProcessor<?> messageProcessor) {
100-
this.messageProcessor = messageProcessor;
97+
this.headerEnricher.setMessageProcessor(messageProcessor);
10198
return _this();
10299
}
103100

@@ -110,7 +107,7 @@ public HeaderEnricherSpec messageProcessor(MessageProcessor<?> messageProcessor)
110107
* @see #messageProcessor(MessageProcessor)
111108
*/
112109
public HeaderEnricherSpec messageProcessor(String expression) {
113-
return messageProcessor(new ExpressionEvaluatingMessageProcessor<>(PARSER.parseExpression(expression)));
110+
return messageProcessor(new ExpressionEvaluatingMessageProcessor<>(expression));
114111
}
115112

116113
/**
@@ -371,6 +368,7 @@ public <P> HeaderEnricherSpec headerFunction(String name, Function<Message<P>, O
371368
*/
372369
public <P> HeaderEnricherSpec headerFunction(String name, Function<Message<P>, Object> function,
373370
Boolean overwrite) {
371+
374372
return headerExpression(name, new FunctionExpression<>(function), overwrite);
375373
}
376374

@@ -391,6 +389,7 @@ private HeaderEnricherSpec headerExpression(String name, Expression expression,
391389
*/
392390
public <V> HeaderEnricherSpec header(String headerName,
393391
HeaderValueMessageProcessor<V> headerValueMessageProcessor) {
392+
394393
Assert.hasText(headerName, "'headerName' must not be empty");
395394
this.headerToAdd.put(headerName, headerValueMessageProcessor);
396395
return _this();
@@ -432,14 +431,7 @@ public HeaderEnricherSpec headerChannelsToString(String timeToLiveExpression) {
432431

433432
@Override
434433
protected Tuple2<ConsumerEndpointFactoryBean, MessageTransformingHandler> doGet() {
435-
HeaderEnricher headerEnricher = new HeaderEnricher(new HashMap<>(this.headerToAdd));
436-
headerEnricher.setDefaultOverwrite(this.defaultOverwrite);
437-
headerEnricher.setShouldSkipNulls(this.shouldSkipNulls);
438-
headerEnricher.setMessageProcessor(this.messageProcessor);
439-
440-
this.componentsToRegister.put(headerEnricher, null);
441-
442-
this.handler = new MessageTransformingHandler(headerEnricher);
434+
this.componentsToRegister.put(this.headerEnricher, null);
443435
return super.doGet();
444436
}
445437

spring-integration-core/src/test/java/org/springframework/integration/dsl/transformers/TransformerTests.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.integration.dsl.IntegrationFlows;
5252
import org.springframework.integration.dsl.Transformers;
5353
import org.springframework.integration.dsl.channel.MessageChannels;
54+
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
5455
import org.springframework.integration.handler.advice.IdempotentReceiverInterceptor;
5556
import org.springframework.integration.selector.MetadataStoreSelector;
5657
import org.springframework.integration.support.MessageBuilder;
@@ -91,7 +92,6 @@ public class TransformerTests {
9192
private PollableChannel enricherErrorChannel;
9293

9394

94-
9595
@Test
9696
public void testContentEnricher() {
9797
QueueChannel replyChannel = new QueueChannel();
@@ -218,6 +218,9 @@ public void testCodec() throws Exception {
218218
@Autowired
219219
private PollableChannel idempotentDiscardChannel;
220220

221+
@Autowired
222+
private PollableChannel adviceChannel;
223+
221224
@Test
222225
public void transformWithHeader() {
223226
QueueChannel replyChannel = new QueueChannel();
@@ -240,6 +243,7 @@ public void transformWithHeader() {
240243
}
241244

242245
assertNotNull(this.idempotentDiscardChannel.receive(10000));
246+
assertNotNull(this.adviceChannel.receive(10000));
243247
}
244248

245249
@Configuration
@@ -328,7 +332,7 @@ public IntegrationFlow pojoTransformFlow() {
328332
return f -> f
329333
.enrichHeaders(h -> h
330334
.header("Foo", "Bar")
331-
.advice(idempotentReceiverInterceptor()))
335+
.advice(idempotentReceiverInterceptor(), requestHandlerAdvice()))
332336
.transform(new PojoTransformer());
333337
}
334338

@@ -346,6 +350,26 @@ public IdempotentReceiverInterceptor idempotentReceiverInterceptor() {
346350
return idempotentReceiverInterceptor;
347351
}
348352

353+
@Bean
354+
public AbstractRequestHandlerAdvice requestHandlerAdvice() {
355+
return new AbstractRequestHandlerAdvice() {
356+
357+
@Override
358+
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message)
359+
throws Exception {
360+
361+
adviceChannel().send(message);
362+
return callback.execute();
363+
}
364+
365+
};
366+
}
367+
368+
@Bean
369+
public PollableChannel adviceChannel() {
370+
return new QueueChannel();
371+
}
372+
349373
@Bean
350374
public PollableChannel subFlowTestReplyChannel() {
351375
return new QueueChannel();

0 commit comments

Comments
 (0)