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>