org.springframework.http.converter
Class AbstractHttpMessageConverter<T>

java.lang.Object
  extended by org.springframework.http.converter.AbstractHttpMessageConverter<T>
All Implemented Interfaces:
HttpMessageConverter<T>
Direct Known Subclasses:
AbstractXmlHttpMessageConverter, BufferedImageHttpMessageConverter, ByteArrayHttpMessageConverter, FormHttpMessageConverter, MappingJacksonHttpMessageConverter, StringHttpMessageConverter

public abstract class AbstractHttpMessageConverter<T>
extends Object
implements HttpMessageConverter<T>

Abstract base class for most HttpMessageConverter implementations.

This base class adds support for setting supported MediaTypes, through the supportedMediaTypes bean property. It also adds support for Content-Type and Content-Length when writing to output messages.

Since:
3.0
Author:
Arjen Poutsma

Field Summary
protected  Log logger
          Logger available to subclasses
 
Constructor Summary
protected AbstractHttpMessageConverter()
          Construct an AbstractHttpMessageConverter with no supported media types.
protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
          Construct an AbstractHttpMessageConverter with multiple supported media type.
protected AbstractHttpMessageConverter(MediaType supportedMediaType)
          Construct an AbstractHttpMessageConverter with one supported media type.
 
Method Summary
protected  Long getContentLength(T t)
          Returns the content length for the given type.
protected  MediaType getContentType(T t)
          Returns the content type for the given type.
 List<MediaType> getSupportedMediaTypes()
          Return the list of MediaType objects supported by this converter.
 T read(Class<T> clazz, HttpInputMessage inputMessage)
          Read an object of the given type form the given input message, and returns it.
protected abstract  T readInternal(Class<T> clazz, HttpInputMessage inputMessage)
          Abstract template method that reads the actualy object.
 void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
          Set the list of MediaType objects supported by this converter.
 void write(T t, HttpOutputMessage outputMessage)
          Write an given object to the given output message.
protected abstract  void writeInternal(T t, HttpOutputMessage outputMessage)
          Abstract template method that writes the actual body.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
supports
 

Field Detail

logger

protected final Log logger
Logger available to subclasses

Constructor Detail

AbstractHttpMessageConverter

protected AbstractHttpMessageConverter()
Construct an AbstractHttpMessageConverter with no supported media types.

See Also:
setSupportedMediaTypes(java.util.List)

AbstractHttpMessageConverter

protected AbstractHttpMessageConverter(MediaType supportedMediaType)
Construct an AbstractHttpMessageConverter with one supported media type.


AbstractHttpMessageConverter

protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
Construct an AbstractHttpMessageConverter with multiple supported media type.

Method Detail

setSupportedMediaTypes

public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
Set the list of MediaType objects supported by this converter.


getSupportedMediaTypes

public List<MediaType> getSupportedMediaTypes()
Description copied from interface: HttpMessageConverter
Return the list of MediaType objects supported by this converter.

Specified by:
getSupportedMediaTypes in interface HttpMessageConverter<T>

read

public final T read(Class<T> clazz,
                    HttpInputMessage inputMessage)
             throws IOException
Read an object of the given type form the given input message, and returns it.

This implementation simple delegates to readInternal(Class, HttpInputMessage). Future implementations might add some default behavior, however.

Specified by:
read in interface HttpMessageConverter<T>
Parameters:
clazz - the type of object to return
inputMessage - the HTTP input message to read from
Returns:
the converted object
Throws:
IOException - in case of I/O errors

readInternal

protected abstract T readInternal(Class<T> clazz,
                                  HttpInputMessage inputMessage)
                           throws IOException,
                                  HttpMessageNotReadableException
Abstract template method that reads the actualy object. Invoked from read(Class, HttpInputMessage).

Parameters:
clazz - the type of object to return
inputMessage - the HTTP input message to read from
Returns:
the converted object
Throws:
IOException - in case of I/O errors
HttpMessageNotReadableException - in case of conversion errors

write

public final void write(T t,
                        HttpOutputMessage outputMessage)
                 throws IOException
Write an given object to the given output message.

This implementation delegates to getContentType(Object) and getContentLength(Object), and sets the corresponding headers on the output message. It then calls writeInternal(Object, HttpOutputMessage).

Specified by:
write in interface HttpMessageConverter<T>
Parameters:
t - the object to write to the output message
outputMessage - the message to write to
Throws:
IOException - in case of I/O errors

getContentType

protected MediaType getContentType(T t)
Returns the content type for the given type.

By default, this returns the first element of the supportedMediaTypes property, if any. Can be overriden in subclasses.

Parameters:
t - the type to return the content type for
Returns:
the content type, or null if not known

getContentLength

protected Long getContentLength(T t)
Returns the content length for the given type.

By default, this returns null. Can be overriden in subclasses.

Parameters:
t - the type to return the content length for
Returns:
the content length, or null if not known

writeInternal

protected abstract void writeInternal(T t,
                                      HttpOutputMessage outputMessage)
                               throws IOException,
                                      HttpMessageNotWritableException
Abstract template method that writes the actual body. Invoked from write(Object, HttpOutputMessage).

Parameters:
t - the object to write to the output message
outputMessage - the message to write to
Throws:
IOException - in case of I/O errors
HttpMessageNotWritableException - in case of conversion errors