INT-3317: Remove comparator from resequencer
JIRA: https://jira.spring.io/browse/INT-3317 In addition add `AbstractCorrelatingMessageHandler.discardChannelName` for convenience with JavaConfig INT-3317: Add `AbstractCorrMH.outputChannelName` INT-3317: Fix further `BF` population Also add `MessageFilter.discardChannelName` Move `XPathExpressionEvaluatingHeaderValueMessageProcessor` to the `xml.transformer.support` package Additional polishing Polishing PR comments.
This commit is contained in:
committed by
Gary Russell
parent
75af72a77c
commit
8429424d46
@@ -26,6 +26,7 @@ import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractTransformerParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.integration.xml.transformer.XPathHeaderEnricher;
|
||||
import org.springframework.integration.xml.transformer.support.XPathExpressionEvaluatingHeaderValueMessageProcessor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -61,7 +62,7 @@ public class XPathHeaderEnricherParser extends AbstractTransformerParser {
|
||||
String elementName = node.getLocalName();
|
||||
if ("header".equals(elementName)) {
|
||||
BeanDefinitionBuilder builder =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(XPathHeaderEnricher.XPathExpressionEvaluatingHeaderValueMessageProcessor.class);
|
||||
BeanDefinitionBuilder.genericBeanDefinition(XPathExpressionEvaluatingHeaderValueMessageProcessor.class);
|
||||
String expressionString = headerElement.getAttribute("xpath-expression");
|
||||
String expressionRef = headerElement.getAttribute("xpath-expression-ref");
|
||||
boolean isExpressionString = StringUtils.hasText(expressionString);
|
||||
|
||||
@@ -18,24 +18,8 @@ package org.springframework.integration.xml.transformer;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.transformer.HeaderEnricher;
|
||||
import org.springframework.integration.transformer.support.HeaderValueMessageProcessor;
|
||||
import org.springframework.integration.util.BeanFactoryTypeConverter;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
import org.springframework.integration.xml.transformer.support.XPathExpressionEvaluatingHeaderValueMessageProcessor;
|
||||
|
||||
/**
|
||||
* Transformer implementation that evaluates XPath expressions against the
|
||||
@@ -44,7 +28,6 @@ import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
*
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XPathHeaderEnricher extends HeaderEnricher {
|
||||
@@ -60,72 +43,4 @@ public class XPathHeaderEnricher extends HeaderEnricher {
|
||||
}
|
||||
|
||||
|
||||
public static class XPathExpressionEvaluatingHeaderValueMessageProcessor implements HeaderValueMessageProcessor<Object>,
|
||||
BeanFactoryAware {
|
||||
|
||||
private static final XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
private final BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
|
||||
|
||||
private final XPathExpression expression;
|
||||
|
||||
private volatile XPathEvaluationType evaluationType = XPathEvaluationType.STRING_RESULT;
|
||||
|
||||
private volatile TypeDescriptor headerTypeDescriptor;
|
||||
|
||||
private volatile Boolean overwrite = null;
|
||||
|
||||
public XPathExpressionEvaluatingHeaderValueMessageProcessor(String expression) {
|
||||
Assert.hasText(expression, "expression must have text");
|
||||
this.expression = XPathExpressionFactory.createXPathExpression(expression);
|
||||
}
|
||||
|
||||
public XPathExpressionEvaluatingHeaderValueMessageProcessor(XPathExpression expression) {
|
||||
Assert.notNull(expression, "expression must not be null");
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public void setEvaluationType(XPathEvaluationType evaluationType) {
|
||||
this.evaluationType = evaluationType;
|
||||
}
|
||||
|
||||
public void setHeaderType(Class<?> headerType) {
|
||||
if (headerType != null) {
|
||||
this.headerTypeDescriptor = TypeDescriptor.valueOf(headerType);
|
||||
}
|
||||
}
|
||||
|
||||
public void setOverwrite(Boolean overwrite) {
|
||||
this.overwrite = overwrite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isOverwrite() {
|
||||
return this.overwrite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
ConversionService conversionService = IntegrationContextUtils.getConversionService(beanFactory);
|
||||
if (conversionService != null) {
|
||||
this.typeConverter.setConversionService(conversionService);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object processMessage(Message<?> message) {
|
||||
Node node = converter.convertToNode(message.getPayload());
|
||||
Object result = this.evaluationType.evaluateXPath(this.expression, node);
|
||||
if (result instanceof String && ((String) result).length() == 0) {
|
||||
result = null;
|
||||
}
|
||||
if (result != null && this.headerTypeDescriptor != null) {
|
||||
return this.typeConverter.convertValue(result, TypeDescriptor.forObject(result), this.headerTypeDescriptor);
|
||||
}
|
||||
else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2014 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.transformer.support;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.transformer.support.HeaderValueMessageProcessor;
|
||||
import org.springframework.integration.util.BeanFactoryTypeConverter;
|
||||
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
|
||||
import org.springframework.integration.xml.XmlPayloadConverter;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XPathExpressionEvaluatingHeaderValueMessageProcessor implements HeaderValueMessageProcessor<Object>,
|
||||
BeanFactoryAware {
|
||||
|
||||
private static final XmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
private final BeanFactoryTypeConverter typeConverter = new BeanFactoryTypeConverter();
|
||||
|
||||
private final XPathExpression expression;
|
||||
|
||||
private volatile XPathEvaluationType evaluationType = XPathEvaluationType.STRING_RESULT;
|
||||
|
||||
private volatile TypeDescriptor headerTypeDescriptor;
|
||||
|
||||
private volatile Boolean overwrite = null;
|
||||
|
||||
public XPathExpressionEvaluatingHeaderValueMessageProcessor(String expression) {
|
||||
Assert.hasText(expression, "expression must have text");
|
||||
this.expression = XPathExpressionFactory.createXPathExpression(expression);
|
||||
}
|
||||
|
||||
public XPathExpressionEvaluatingHeaderValueMessageProcessor(XPathExpression expression) {
|
||||
Assert.notNull(expression, "expression must not be null");
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public void setEvaluationType(XPathEvaluationType evaluationType) {
|
||||
this.evaluationType = evaluationType;
|
||||
}
|
||||
|
||||
public void setHeaderType(Class<?> headerType) {
|
||||
if (headerType != null) {
|
||||
this.headerTypeDescriptor = TypeDescriptor.valueOf(headerType);
|
||||
}
|
||||
}
|
||||
|
||||
public void setOverwrite(Boolean overwrite) {
|
||||
this.overwrite = overwrite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isOverwrite() {
|
||||
return this.overwrite;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
ConversionService conversionService = IntegrationContextUtils.getConversionService(beanFactory);
|
||||
if (conversionService != null) {
|
||||
this.typeConverter.setConversionService(conversionService);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object processMessage(Message<?> message) {
|
||||
Node node = converter.convertToNode(message.getPayload());
|
||||
Object result = this.evaluationType.evaluateXPath(this.expression, node);
|
||||
if (result instanceof String && ((String) result).length() == 0) {
|
||||
result = null;
|
||||
}
|
||||
if (result != null && this.headerTypeDescriptor != null) {
|
||||
return this.typeConverter.convertValue(result, TypeDescriptor.forObject(result), this.headerTypeDescriptor);
|
||||
}
|
||||
else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Contains support classes for Transformers.
|
||||
*
|
||||
* @since 3.0
|
||||
*
|
||||
*/
|
||||
package org.springframework.integration.xml.transformer.support;
|
||||
@@ -27,7 +27,7 @@ import java.util.TimeZone;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.xml.transformer.XPathHeaderEnricher.XPathExpressionEvaluatingHeaderValueMessageProcessor;
|
||||
import org.springframework.integration.xml.transformer.support.XPathExpressionEvaluatingHeaderValueMessageProcessor;
|
||||
import org.springframework.integration.xml.xpath.XPathEvaluationType;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
|
||||
@@ -27,8 +27,10 @@ import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -58,6 +60,7 @@ public class XsltPayloadTransformerTests {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
transformer = new XsltPayloadTransformer(getXslResource());
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@@ -111,6 +114,7 @@ public class XsltPayloadTransformerTests {
|
||||
Integer returnValue = new Integer(13);
|
||||
transformer = new XsltPayloadTransformer(getXslResource(),
|
||||
new StubResultTransformer(returnValue));
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object transformed = transformer
|
||||
.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
@@ -123,6 +127,7 @@ public class XsltPayloadTransformerTests {
|
||||
Integer returnValue = new Integer(13);
|
||||
transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue),
|
||||
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object transformed = transformer
|
||||
.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
@@ -133,6 +138,7 @@ public class XsltPayloadTransformerTests {
|
||||
@Test(expected = TransformerFactoryConfigurationError.class)
|
||||
public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception {
|
||||
transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz");
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
transformer.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
}
|
||||
@@ -152,6 +158,7 @@ public class XsltPayloadTransformerTests {
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl",
|
||||
this.getClass());
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
assertEquals(transformer.doTransform(buildMessage(docAsString)),
|
||||
outputAsString);
|
||||
@@ -165,6 +172,7 @@ public class XsltPayloadTransformerTests {
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setResultFactory(new StringResultFactory());
|
||||
transformer.setAlwaysUseResultFactory(true);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
|
||||
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
|
||||
@@ -178,6 +186,7 @@ public class XsltPayloadTransformerTests {
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setResultFactory(new StringResultFactory());
|
||||
transformer.setAlwaysUseResultFactory(true);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
|
||||
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
|
||||
@@ -188,6 +197,7 @@ public class XsltPayloadTransformerTests {
|
||||
transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText());
|
||||
transformer.setResultFactory(new StringResultFactory());
|
||||
transformer.setAlwaysUseResultFactory(true);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
|
||||
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
|
||||
@@ -199,12 +209,21 @@ public class XsltPayloadTransformerTests {
|
||||
}
|
||||
|
||||
private Resource getXslResource() throws Exception {
|
||||
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"order\"><bob>test</bob></xsl:template></xsl:stylesheet>";
|
||||
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
|
||||
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
|
||||
" <xsl:template match=\"order\">" +
|
||||
" <bob>test</bob>" +
|
||||
" </xsl:template>" +
|
||||
"</xsl:stylesheet>";
|
||||
return new ByteArrayResource(xsl.getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
private Resource getXslResourceThatOutputsText() throws Exception {
|
||||
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"text\" encoding=\"UTF-8\" /><xsl:template match=\"order\">hello world</xsl:template></xsl:stylesheet>";
|
||||
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
|
||||
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
|
||||
" <xsl:output method=\"text\" encoding=\"UTF-8\" />" +
|
||||
" <xsl:template match=\"order\">hello world</xsl:template>" +
|
||||
"</xsl:stylesheet>";
|
||||
return new ByteArrayResource(xsl.getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user