Removed accidental backport of StAX code in 3.2.5 (restoring Java 5 compatibility)
Issue: SPR-11341
This commit is contained in:
parent
603d79ac9b
commit
09c9137b75
|
|
@ -17,21 +17,18 @@
|
||||||
package org.springframework.http.converter.xml;
|
package org.springframework.http.converter.xml;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
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;
|
||||||
import javax.xml.stream.XMLInputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.XMLStreamReader;
|
|
||||||
import javax.xml.transform.Result;
|
import javax.xml.transform.Result;
|
||||||
import javax.xml.transform.Source;
|
import javax.xml.transform.Source;
|
||||||
import javax.xml.transform.TransformerException;
|
import javax.xml.transform.TransformerException;
|
||||||
import javax.xml.transform.TransformerFactory;
|
import javax.xml.transform.TransformerFactory;
|
||||||
import javax.xml.transform.dom.DOMResult;
|
|
||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.sax.SAXSource;
|
import javax.xml.transform.sax.SAXSource;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
|
@ -43,7 +40,6 @@ 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.HttpHeaders;
|
|
||||||
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;
|
||||||
|
|
@ -52,7 +48,6 @@ import org.springframework.http.converter.HttpMessageConversionException;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
import org.springframework.util.xml.StaxUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter}
|
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter}
|
||||||
|
|
@ -63,10 +58,21 @@ import org.springframework.util.xml.StaxUtils;
|
||||||
*/
|
*/
|
||||||
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<?>>(4);
|
||||||
|
|
||||||
|
static {
|
||||||
|
SUPPORTED_CLASSES.add(DOMSource.class);
|
||||||
|
SUPPORTED_CLASSES.add(SAXSource.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}.
|
||||||
|
|
@ -77,21 +83,21 @@ 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
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
|
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
|
||||||
throws IOException, HttpMessageNotReadableException {
|
throws IOException, HttpMessageNotReadableException {
|
||||||
|
|
||||||
|
|
@ -99,9 +105,6 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
||||||
if (DOMSource.class.equals(clazz)) {
|
if (DOMSource.class.equals(clazz)) {
|
||||||
return (T) readDOMSource(body);
|
return (T) readDOMSource(body);
|
||||||
}
|
}
|
||||||
else if (StaxUtils.isStaxSourceClass(clazz)) {
|
|
||||||
return (T) readStAXSource(body);
|
|
||||||
}
|
|
||||||
else if (SAXSource.class.equals(clazz)) {
|
else if (SAXSource.class.equals(clazz)) {
|
||||||
return (T) readSAXSource(body);
|
return (T) readSAXSource(body);
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +121,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);
|
||||||
|
|
@ -134,7 +138,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)));
|
||||||
}
|
}
|
||||||
|
|
@ -143,18 +148,6 @@ public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Source readStAXSource(InputStream body) {
|
|
||||||
try {
|
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
|
|
||||||
inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", processExternalEntities);
|
|
||||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
|
|
||||||
return StaxUtils.createStaxSource(streamReader);
|
|
||||||
}
|
|
||||||
catch (XMLStreamException ex) {
|
|
||||||
throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private StreamSource readStreamSource(InputStream body) throws IOException {
|
private StreamSource readStreamSource(InputStream body) throws IOException {
|
||||||
byte[] bytes = StreamUtils.copyToByteArray(body);
|
byte[] bytes = StreamUtils.copyToByteArray(body);
|
||||||
return new StreamSource(new ByteArrayInputStream(bytes));
|
return new StreamSource(new ByteArrayInputStream(bytes));
|
||||||
|
|
@ -194,21 +187,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,27 +16,24 @@
|
||||||
|
|
||||||
package org.springframework.http.converter.xml;
|
package org.springframework.http.converter.xml;
|
||||||
|
|
||||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
import static org.junit.Assert.assertNotEquals;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.stream.XMLStreamException;
|
|
||||||
import javax.xml.stream.XMLStreamReader;
|
|
||||||
import javax.xml.transform.Source;
|
import javax.xml.transform.Source;
|
||||||
import javax.xml.transform.dom.DOMSource;
|
import javax.xml.transform.dom.DOMSource;
|
||||||
import javax.xml.transform.sax.SAXSource;
|
import javax.xml.transform.sax.SAXSource;
|
||||||
import javax.xml.transform.stax.StAXSource;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
import javax.xml.transform.stream.StreamSource;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import org.xml.sax.XMLReader;
|
||||||
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
|
@ -44,12 +41,11 @@ import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.MockHttpInputMessage;
|
import org.springframework.http.MockHttpInputMessage;
|
||||||
import org.springframework.http.MockHttpOutputMessage;
|
import org.springframework.http.MockHttpOutputMessage;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||||
import org.xml.sax.InputSource;
|
import static org.junit.Assert.assertEquals;
|
||||||
import org.xml.sax.SAXException;
|
import static org.junit.Assert.*;
|
||||||
import org.xml.sax.XMLReader;
|
import static org.junit.Assert.assertTrue;
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
|
@ -62,16 +58,17 @@ public class SourceHttpMessageConverterTests {
|
||||||
|
|
||||||
private String bodyExternal;
|
private String bodyExternal;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws IOException {
|
public void setUp() throws IOException {
|
||||||
converter = new SourceHttpMessageConverter<Source>();
|
converter = new SourceHttpMessageConverter<Source>();
|
||||||
Resource external = new ClassPathResource("external.txt", getClass());
|
Resource external = new ClassPathResource("external.txt", getClass());
|
||||||
|
|
||||||
bodyExternal = "<!DOCTYPE root [" +
|
bodyExternal = "<!DOCTYPE root [" +
|
||||||
" <!ELEMENT root ANY >\n" +
|
" <!ELEMENT root ANY >\n" +
|
||||||
" <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]><root>&ext;</root>";
|
" <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]><root>&ext;</root>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canRead() {
|
public void canRead() {
|
||||||
assertTrue(converter.canRead(Source.class, new MediaType("application", "xml")));
|
assertTrue(converter.canRead(Source.class, new MediaType("application", "xml")));
|
||||||
|
|
@ -131,38 +128,6 @@ public class SourceHttpMessageConverterTests {
|
||||||
reader.parse(inputSource);
|
reader.parse(inputSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void readStAXSource() throws Exception {
|
|
||||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
|
|
||||||
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
|
|
||||||
StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
|
|
||||||
XMLStreamReader streamReader = result.getXMLStreamReader();
|
|
||||||
assertTrue(streamReader.hasNext());
|
|
||||||
streamReader.nextTag();
|
|
||||||
String s = streamReader.getLocalName();
|
|
||||||
assertEquals("root", s);
|
|
||||||
s = streamReader.getElementText();
|
|
||||||
assertEquals("Hello World", s);
|
|
||||||
streamReader.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void readStAXSourceExternal() throws Exception {
|
|
||||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes("UTF-8"));
|
|
||||||
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
|
|
||||||
StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
|
|
||||||
XMLStreamReader streamReader = result.getXMLStreamReader();
|
|
||||||
assertTrue(streamReader.hasNext());
|
|
||||||
streamReader.next();
|
|
||||||
streamReader.next();
|
|
||||||
String s = streamReader.getLocalName();
|
|
||||||
assertEquals("root", s);
|
|
||||||
s = streamReader.getElementText();
|
|
||||||
assertNotEquals("Foo Bar", s);
|
|
||||||
streamReader.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void readStreamSource() throws Exception {
|
public void readStreamSource() throws Exception {
|
||||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
|
||||||
|
|
@ -203,7 +168,6 @@ public class SourceHttpMessageConverterTests {
|
||||||
public void writeSAXSource() throws Exception {
|
public void writeSAXSource() throws Exception {
|
||||||
String xml = "<root>Hello World</root>";
|
String xml = "<root>Hello World</root>";
|
||||||
SAXSource saxSource = new SAXSource(new InputSource(new StringReader(xml)));
|
SAXSource saxSource = new SAXSource(new InputSource(new StringReader(xml)));
|
||||||
|
|
||||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||||
converter.write(saxSource, null, outputMessage);
|
converter.write(saxSource, null, outputMessage);
|
||||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||||
|
|
@ -216,7 +180,6 @@ public class SourceHttpMessageConverterTests {
|
||||||
public void writeStreamSource() throws Exception {
|
public void writeStreamSource() throws Exception {
|
||||||
String xml = "<root>Hello World</root>";
|
String xml = "<root>Hello World</root>";
|
||||||
StreamSource streamSource = new StreamSource(new StringReader(xml));
|
StreamSource streamSource = new StreamSource(new StringReader(xml));
|
||||||
|
|
||||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||||
converter.write(streamSource, null, outputMessage);
|
converter.write(streamSource, null, outputMessage);
|
||||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue