Skip to content

Commit 9d4aa8b

Browse files
artembilangaryrussell
authored andcommitted
GH-2993: Catch and log unsupported XML attributes
Fixes #2993 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` * Copy `TransformerFactoryUtils` from the latest Spring WS, where all the attributes are wrapped into `try..catch` and warn log. And use this new class everywhere instead of non-patched in Spring WS * Upgrade to the latest Spring WS 2.4.5
1 parent 0bb0878 commit 9d4aa8b

File tree

6 files changed

+101
-5
lines changed

6 files changed

+101
-5
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ subprojects { subproject ->
138138
springSocialTwitterVersion = '1.1.2.RELEASE'
139139
springRetryVersion = '1.1.3.RELEASE'
140140
springVersion = project.hasProperty('springVersion') ? project.springVersion : '4.3.23.RELEASE'
141-
springWsVersion = '2.4.4.RELEASE'
141+
springWsVersion = '2.4.5.RELEASE'
142142
xmlUnitVersion = '1.6'
143143
xstreamVersion = '1.4.7'
144144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.xml;
18+
19+
import javax.xml.XMLConstants;
20+
import javax.xml.transform.TransformerFactory;
21+
import javax.xml.transform.TransformerFactoryConfigurationError;
22+
23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
25+
26+
/**
27+
* The {@link TransformerFactory} configuration wrapper with some common settings.
28+
* Copy of {@link org.springframework.xml.transform.TransformerFactoryUtils}, but with
29+
* catching exception for unknown properties.
30+
*
31+
* @author Greg Turnquist
32+
* @author Artem Bilan
33+
*
34+
* @since 4.3.21
35+
*/
36+
37+
public final class TransformerFactoryUtils {
38+
39+
private static final Log LOG = LogFactory.getLog(TransformerFactoryUtils.class);
40+
41+
/**
42+
* Build a new {@link TransformerFactory} using the default constructor.
43+
*/
44+
public static TransformerFactory newInstance() {
45+
return defaultSettings(TransformerFactory.newInstance());
46+
}
47+
48+
/**
49+
* Build an {@link TransformerFactory} and prevent external entities from accessing.
50+
* @see TransformerFactory#newInstance()
51+
*/
52+
public static TransformerFactory newInstance(Class<? extends TransformerFactory> transformerFactoryClass) {
53+
try {
54+
return defaultSettings(transformerFactoryClass.newInstance());
55+
}
56+
catch (InstantiationException e) {
57+
throw new TransformerFactoryConfigurationError(e,
58+
"Could not instantiate TransformerFactory [" + transformerFactoryClass + "]");
59+
}
60+
catch (IllegalAccessException e) {
61+
throw new TransformerFactoryConfigurationError(e,
62+
"Could not instantiate TransformerFactory [" + transformerFactoryClass + "]");
63+
}
64+
}
65+
66+
/**
67+
* Prevent external entities from accessing.
68+
*/
69+
private static TransformerFactory defaultSettings(TransformerFactory factory) {
70+
try {
71+
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
72+
}
73+
catch (IllegalArgumentException e) {
74+
if (LOG.isWarnEnabled()) {
75+
LOG.warn(XMLConstants.ACCESS_EXTERNAL_DTD + " property not supported by " +
76+
factory.getClass().getCanonicalName());
77+
}
78+
}
79+
80+
try {
81+
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
82+
}
83+
catch (IllegalArgumentException e) {
84+
if (LOG.isWarnEnabled()) {
85+
LOG.warn(XMLConstants.ACCESS_EXTERNAL_STYLESHEET + " property not supported by " +
86+
factory.getClass().getCanonicalName());
87+
}
88+
}
89+
90+
return factory;
91+
}
92+
93+
private TransformerFactoryUtils() {
94+
}
95+
96+
}

spring-integration-xml/src/main/java/org/springframework/integration/xml/source/StringSourceFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
import org.w3c.dom.Document;
2828

29+
import org.springframework.integration.xml.TransformerFactoryUtils;
2930
import org.springframework.messaging.MessagingException;
3031
import org.springframework.util.Assert;
3132
import org.springframework.util.FileCopyUtils;
3233
import org.springframework.xml.transform.StringResult;
3334
import org.springframework.xml.transform.StringSource;
34-
import org.springframework.xml.transform.TransformerFactoryUtils;
3535

3636
/**
3737
* {@link SourceFactory} implementation which supports creation of a {@link StringSource}

spring-integration-xml/src/main/java/org/springframework/integration/xml/splitter/XPathMessageSplitter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.integration.util.Function;
4545
import org.springframework.integration.util.FunctionIterator;
4646
import org.springframework.integration.xml.DefaultXmlPayloadConverter;
47+
import org.springframework.integration.xml.TransformerFactoryUtils;
4748
import org.springframework.integration.xml.XmlPayloadConverter;
4849
import org.springframework.messaging.Message;
4950
import org.springframework.messaging.MessageHandlingException;
@@ -52,7 +53,6 @@
5253
import org.springframework.xml.DocumentBuilderFactoryUtils;
5354
import org.springframework.xml.namespace.SimpleNamespaceContext;
5455
import org.springframework.xml.transform.StringResult;
55-
import org.springframework.xml.transform.TransformerFactoryUtils;
5656
import org.springframework.xml.xpath.XPathException;
5757
import org.springframework.xml.xpath.XPathExpression;
5858
import org.springframework.xml.xpath.XPathExpressionFactory;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import javax.xml.transform.dom.DOMResult;
2727
import javax.xml.transform.dom.DOMSource;
2828

29+
import org.springframework.integration.xml.TransformerFactoryUtils;
2930
import org.springframework.messaging.MessagingException;
3031
import org.springframework.util.Assert;
3132
import org.springframework.xml.transform.StringResult;
32-
import org.springframework.xml.transform.TransformerFactoryUtils;
3333

3434
/**
3535
* Converts the passed {@link Result} to an instance of {@link String}.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.expression.Expression;
4444
import org.springframework.expression.spel.support.StandardEvaluationContext;
4545
import org.springframework.integration.expression.ExpressionUtils;
46+
import org.springframework.integration.xml.TransformerFactoryUtils;
4647
import org.springframework.integration.xml.result.DomResultFactory;
4748
import org.springframework.integration.xml.result.ResultFactory;
4849
import org.springframework.integration.xml.source.DomSourceFactory;
@@ -56,7 +57,6 @@
5657
import org.springframework.util.StringUtils;
5758
import org.springframework.xml.transform.StringResult;
5859
import org.springframework.xml.transform.StringSource;
59-
import org.springframework.xml.transform.TransformerFactoryUtils;
6060

6161
/**
6262
* Thread safe XSLT transformer implementation which returns a transformed

0 commit comments

Comments
 (0)