diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/EnricherParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/EnricherParser.java index cc18017a47..55f084912c 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/EnricherParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/EnricherParser.java @@ -41,16 +41,9 @@ public class EnricherParser extends AbstractConsumerEndpointParser { @Override protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) { final BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ContentEnricher.class); - String requestChannel = element.getAttribute("request-channel"); - String replyChannel = element.getAttribute("reply-channel"); - if (StringUtils.hasText(requestChannel)) { - builder.addConstructorArgReference(requestChannel); - } - - if (StringUtils.hasText(replyChannel)) { - builder.addConstructorArgReference(replyChannel); - } + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "request-channel"); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel"); List propertyElements = DomUtils.getChildElementsByTagName(element, "property"); if (!CollectionUtils.isEmpty(propertyElements)) { diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/ContentEnricher.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/ContentEnricher.java index 1c95d20c1c..99e586c10d 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/ContentEnricher.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/ContentEnricher.java @@ -36,8 +36,10 @@ import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** - * Content Enricher is a Message Transformer that invokes any downstream message flow via - * its request channel and then applies values from the reply Message to the original payload. + * Content Enricher is a Message Transformer that can augment a message's payload + * with either static values or by optionally invoking a downstream message flow + * via its request channel and then applying values from the reply Message to the + * original payload. * * @author Mark Fisher * @since 2.1 @@ -46,7 +48,7 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem private final Map propertyExpressions = new HashMap(); - private final Gateway gateway; + private Gateway gateway = null; private final SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); @@ -56,38 +58,8 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem private Expression requestPayloadExpression; - /** - * Create a Content Enricher without providing a request channel. This is - * useful when only static values shall be enriched. - */ - public ContentEnricher() { - this.evaluationContext.addPropertyAccessor(new MapAccessor()); - this.gateway = null; - } - - /** - * Create a Content Enricher with the given request channel. An anonymous reply channel - * will be created for each request. - */ - public ContentEnricher(MessageChannel requestChannel) { - this(requestChannel, null); - } - - /** - * Create a Content Enricher with the given request and reply channels. - */ - public ContentEnricher(MessageChannel requestChannel, MessageChannel replyChannel) { - Assert.notNull(requestChannel, "requestChannel must not be null"); - - this.gateway = new Gateway(); - - this.gateway.setRequestChannel(requestChannel); - if (replyChannel != null) { - this.gateway.setReplyChannel(replyChannel); - } - this.evaluationContext.addPropertyAccessor(new MapAccessor()); - } - + private MessageChannel requestChannel; + private MessageChannel replyChannel; /** * Provide the map of expressions to evaluate when enriching the target payload. @@ -108,6 +80,25 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem } } + /** + * Sets the content enricher's request channel. If specified, then an internal + * {@link Gateway} will be initialized. Setting a request channel is optional. + * Not setting a request channel is useful in situations where + * message payloads shall be enriched with static values only. + */ + public void setRequestChannel(MessageChannel requestChannel) { + this.requestChannel = requestChannel; + } + + /** + * Sets the content enricher's reply channel. If not specified, yet the request + * channel is set, an anonymous reply channel will automatically created + * for each request. + */ + public void setReplyChannel(MessageChannel replyChannel) { + this.replyChannel = replyChannel; + } + /** * By default the original message's payload will be used as the actual payload * that will be send to the request-channel. @@ -144,14 +135,32 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem this.shouldClonePayload = shouldClonePayload; } + + /** + * Initializes the Content Enricher. Will instantiate an internal Gateway if + * the requestChannel is set. + */ @Override public void onInit() { super.onInit(); - if (this.gateway != null) { - this.gateway.afterPropertiesSet(); + if (this.replyChannel != null) { + Assert.notNull(this.requestChannel, "If the replyChannel is set, then the requestChannel must not be null"); } + if (this.requestChannel != null) { + this.gateway = new Gateway(); + this.gateway.setRequestChannel(requestChannel); + + if (replyChannel != null) { + this.gateway.setReplyChannel(replyChannel); + } + + this.gateway.afterPropertiesSet(); + } + + this.evaluationContext.addPropertyAccessor(new MapAccessor()); + } @Override diff --git a/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java b/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java index 8ead98e0b5..159ce46d82 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java @@ -52,11 +52,16 @@ public class ContentEnricherTests { return new Source("John", "Doe"); } }); - ContentEnricher enricher = new ContentEnricher(requestChannel); + + ContentEnricher enricher = new ContentEnricher(); + enricher.setRequestChannel(requestChannel); + SpelExpressionParser parser = new SpelExpressionParser(); Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.afterPropertiesSet(); + Target target = new Target("replace me"); Message requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build(); enricher.handleMessage(requestMessage); @@ -82,10 +87,13 @@ public class ContentEnricherTests { @Test public void testContentEnricherWithNullRequestChannel() { + ContentEnricher enricher = new ContentEnricher(); + enricher.setReplyChannel(new QueueChannel()); + try { - new ContentEnricher(null); + enricher.afterPropertiesSet(); } catch (IllegalArgumentException e) { - assertEquals("requestChannel must not be null", e.getMessage()); + assertEquals("If the replyChannel is set, then the requestChannel must not be null", e.getMessage()); return; } @@ -102,11 +110,15 @@ public class ContentEnricherTests { return new Source("John", "Doe"); } }); - ContentEnricher enricher = new ContentEnricher(requestChannel); + ContentEnricher enricher = new ContentEnricher(); + enricher.setRequestChannel(requestChannel); + SpelExpressionParser parser = new SpelExpressionParser(); Map propertyExpressions = new HashMap(); propertyExpressions.put("child.name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.afterPropertiesSet(); + Target target = new Target("test"); Message requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build(); enricher.handleMessage(requestMessage); @@ -126,12 +138,16 @@ public class ContentEnricherTests { return new Source("John", "Doe"); } }); - ContentEnricher enricher = new ContentEnricher(requestChannel); + ContentEnricher enricher = new ContentEnricher(); + enricher.setRequestChannel(requestChannel); + enricher.setShouldClonePayload(true); SpelExpressionParser parser = new SpelExpressionParser(); Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.afterPropertiesSet(); + Target target = new Target("replace me"); Message requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build(); enricher.handleMessage(requestMessage); @@ -151,12 +167,16 @@ public class ContentEnricherTests { return new Source("John", "Doe"); } }); - ContentEnricher enricher = new ContentEnricher(requestChannel); + ContentEnricher enricher = new ContentEnricher(); + enricher.setRequestChannel(requestChannel); + enricher.setShouldClonePayload(true); SpelExpressionParser parser = new SpelExpressionParser(); Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.afterPropertiesSet(); + TargetUser target = new TargetUser(); target.setName("replace me"); @@ -179,12 +199,16 @@ public class ContentEnricherTests { return new Source("John", "Doe"); } }); - ContentEnricher enricher = new ContentEnricher(requestChannel); + ContentEnricher enricher = new ContentEnricher(); + enricher.setRequestChannel(requestChannel); + enricher.setShouldClonePayload(true); SpelExpressionParser parser = new SpelExpressionParser(); Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.afterPropertiesSet(); + UncloneableTargetUser target = new UncloneableTargetUser(); target.setName("replace me"); @@ -222,7 +246,9 @@ public class ContentEnricherTests { return new Source("John", "Doe"); } }); - ContentEnricher enricher = new ContentEnricher(requestChannel); + + ContentEnricher enricher = new ContentEnricher(); + enricher.setRequestChannel(requestChannel); enricher.afterPropertiesSet();