From 7b70e3dcfed2462d5fc4741876977c9fe9b31c1b Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 13 Oct 2010 16:21:03 -0400 Subject: [PATCH] INT-1382 added support for expression sub-elements for header-enricher --- .../xml/HeaderEnricherParserSupport.java | 21 ++++++-- .../transformer/HeaderEnricher.java | 8 +++ ...HeaderEnricherIntegrationTests-context.xml | 24 +++++++++ ...ressionHeaderEnricherIntegrationTests.java | 53 +++++++++++++++++++ .../transformer/expressions.properties | 3 +- 5 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests-context.xml create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/HeaderEnricherParserSupport.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/HeaderEnricherParserSupport.java index db5c95bce3..9d3b72cf86 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/HeaderEnricherParserSupport.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/HeaderEnricherParserSupport.java @@ -29,6 +29,7 @@ import org.springframework.beans.factory.support.ManagedMap; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; +import org.springframework.util.xml.DomUtils; /** * Base support class for 'header-enricher' parsers. @@ -110,12 +111,17 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar if (headerName != null) { String value = headerElement.getAttribute("value"); String ref = headerElement.getAttribute("ref"); - String expression = headerElement.getAttribute("expression"); String method = headerElement.getAttribute("method"); + String expression = headerElement.getAttribute("expression"); + Element expressionElement = DomUtils.getChildElementByTagName(headerElement, "expression"); + if (StringUtils.hasText(expression) && expressionElement != null) { + parserContext.getReaderContext().error("The 'expression' attribute and sub-element are mutually exclusive", element); + return; + } boolean isValue = StringUtils.hasText(value); boolean isRef = StringUtils.hasText(ref); - boolean isExpression = StringUtils.hasText(expression); boolean hasMethod = StringUtils.hasText(method); + boolean isExpression = StringUtils.hasText(expression) || expressionElement != null; if (!(isValue ^ (isRef ^ isExpression))) { parserContext.getReaderContext().error( "Exactly one of the 'ref', 'value', or 'expression' attributes is required.", element); @@ -139,7 +145,16 @@ public abstract class HeaderEnricherParserSupport extends AbstractTransformerPar } valueProcessorBuilder = BeanDefinitionBuilder.genericBeanDefinition( IntegrationNamespaceUtils.BASE_PACKAGE + ".transformer.HeaderEnricher$ExpressionEvaluatingHeaderValueMessageProcessor"); - valueProcessorBuilder.addConstructorArgValue(expression); + if (expressionElement != null) { + BeanDefinitionBuilder dynamicExpressionBuilder = BeanDefinitionBuilder.genericBeanDefinition( + "org.springframework.integration.expression.DynamicExpression"); + dynamicExpressionBuilder.addConstructorArgValue(expressionElement.getAttribute("key")); + dynamicExpressionBuilder.addConstructorArgReference(expressionElement.getAttribute("source")); + valueProcessorBuilder.addConstructorArgValue(dynamicExpressionBuilder.getBeanDefinition()); + } + else { + valueProcessorBuilder.addConstructorArgValue(expression); + } valueProcessorBuilder.addConstructorArgValue(headerType); } else { diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java index 39d65cdae5..0e8aaff1c9 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/HeaderEnricher.java @@ -175,6 +175,14 @@ public class HeaderEnricher implements Transformer { private final ExpressionEvaluatingMessageProcessor targetProcessor; + /** + * Create a header value processor for the given Expression and the expected type + * of the expression evaluation result. The expectedType may be null if unknown. + */ + public ExpressionEvaluatingHeaderValueMessageProcessor(Expression expression, Class expectedType) { + this.targetProcessor = new ExpressionEvaluatingMessageProcessor(expression, expectedType); + } + /** * Create a header value processor for the given expression string and the expected type * of the expression evaluation result. The expectedType may be null if unknown. diff --git a/spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests-context.xml new file mode 100644 index 0000000000..69a903fd3f --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests-context.xml @@ -0,0 +1,24 @@ + + + + + + + + +
+ +
+
+ + + + + +
diff --git a/spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests.java new file mode 100644 index 0000000000..cad648d41d --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/transformer/DynamicExpressionHeaderEnricherIntegrationTests.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.transformer; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.Message; +import org.springframework.integration.MessageChannel; +import org.springframework.integration.core.PollableChannel; +import org.springframework.integration.support.MessageBuilder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class DynamicExpressionHeaderEnricherIntegrationTests { + + @Autowired + private MessageChannel input; + + @Autowired + private PollableChannel output; + + + @Test + public void dynamicExpressionHeader() { + Message message = MessageBuilder.withPayload("test").build(); + this.input.send(message); + Message result = output.receive(0); + assertEquals("foo", result.getHeaders().get("testHeader")); + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/transformer/expressions.properties b/spring-integration-core/src/test/java/org/springframework/integration/transformer/expressions.properties index 77ccc19867..90fa9aeacb 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/transformer/expressions.properties +++ b/spring-integration-core/src/test/java/org/springframework/integration/transformer/expressions.properties @@ -1 +1,2 @@ -test.transform=payload.foo + headers.bar \ No newline at end of file +test.transform=payload.foo + headers.bar +test.header='foo'