Skip to content

Commit 5e6506a

Browse files
artembilangaryrussell
authored andcommitted
GH-2723: Handle unsupported XML properties
Fixes #2723 Not all XML components support all the configuration properties. For example Saxon HE doesn't support `XMLConstants.ACCESS_EXTERNAL_DTD` and end up with an exception like: `IllegalArgumentException: Unknown configuration property http://javax.xml.XMLConstants/property/accessExternalDTD` * Change `XsltPayloadTransformer` to re-use `TransformerFactoryUtils` from spring-ws as a centralized source of `TransformerFactory` configuration. * Wrap `XMLConstants.ACCESS_EXTERNAL_STYLESHEET` to the `try..catch` and log INFO about not supported property **Cherry-pick to 5.0.x & 4.3.x**
1 parent c723b69 commit 5e6506a

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java

+25-11
Original file line numberDiff line numberDiff line change
@@ -240,25 +240,39 @@ protected void onInit() throws Exception {
240240
super.onInit();
241241
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
242242
if (this.templates == null) {
243-
TransformerFactory transformerFactory;
244-
if (this.transformerFactoryClassName != null) {
245-
transformerFactory = TransformerFactory.newInstance(this.transformerFactoryClassName,
246-
this.classLoader);
247-
}
248-
else {
249-
transformerFactory = TransformerFactoryUtils.newInstance();
250-
}
251-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
252-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file");
253243
try {
244+
TransformerFactory transformerFactory = createTransformerFactory();
254245
this.templates = transformerFactory.newTemplates(createStreamSourceOnResource(this.xslResource));
255246
}
256-
catch (TransformerConfigurationException | IOException e) {
247+
catch (ClassNotFoundException | TransformerConfigurationException | IOException e) {
257248
throw new IllegalStateException(e);
258249
}
259250
}
260251
}
261252

253+
private TransformerFactory createTransformerFactory() throws ClassNotFoundException {
254+
TransformerFactory transformerFactory;
255+
if (this.transformerFactoryClassName != null) {
256+
@SuppressWarnings("unchecked")
257+
Class<TransformerFactory> transformerFactoryClass =
258+
(Class<TransformerFactory>) ClassUtils.forName(this.transformerFactoryClassName, this.classLoader);
259+
transformerFactory = TransformerFactoryUtils.newInstance(transformerFactoryClass);
260+
}
261+
else {
262+
transformerFactory = TransformerFactoryUtils.newInstance();
263+
}
264+
try {
265+
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file");
266+
}
267+
catch (IllegalArgumentException ex) {
268+
if (logger.isInfoEnabled()) {
269+
logger.info("The '" + XMLConstants.ACCESS_EXTERNAL_STYLESHEET + "' property is not supported by "
270+
+ transformerFactory.getClass().getCanonicalName());
271+
}
272+
}
273+
return transformerFactory;
274+
}
275+
262276
@Override
263277
protected Object doTransform(Message<?> message) throws Exception {
264278
Transformer transformer = buildTransformer(message);

spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import javax.xml.transform.Templates;
2828
import javax.xml.transform.TransformerException;
2929
import javax.xml.transform.TransformerFactory;
30-
import javax.xml.transform.TransformerFactoryConfigurationError;
3130
import javax.xml.transform.dom.DOMResult;
3231

3332
import org.junit.Before;
@@ -148,7 +147,7 @@ public void testXsltPayloadWithTransformerFactoryClassName() throws Exception {
148147
transformed);
149148
}
150149

151-
@Test(expected = TransformerFactoryConfigurationError.class)
150+
@Test(expected = IllegalStateException.class)
152151
public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception {
153152
transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText(), "foo.bar.Baz");
154153
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
@@ -249,6 +248,7 @@ public StubResultTransformer(Object objectToReturn) {
249248
this.objectToReturn = objectToReturn;
250249
}
251250

251+
@Override
252252
public Object transformResult(Result result) {
253253
return objectToReturn;
254254
}

0 commit comments

Comments
 (0)