SourceHttpMessageConverter's supports implementation needs to check for StAXSource
Issue: SPR-11341
This commit is contained in:
@@ -20,7 +20,8 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
@@ -42,6 +43,7 @@ import org.xml.sax.InputSource;
|
|||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.XMLReader;
|
import org.xml.sax.XMLReader;
|
||||||
import org.xml.sax.helpers.XMLReaderFactory;
|
import org.xml.sax.helpers.XMLReaderFactory;
|
||||||
|
|
||||||
import org.springframework.http.HttpInputMessage;
|
import org.springframework.http.HttpInputMessage;
|
||||||
import org.springframework.http.HttpOutputMessage;
|
import org.springframework.http.HttpOutputMessage;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@@ -60,10 +62,22 @@ import org.springframework.util.StreamUtils;
|
|||||||
*/
|
*/
|
||||||
public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMessageConverter<T> {
|
public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMessageConverter<T> {
|
||||||
|
|
||||||
|
private static final Set<Class<?>> SUPPORTED_CLASSES = new HashSet<Class<?>>(5);
|
||||||
|
|
||||||
|
static {
|
||||||
|
SUPPORTED_CLASSES.add(DOMSource.class);
|
||||||
|
SUPPORTED_CLASSES.add(SAXSource.class);
|
||||||
|
SUPPORTED_CLASSES.add(StAXSource.class);
|
||||||
|
SUPPORTED_CLASSES.add(StreamSource.class);
|
||||||
|
SUPPORTED_CLASSES.add(Source.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private final TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
private final TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||||
|
|
||||||
private boolean processExternalEntities = false;
|
private boolean processExternalEntities = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes}
|
* Sets the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes}
|
||||||
* to {@code text/xml} and {@code application/xml}, and {@code application/*-xml}.
|
* to {@code text/xml} and {@code application/xml}, and {@code application/*-xml}.
|
||||||
@@ -74,18 +88,17 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether external XML entities are processed when converting
|
* Indicates whether external XML entities are processed when converting to a Source.
|
||||||
* to a Source.
|
|
||||||
* <p>Default is {@code false}, meaning that external entities are not resolved.
|
* <p>Default is {@code false}, meaning that external entities are not resolved.
|
||||||
*/
|
*/
|
||||||
public void setProcessExternalEntities(boolean processExternalEntities) {
|
public void setProcessExternalEntities(boolean processExternalEntities) {
|
||||||
this.processExternalEntities = processExternalEntities;
|
this.processExternalEntities = processExternalEntities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supports(Class<?> clazz) {
|
public boolean supports(Class<?> clazz) {
|
||||||
return DOMSource.class.equals(clazz) || SAXSource.class.equals(clazz)
|
return SUPPORTED_CLASSES.contains(clazz);
|
||||||
|| StreamSource.class.equals(clazz) || Source.class.equals(clazz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -108,7 +121,7 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new HttpMessageConversionException("Could not read class [" + clazz +
|
throw new HttpMessageConversionException("Could not read class [" + clazz +
|
||||||
"]. Only DOMSource, SAXSource, and StreamSource are supported.");
|
"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +129,8 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
|||||||
try {
|
try {
|
||||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||||
documentBuilderFactory.setNamespaceAware(true);
|
documentBuilderFactory.setNamespaceAware(true);
|
||||||
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", processExternalEntities);
|
documentBuilderFactory.setFeature(
|
||||||
|
"http://xml.org/sax/features/external-general-entities", this.processExternalEntities);
|
||||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||||
Document document = documentBuilder.parse(body);
|
Document document = documentBuilder.parse(body);
|
||||||
return new DOMSource(document);
|
return new DOMSource(document);
|
||||||
@@ -132,7 +146,8 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
|||||||
private SAXSource readSAXSource(InputStream body) throws IOException {
|
private SAXSource readSAXSource(InputStream body) throws IOException {
|
||||||
try {
|
try {
|
||||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||||
reader.setFeature("http://xml.org/sax/features/external-general-entities", processExternalEntities);
|
reader.setFeature(
|
||||||
|
"http://xml.org/sax/features/external-general-entities", this.processExternalEntities);
|
||||||
byte[] bytes = StreamUtils.copyToByteArray(body);
|
byte[] bytes = StreamUtils.copyToByteArray(body);
|
||||||
return new SAXSource(reader, new InputSource(new ByteArrayInputStream(bytes)));
|
return new SAXSource(reader, new InputSource(new ByteArrayInputStream(bytes)));
|
||||||
}
|
}
|
||||||
@@ -144,7 +159,8 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
|||||||
private Source readStAXSource(InputStream body) {
|
private Source readStAXSource(InputStream body) {
|
||||||
try {
|
try {
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
|
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
|
||||||
inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", processExternalEntities);
|
inputFactory.setProperty(
|
||||||
|
"javax.xml.stream.isSupportingExternalEntities", this.processExternalEntities);
|
||||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
|
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
|
||||||
return new StAXSource(streamReader);
|
return new StAXSource(streamReader);
|
||||||
}
|
}
|
||||||
@@ -192,21 +208,21 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
|||||||
|
|
||||||
private static class CountingOutputStream extends OutputStream {
|
private static class CountingOutputStream extends OutputStream {
|
||||||
|
|
||||||
private long count = 0;
|
long count = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) throws IOException {
|
public void write(int b) throws IOException {
|
||||||
count++;
|
this.count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(byte[] b) throws IOException {
|
public void write(byte[] b) throws IOException {
|
||||||
count += b.length;
|
this.count += b.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
count += len;
|
this.count += len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user