org.glassfish.jersey.message
Interface MessageBodyWorkers

All Known Implementing Classes:
MessageBodyFactory

public interface MessageBodyWorkers

An injectable interface providing lookup of MessageBodyReader and MessageBodyWriter instances.

Author:
Paul Sandoz
See Also:
Context, MessageBodyReader, MessageBodyWriter

Nested Class Summary
static interface MessageBodyWorkers.MessageBodySizeCallback
          Callback which will be used to pass back the size of the entity.
 
Method Summary
<T> MessageBodyReader<T>
getMessageBodyReader(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType)
          Get a message body reader that matches a set of criteria.
<T> List<MediaType>
getMessageBodyReaderMediaTypes(Class<T> type, Type genericType, Annotation[] annotations)
          Get the list of media types supported for a Java type.
<T> MessageBodyWriter<T>
getMessageBodyWriter(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType)
          Get a message body writer that matches a set of criteria.
<T> MediaType
getMessageBodyWriterMediaType(Class<T> type, Type genericType, Annotation[] annotations, List<MediaType> acceptableMediaTypes)
          Get the most acceptable media type supported for a Java type given a set of acceptable media types.
<T> List<MediaType>
getMessageBodyWriterMediaTypes(Class<T> type, Type genericType, Annotation[] annotations)
          Get the list of media types supported for a Java type.
 List<ReaderInterceptor> getReaderInterceptors()
          Returns global reader interceptors.
 Map<MediaType,List<MessageBodyReader>> getReaders(MediaType mediaType)
          Get the map of media type to list of message body writers that are compatible with a media type.
 List<WriterInterceptor> getWriterInterceptors()
          Returns global writer interceptors.
 Map<MediaType,List<MessageBodyWriter>> getWriters(MediaType mediaType)
          Get the map of media type to list of message body writers that are compatible with a media type.
 String readersToString(Map<MediaType,List<MessageBodyReader>> readers)
          Convert a map media type to list of message body readers to a string.
<T> Object
readFrom(Class<T> rawType, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, PropertiesDelegate propertiesDelegate, InputStream entityStream, boolean intercept)
          Reads a type from the entityStream using interceptors.
 String writersToString(Map<MediaType,List<MessageBodyWriter>> writers)
          Convert a map media type to list of message body writers to a string.
<T> OutputStream
writeTo(Object entity, Class<T> rawType, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, PropertiesDelegate propertiesDelegate, OutputStream entityStream, MessageBodyWorkers.MessageBodySizeCallback sizeCallback, boolean intercept)
          Writers a type to the entityStream using interceptors.
<T> OutputStream
writeTo(Object entity, Class<T> rawType, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, PropertiesDelegate propertiesDelegate, OutputStream entityStream, MessageBodyWorkers.MessageBodySizeCallback sizeCallback, boolean intercept, boolean writeEntity)
          Writers a type to the entityStream using interceptors.
 

Method Detail

getReaders

Map<MediaType,List<MessageBodyReader>> getReaders(MediaType mediaType)
Get the map of media type to list of message body writers that are compatible with a media type.

Parameters:
mediaType - the compatible media type.
Returns:
the map of media type to list of message body writers.

getWriters

Map<MediaType,List<MessageBodyWriter>> getWriters(MediaType mediaType)
Get the map of media type to list of message body writers that are compatible with a media type.

Parameters:
mediaType - the compatible media type.
Returns:
the map of media type to list of message body writers.

readersToString

String readersToString(Map<MediaType,List<MessageBodyReader>> readers)
Convert a map media type to list of message body readers to a string.

Parameters:
readers - the map media type to list of message body readers
Returns:
the string representation.

writersToString

String writersToString(Map<MediaType,List<MessageBodyWriter>> writers)
Convert a map media type to list of message body writers to a string.

Parameters:
writers - the map media type to list of message body readers
Returns:
the string representation.

getMessageBodyReader

<T> MessageBodyReader<T> getMessageBodyReader(Class<T> type,
                                              Type genericType,
                                              Annotation[] annotations,
                                              MediaType mediaType)
Get a message body reader that matches a set of criteria.

Type Parameters:
T - the type of object to be read.
Parameters:
type - the class of object to be read.
genericType - the type of object to be produced. E.g. if the message body is to be converted into a method parameter, this will be the formal type of the method parameter as returned by Class.getGenericParameterTypes.
annotations - an array of the annotations on the declaration of the artifact that will be initialized with the produced instance. E.g. if the message body is to be converted into a method parameter, this will be the annotations on that parameter returned by Class.getParameterAnnotations.
mediaType - the media type of the data that will be read, this will be compared to the values of Consumes for each candidate reader and only matching readers will be queried.
Returns:
a MessageBodyReader that matches the supplied criteria or null if none is found.

getMessageBodyWriter

<T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type,
                                              Type genericType,
                                              Annotation[] annotations,
                                              MediaType mediaType)
Get a message body writer that matches a set of criteria.

Type Parameters:
T - the type of the object that is to be written.
Parameters:
type - the class of object that is to be written.
genericType - the type of object to be written. E.g. if the message body is to be produced from a field, this will be the declared type of the field as returned by Field.getGenericType.
annotations - an array of the annotations on the declaration of the artifact that will be written. E.g. if the message body is to be produced from a field, this will be the annotations on that field returned by Field.getDeclaredAnnotations.
mediaType - the media type of the data that will be written, this will be compared to the values of Produces for each candidate writer and only matching writers will be queried.
Returns:
a MessageBodyReader that matches the supplied criteria or null if none is found.

getMessageBodyReaderMediaTypes

<T> List<MediaType> getMessageBodyReaderMediaTypes(Class<T> type,
                                                   Type genericType,
                                                   Annotation[] annotations)
Get the list of media types supported for a Java type.

Type Parameters:
T - the type of object that is to be read.
Parameters:
type - the class of object that is to be read.
genericType - the type of object to be read. E.g. if the message body is to be read as a method parameter, this will be the declared type of the parameter as returned by Method.getGenericParameterTypes.
annotations - an array of the annotations on the declaration of the artifact that will be read. E.g. if the message body is to be consumed as a method parameter, this will be the annotations on that parameter returned by Method.getParameterAnnotations.
Returns:
the list of supported media types, the list is ordered as follows: a/b < a/* < \*\/*

getMessageBodyWriterMediaTypes

<T> List<MediaType> getMessageBodyWriterMediaTypes(Class<T> type,
                                                   Type genericType,
                                                   Annotation[] annotations)
Get the list of media types supported for a Java type.

Type Parameters:
T - the type of object that is to be written.
Parameters:
type - the class of object that is to be written.
genericType - the type of object to be written. E.g. if the message body is to be produced from a field, this will be the declared type of the field as returned by Field.getGenericType.
annotations - an array of the annotations on the declaration of the artifact that will be written. E.g. if the message body is to be produced from a field, this will be the annotations on that field returned by Field.getDeclaredAnnotations.
Returns:
the list of supported media types, the list is ordered as follows: a/b < a/* < \*\/*

getMessageBodyWriterMediaType

<T> MediaType getMessageBodyWriterMediaType(Class<T> type,
                                            Type genericType,
                                            Annotation[] annotations,
                                            List<MediaType> acceptableMediaTypes)
Get the most acceptable media type supported for a Java type given a set of acceptable media types.

Type Parameters:
T - the type of object that is to be written.
Parameters:
type - the class of object that is to be written.
genericType - the type of object to be written. E.g. if the message body is to be produced from a field, this will be the declared type of the field as returned by Field.getGenericType.
annotations - an array of the annotations on the declaration of the artifact that will be written. E.g. if the message body is to be produced from a field, this will be the annotations on that field returned by Field.getDeclaredAnnotations.
acceptableMediaTypes - the list of acceptable media types, sorted according to the quality with the media type of highest quality occurring first first.
Returns:
the best media types

getReaderInterceptors

List<ReaderInterceptor> getReaderInterceptors()
Returns global reader interceptors.

Returns:
Reader interceptors.

getWriterInterceptors

List<WriterInterceptor> getWriterInterceptors()
Returns global writer interceptors.

Returns:
Writer interceptors.

readFrom

<T> Object readFrom(Class<T> rawType,
                    Type type,
                    Annotation[] annotations,
                    MediaType mediaType,
                    MultivaluedMap<String,String> httpHeaders,
                    PropertiesDelegate propertiesDelegate,
                    InputStream entityStream,
                    boolean intercept)
                throws WebApplicationException,
                       IOException
Reads a type from the entityStream using interceptors. If the parameter intercept is true then reader interceptors are excecuted before calling the message body reader. The appropriate message body reader is choosen after the interceptor execution based on parameter passed to this method and modified by the interceptors.

Parameters:
rawType - raw Java entity type.
type - generic Java entity type.
annotations - an array of the annotations on the declaration of the artifact that will be initialized with the produced instance. E.g. if the message body is to be converted into a method parameter, this will be the annotations on that parameter returned by Method.getParameterAnnotations.
mediaType - the media type of the HTTP entity.
httpHeaders - the mutable HTTP headers associated with HTTP entity.
propertiesDelegate - request-scoped properties delegate.
entityStream - the InputStream of the HTTP entity. The stream is not closed after reading the entity.
intercept - true if the user interceptors should be executed. Otherwise only exception wrapping interceptor will be executed in the client.
Returns:
the entity that was read from the entityStream.
Throws:
WebApplicationException - Thrown when message body reader fails.
IOException - Thrown when reading from the entityStream fails.

writeTo

<T> OutputStream writeTo(Object entity,
                         Class<T> rawType,
                         Type type,
                         Annotation[] annotations,
                         MediaType mediaType,
                         MultivaluedMap<String,Object> httpHeaders,
                         PropertiesDelegate propertiesDelegate,
                         OutputStream entityStream,
                         MessageBodyWorkers.MessageBodySizeCallback sizeCallback,
                         boolean intercept)
                     throws IOException,
                            WebApplicationException
Writers a type to the entityStream using interceptors. If the parameter intercept is true then writer interceptors are excecuted before calling the message body writer. The appropriate message body writer is choosen after the interceptor execution based on parameter passed to this method and modified by the interceptors.

Parameters:
entity - Entity to be written to the entityStream
rawType - raw Java entity type.
type - generic Java entity type.
annotations - an array of the annotations on the resource method that returns the object.
mediaType - the media type of the HTTP entity.
httpHeaders - the mutable HTTP headers associated with HTTP entity.
propertiesDelegate - request-scoped properties delegate.
entityStream - the OutputStream for the HTTP entity.
sizeCallback - the MessageBodyWorkers.MessageBodySizeCallback which will be invoked to pass the size of the written entity. The callback will be invoked before the first byte is written to the entityStream.
intercept - true if the user interceptors should be executed. Otherwise only exception wrapping interceptor will be executed in the client.
Returns:
Outer output stream that should be closed by the caller.
Throws:
WebApplicationException - Thrown when message body reader fails.
IOException - Thrown when reading from the entityStream fails.

writeTo

<T> OutputStream writeTo(Object entity,
                         Class<T> rawType,
                         Type type,
                         Annotation[] annotations,
                         MediaType mediaType,
                         MultivaluedMap<String,Object> httpHeaders,
                         PropertiesDelegate propertiesDelegate,
                         OutputStream entityStream,
                         MessageBodyWorkers.MessageBodySizeCallback sizeCallback,
                         boolean intercept,
                         boolean writeEntity)
                     throws IOException,
                            WebApplicationException
Writers a type to the entityStream using interceptors. If the parameter intercept is true then writer interceptors are excecuted before calling the message body writer. The appropriate message body writer is chosen after the interceptor execution based on parameter passed to this method and modified by the interceptors.

Parameters:
entity - Entity to be written to the entityStream
rawType - raw Java entity type.
type - generic Java entity type.
annotations - an array of the annotations on the resource method that returns the object.
mediaType - the media type of the HTTP entity.
httpHeaders - the mutable HTTP headers associated with HTTP entity.
propertiesDelegate - request-scoped properties delegate.
entityStream - the OutputStream for the HTTP entity.
sizeCallback - the MessageBodyWorkers.MessageBodySizeCallback which will be invoked to pass the size of the written entity. The callback will be invoked before the first byte is written to the entityStream.
intercept - true if the user interceptors should be executed. Otherwise only exception wrapping interceptor will be executed in the client.
writeEntity - true if the entity should be written. Otherwise only headers will be written to underlying OutputStream.
Returns:
Outer output stream that should be closed by the caller.
Throws:
WebApplicationException - Thrown when message body reader fails.
IOException - Thrown when reading from the entityStream fails.


Copyright © 2007-2012 Oracle Corporation. All Rights Reserved. Use is subject to license terms.