Skip to content

Commit 3515acf

Browse files
Arjen Poutsmarstoyanchev
authored andcommitted
Added 'processExternalEntities' to JAXB2Marshaller
Added 'processExternalEntities' property to the JAXB2Marshaller, which indicates whether external XML entities are processed when unmarshalling. Default is false, meaning that external entities are not resolved. Processing of external entities will only be enabled/disabled when the Source} passed to #unmarshal(Source) is a SAXSource or StreamSource. It has no effect for DOMSource or StAXSource instances.
1 parent 7fdd0c2 commit 3515acf

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
import javax.xml.stream.XMLStreamWriter;
6262
import javax.xml.transform.Result;
6363
import javax.xml.transform.Source;
64+
import javax.xml.transform.dom.DOMSource;
6465
import javax.xml.transform.sax.SAXSource;
66+
import javax.xml.transform.stream.StreamSource;
6567
import javax.xml.validation.Schema;
6668
import javax.xml.validation.SchemaFactory;
6769

@@ -173,6 +175,8 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
173175

174176
private Schema schema;
175177

178+
private boolean processExternalEntities = false;
179+
176180

177181
/**
178182
* Set multiple JAXB context paths. The given array of context paths gets
@@ -385,6 +389,19 @@ public void setMappedClass(Class<?> mappedClass) {
385389
this.mappedClass = mappedClass;
386390
}
387391

392+
/**
393+
* Indicates whether external XML entities are processed when unmarshalling.
394+
* <p>Default is {@code false}, meaning that external entities are not resolved.
395+
* Note that processing of external entities will only be enabled/disabled when the
396+
* {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or
397+
* {@link StreamSource}. It has no effect for {@link DOMSource} or {@link StAXSource}
398+
* instances.
399+
*/
400+
public void setProcessExternalEntities(boolean processExternalEntities) {
401+
this.processExternalEntities = processExternalEntities;
402+
}
403+
404+
@Override
388405
public void setBeanClassLoader(ClassLoader classLoader) {
389406
this.beanClassLoader = classLoader;
390407
}
@@ -704,6 +721,8 @@ public Object unmarshal(Source source) throws XmlMappingException {
704721
}
705722

706723
public Object unmarshal(Source source, MimeContainer mimeContainer) throws XmlMappingException {
724+
source = processSource(source);
725+
707726
try {
708727
Unmarshaller unmarshaller = createUnmarshaller();
709728
if (this.mtomEnabled && mimeContainer != null) {
@@ -744,6 +763,44 @@ protected Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxS
744763
}
745764
}
746765

766+
private Source processSource(Source source) {
767+
if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) {
768+
return source;
769+
}
770+
771+
XMLReader xmlReader = null;
772+
InputSource inputSource = null;
773+
774+
if (source instanceof SAXSource) {
775+
SAXSource saxSource = (SAXSource) source;
776+
xmlReader = saxSource.getXMLReader();
777+
inputSource = saxSource.getInputSource();
778+
}
779+
else if (source instanceof StreamSource) {
780+
StreamSource streamSource = (StreamSource) source;
781+
if (streamSource.getInputStream() != null) {
782+
inputSource = new InputSource(streamSource.getInputStream());
783+
}
784+
else if (streamSource.getReader() != null) {
785+
inputSource = new InputSource(streamSource.getReader());
786+
}
787+
}
788+
789+
try {
790+
if (xmlReader == null) {
791+
xmlReader = XMLReaderFactory.createXMLReader();
792+
}
793+
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities",
794+
this.processExternalEntities);
795+
796+
return new SAXSource(xmlReader, inputSource);
797+
}
798+
catch (SAXException ex) {
799+
logger.warn("Processing of external entities could not be disabled", ex);
800+
return source;
801+
}
802+
}
803+
747804
/**
748805
* Return a newly created JAXB unmarshaller.
749806
* Note: JAXB unmarshallers are not necessarily thread-safe.

0 commit comments

Comments
 (0)