INT-1517, combined Single and Multi channel XPath routers, modified XPathRouterParser to inherit form the base parser for all routers, combine single/multiple xpath router tests into one

This commit is contained in:
Oleg Zhurakousky
2010-10-14 06:45:22 -04:00
parent 3a8444c949
commit 1d0b773f2b
10 changed files with 101 additions and 332 deletions

View File

@@ -20,9 +20,11 @@ import java.util.List;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
@@ -38,6 +40,10 @@ public abstract class AbstractChannelNameResolvingRouterParser extends AbstractR
protected final BeanDefinition parseRouter(Element element, ParserContext parserContext) {
BeanDefinition beanDefinition = this.doParseRouter(element, parserContext);
if (beanDefinition != null) {
String channelResolver = element.getAttribute("channel-resolver");
if (StringUtils.hasText(channelResolver)){
beanDefinition.getPropertyValues().add("channelResolver", new RuntimeBeanReference(channelResolver));
}
// check if mapping is provided otherwise returned values will be treated as channel names
List<Element> childElements = DomUtils.getChildElementsByTagName(element, "mapping");
if (childElements != null && childElements.size() > 0) {
@@ -48,12 +54,9 @@ public abstract class AbstractChannelNameResolvingRouterParser extends AbstractR
if (beanClassName.endsWith("PayloadTypeRouter")){
key = childElement.getAttribute("type");
}
else if (beanClassName.endsWith("HeaderValueRouter")){
else {
key = childElement.getAttribute("value");
}
else {
throw new BeanCreationException("Building '" + beanClassName + "' is not supported by this parser");
}
channelMap.put(key, childElement.getAttribute("channel"));
}
beanDefinition.getPropertyValues().add("channelIdentifierMap", channelMap);

View File

@@ -16,84 +16,48 @@
package org.springframework.integration.xml.config;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.BeanDefinition;
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.AbstractConsumerEndpointParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.config.xml.AbstractChannelNameResolvingRouterParser;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Parser for the &lt;xpath-router/&gt; element.
*
* @author Jonas Partner
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class XPathRouterParser extends AbstractConsumerEndpointParser {
public class XPathRouterParser extends AbstractChannelNameResolvingRouterParser {
private XPathExpressionParser xpathParser = new XPathExpressionParser();
@Override
protected boolean shouldGenerateId() {
return false;
}
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
protected BeanDefinition doParseRouter(Element element,
ParserContext parserContext) {
BeanDefinitionBuilder xpathRouterBuilder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.integration.xml.router.XPathRouter");
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(
element.getNamespaceURI(), "xpath-expression");
Assert.isTrue(xPathExpressionNodes.getLength() < 2,
"Only one xpath-expression child can be specified.");
Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified.");
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
boolean xPathExpressionChildPresent = (xPathExpressionNodes.getLength() == 1);
boolean xPathReferencePresent = StringUtils.hasText(xPathExpressionRef);
Assert.isTrue(xPathExpressionChildPresent ^ xPathReferencePresent,
"Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required.");
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
String classname = "org.springframework.integration.xml.router." +
((multiChannel) ? "XPathMultiChannelRouter" : "XPathSingleChannelRouter");
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(classname);
List<Element> childElements = DomUtils.getChildElementsByTagName(element, "mapping");
if (childElements != null && childElements.size() > 0) {
ManagedMap<String, String> channelMap = new ManagedMap<String, String>();
for (Element childElement : childElements) {
String key = childElement.getAttribute("value");
channelMap.put(key, childElement.getAttribute("channel"));
}
builder.addPropertyValue("channelIdentifierMap", channelMap);
}
if (xPathExpressionChildPresent) {
BeanDefinition beanDefinition = this.xpathParser.parse(
(Element) xPathExpressionNodes.item(0), parserContext);
builder.addConstructorArgValue(beanDefinition);
xpathRouterBuilder.addConstructorArgValue(beanDefinition);
}
else {
builder.addConstructorArgReference(xPathExpressionRef);
xpathRouterBuilder.addConstructorArgReference(xPathExpressionRef);
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "resolution-required");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-channel-name-resolution-failures");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel-resolver");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "default-output-channel");
return builder;
return xpathRouterBuilder.getBeanDefinition();
}
}

View File

@@ -1,95 +0,0 @@
/*
* 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.router;
import java.util.List;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.util.Assert;
import org.springframework.xml.xpath.NodeMapper;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
/**
* A router that evaluates the XPath expression using
* {@link XPathExpression#evaluateAsNodeList(Node)} which returns zero or more
* nodes in conjunction with an instance of {@link NodeMapper} to produce zero
* or more channel names. An instance of {@link XmlPayloadConverter} is used to
* extract the payload as a {@link Node}.
*
* @author Jonas Partner
*/
public class XPathMultiChannelRouter extends AbstractXPathRouter {
private volatile NodeMapper nodeMapper = new TextContentNodeMapper();
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, Map)
*/
public XPathMultiChannelRouter(String expression, Map<String, String> namespaces) {
super(expression, namespaces);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, String, String)
*/
public XPathMultiChannelRouter(String expression, String prefix, String namespace) {
super(expression, prefix, namespace);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String)
*/
public XPathMultiChannelRouter(String expression) {
super(expression);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(XPathExpression)
*/
public XPathMultiChannelRouter(XPathExpression expression) {
super(expression);
}
public void setNodeMapper(NodeMapper nodeMapper) {
Assert.notNull(nodeMapper, "NodeMapper must not be null");
this.nodeMapper = nodeMapper;
}
@SuppressWarnings("unchecked")
public List<Object> getChannelIndicatorList(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
return getXPathExpression().evaluate(node, this.nodeMapper);
}
private static class TextContentNodeMapper implements NodeMapper {
public Object mapNode(Node node, int nodeNum) throws DOMException {
return node.getTextContent();
}
}
}

View File

@@ -17,21 +17,29 @@
package org.springframework.integration.xml.router;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.router.AbstractMessageRouter;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.NodeMapper;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
/**
* Abstract base class for Message Routers that use
* {@link XPathExpression} evaluation to determine channel names.
*
* @author Jonas Partner
* @author Oleg Zhurakousky
*/
public abstract class AbstractXPathRouter extends AbstractMessageRouter {
public class XPathRouter extends AbstractMessageRouter {
private volatile NodeMapper nodeMapper = new TextContentNodeMapper();
private final XPathExpression xPathExpression;
@@ -45,7 +53,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
* @param expression
* @param namespaces
*/
public AbstractXPathRouter(String expression, Map<String, String> namespaces) {
public XPathRouter(String expression, Map<String, String> namespaces) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
}
@@ -57,7 +65,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
* @param prefix
* @param namespace
*/
public AbstractXPathRouter(String expression, String prefix, String namespace) {
public XPathRouter(String expression, String prefix, String namespace) {
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put(prefix, namespace);
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
@@ -69,7 +77,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
*
* @param expression
*/
public AbstractXPathRouter(String expression) {
public XPathRouter(String expression) {
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression);
}
@@ -78,7 +86,7 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
*
* @param expression
*/
public AbstractXPathRouter(XPathExpression expression) {
public XPathRouter(XPathExpression expression) {
this.xPathExpression = expression;
}
@@ -103,4 +111,20 @@ public abstract class AbstractXPathRouter extends AbstractMessageRouter {
public String getComponentType(){
return "xml:xpath-router";
}
@Override
@SuppressWarnings("unchecked")
public List<Object> getChannelIndicatorList(Message<?> message) {
Node node = getConverter().convertToNode(message.getPayload());
return getXPathExpression().evaluate(node, this.nodeMapper);
}
private static class TextContentNodeMapper implements NodeMapper {
public Object mapNode(Node node, int nodeNum) throws DOMException {
return node.getTextContent();
}
}
}

View File

@@ -1,93 +0,0 @@
/*
* Copyright 2002-2008 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.router;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
import org.springframework.integration.xml.XmlPayloadConverter;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.Node;
/**
* Router that evaluates the payload using {@link XPathExpression#evaluateAsString(Node)}
* to extract a channel name. The payload is extracted as a node using the
* provided {@link XmlPayloadConverter} with {@link DefaultXmlPayloadConverter}
* being the default.
*
* <p>The provided {@link XPathExpression} must evaluate to a non-empty String.
*
* @author Jonas Partner
*/
public class XPathSingleChannelRouter extends AbstractXPathRouter {
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, Map)
*/
public XPathSingleChannelRouter(String expression, Map<String, String> namespaces) {
super(expression, namespaces);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String, String, String)
*/
public XPathSingleChannelRouter(String expression, String prefix, String namespace) {
super(expression, prefix, namespace);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(String)
*/
public XPathSingleChannelRouter(String expression) {
super(expression);
}
/**
* @see AbstractXPathRouter#AbstractXPathRouter(XPathExpression)
*/
public XPathSingleChannelRouter(XPathExpression expression) {
super(expression);
}
/**
* Evaluates the payload using {@link XPathExpression#evaluateAsString(Node)}
*
* @throws MessagingException if the {@link XPathExpression} evaluates to
* an empty string
*/
@Override
protected List<Object> getChannelIndicatorList(Message<?> message) {
List<Object> channels = new ArrayList<Object>();
Node node = getConverter().convertToNode(message.getPayload());
String result = getXPathExpression().evaluateAsString(node);
if (result == null || "".equals(result)) {
return null;
} else {
channels.add(result);
}
return channels;
}
}

View File

@@ -387,7 +387,6 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="multi-channel" type="xsd:string" default="false"/>
<xsd:attribute name="channel-resolver" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:appinfo>

View File

@@ -43,6 +43,7 @@ import org.w3c.dom.Document;
/**
* @author Jonas Partner
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
@ContextConfiguration
public class XPathRouterParserTests {

View File

@@ -17,7 +17,7 @@
<int-xml:mapping value="channelA" channel="channelB"/>
</int-xml:xpath-router>
<int-xml:xpath-router id="xpathRouterWithMappingMultiChannel" input-channel="multiChannelRouterChannel" multi-channel="true">
<int-xml:xpath-router id="xpathRouterWithMappingMultiChannel" input-channel="multiChannelRouterChannel">
<int-xml:xpath-expression expression="/root/name"/>
<int-xml:mapping value="channelA" channel="channelA"/>
<int-xml:mapping value="channelB" channel="channelA"/>

View File

@@ -18,6 +18,8 @@ package org.springframework.integration.xml.router;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
@@ -31,14 +33,14 @@ import org.springframework.xml.xpath.XPathExpressionFactory;
/**
* @author Jonas Partner
*/
public class XPathMultiChannelRouterTests {
public class XPathRouterTests {
@Test
@SuppressWarnings("unchecked")
public void simpleSingleAttribute() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(doc)).toArray();
assertEquals("Wrong number of channels returned", 1, channelNames.length);
assertEquals("Wrong channel name", "one", channelNames[0]);
@@ -49,7 +51,7 @@ public class XPathMultiChannelRouterTests {
public void multipleNodeValues() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage(doc)).toArray();
assertEquals("Wrong number of channels returned", 2, channelNames.length);
assertEquals("Wrong channel name", "bOne", channelNames[0]);
@@ -60,7 +62,7 @@ public class XPathMultiChannelRouterTests {
@SuppressWarnings("unchecked")
public void multipleNodeValuesAsString() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>")).toArray();
assertEquals("Wrong number of channels returned", 2, channelNames.length);
assertEquals("Wrong channel name", "bOne", channelNames[0]);
@@ -70,17 +72,59 @@ public class XPathMultiChannelRouterTests {
@Test(expected = MessagingException.class)
public void nonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelRouter router = new XPathMultiChannelRouter(expression);
XPathRouter router = new XPathRouter(expression);
router.getChannelIndicatorList(new GenericMessage<String>("test"));
}
@Test
public void nodePayload() throws Exception {
XPathMultiChannelRouter router = new XPathMultiChannelRouter("./three/text()");
XPathRouter router = new XPathRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three><three>dave</three></two></one>");
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage<Node>(testDocument.getElementsByTagName("two").item(0))).toArray();
assertEquals("bob",channelNames[0]);
assertEquals("dave",channelNames[1]);
}
@Test
public void testSimpleDocType() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathRouter router = new XPathRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<Document>(doc)).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test
public void testSimpleStringDoc() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathRouter router = new XPathRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<String>("<doc type='one' />")).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathRouter router = new XPathRouter(expression);
router.getChannelIndicatorList(new GenericMessage<String>("test"));
}
@Test
public void testNodePayload() throws Exception {
XPathRouter router = new XPathRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage<Node>(testDocument
.getElementsByTagName("two").item(0))).toArray();
assertEquals("bob", channelNames[0]);
}
@Test
public void testEvaluationReturnsEmptyString() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type");
XPathRouter router = new XPathRouter(expression);
List<Object> channelNames = router.getChannelIndicatorList(new GenericMessage<Document>(doc));
assertEquals(0, channelNames.size());
}
}

View File

@@ -1,78 +0,0 @@
/*
* 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.router;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.springframework.integration.MessagingException;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
/**
* @author Jonas Partner
*/
public class XPathSingleChannelRouterTests {
@Test
public void testSimpleDocType() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<Document>(doc)).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test
public void testSimpleStringDoc() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
Object channelName = router.getChannelIndicatorList(new GenericMessage<String>("<doc type='one' />")).toArray()[0];
assertEquals("Wrong channel name", "one", channelName);
}
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
router.getChannelIndicatorList(new GenericMessage<String>("test"));
}
@Test
public void testNodePayload() throws Exception {
XPathSingleChannelRouter router = new XPathSingleChannelRouter("./three/text()");
Document testDocument = XmlTestUtil.getDocumentForString("<one><two><three>bob</three></two></one>");
Object[] channelNames = router.getChannelIndicatorList(new GenericMessage<Node>(testDocument
.getElementsByTagName("two").item(0))).toArray();
assertEquals("bob", channelNames[0]);
}
@Test
public void testEvaluationReturnsEmptyString() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type='one' />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/somethingelse/@type");
XPathSingleChannelRouter router = new XPathSingleChannelRouter(expression);
Object channelNames = router.getChannelIndicatorList(new GenericMessage<Document>(doc));
assertEquals("Wrong channel name", null, channelNames);
}
}