Refactored router hierarchy by adding an AbstractMessageRouter base class and removing the ChannelResolver delegation. The routers that return channel names now accept a ChannelMapping strategy. The BeanNameChannelMapping is typically used as a default. The mapping routers also support "prefix" and "suffix" properties.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* 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.
|
||||
@@ -19,9 +19,7 @@ package org.springframework.integration.xml.config;
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
@@ -32,7 +30,6 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
|
||||
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
|
||||
registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser());
|
||||
registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
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.xml.router.XPathMultiChannelNameResolver;
|
||||
import org.springframework.integration.xml.router.XPathSingleChannelNameResolver;
|
||||
import org.springframework.integration.xml.router.XPathMultiChannelRouter;
|
||||
import org.springframework.integration.xml.router.XPathSingleChannelRouter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -47,32 +47,30 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
|
||||
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
|
||||
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
|
||||
|
||||
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(element.getNamespaceURI(), "xpath-expression");
|
||||
Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified");
|
||||
boolean xPathExpressionChildPresent = xPathExpressionNodes.getLength() == 1;
|
||||
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(
|
||||
element.getNamespaceURI(), "xpath-expression");
|
||||
Assert.isTrue(xPathExpressionNodes.getLength() < 2,
|
||||
"Only one xpath-expression child can be specified.");
|
||||
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.");
|
||||
if (multiChannel) {
|
||||
builder.getBeanDefinition().setBeanClass(XPathMultiChannelNameResolver.class);
|
||||
builder.getBeanDefinition().setBeanClass(XPathMultiChannelRouter.class);
|
||||
}
|
||||
else {
|
||||
builder.getBeanDefinition().setBeanClass(XPathSingleChannelNameResolver.class);
|
||||
builder.getBeanDefinition().setBeanClass(XPathSingleChannelRouter.class);
|
||||
}
|
||||
|
||||
if (xPathExpressionChildPresent) {
|
||||
BeanDefinition beanDefinition = xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext);
|
||||
BeanDefinition beanDefinition = this.xpathParser.parse(
|
||||
(Element) xPathExpressionNodes.item(0), parserContext);
|
||||
builder.addConstructorArgValue(beanDefinition);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
builder.addConstructorArgReference(xPathExpressionRef);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
xmlns:tool="http://www.springframework.org/schema/tool"
|
||||
targetNamespace="http://www.springframework.org/schema/integration/xml"
|
||||
elementFormDefault="qualified" attributeFormDefault="unqualified">
|
||||
|
||||
<xsd:import namespace="http://www.springframework.org/schema/beans" />
|
||||
<xsd:import namespace="http://www.springframework.org/schema/tool" />
|
||||
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines the configuration elements for Spring Integration's XML support.
|
||||
@@ -20,8 +22,7 @@
|
||||
</xsd:annotation>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="transformerType">
|
||||
<xsd:attribute name="marshaller" type="xsd:string"
|
||||
use="required" />
|
||||
<xsd:attribute name="marshaller" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="result-type" use="optional">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
@@ -31,8 +32,7 @@
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="result-factory" use="optional" />
|
||||
<xsd:attribute name="result-transformer" type="xsd:string"
|
||||
use="optional" />
|
||||
<xsd:attribute name="result-transformer" type="xsd:string" use="optional" />
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
@@ -46,8 +46,7 @@
|
||||
</xsd:annotation>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="transformerType">
|
||||
<xsd:attribute name="unmarshaller" type="xsd:string"
|
||||
use="required" />
|
||||
<xsd:attribute name="unmarshaller" type="xsd:string" use="required" />
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
@@ -61,14 +60,10 @@
|
||||
</xsd:annotation>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="transformerType">
|
||||
<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:attribute name="result-type" use="optional">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
@@ -77,13 +72,12 @@
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="result-transformer" type="xsd:string"
|
||||
use="optional" />
|
||||
<xsd:attribute name="result-transformer" type="xsd:string" use="optional" />
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
<xsd:element name="xpath-router">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
@@ -95,26 +89,23 @@
|
||||
<xsd:element ref="xpath-expression" maxOccurs="1" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="id" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="xpath-expression-ref" type="xsd:string"
|
||||
use="optional" />
|
||||
<xsd:attribute name="multi-channel" type="xsd:boolean"
|
||||
default="false" />
|
||||
<xsd:attribute name="xpath-expression-ref" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="multi-channel" type="xsd:boolean" default="false" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
<xsd:element name="xpath-selector">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an XPath selector.
|
||||
Defines an XPath selector.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="xpath-expression" maxOccurs="1" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<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-ref" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="evaluation-result-type" use="required">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
@@ -123,12 +114,10 @@
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="string-test-value" type="xsd:string"
|
||||
use="optional" />
|
||||
|
||||
<xsd:attribute name="string-test-value" type="xsd:string" use="optional" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
<xsd:element name="xpath-expression">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
@@ -140,23 +129,17 @@
|
||||
<xsd:element ref="beans:map" minOccurs="0" maxOccurs="1"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="id" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="expression" type="xsd:string"
|
||||
use="optional" />
|
||||
<xsd:attribute name="ns-prefix" type="xsd:string"
|
||||
use="optional" />
|
||||
<xsd:attribute name="ns-uri" type="xsd:string"
|
||||
use="optional" />
|
||||
<xsd:attribute name="namespace-map" type="xsd:string"
|
||||
use="optional" />
|
||||
<xsd:attribute name="expression" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="ns-prefix" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="ns-uri" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="namespace-map" type="xsd:string" use="optional" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
|
||||
<xsd:complexType name="transformerType">
|
||||
<xsd:attribute name="id" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="input-channel" type="xsd:string"
|
||||
use="required" />
|
||||
<xsd:attribute name="output-channel" type="xsd:string"
|
||||
use="required" />
|
||||
<xsd:attribute name="input-channel" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="output-channel" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
|
||||
</xsd:schema>
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* 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.
|
||||
@@ -13,85 +13,91 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml.router;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.integration.router.AbstractChannelNameResolver;
|
||||
import org.springframework.integration.router.ChannelResolver;
|
||||
import org.springframework.integration.router.AbstractChannelMappingMessageRouter;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link ChannelResolver} classes that use
|
||||
* {@link XPathExpression} evaluation to determine channel names
|
||||
* @author Jonas Partner
|
||||
* Abstract base class for Message Routers that use
|
||||
* {@link XPathExpression} evaluation to determine channel names.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public abstract class AbstractXPathChannelNameResolver extends AbstractChannelNameResolver {
|
||||
public abstract class AbstractXPathRouter extends AbstractChannelMappingMessageRouter {
|
||||
|
||||
private final XPathExpression xPathExpression;
|
||||
|
||||
private volatile XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
|
||||
/**
|
||||
* Creates an channel name resolver using an XPath expression which may
|
||||
* contain zero or more namespace prefixes
|
||||
* @param pathExpression
|
||||
* Create a router that uses an XPath expression. The expression may
|
||||
* contain zero or more namespace prefixes.
|
||||
*
|
||||
* @param expression
|
||||
* @param namespaces
|
||||
*/
|
||||
public AbstractXPathChannelNameResolver(String pathExpression, Map<String, String> namespaces) {
|
||||
this.xPathExpression = XPathExpressionFactory.createXPathExpression(pathExpression, namespaces);
|
||||
public AbstractXPathRouter(String expression, Map<String, String> namespaces) {
|
||||
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a channel name resolver using an XPath expression with one
|
||||
* namespace. For example expression '/ns1:one/@type' prefix 'ns1' namespace
|
||||
* 'www.example.org'
|
||||
* @param pathExpression
|
||||
* Create a router uses an XPath expression with one namespace. For example,
|
||||
* expression='/ns1:one/@type' prefix='ns1' namespace='www.example.org'
|
||||
*
|
||||
* @param expression
|
||||
* @param prefix
|
||||
* @param namespace
|
||||
*/
|
||||
public AbstractXPathChannelNameResolver(String pathExpression, String prefix, String namespace) {
|
||||
public AbstractXPathRouter(String expression, String prefix, String namespace) {
|
||||
Map<String, String> namespaces = new HashMap<String, String>();
|
||||
namespaces.put(prefix, namespace);
|
||||
this.xPathExpression = XPathExpressionFactory.createXPathExpression(pathExpression, namespaces);
|
||||
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a channel name resolver using an XPath expression with no
|
||||
* namespaces For example '/one/@type'
|
||||
* @param pathExpression
|
||||
* Create a router that uses an XPath expression with no namespaces.
|
||||
* For example '/one/@type'
|
||||
*
|
||||
* @param expression
|
||||
*/
|
||||
public AbstractXPathChannelNameResolver(String pathExpression) {
|
||||
this.xPathExpression = XPathExpressionFactory.createXPathExpression(pathExpression);
|
||||
public AbstractXPathRouter(String expression) {
|
||||
this.xPathExpression = XPathExpressionFactory.createXPathExpression(expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a channel name resolver using the provided XPath expression
|
||||
* @param pathExpression
|
||||
* Create a router that uses the provided XPath expression.
|
||||
*
|
||||
* @param expression
|
||||
*/
|
||||
public AbstractXPathChannelNameResolver(XPathExpression pathExpression) {
|
||||
this.xPathExpression = pathExpression;
|
||||
public AbstractXPathRouter(XPathExpression expression) {
|
||||
this.xPathExpression = expression;
|
||||
}
|
||||
|
||||
|
||||
protected XmlPayloadConverter getConverter() {
|
||||
return converter;
|
||||
return this.converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converter used to convert payloads prior to XPAth testing
|
||||
* Converter used to convert payloads prior to XPath testing.
|
||||
*
|
||||
* @param converter
|
||||
*/
|
||||
public void setConverter(XmlPayloadConverter converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
protected XPathExpression getXPathExpresion() {
|
||||
return xPathExpression;
|
||||
protected XPathExpression getXPathExpression() {
|
||||
return this.xPathExpression;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,56 +19,58 @@ package org.springframework.integration.xml.router;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.integration.message.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;
|
||||
|
||||
/**
|
||||
* Evaluates the XPath expression using
|
||||
* 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 XPathMultiChannelNameResolver extends AbstractXPathChannelNameResolver {
|
||||
public class XPathMultiChannelRouter extends AbstractXPathRouter {
|
||||
|
||||
private volatile NodeMapper nodeMapper = new TextContentNodeMapper();
|
||||
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
|
||||
* Map)
|
||||
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String, Map)
|
||||
*/
|
||||
public XPathMultiChannelNameResolver(String pathExpression, Map<String, String> namespaces) {
|
||||
super(pathExpression, namespaces);
|
||||
public XPathMultiChannelRouter(String expression, Map<String, String> namespaces) {
|
||||
super(expression, namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
|
||||
* String, String)
|
||||
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String, String, String)
|
||||
*/
|
||||
public XPathMultiChannelNameResolver(String pathExpression, String prefix, String namespace) {
|
||||
super(pathExpression, prefix, namespace);
|
||||
public XPathMultiChannelRouter(String expression, String prefix, String namespace) {
|
||||
super(expression, prefix, namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String)
|
||||
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String)
|
||||
*/
|
||||
public XPathMultiChannelNameResolver(String pathExpression) {
|
||||
super(pathExpression);
|
||||
public XPathMultiChannelRouter(String expression) {
|
||||
super(expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(XPathExpression)
|
||||
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(XPathExpression)
|
||||
*/
|
||||
public XPathMultiChannelNameResolver(XPathExpression pathExpression) {
|
||||
super(pathExpression);
|
||||
public XPathMultiChannelRouter(XPathExpression expression) {
|
||||
super(expression);
|
||||
}
|
||||
|
||||
|
||||
public void setNodeMapper(NodeMapper nodeMapper) {
|
||||
Assert.notNull(nodeMapper, "NodeMapper must not be null");
|
||||
this.nodeMapper = nodeMapper;
|
||||
@@ -77,10 +79,11 @@ public class XPathMultiChannelNameResolver extends AbstractXPathChannelNameResol
|
||||
@SuppressWarnings("unchecked")
|
||||
public String[] resolveChannelNames(Message<?> message) {
|
||||
Node node = getConverter().convertToNode(message.getPayload());
|
||||
List channelNamesList = getXPathExpresion().evaluate(node, this.nodeMapper);
|
||||
List channelNamesList = getXPathExpression().evaluate(node, this.nodeMapper);
|
||||
return (String[]) channelNamesList.toArray(new String[channelNamesList.size()]);
|
||||
}
|
||||
|
||||
|
||||
private static class TextContentNodeMapper implements NodeMapper {
|
||||
|
||||
public Object mapNode(Node node, int nodeNum) throws DOMException {
|
||||
@@ -1,81 +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.Map;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* The provided {@link XPathExpression} should evaluate to a non empty string
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class XPathSingleChannelNameResolver extends AbstractXPathChannelNameResolver {
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
|
||||
* Map)
|
||||
*/
|
||||
public XPathSingleChannelNameResolver(String pathExpression, Map<String, String> namespaces) {
|
||||
super(pathExpression, namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String,
|
||||
* String, String)
|
||||
*/
|
||||
public XPathSingleChannelNameResolver(String pathExpression, String prefix, String namespace) {
|
||||
super(pathExpression, prefix, namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(String)
|
||||
*/
|
||||
public XPathSingleChannelNameResolver(String pathExpression) {
|
||||
super(pathExpression);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathChannelNameResolver#AbstractXPathChannelNameResolver(XPathExpression)
|
||||
*/
|
||||
public XPathSingleChannelNameResolver(XPathExpression pathExpression) {
|
||||
super(pathExpression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the payload using {@link XPathExpression#evaluateAsString(Node)}
|
||||
* @throws MessagingException if the {@link XPathExpression} evaluates to empty string
|
||||
*/
|
||||
public String[] resolveChannelNames(Message<?> message) {
|
||||
Node node = getConverter().convertToNode(message.getPayload());
|
||||
String result = getXPathExpresion().evaluateAsString(node);
|
||||
if(result.equals("")){
|
||||
throw new MessagingException(message,"XPath expression evaluated to empty string");
|
||||
}
|
||||
return new String[] { result };
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.Map;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
|
||||
/**
|
||||
* 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#AbstractXPathChannelNameResolver(String, Map)
|
||||
*/
|
||||
public XPathSingleChannelRouter(String expression, Map<String, String> namespaces) {
|
||||
super(expression, namespaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String, String, String)
|
||||
*/
|
||||
public XPathSingleChannelRouter(String expression, String prefix, String namespace) {
|
||||
super(expression, prefix, namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(String)
|
||||
*/
|
||||
public XPathSingleChannelRouter(String expression) {
|
||||
super(expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AbstractXPathRouter#AbstractXPathChannelNameResolver(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
|
||||
*/
|
||||
public String[] resolveChannelNames(Message<?> message) {
|
||||
Node node = getConverter().convertToNode(message.getPayload());
|
||||
String result = getXPathExpression().evaluateAsString(node);
|
||||
if ("".equals(result)) {
|
||||
throw new MessagingException(message,"XPath expression must not be empty");
|
||||
}
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
return new String[] { result };
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user