Skip to content

Commit a283ad6

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 4c6ffec commit a283ad6

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.messaging.Message;
5050
import org.springframework.messaging.MessagingException;
5151
import org.springframework.util.Assert;
52+
import org.springframework.util.ClassUtils;
5253
import org.springframework.util.ObjectUtils;
5354
import org.springframework.util.PatternMatchUtils;
5455
import org.springframework.util.StringUtils;
@@ -225,24 +226,39 @@ protected void onInit() {
225226
super.onInit();
226227
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory());
227228
if (this.templates == null) {
228-
TransformerFactory transformerFactory;
229-
if (this.transformerFactoryClassName != null) {
230-
transformerFactory = TransformerFactory.newInstance(this.transformerFactoryClassName, this.classLoader);
231-
}
232-
else {
233-
transformerFactory = TransformerFactoryUtils.newInstance();
234-
}
235-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
236-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file");
237229
try {
230+
TransformerFactory transformerFactory = createTransformerFactory();
238231
this.templates = transformerFactory.newTemplates(createStreamSourceOnResource(this.xslResource));
239232
}
240-
catch (TransformerConfigurationException | IOException e) {
233+
catch (ClassNotFoundException | TransformerConfigurationException | IOException e) {
241234
throw new IllegalStateException(e);
242235
}
243236
}
244237
}
245238

239+
private TransformerFactory createTransformerFactory() throws ClassNotFoundException {
240+
TransformerFactory transformerFactory;
241+
if (this.transformerFactoryClassName != null) {
242+
@SuppressWarnings("unchecked")
243+
Class<TransformerFactory> transformerFactoryClass =
244+
(Class<TransformerFactory>) ClassUtils.forName(this.transformerFactoryClassName, this.classLoader);
245+
transformerFactory = TransformerFactoryUtils.newInstance(transformerFactoryClass);
246+
}
247+
else {
248+
transformerFactory = TransformerFactoryUtils.newInstance();
249+
}
250+
try {
251+
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "file,jar:file");
252+
}
253+
catch (IllegalArgumentException ex) {
254+
if (logger.isInfoEnabled()) {
255+
logger.info("The '" + XMLConstants.ACCESS_EXTERNAL_STYLESHEET + "' property is not supported by "
256+
+ transformerFactory.getClass().getCanonicalName());
257+
}
258+
}
259+
return transformerFactory;
260+
}
261+
246262
@Override
247263
protected Object doTransform(Message<?> message) throws Exception {
248264
Transformer transformer = buildTransformer(message);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import javax.xml.transform.Templates;
2727
import javax.xml.transform.TransformerException;
2828
import javax.xml.transform.TransformerFactory;
29-
import javax.xml.transform.TransformerFactoryConfigurationError;
3029
import javax.xml.transform.dom.DOMResult;
3130

3231
import org.junit.Before;
@@ -161,10 +160,12 @@ public void testXsltPayloadWithTransformerFactoryClassName() throws Exception {
161160

162161
@Test
163162
public void testXsltPayloadWithBadTransformerFactoryClassName() throws IOException {
164-
XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText(), "foo.bar.Baz");
163+
XsltPayloadTransformer transformer =
164+
new XsltPayloadTransformer(getXslResourceThatOutputsText(), "foo.bar.Baz");
165165
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
166166
assertThatThrownBy(transformer::afterPropertiesSet)
167-
.isExactlyInstanceOf(TransformerFactoryConfigurationError.class);
167+
.isExactlyInstanceOf(IllegalStateException.class)
168+
.hasCauseExactlyInstanceOf(ClassNotFoundException.class);
168169
}
169170

170171
@Test

0 commit comments

Comments
 (0)