Namespace support for XPathRouter INT-300

This commit is contained in:
Jonas Partner
2008-07-17 09:06:56 +00:00
parent 8dfe625163
commit b9ea75ea98
13 changed files with 354 additions and 56 deletions

View File

@@ -29,6 +29,7 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("marshalling-transformer", new XmlMarshallingTransformerParser());
registerBeanDefinitionParser("unmarshalling-transformer", new XmlUnmarshallingTransformerParser());
registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser());
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2002-2007 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.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.router.MultiChannelRouter;
import org.springframework.integration.router.SingleChannelRouter;
import org.springframework.integration.xml.router.XPathMultiChannelNameResolver;
import org.springframework.integration.xml.router.XPathSingleChannelNameResolver;
import org.springframework.util.StringUtils;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Element;
/**
*
* @author Jonas Partner
*
*/
public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
@Override
protected boolean shouldGenerateId() {
return false;
}
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
boolean resolutionRequired = Boolean.parseBoolean(element.getAttribute("resolution-required"));
String xPathExpression = element.getAttribute("xpath-expression");
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
if ((StringUtils.hasText(xPathExpression) && StringUtils.hasText(xPathExpressionRef))
|| (!StringUtils.hasText(xPathExpression) && !StringUtils.hasText(xPathExpressionRef))) {
throw new ConfigurationException("Exactl one of xpath-expression or xpath-expression-ref is required");
}
BeanDefinitionBuilder resolverDefinitionBuilder = null;
if (multiChannel) {
builder.getBeanDefinition().setBeanClass(MultiChannelRouter.class);
resolverDefinitionBuilder = BeanDefinitionBuilder
.genericBeanDefinition(XPathMultiChannelNameResolver.class);
}
else {
builder.getBeanDefinition().setBeanClass(SingleChannelRouter.class);
resolverDefinitionBuilder = BeanDefinitionBuilder
.genericBeanDefinition(XPathSingleChannelNameResolver.class);
}
if (StringUtils.hasText(xPathExpression)) {
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xPathExpression);
resolverDefinitionBuilder.addConstructorArgValue(expression);
}
else {
resolverDefinitionBuilder.addConstructorArgReference(xPathExpressionRef);
}
builder.getBeanDefinition().getPropertyValues().addPropertyValue("resolutionRequired", resolutionRequired);
builder.getBeanDefinition().getPropertyValues().addPropertyValue("channelNameResolver",
resolverDefinitionBuilder.getBeanDefinition());
}
}

View File

@@ -38,7 +38,7 @@
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="unmarshalling-transformer">
<xsd:complexType>
<xsd:annotation>
@@ -51,22 +51,46 @@
use="required" />
</xsd:complexType>
</xsd:element>
<xsd:element name="xslt-transformer">
<xsd:element name="xslt-transformer">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a XML unmarshalling transformer.
Defines a XML XSLT transformer.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="xsl-resource" type="xsd:string" use="optional" />
<xsd:attribute name="xsl-templates" type="xsd:string" use="optional" />
<xsd:attribute name="source-factory" type="xsd:string" use="optional" />
<xsd:attribute name="result-factory" type="xsd:string" use="optional" />
<xsd:attribute name="xsl-resource" type="xsd:string"
use="optional" />
<xsd:attribute name="xsl-templates" type="xsd:string"
use="optional" />
<xsd:attribute name="source-factory" type="xsd:string"
use="optional" />
<xsd:attribute name="result-factory" type="xsd:string"
use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="xpath-router">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a XPath router.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="xpath-expression-ref" type="xsd:string"
use="optional" />
<xsd:attribute name="xpath-expression" type="xsd:string"
use="optional" />
<xsd:attribute name="multi-channel" type="xsd:boolean"
default="false" />
<xsd:attribute name="resolution-required" type="xsd:boolean"
default="true" />
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -20,6 +20,11 @@ import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.w3c.dom.Node;
/**
*
* @author Jonas Partner
*
*/
public class AbstractXPathChannelNameResolver {
protected Node extractNode(Message<?> message) {

View File

@@ -26,36 +26,40 @@ import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
/**
*
* @author Jonas Partner
*
*/
public class XPathMultiChannelNameResolver extends AbstractXPathChannelNameResolver implements MultiChannelNameResolver {
private final XPathExpression xPathExpression;
private NodeMapper nodeMapper = new TextContentNodeMapper();
public XPathMultiChannelNameResolver(XPathExpression xPathExpression){
public XPathMultiChannelNameResolver(XPathExpression xPathExpression) {
Assert.notNull("XPAthExpression must be provided");
this.xPathExpression = xPathExpression;
}
public void setNodeMapper(NodeMapper nodeMapper){
Assert.notNull(nodeMapper,"NodeMapper can not be null");
public void setNodeMapper(NodeMapper nodeMapper) {
Assert.notNull(nodeMapper, "NodeMapper can not be null");
this.nodeMapper = nodeMapper;
}
@SuppressWarnings("unchecked")
public String[] resolve(Message<?> message) {
Node node =extractNode(message);
Node node = extractNode(message);
List channelNamesList = xPathExpression.evaluate(node, nodeMapper);
return (String[])channelNamesList.toArray(new String[channelNamesList.size()]);
return (String[]) channelNamesList.toArray(new String[channelNamesList.size()]);
}
private static class TextContentNodeMapper implements NodeMapper{
private static class TextContentNodeMapper implements NodeMapper {
public Object mapNode(Node node, int nodeNum) throws DOMException {
return node.getTextContent();
}
}
}

View File

@@ -17,27 +17,29 @@
package org.springframework.integration.xml.router;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.router.ChannelNameResolver;
import org.springframework.util.Assert;
import org.springframework.xml.xpath.XPathExpression;
import org.w3c.dom.Node;
/**
*
* @author Jonas Partner
*
*/
public class XPathSingleChannelNameResolver extends AbstractXPathChannelNameResolver implements ChannelNameResolver {
private final XPathExpression xPathExpression;
public XPathSingleChannelNameResolver(XPathExpression xPathExpression){
Assert.notNull("XPAthExpression must be provided");
public XPathSingleChannelNameResolver(XPathExpression xPathExpression) {
Assert.notNull("XPathExpression must be provided");
this.xPathExpression = xPathExpression;
}
public String resolve(Message<?> message) {
Node node = extractNode(message);
System.out.println(xPathExpression.evaluateAsString(node));
return xPathExpression.evaluateAsString(node);
}
}

View File

@@ -28,38 +28,41 @@ import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Document;
/**
*
* @author Jonas Partner
*
*/
public class XPathMultiChannelNameResolverTests {
@SuppressWarnings("unchecked")
@Test
public void testSimpleSingleeAttribute() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression);
String[] channelNames = resolver.resolve(new GenericMessage(doc));
assertEquals("Wrong number of channels returend",1,channelNames.length);
assertEquals("Wrong channel name","one",channelNames[0]);
assertEquals("Wrong number of channels returend", 1, channelNames.length);
assertEquals("Wrong channel name", "one", channelNames[0]);
}
@SuppressWarnings("unchecked")
@Test
public void testMultipleNodeValues() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\"><book>bOne</book><book>bTwo</book></doc>");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/book");
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression);
String[] channelNames = resolver.resolve(new GenericMessage(doc));
assertEquals("Wrong number of channels returend",2,channelNames.length);
assertEquals("Wrong channel name","bOne",channelNames[0]);
assertEquals("Wrong channel name","bTwo",channelNames[1]);
assertEquals("Wrong number of channels returend", 2, channelNames.length);
assertEquals("Wrong channel name", "bOne", channelNames[0]);
assertEquals("Wrong channel name", "bTwo", channelNames[1]);
}
@Test(expected=MessagingException.class)
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathMultiChannelNameResolver resolver = new XPathMultiChannelNameResolver(expression);
resolver.resolve(new StringMessage("test"));
}
}

View File

@@ -28,26 +28,28 @@ import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Document;
/**
*
* @author Jonas Partner
*
*/
public class XPathSingleChannelNameResolverTests {
@SuppressWarnings("unchecked")
@Test
public void testSimpleDocType() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<doc type=\"one\" />");
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression);
String channelName = resolver.resolve(new GenericMessage(doc));
assertEquals("Wrong channel name","one",channelName);
assertEquals("Wrong channel name", "one", channelName);
}
@Test(expected=MessagingException.class)
@Test(expected = MessagingException.class)
public void testNonNodePayload() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/doc/@type");
XPathSingleChannelNameResolver resolver = new XPathSingleChannelNameResolver(expression);
resolver.resolve(new StringMessage("test"));
}
}

View File

@@ -26,6 +26,11 @@ import org.springframework.oxm.Marshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.xml.transform.StringSource;
/**
*
* @author Jonas Partner
*
*/
public class StubMarshaller implements Marshaller {
public void marshal(Object graph, Result result) throws XmlMappingException, IOException {

View File

@@ -24,6 +24,11 @@ import javax.xml.transform.Source;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
/**
*
* @author Jonas Partner
*
*/
public class StubUnmarshaller implements Unmarshaller {
public LinkedList<Source> sourcesPassed = new LinkedList<Source>();

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:si-xml="http://www.springframework.org/schema/integration-xml"
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-xml http://www.springframework.org/schema/integration/spring-integration-xml-1.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<bean id="messageChannel"
class="org.springframework.integration.channel.QueueChannel" />
<si:message-bus error-channel="errors" />
<si:channel id="inputOne" />
<si:channel id="inputTwo" />
<si:channel id="outputOne" />
<si:channel id="outputTwo" />
<si:channel id="errors" />
<si-xml:xpath-router id="routerOne"
xpath-expression-ref="xpathExpression" />
<si:service-activator ref="routerOne" input-channel="inputOne" />
<si-xml:xpath-router id="routerTwo"
xpath-expression-ref="xpathExpressionMulti" multi-channel="true" />
<si:service-activator ref="routerTwo" input-channel="inputTwo" />
<bean id="xpathExpression"
class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
<property name="expression" value="/name" />
</bean>
<bean id="xpathExpressionMulti"
class="org.springframework.xml.xpath.XPathExpressionFactoryBean">
<property name="expression" value="//name" />
</bean>
</beans>

View File

@@ -0,0 +1,116 @@
/*
* Copyright 2002-2007 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.assertNotNull;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.w3c.dom.Document;
@ContextConfiguration
public class XPathRouterParserTests extends AbstractJUnit4SpringContextTests{
@Autowired @Qualifier("inputOne")
MessageChannel inputOne;
@Autowired @Qualifier("inputTwo")
MessageChannel inputTwo;
@Autowired @Qualifier("outputOne")
MessageChannel outputOne;
@Autowired @Qualifier("outputTwo")
MessageChannel outputTwo;
@Autowired @Qualifier("errors")
MessageChannel errors;
@SuppressWarnings("unchecked")
@Test
public void testOutputOne() throws Exception{
Document doc =XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputOne.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>)outputOne.receive(1000);
assertNotNull("Did not recevie message from outputOne", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputTwo() throws Exception{
Document doc =XmlTestUtil.getDocumentForString("<name>outputTwo</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputOne.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>)outputTwo.receive(1000);
assertNotNull("Did not recevie message from two", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputThree() throws Exception{
Document doc =XmlTestUtil.getDocumentForString("<name>outputThree</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputOne.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>)errors.receive(1000);
assertNotNull("Did not recevie message on errors", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputOneMulti() throws Exception{
Document doc =XmlTestUtil.getDocumentForString("<name>outputOne</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputTwo.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>)outputOne.receive(1000);
assertNotNull("Did not recevie message from outputOne", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputOneAndTwoMulti() throws Exception{
Document doc =XmlTestUtil.getDocumentForString("<doc><name>outputOne</name><name>outputTwo</name></doc>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputTwo.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>)outputTwo.receive(1000);
assertNotNull("Did not recevie message from two", received);
}
@SuppressWarnings("unchecked")
@Test
public void testOutputThreeMulti() throws Exception{
Document doc =XmlTestUtil.getDocumentForString("<name>outputThree</name>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
inputTwo.send(docMessage);
GenericMessage<Document> received = (GenericMessage<Document>) errors.receive(1000);
assertNotNull("Did not recevie message on errors", received);
}
}

View File

@@ -5,12 +5,14 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration-xml http://www.springframework.org/schema/integration/spring-integration-xml-1.0.xsd">
<si-xml:xslt-transformer
id="xsltTransfomerWithResource" xsl-resource="org/springframework/integration/xml/config/test.xsl" />
<si-xml:xslt-transformer id="xsltTransformerWithTemplates" xsl-templates="templates" />
<bean id="templates" class="org.springframework.integration.xml.config.TestTemplatesFactory" />
<si-xml:xslt-transformer id="xsltTransfomerWithResource"
xsl-resource="org/springframework/integration/xml/config/test.xsl" />
<si-xml:xslt-transformer id="xsltTransformerWithTemplates"
xsl-templates="templates" />
<bean id="templates"
class="org.springframework.integration.xml.config.TestTemplatesFactory" />
</beans>