INT-1166 added namespace support for the <xpath-header-enricher> element
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -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<String, Object> headers = new ManagedMap<String, Object>();
|
||||
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<String, Object> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -234,6 +234,105 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="xpath-header-enricher">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
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.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="inputOutputEndpoint">
|
||||
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
|
||||
<xsd:element type="xpathHeaderType" name="header"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="default-overwrite">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
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.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:simpleType>
|
||||
<xsd:union memberTypes="xsd:boolean xsd:string" />
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="should-skip-nulls">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
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.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:simpleType>
|
||||
<xsd:union memberTypes="xsd:boolean xsd:string" />
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:complexType name="xpathHeaderType">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an XPath expression to be configured within an <xpath-header-enricher/> element.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
The name of the header to be enriched.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="xpath-expression">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
The XPath Expression as a String. Either this or 'xpath-expression-ref' must be provided, but not both.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="xpath-expression-ref">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
The XPath Expression reference. Either this or 'xpath-expression' must be provided, but not both.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="evaluation-type" default="STRING_RESULT">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
The result type expected from the XPath evaluation. This will be the type of the header value.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
<xsd:enumeration value="BOOLEAN_RESULT"/>
|
||||
<xsd:enumeration value="STRING_RESULT"/>
|
||||
<xsd:enumeration value="NUMBER_RESULT"/>
|
||||
<xsd:enumeration value="NODE_RESULT"/>
|
||||
<xsd:enumeration value="NODE_LIST_RESULT"/>
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="overwrite">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
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.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:simpleType>
|
||||
<xsd:union memberTypes="xsd:boolean xsd:string" />
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:element name="xpath-router">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration/xml"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/xml
|
||||
http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd">
|
||||
|
||||
<si:channel id="output">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<xpath-header-enricher input-channel="input" output-channel="output">
|
||||
<header name="name" xpath-expression="/person/@name" />
|
||||
<header name="age" xpath-expression="/person/@age" evaluation-type="NUMBER_RESULT" />
|
||||
<header name="married" xpath-expression="/person/@married = 'true'" evaluation-type="BOOLEAN_RESULT" />
|
||||
<header name="node-test" xpath-expression="/person/@age" evaluation-type="NODE_RESULT" />
|
||||
<header name="node-list-test" xpath-expression="/person/@*" evaluation-type="NODE_LIST_RESULT" />
|
||||
<header name="ref-test" xpath-expression-ref="testExpression" evaluation-type="NUMBER_RESULT" />
|
||||
</xpath-header-enricher>
|
||||
|
||||
<xpath-expression id="testExpression" expression="/person/@age * 2"/>
|
||||
|
||||
<xpath-header-enricher id="defaultHeaderEnricher" input-channel="defaultInput">
|
||||
<header name="foo" xpath-expression="/person/@name" overwrite="true" />
|
||||
</xpath-header-enricher>
|
||||
|
||||
<xpath-header-enricher id="customHeaderEnricher" input-channel="customInput" default-overwrite="true" should-skip-nulls="false">
|
||||
<header name="foo" xpath-expression="/person/@name" overwrite="false" />
|
||||
</xpath-header-enricher>
|
||||
|
||||
</beans:beans>
|
||||
@@ -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("<person name='John Doe' age='42' married='true'/>").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<Node> nodeList = (List<Node>) 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user