Top Description Inners Fields Constructors Methods
org.springframework.http.converter.xml

public Class SourceHttpMessageConverter<T extends Source>

extends AbstractHttpMessageConverter<T extends Source>
Class Inheritance
Type Parameters
<T>
the converted object type
Imports
java.io.ByteArrayInputStream, .IOException, .InputStream, .OutputStream, .StringReader, java.util.Set, javax.xml.parsers.DocumentBuilder, .DocumentBuilderFactory, .ParserConfigurationException, .SAXParser, .SAXParserFactory, javax.xml.stream.XMLInputFactory, .XMLResolver, .XMLStreamException, .XMLStreamReader, javax.xml.transform.Result, .Source, .TransformerException, .TransformerFactory, javax.xml.transform.dom.DOMSource, javax.xml.transform.sax.SAXSource, javax.xml.transform.stax.StAXSource, javax.xml.transform.stream.StreamResult, .StreamSource, org.w3c.dom.Document, org.xml.sax.EntityResolver, .InputSource, .SAXException, .XMLReader, org.springframework.http.HttpInputMessage, .HttpOutputMessage, .MediaType, org.springframework.http.converter.AbstractHttpMessageConverter, .HttpMessageNotReadableException, .HttpMessageNotWritableException, org.springframework.lang.Nullable, org.springframework.util.StreamUtils

Implementation of org.springframework.http.converter.HttpMessageConverter that can read and write Source objects.
Authors
Arjen Poutsma, Rossen Stoyanchev
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final EntityResolver
private static final XMLResolver
private boolean
private boolean
private static final Set<Class<?>>
private final TransformerFactory
Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
logger

Constructor Summary

AccessConstructor and Description
public
SourceHttpMessageConverter()

Sets the supportedMediaTypes to text/xml and application/xml, and application/*+xml.

Method Summary

Modifier and TypeMethod and Description
protected Long
getContentLength(T
the type to return the content length for
t
,
MediaType contentType)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Returns the content length for the given type.

public boolean
isProcessExternalEntities()

Return whether XML external entities are allowed.

public boolean
isSupportDtd()

Return whether DTD parsing is supported.

private DOMSource
protected T
readInternal(Class<? extends T>
the type of object to return
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object.

private SAXSource
private Source
private StreamSource
public void
setProcessExternalEntities(boolean processExternalEntities)

Indicate whether external XML entities are processed when converting to a Source.

public void
setSupportDtd(boolean supportDtd)

Indicate whether DTD parsing should be supported.

public boolean
supports(Class<?>
the class to test for support
clazz
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.

protected boolean
supportsRepeatableWrites(T
the object t
t
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.

private void
transform(Source source, Result result)

protected void
writeInternal(T
the object to write to the output message
t
,
HttpOutputMessage
the HTTP output message to write to
outputMessage
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body.

Inherited from org.springframework.http.converter.AbstractHttpMessageConverter:
addDefaultHeaderscanReadcanReadcanWritecanWritegetDefaultCharsetgetDefaultContentTypegetSupportedMediaTypesreadsetDefaultCharsetsetSupportedMediaTypeswrite

Field Detail

NO_OP_ENTITY_RESOLVERback to summary
private static final EntityResolver NO_OP_ENTITY_RESOLVER
NO_OP_XML_RESOLVERback to summary
private static final XMLResolver NO_OP_XML_RESOLVER
processExternalEntitiesback to summary
private boolean processExternalEntities
supportDtdback to summary
private boolean supportDtd
SUPPORTED_CLASSESback to summary
private static final Set<Class<?>> SUPPORTED_CLASSES
transformerFactoryback to summary
private final TransformerFactory transformerFactory

Constructor Detail

SourceHttpMessageConverterback to summary
public SourceHttpMessageConverter()

Sets the supportedMediaTypes to text/xml and application/xml, and application/*+xml.

Method Detail

getContentLengthback to summary
protected Long getContentLength(T t, MediaType contentType)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.getContentLength.

Returns the content length for the given type.

By default, this returns null, meaning that the content length is unknown. Can be overridden in subclasses.

Parameters
t:T

the type to return the content length for

Returns:Long

the content length, or null if not known

Annotations
@Override
@Nullable
isProcessExternalEntitiesback to summary
public boolean isProcessExternalEntities()

Return whether XML external entities are allowed.

isSupportDtdback to summary
public boolean isSupportDtd()

Return whether DTD parsing is supported.

readDOMSourceback to summary
private DOMSource readDOMSource(InputStream body, HttpInputMessage inputMessage) throws IOException
readInternalback to summary
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.readInternal.

Abstract template method that reads the actual object. Invoked from read.

Parameters
clazz:Class<? extends T>

the type of object to return

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:T

the converted object

Annotations
@Override
@SuppressWarnings:unchecked
Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
readSAXSourceback to summary
private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException
readStAXSourceback to summary
private Source readStAXSource(InputStream body, HttpInputMessage inputMessage)
readStreamSourceback to summary
private StreamSource readStreamSource(InputStream body) throws IOException
setProcessExternalEntitiesback to summary
public void setProcessExternalEntities(boolean processExternalEntities)

Indicate whether external XML entities are processed when converting to a Source.

Default is false, meaning that external entities are not resolved.

Note

setting this option to true also automatically sets setSupportDtd to true.

setSupportDtdback to summary
public void setSupportDtd(boolean supportDtd)

Indicate whether DTD parsing should be supported.

Default is false meaning that DTD is disabled.

supportsback to summary
public boolean supports(Class<?> clazz)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.

Parameters
clazz:Class<?>

the class to test for support

Returns:boolean

true if supported; false otherwise

Annotations
@Override
supportsRepeatableWritesback to summary
protected boolean supportsRepeatableWrites(T t)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supportsRepeatableWrites.

Indicates whether this message converter can write the given object multiple times.

Default implementation returns false.

Parameters
t:T

the object t

Returns:boolean

true if t can be written repeatedly; false otherwise

Annotations
@Override
transformback to summary
private void transform(Source source, Result result) throws TransformerException
writeInternalback to summary
protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.writeInternal.

Abstract template method that writes the actual body. Invoked from write.

Parameters
t:T

the object to write to the output message

outputMessage:HttpOutputMessage

the HTTP output message to write to

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotWritableException:
in case of conversion errors
org.springframework.http.converter.xml back to summary

private Class SourceHttpMessageConverter.CountingOutputStream

extends OutputStream
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
write(int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.

public void
write(byte[]
the data.
b
)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.

public void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

Inherited from java.io.OutputStream:
closeflushnullOutputStream

Field Detail

countback to summary
pack-priv long count

Constructor Detail

CountingOutputStreamback to summary
private CountingOutputStream()

Method Detail

writeback to summary
public void write(int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters
b:byte[]

the data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.