initial implementation of XPathFilter
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 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.
|
||||
@@ -31,6 +31,7 @@ public class IntegrationXmlNamespaceHandler extends AbstractIntegrationNamespace
|
||||
registerBeanDefinitionParser("xpath-transformer", new XPathTransformerParser());
|
||||
registerBeanDefinitionParser("xpath-header-enricher", new XPathHeaderEnricherParser());
|
||||
registerBeanDefinitionParser("xpath-router", new XPathRouterParser());
|
||||
registerBeanDefinitionParser("xpath-filter", new XPathFilterParser());
|
||||
registerBeanDefinitionParser("xpath-selector", new XPathSelectorParser());
|
||||
registerBeanDefinitionParser("xpath-expression", new XPathExpressionParser());
|
||||
registerBeanDefinitionParser("xpath-splitter", new XPathMessageSplitterParser());
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.NodeList;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.FilterFactoryBean;
|
||||
import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.integration.xml.selector.BooleanTestXPathMessageSelector;
|
||||
import org.springframework.integration.xml.selector.RegexTestXPathMessageSelector;
|
||||
import org.springframework.integration.xml.selector.StringValueTestXPathMessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parser for the <xpath-filter> element.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 2.1
|
||||
*/
|
||||
public class XPathFilterParser extends AbstractConsumerEndpointParser {
|
||||
|
||||
private XPathExpressionParser xpathParser = new XPathExpressionParser();
|
||||
|
||||
|
||||
@Override
|
||||
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
|
||||
final BeanDefinitionBuilder selectorBuilder = BeanDefinitionBuilder.genericBeanDefinition();
|
||||
selectorBuilder.getBeanDefinition().setBeanClass(BooleanTestXPathMessageSelector.class);
|
||||
this.configureXPathExpression(element, selectorBuilder, parserContext);
|
||||
if (element.hasAttribute("match-value")) {
|
||||
selectorBuilder.addConstructorArgValue(element.getAttribute("match-value"));
|
||||
String matchType = element.getAttribute("match-type");
|
||||
if ("exact".equals(matchType)) {
|
||||
selectorBuilder.getBeanDefinition().setBeanClass(StringValueTestXPathMessageSelector.class);
|
||||
}
|
||||
else if ("case-insensitive".equals(matchType)) {
|
||||
selectorBuilder.getBeanDefinition().setBeanClass(StringValueTestXPathMessageSelector.class);
|
||||
selectorBuilder.addPropertyValue("caseSensitive", false);
|
||||
}
|
||||
else if ("regex".equals(matchType)) {
|
||||
selectorBuilder.getBeanDefinition().setBeanClass(RegexTestXPathMessageSelector.class);
|
||||
}
|
||||
}
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FilterFactoryBean.class);
|
||||
builder.addPropertyValue("targetObject", selectorBuilder.getBeanDefinition());
|
||||
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "discard-channel");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "throw-exception-on-rejection");
|
||||
return builder;
|
||||
}
|
||||
|
||||
private void configureXPathExpression(Element element, BeanDefinitionBuilder selectorBuilder, ParserContext parserContext) {
|
||||
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
|
||||
NodeList xPathExpressionNodes = element.getElementsByTagNameNS(element.getNamespaceURI(), "xpath-expression");
|
||||
Assert.isTrue(xPathExpressionNodes.getLength() <= 1, "At most one xpath-expression child may 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 (xPathExpressionChildPresent) {
|
||||
BeanDefinition beanDefinition = xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext);
|
||||
selectorBuilder.addConstructorArgValue(beanDefinition);
|
||||
}
|
||||
else {
|
||||
selectorBuilder.addConstructorArgReference(xPathExpressionRef);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.selector;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.core.MessageSelector;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
|
||||
/**
|
||||
* XPath {@link MessageSelector} that tests if a provided String value
|
||||
* matches a given Regular Expression.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 2.1
|
||||
*/
|
||||
public class RegexTestXPathMessageSelector extends AbstractXPathMessageSelector {
|
||||
|
||||
private final String regex;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a selector which attempts to match the given regex and supports multiple namespaces.
|
||||
*
|
||||
* @param expression XPath expression as a String
|
||||
* @param namespaces Map of namespaces where the keys are namespace prefixes
|
||||
* @param regex regular expression to match
|
||||
*/
|
||||
public RegexTestXPathMessageSelector(String expression, Map<String, String> namespaces, String regex) {
|
||||
super(expression, namespaces);
|
||||
Assert.notNull(regex, "regex must not be null");
|
||||
this.regex = regex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a selector which attempts to match the given regex and supports a single namespace.
|
||||
*
|
||||
* @param expression XPath expression as a String
|
||||
* @param prefix namespace prefix
|
||||
* @param namespace namespace URI
|
||||
* @param regex regular expression to match
|
||||
*/
|
||||
public RegexTestXPathMessageSelector(String expression, String prefix, String namespace, String regex) {
|
||||
super(expression, prefix, namespace);
|
||||
Assert.notNull(regex, "regex must not be null");
|
||||
this.regex = regex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a non-namespaced selector which attempts to match the given regex.
|
||||
*
|
||||
* @param expression XPath expression as a String
|
||||
* @param regex regular expression to match
|
||||
*/
|
||||
public RegexTestXPathMessageSelector(String expression, String regex) {
|
||||
super(expression);
|
||||
Assert.notNull(regex, "regex must not be null");
|
||||
this.regex = regex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a selector which attempts to match the given regex against the evaluation result
|
||||
* of the provided {@link XPathExpression}.
|
||||
*
|
||||
* @param expression XPath expression
|
||||
* @param regex regular expression to match
|
||||
*/
|
||||
public RegexTestXPathMessageSelector(XPathExpression expression, String regex) {
|
||||
super(expression);
|
||||
Assert.notNull(regex, "regex must not be null");
|
||||
this.regex = regex;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Evaluate the payload and return true if the value returned by the
|
||||
* {@link XPathExpression} matches the <code>regex</code>.
|
||||
*/
|
||||
public boolean accept(Message<?> message) {
|
||||
Node nodeToTest = getConverter().convertToNode(message.getPayload());
|
||||
String xPathResult = getXPathExpresion().evaluateAsString(nodeToTest);
|
||||
return StringUtils.hasText(xPathResult) ? xPathResult.matches(this.regex) : false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -446,11 +446,81 @@
|
||||
|
||||
<xsd:element name="xpath-router" type="XPathRouterType"/>
|
||||
|
||||
<xsd:element name="xpath-filter">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an XPath-based Message Filter. If the XPath expression will evaluate to a boolean,
|
||||
no configuration attributes are required. If the XPath expression will evaluate to a String,
|
||||
a "match-value" should be provided against which the evaluation result will be matched.
|
||||
There are three options for the "match-type": exact, case-insensitive, and regex. These
|
||||
correspond to the equals, equals-ignore-case, and matches operations on java.lang.String,
|
||||
respectively. When providing a 'match-type' value of 'regex', the value provided in
|
||||
'match-value' must be a valid Regular Expression.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="baseFilterType">
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="xpath-expression" minOccurs="0" maxOccurs="1">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
The XPath expression to evaluate.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:element>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="xpath-expression-ref" type="xsd:string" use="optional">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Reference to an XPath expression instance to evaluate.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.xml.xpath.XPathExpression"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="match-value" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
String value to be matched against the XPath evaluation result. If this is not provided,
|
||||
then the XPath evaluation MUST produce a boolean result directly.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="match-type" default="exact">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Type of match to apply between the XPath evaluation result and the 'match-value'.
|
||||
Default is "exact".
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:simpleType>
|
||||
<xsd:union memberTypes="matchTypeEnumeration xsd:string" />
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
<xsd:simpleType name="matchTypeEnumeration">
|
||||
<xsd:restriction base="xsd:token">
|
||||
<xsd:enumeration value="exact"/>
|
||||
<xsd:enumeration value="case-insensitive"/>
|
||||
<xsd:enumeration value="regex"/>
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
|
||||
<xsd:element name="xpath-selector">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an XPath selector.
|
||||
NOTE: this element is deprecated as of 2.1. Please use <xpath-filter> instead.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:sequence>
|
||||
@@ -547,62 +617,85 @@
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="validating-filter">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an XML validating filter.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="baseFilterType">
|
||||
<xsd:attribute name="xml-validator" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Reference to a custom 'org.springframework.xml.validation.XmlValidator' strategy
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.xml.validation.XmlValidator" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="schema-location"/>
|
||||
<xsd:attribute name="schema-type" default="xml-schema">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
<xsd:enumeration value="xml-schema"/>
|
||||
<xsd:enumeration value="relax-ng"/>
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:complexType name="baseFilterType">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Base type for XML filters.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="input-channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.core.MessageChannel"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="output-channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines an XML validating filter.
|
||||
Message Channel where you want accepted messages to be sent.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.core.MessageChannel" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="input-channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.core.MessageChannel"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="xml-validator" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Allows you to plug-in custom 'org.springframework.xml.validation.XmlValidator' strategy
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.xml.validation.XmlValidator" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="output-channel" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="discard-channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Allows you to point to a Message Channel where you want discarded messages to be sent.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.core.MessageChannel" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="schema-location"/>
|
||||
<xsd:attribute name="schema-type" default="xml-schema">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
<xsd:enumeration value="xml-schema"/>
|
||||
<xsd:enumeration value="relax-ng"/>
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="throw-exception-on-rejection" type="xsd:boolean" default="false"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="discard-channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Message Channel where you want rejected messages to be sent.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type type="org.springframework.integration.core.MessageChannel" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="throw-exception-on-rejection" type="xsd:boolean" default="false"/>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="inputOutputEndpoint">
|
||||
<xsd:sequence>
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
|
||||
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-xml:xpath-filter id="booleanFilter" input-channel="booleanFilterInput" discard-channel="booleanFilterRejections">
|
||||
<si-xml:xpath-expression expression="/name"/>
|
||||
</si-xml:xpath-filter>
|
||||
|
||||
<si:channel id="booleanFilterRejections">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<si-xml:xpath-filter id="booleanFilterWithNamespace" input-channel="booleanFilterWithNamespaceInput" discard-channel="booleanFilterWithNamespaceRejections">
|
||||
<si-xml:xpath-expression expression="/ns:name" ns-prefix="ns" ns-uri="www.example.org"/>
|
||||
</si-xml:xpath-filter>
|
||||
|
||||
<si:channel id="booleanFilterWithNamespaceRejections">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<si-xml:xpath-filter id="nestedNamespaceMapFilter" input-channel="nestedNamespaceMapFilterInput" discard-channel="nestedNamespaceMapFilterRejections">
|
||||
<si-xml:xpath-expression expression="/ns:name">
|
||||
<map>
|
||||
<entry key="ns" value="www.example.org"/>
|
||||
</map>
|
||||
</si-xml:xpath-expression>
|
||||
</si-xml:xpath-filter>
|
||||
|
||||
<si:channel id="nestedNamespaceMapFilterRejections">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<si-xml:xpath-filter id="stringFilterWithNamespace" input-channel="stringFilterWithNamespaceInput" discard-channel="stringFilterWithNamespaceRejections"
|
||||
match-value="outputOne">
|
||||
<si-xml:xpath-expression expression="/ns:name" ns-prefix="ns" ns-uri="www.example.org"/>
|
||||
</si-xml:xpath-filter>
|
||||
|
||||
<si:channel id="stringFilterWithNamespaceRejections">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<si-xml:xpath-filter id="stringFilterIgnoresCase" input-channel="stringFilterIgnoresCaseInput" discard-channel="stringFilterIgnoresCaseRejections"
|
||||
match-value="outputOne" match-type="case-insensitive">
|
||||
<si-xml:xpath-expression expression="name"/>
|
||||
</si-xml:xpath-filter>
|
||||
|
||||
<si:channel id="stringFilterIgnoresCaseRejections">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
<si-xml:xpath-filter id="stringFilterRegex" input-channel="stringFilterRegexInput" discard-channel="stringFilterRegexRejections"
|
||||
match-value="[A-Za-z]+" match-type="regex">
|
||||
<si-xml:xpath-expression expression="name"/>
|
||||
</si-xml:xpath-filter>
|
||||
|
||||
<si:channel id="stringFilterRegexRejections">
|
||||
<si:queue/>
|
||||
</si:channel>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.assertNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.PollableChannel;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 2.1
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class XPathFilterParserTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void simpleStringExpressionBoolean() throws Exception {
|
||||
MessageChannel inputChannel = context.getBean("booleanFilterInput", MessageChannel.class);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
PollableChannel discardChannel = context.getBean("booleanFilterRejections", PollableChannel.class);
|
||||
Message<?> shouldBeAccepted = MessageBuilder.withPayload("<name>outputOne</name>").setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeRejected = MessageBuilder.withPayload("<other>outputOne</other>").setReplyChannel(replyChannel).build();
|
||||
inputChannel.send(shouldBeAccepted);
|
||||
inputChannel.send(shouldBeRejected);
|
||||
assertEquals(shouldBeAccepted, replyChannel.receive(0));
|
||||
assertEquals(shouldBeRejected, discardChannel.receive(0));
|
||||
assertNull(replyChannel.receive(0));
|
||||
assertNull(discardChannel.receive(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringExpressionWithNamespaceBoolean() throws Exception {
|
||||
MessageChannel inputChannel = context.getBean("booleanFilterWithNamespaceInput", MessageChannel.class);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
PollableChannel discardChannel = context.getBean("booleanFilterWithNamespaceRejections", PollableChannel.class);
|
||||
Document docToAccept = XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>");
|
||||
Document docToReject = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
|
||||
Message<?> shouldBeAccepted = MessageBuilder.withPayload(docToAccept).setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeRejected = MessageBuilder.withPayload(docToReject).setReplyChannel(replyChannel).build();
|
||||
inputChannel.send(shouldBeAccepted);
|
||||
inputChannel.send(shouldBeRejected);
|
||||
assertEquals(shouldBeAccepted, replyChannel.receive(0));
|
||||
assertEquals(shouldBeRejected, discardChannel.receive(0));
|
||||
assertNull(replyChannel.receive(0));
|
||||
assertNull(discardChannel.receive(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringExpressionWithNestedMapBoolean() throws Exception {
|
||||
MessageChannel inputChannel = context.getBean("nestedNamespaceMapFilterInput", MessageChannel.class);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
PollableChannel discardChannel = context.getBean("nestedNamespaceMapFilterRejections", PollableChannel.class);
|
||||
Document docToAccept = XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>");
|
||||
Document docToReject = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
|
||||
Message<?> shouldBeAccepted = MessageBuilder.withPayload(docToAccept).setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeRejected = MessageBuilder.withPayload(docToReject).setReplyChannel(replyChannel).build();
|
||||
inputChannel.send(shouldBeAccepted);
|
||||
inputChannel.send(shouldBeRejected);
|
||||
assertEquals(shouldBeAccepted, replyChannel.receive(0));
|
||||
assertEquals(shouldBeRejected, discardChannel.receive(0));
|
||||
assertNull(replyChannel.receive(0));
|
||||
assertNull(discardChannel.receive(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringExpressionWithNamespaceString() throws Exception {
|
||||
MessageChannel inputChannel = context.getBean("stringFilterWithNamespaceInput", MessageChannel.class);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
PollableChannel discardChannel = context.getBean("stringFilterWithNamespaceRejections", PollableChannel.class);
|
||||
Document docToAccept = XmlTestUtil.getDocumentForString("<ns1:name xmlns:ns1='www.example.org'>outputOne</ns1:name>");
|
||||
Document docToReject = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
|
||||
Message<?> shouldBeAccepted = MessageBuilder.withPayload(docToAccept).setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeRejected = MessageBuilder.withPayload(docToReject).setReplyChannel(replyChannel).build();
|
||||
inputChannel.send(shouldBeAccepted);
|
||||
inputChannel.send(shouldBeRejected);
|
||||
assertEquals(shouldBeAccepted, replyChannel.receive(0));
|
||||
assertEquals(shouldBeRejected, discardChannel.receive(0));
|
||||
assertNull(replyChannel.receive(0));
|
||||
assertNull(discardChannel.receive(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringExpressionIgnoresCase() throws Exception {
|
||||
MessageChannel inputChannel = context.getBean("stringFilterIgnoresCaseInput", MessageChannel.class);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
PollableChannel discardChannel = context.getBean("stringFilterIgnoresCaseRejections", PollableChannel.class);
|
||||
Document docToAccept1 = XmlTestUtil.getDocumentForString("<name>OUTPUTONE</name>");
|
||||
Document docToAccept2 = XmlTestUtil.getDocumentForString("<name>outputOne</name>");
|
||||
Document docToReject = XmlTestUtil.getDocumentForString("<name>outputTwo</name>");
|
||||
Message<?> shouldBeAccepted1 = MessageBuilder.withPayload(docToAccept1).setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeAccepted2 = MessageBuilder.withPayload(docToAccept2).setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeRejected = MessageBuilder.withPayload(docToReject).setReplyChannel(replyChannel).build();
|
||||
inputChannel.send(shouldBeAccepted1);
|
||||
inputChannel.send(shouldBeAccepted2);
|
||||
inputChannel.send(shouldBeRejected);
|
||||
assertEquals(shouldBeAccepted1, replyChannel.receive(0));
|
||||
assertEquals(shouldBeAccepted2, replyChannel.receive(0));
|
||||
assertEquals(shouldBeRejected, discardChannel.receive(0));
|
||||
assertNull(replyChannel.receive(0));
|
||||
assertNull(discardChannel.receive(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringExpressionRegex() throws Exception {
|
||||
MessageChannel inputChannel = context.getBean("stringFilterRegexInput", MessageChannel.class);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
PollableChannel discardChannel = context.getBean("stringFilterRegexRejections", PollableChannel.class);
|
||||
Document docToAccept1 = XmlTestUtil.getDocumentForString("<name>aBcDeFgHiJk</name>");
|
||||
Document docToAccept2 = XmlTestUtil.getDocumentForString("<name>xyz</name>");
|
||||
Document docToReject = XmlTestUtil.getDocumentForString("<name>abc123</name>");
|
||||
Message<?> shouldBeAccepted1 = MessageBuilder.withPayload(docToAccept1).setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeAccepted2 = MessageBuilder.withPayload(docToAccept2).setReplyChannel(replyChannel).build();
|
||||
Message<?> shouldBeRejected = MessageBuilder.withPayload(docToReject).setReplyChannel(replyChannel).build();
|
||||
inputChannel.send(shouldBeAccepted1);
|
||||
inputChannel.send(shouldBeAccepted2);
|
||||
inputChannel.send(shouldBeRejected);
|
||||
assertEquals(shouldBeAccepted1, replyChannel.receive(0));
|
||||
assertEquals(shouldBeAccepted2, replyChannel.receive(0));
|
||||
assertEquals(shouldBeRejected, discardChannel.receive(0));
|
||||
assertNull(replyChannel.receive(0));
|
||||
assertNull(discardChannel.receive(0));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user