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.
@@ -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>");