org.springframework.http.converter
Class ResourceHttpMessageConverter

java.lang.Object
  extended by org.springframework.http.converter.ResourceHttpMessageConverter
All Implemented Interfaces:
HttpMessageConverter<Resource>

public class ResourceHttpMessageConverter
extends Object
implements HttpMessageConverter<Resource>

Implementation of HttpMessageConverter that can read and write Resources.

By default, this converter can read all media types. The Java Activation Framework (JAF) - if available - is used to determine the Content-Type of written resources. If JAF is not available, application/octet-stream is used.

Since:
3.0.2
Author:
Arjen Poutsma

Constructor Summary
ResourceHttpMessageConverter()
           
 
Method Summary
 boolean canRead(Class<?> clazz, MediaType mediaType)
          Indicates whether the given class can be read by this converter.
 boolean canWrite(Class<?> clazz, MediaType mediaType)
          Indicates whether the given class can be written by this converter.
protected  Long getContentLength(Resource resource, MediaType contentType)
           
 List<MediaType> getSupportedMediaTypes()
          Return the list of MediaType objects supported by this converter.
 Resource read(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
          Read an object of the given type form the given input message, and returns it.
 void write(Resource resource, MediaType contentType, HttpOutputMessage outputMessage)
          Write an given object to the given output message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceHttpMessageConverter

public ResourceHttpMessageConverter()
Method Detail

canRead

public boolean canRead(Class<?> clazz,
                       MediaType mediaType)
Description copied from interface: HttpMessageConverter
Indicates whether the given class can be read by this converter.

Specified by:
canRead in interface HttpMessageConverter<Resource>
Parameters:
clazz - the class to test for readability
mediaType - the media type to read, can be null if not specified. Typically the value of a Content-Type header.
Returns:
true if readable; false otherwise

canWrite

public boolean canWrite(Class<?> clazz,
                        MediaType mediaType)
Description copied from interface: HttpMessageConverter
Indicates whether the given class can be written by this converter.

Specified by:
canWrite in interface HttpMessageConverter<Resource>
Parameters:
clazz - the class to test for writability
mediaType - the media type to write, can be null if not specified. Typically the value of an Accept header.
Returns:
true if writable; false otherwise

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<Resource>
Returns:
the list of supported media types

read

public Resource read(Class<? extends Resource> clazz,
                     HttpInputMessage inputMessage)
              throws IOException,
                     HttpMessageNotReadableException
Description copied from interface: HttpMessageConverter
Read an object of the given type form the given input message, and returns it.

Specified by:
read in interface HttpMessageConverter<Resource>
Parameters:
clazz - the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
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 void write(Resource resource,
                  MediaType contentType,
                  HttpOutputMessage outputMessage)
           throws IOException,
                  HttpMessageNotWritableException
Description copied from interface: HttpMessageConverter
Write an given object to the given output message.

Specified by:
write in interface HttpMessageConverter<Resource>
Parameters:
resource - the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.
contentType - the content type to use when writing. May be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.
outputMessage - the message to write to
Throws:
IOException - in case of I/O errors
HttpMessageNotWritableException - in case of conversion errors

getContentLength

protected Long getContentLength(Resource resource,
                                MediaType contentType)
                         throws IOException
Throws:
IOException