diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java index 977aa159cd..072505af96 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java @@ -29,6 +29,7 @@ public class IntegrationXmlNamespaceHandler extends AbstractIntegrationNamespace registerBeanDefinitionParser("unmarshalling-transformer", new UnmarshallingTransformerParser()); registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser()); registerBeanDefinitionParser("xpath-transformer", new XPathTransformerParser()); + registerBeanDefinitionParser("xpath-header-enricher", new XPathHeaderEnricherParser()); registerBeanDefinitionParser("xpath-router", new XPathRouterParser()); registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser()); registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser()); diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathHeaderEnricherParser.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathHeaderEnricherParser.java new file mode 100644 index 0000000000..ec6036643a --- /dev/null +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XPathHeaderEnricherParser.java @@ -0,0 +1,86 @@ +/* + * 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.xml.config; + +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.ManagedMap; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.config.xml.AbstractTransformerParser; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.springframework.util.StringUtils; + +/** + * Parser for <xpath-header-enricher> elements. + * + * @author Mark Fisher + * @since 2.0 + */ +public class XPathHeaderEnricherParser extends AbstractTransformerParser { + + @Override + protected final String getTransformerClassName() { + return "org.springframework.integration.xml.enricher.XPathHeaderEnricher"; + } + + @Override + protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + ManagedMap headers = new ManagedMap(); + this.processHeaders(element, headers, parserContext); + builder.addConstructorArgValue(headers); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-overwrite"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "should-skip-nulls"); + } + + protected void processHeaders(Element element, ManagedMap headers, ParserContext parserContext) { + Object source = parserContext.extractSource(element); + NodeList childNodes = element.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + Node node = childNodes.item(i); + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element headerElement = (Element) node; + String elementName = node.getLocalName(); + if ("header".equals(elementName)) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( + "org.springframework.integration.xml.enricher.XPathHeaderEnricher$XPathExpressionEvaluatingHeaderValueMessageProcessor"); + String expressionString = headerElement.getAttribute("xpath-expression"); + String expressionRef = headerElement.getAttribute("xpath-expression-ref"); + boolean isExpressionString = StringUtils.hasText(expressionString); + boolean isExpressionRef = StringUtils.hasText(expressionRef); + if (!(isExpressionString ^ isExpressionRef)) { + parserContext.getReaderContext().error( + "Exactly one of the 'xpath-expression' or 'xpath-expression-ref' attributes is required.", source); + } + if (isExpressionString) { + builder.addConstructorArgValue(expressionString); + } + else { + builder.addConstructorArgReference(expressionRef); + } + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, headerElement, "evaluation-type"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, headerElement, "overwrite"); + String headerName = headerElement.getAttribute("name"); + headers.put(headerName, builder.getBeanDefinition()); + } + } + } + } + +} diff --git a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd index 2a8dfd8d35..4cb12775ee 100644 --- a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd +++ b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd @@ -234,6 +234,105 @@ + + + + Defines a Header Enricher Message Transformer that evaluates XPath expressions against the + message payload and inserts the result of the evaluation into a messsage header. + + + + + + + + + + + + Specify the default boolean value for whether to overwrite existing header values. This will + only take effect for sub-elements that do not provide their own 'overwrite' attribute. If the + 'default-overwrite' attribute is not provided, then the specified header values will NOT + overwrite any existing ones with the same header names. + + + + + + + + + + Specify whether null values, such as might be returned from an expression evaluation, should be + skipped. The default value is true. Set this to false if a null value should trigger removal of + the corresponding header instead. + + + + + + + + + + + + + + + Defines an XPath expression to be configured within an <xpath-header-enricher/> element. + + + + + + The name of the header to be enriched. + + + + + + + The XPath Expression as a String. Either this or 'xpath-expression-ref' must be provided, but not both. + + + + + + + The XPath Expression reference. Either this or 'xpath-expression' must be provided, but not both. + + + + + + + The result type expected from the XPath evaluation. This will be the type of the header value. + + + + + + + + + + + + + + + + Boolean value to indicate whether this header value should overwrite an existing header value + for the same name if already present on the input Message. + + + + + + + + diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathHeaderEnricherParserTests-context.xml b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathHeaderEnricherParserTests-context.xml new file mode 100644 index 0000000000..75efb2ea2b --- /dev/null +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathHeaderEnricherParserTests-context.xml @@ -0,0 +1,36 @@ + + + + + + + + +
+
+
+
+
+
+ + + + + +
+ + + +
+ + + diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathHeaderEnricherParserTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathHeaderEnricherParserTests.java new file mode 100644 index 0000000000..8a55dac03d --- /dev/null +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/config/XPathHeaderEnricherParserTests.java @@ -0,0 +1,159 @@ +/* + * 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.xml.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.w3c.dom.Node; + +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.integration.channel.PollableChannel; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.core.Message; +import org.springframework.integration.core.MessageChannel; +import org.springframework.integration.message.MessageBuilder; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + * @since 2.0 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class XPathHeaderEnricherParserTests { + + @Autowired + private MessageChannel input; + + @Autowired + private PollableChannel output; + + @Autowired + private ApplicationContext context; + + private final Message message = MessageBuilder.withPayload("").build(); + + + @Test + public void stringResultByDefault() { + Message result = this.getResultMessage(); + assertEquals("John Doe", result.getHeaders().get("name")); + } + + @Test + public void numberResult() { + Message result = this.getResultMessage(); + assertEquals(new Double(42), result.getHeaders().get("age")); + } + + @Test + public void booleanResult() { + Message result = this.getResultMessage(); + assertEquals(Boolean.TRUE, result.getHeaders().get("married")); + } + + @Test + public void nodeResult() { + Message result = this.getResultMessage(); + Object header = result.getHeaders().get("node-test"); + assertTrue(header instanceof Node); + Node node = (Node) header; + assertEquals("42", node.getTextContent()); + } + + @Test + @SuppressWarnings("unchecked") + public void nodeListResult() { + Message result = this.getResultMessage(); + Object header = result.getHeaders().get("node-list-test"); + assertTrue(List.class.isAssignableFrom(header.getClass())); + List nodeList = (List) header; + assertEquals(3, nodeList.size()); + } + + @Test + public void expressionRef() { + Message result = this.getResultMessage(); + assertEquals(new Double(84), result.getHeaders().get("ref-test")); + } + + @Test + public void defaultOverwrite() { + assertEquals(false, this.getEnricherProperty("defaultHeaderEnricher", "defaultOverwrite")); + } + + @Test + public void defaultShouldSkipNulls() { + assertEquals(true, this.getEnricherProperty("defaultHeaderEnricher", "shouldSkipNulls")); + } + + @Test + public void customOverwrite() { + assertEquals(true, this.getEnricherProperty("customHeaderEnricher", "defaultOverwrite")); + } + + @Test + public void customShouldSkipNulls() { + assertEquals(false, this.getEnricherProperty("customHeaderEnricher", "shouldSkipNulls")); + } + + @Test + public void childOverridesDefaultOverwrite() { + QueueChannel replyChannel = new QueueChannel(); + Message request = MessageBuilder.fromMessage(this.message) + .setHeader("foo", "bar") + .setReplyChannel(replyChannel) + .build(); + this.context.getBean("defaultInput", MessageChannel.class).send(request); + Message reply = replyChannel.receive(); + assertEquals("John Doe", reply.getHeaders().get("foo")); + } + + @Test + public void childOverridesCustomOverwrite() { + QueueChannel replyChannel = new QueueChannel(); + Message request = MessageBuilder.fromMessage(this.message) + .setHeader("foo", "bar") + .setReplyChannel(replyChannel) + .build(); + this.context.getBean("customInput", MessageChannel.class).send(request); + Message reply = replyChannel.receive(); + assertEquals("bar", reply.getHeaders().get("foo")); + } + + + private Message getResultMessage() { + this.input.send(message); + return output.receive(0); + } + + private boolean getEnricherProperty(String beanName, String propertyName) { + Object endpoint = this.context.getBean(beanName); + Object handler = new DirectFieldAccessor(endpoint).getPropertyValue("handler"); + Object enricher = new DirectFieldAccessor(handler).getPropertyValue("transformer"); + return ((Boolean) new DirectFieldAccessor(enricher).getPropertyValue(propertyName)).booleanValue(); + } + +}