GH-2197: Rely on the Document creation for Source

Resolves https://github.com/spring-projects/spring-integration/issues/2197

Sometime we would like to avoid some XML definition features (e.g. loading of the DTD).
The `XmlValidatingMessageSelector` doesn't allow to do that easily because it doesn't
rely on the `Document` creation via `DocumentBuilderFactory`, but directly
manipulates with `Source`

* Change `DefaultXmlPayloadConverter.convertToSource()` to fallback to the `convertToDocument()`
for non-Document payloads.
This way we can configure `DefaultXmlPayloadConverter` with custom `DocumentBuilderFactory` and
bypass DTD loading, for example
* Expose `xml-converter` option for the `<int-xml:validating-filter>`  component
This commit is contained in:
Artem Bilan
2017-07-05 16:42:14 -04:00
committed by Gary Russell
parent 6c62af53f4
commit d79e70c6df
7 changed files with 68 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -33,7 +33,6 @@ import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.springframework.messaging.MessagingException;
import org.springframework.xml.transform.StringSource;
/**
* Default implementation of {@link XmlPayloadConverter}. Supports
@@ -133,18 +132,14 @@ public class DefaultXmlPayloadConverter implements XmlPayloadConverter {
public Source convertToSource(Object object) {
Source source = null;
if (object instanceof Source) {
source = (Source) object;
return (Source) object;
}
else if (object instanceof Document) {
source = new DOMSource((Document) object);
}
else if (object instanceof String) {
source = new StringSource((String) object);
return new DOMSource((Document) object);
}
else {
throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]");
return convertToSource(convertToDocument(object));
}
return source;
}
protected synchronized DocumentBuilder getDocumentBuilder() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 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.
@@ -44,7 +44,7 @@ public class XmlPayloadValidatingFilterParser extends AbstractConsumerEndpointPa
String schemaLocation = element.getAttribute("schema-location");
boolean validatorDefined = StringUtils.hasText(validator);
boolean schemaLocationDefined = StringUtils.hasText(schemaLocation);
if (!(validatorDefined ^ schemaLocationDefined)) {
if (validatorDefined == schemaLocationDefined) {
throw new BeanDefinitionStoreException(
"Exactly one of 'xml-validator' or 'schema-location' is allowed on the 'validating-filter' element");
}
@@ -59,8 +59,11 @@ public class XmlPayloadValidatingFilterParser extends AbstractConsumerEndpointPa
selectorBuilder.addConstructorArgReference(validator);
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(selectorBuilder, element, "throw-exception-on-rejection");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(selectorBuilder, element, "xml-converter", "converter");
filterBuilder.addPropertyValue("targetObject", selectorBuilder.getBeanDefinition());
IntegrationNamespaceUtils.setValueIfAttributeDefined(filterBuilder, element, "send-timeout");
return filterBuilder;
}

View File

@@ -812,6 +812,18 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="xml-converter" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Reference to a custom 'org.springframework.integration.xml.XmlPayloadConverter' strategy
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.integration.xml.XmlPayloadConverter" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="schema-location"/>
<xsd:attribute name="schema-type" default="xml-schema">
<xsd:simpleType>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -41,7 +41,6 @@ import org.xml.sax.InputSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.messaging.MessagingException;
import org.springframework.xml.transform.StringSource;
/**
*
@@ -105,7 +104,7 @@ public class DefaultXmlPayloadConverterTests {
@Test
public void testGetSourcePassingString() throws Exception {
Source source = converter.convertToSource(TEST_DOCUMENT_AS_STRING);
assertEquals(StringSource.class, source.getClass());
assertEquals(DOMSource.class, source.getClass());
}
@Test

View File

@@ -1,11 +1,11 @@
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-xml="http://www.springframework.org/schema/integration/xml"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xml="http://www.springframework.org/schema/integration/xml"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
@@ -15,7 +15,12 @@
<util:properties id="props">
<prop key="xmlSchema">xml-schema</prop>
</util:properties>
</util:properties>
<bean id="xmlPayloadConverter" class="org.springframework.integration.xml.DefaultXmlPayloadConverter">
<constructor-arg
value="#{T (org.springframework.integration.xml.config.XmlPayloadValidatingFilterParserTests).DOCUMENT_BUILDER_FACTORY}"/>
</bean>
<int-xml:validating-filter id="parseOnly"
order="2"
@@ -34,6 +39,7 @@
input-channel="inputChannelA"
output-channel="validOutputChannel"
discard-channel="invalidOutputChannel"
xml-converter="xmlPayloadConverter"
schema-location="org/springframework/integration/xml/config/validationTestsSchema.xsd"/>
<int-xml:validating-filter id="filterB"
@@ -49,7 +55,8 @@
discard-channel="invalidOutputChannel"
xml-validator="xmlValidator"/>
<bean id="xmlValidator" class="org.springframework.xml.validation.XmlValidatorFactory" factory-method="createValidator">
<bean id="xmlValidator" class="org.springframework.xml.validation.XmlValidatorFactory"
factory-method="createValidator">
<constructor-arg value="classpath:org/springframework/integration/xml/config/validationTestsSchema.xsd"/>
<constructor-arg value="http://www.w3.org/2001/XMLSchema"/>
</bean>

View File

@@ -26,6 +26,9 @@ import static org.junit.Assert.fail;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -40,6 +43,7 @@ import org.springframework.integration.support.SmartLifecycleRoleController;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xml.AggregatedXmlMessageValidationException;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
@@ -60,6 +64,19 @@ import org.springframework.util.MultiValueMap;
@DirtiesContext
public class XmlPayloadValidatingFilterParserTests {
public static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
static {
DOCUMENT_BUILDER_FACTORY.setNamespaceAware(true);
try {
DOCUMENT_BUILDER_FACTORY.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
DOCUMENT_BUILDER_FACTORY.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
}
catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
}
@Autowired
private ApplicationContext ac;
@@ -79,10 +96,10 @@ public class XmlPayloadValidatingFilterParserTests {
@Test
public void testValidMessage() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<greeting>hello</greeting>");
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
PollableChannel validChannel = ac.getBean("validOutputChannel", PollableChannel.class);
MessageChannel inputChannel = ac.getBean("inputChannelA", MessageChannel.class);
Message<String> docMessage =
new GenericMessage<>("<!DOCTYPE greeting SYSTEM \"greeting.dtd\"><greeting>hello</greeting>");
PollableChannel validChannel = this.ac.getBean("validOutputChannel", PollableChannel.class);
MessageChannel inputChannel = this.ac.getBean("inputChannelA", MessageChannel.class);
inputChannel.send(docMessage);
assertNotNull(validChannel.receive(100));
}
@@ -98,6 +115,7 @@ public class XmlPayloadValidatingFilterParserTests {
assertNotNull(invalidChannel.receive(100));
assertNull(validChannel.receive(100));
}
@Test
public void testInvalidMessageWithThrowException() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<greeting ping=\"pong\"><other/></greeting>");
@@ -117,6 +135,7 @@ public class XmlPayloadValidatingFilterParserTests {
Matchers.containsString("Element 'greeting' is a simple type, so it cannot have attributes,"));
}
}
@Test
public void testValidMessageWithValidator() throws Exception {
Document doc = XmlTestUtil.getDocumentForString("<greeting>hello</greeting>");

View File

@@ -1033,8 +1033,9 @@ Please see below for an overview of all available configuration parameters:
schema-location="" <5>
schema-type="xml-schema" <6>
throw-exception-on-rejection="false" <7>
xml-validator=""> <8>
<int:poller .../> <9>
xml-converter="" <8>
xml-validator=""> <9>
<int:poller .../> <10>
</int-xml:validating-filter>
----
@@ -1069,10 +1070,11 @@ If not set it defaults to `xml-schema` which internally translates to `org.sprin
<7> If `true` a `MessageRejectedException` is thrown in case validation fails for the provided Message's payload._Optional_.
Defaults to `false` if not set.
<8> Reference to a custom `org.springframework.integration.xml.XmlPayloadConverter` strategy.
_Optional_.
<8> Reference to a custom `sorg.springframework.xml.validation.XmlValidator` strategy.
<9> Reference to a custom `sorg.springframework.xml.validation.XmlValidator` strategy.
You can set this attribute or the `schema-location` attribute but not both.
_Optional_.
<9> _Optional_.
<10> _Optional_.