Class MessageBodyFactory
- java.lang.Object
-
- org.glassfish.jersey.message.internal.MessageBodyFactory
-
- All Implemented Interfaces:
MessageBodyWorkers
public class MessageBodyFactory extends Object implements MessageBodyWorkers
A factory for managingMessageBodyReader
,MessageBodyWriter
instances.- Author:
- Paul Sandoz, Marek Potociar, Jakub Podlesak
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MessageBodyFactory.MessageBodyWorkersConfigurator
Configurator which initializes and registerMessageBodyWorkers
instance intoInjectionManager
andBootstrapBag
.
-
Field Summary
Fields Modifier and Type Field Description static org.glassfish.jersey.internal.util.collection.KeyComparator<MediaType>
MEDIA_TYPE_KEY_COMPARATOR
Media type comparator.
-
Constructor Summary
Constructors Constructor Description MessageBodyFactory(Configuration configuration)
Create a new message body factory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> MessageBodyReader<T>
getMessageBodyReader(Class<T> c, Type t, Annotation[] as, MediaType mediaType)
Get a message body reader that matches a set of criteria.<T> MessageBodyReader<T>
getMessageBodyReader(Class<T> c, Type t, Annotation[] as, MediaType mediaType, PropertiesDelegate propertiesDelegate)
Get a message body reader that matches a set of criteria.List<MediaType>
getMessageBodyReaderMediaTypes(Class<?> type, Type genericType, Annotation[] annotations)
Get the list of media types supported for a Java type.List<MediaType>
getMessageBodyReaderMediaTypesByType(Class<?> type)
Get the list of media types supported for a Java type.List<MessageBodyReader>
getMessageBodyReadersForType(Class<?> type)
Get a list ofMessageBodyReader
s that are suitable for the giventype
.<T> MessageBodyWriter<T>
getMessageBodyWriter(Class<T> c, Type t, Annotation[] as, MediaType mediaType)
Get a message body writer that matches a set of criteria.<T> MessageBodyWriter<T>
getMessageBodyWriter(Class<T> c, Type t, Annotation[] as, MediaType mediaType, PropertiesDelegate propertiesDelegate)
Get a message body writer that matches a set of criteria.MediaType
getMessageBodyWriterMediaType(Class<?> c, Type t, Annotation[] as, List<MediaType> acceptableMediaTypes)
Get the most acceptable media type supported for a Java type given a set of acceptable media types.List<MediaType>
getMessageBodyWriterMediaTypes(Class<?> c, Type t, Annotation[] as)
Get the list of media types supported for a Java type.List<MediaType>
getMessageBodyWriterMediaTypesByType(Class<?> type)
Get the list of media types supported for a Java type.List<MessageBodyWriter>
getMessageBodyWritersForType(Class<?> type)
Get a list ofMessageBodyWriter
s that are suitable for the giventype
.List<ReaderModel>
getReaderModelsForType(Class<?> type)
Get a list ofMessageBodyReader
models that are suitable for the giventype
.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.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.List<WriterModel>
getWritersModelsForType(Class<?> type)
Get a list ofMessageBodyWriter
models that are suitable for the giventype
.void
initialize(org.glassfish.jersey.internal.inject.InjectionManager injectionManager)
Must be initialize at the time of completed populatedInjectionManager
.static boolean
isReadable(MessageBodyReader<?> provider, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
Safely invokesisReadable
method on the supplied provider.static boolean
isWriteable(MessageBodyWriter<?> provider, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
Safely invokesisWriteable
method on the supplied provider.String
readersToString(Map<MediaType,List<MessageBodyReader>> readers)
Convert a map media type to list of message body readers to a string.Object
readFrom(Class<?> rawType, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, PropertiesDelegate propertiesDelegate, InputStream entityStream, Iterable<ReaderInterceptor> readerInterceptors, boolean translateNce)
Reads a type from theentityStream
using interceptors.String
writersToString(Map<MediaType,List<MessageBodyWriter>> writers)
Convert a map media type to list of message body writers to a string.OutputStream
writeTo(Object t, Class<?> rawType, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, PropertiesDelegate propertiesDelegate, OutputStream entityStream, Iterable<WriterInterceptor> writerInterceptors)
Writers a type to theentityStream
using interceptors.
-
-
-
Field Detail
-
MEDIA_TYPE_KEY_COMPARATOR
public static final org.glassfish.jersey.internal.util.collection.KeyComparator<MediaType> MEDIA_TYPE_KEY_COMPARATOR
Media type comparator.
-
-
Constructor Detail
-
MessageBodyFactory
public MessageBodyFactory(Configuration configuration)
Create a new message body factory.- Parameters:
configuration
- configuration. Optional - can be null.
-
-
Method Detail
-
initialize
public void initialize(org.glassfish.jersey.internal.inject.InjectionManager injectionManager)
Must be initialize at the time of completed populatedInjectionManager
.- Parameters:
injectionManager
- completed injection manager.
-
getReaders
public Map<MediaType,List<MessageBodyReader>> getReaders(MediaType mediaType)
Description copied from interface:MessageBodyWorkers
Get the map of media type to list of message body writers that are compatible with a media type.- Specified by:
getReaders
in interfaceMessageBodyWorkers
- Parameters:
mediaType
- the compatible media type.- Returns:
- the map of media type to list of message body writers.
-
getWriters
public Map<MediaType,List<MessageBodyWriter>> getWriters(MediaType mediaType)
Description copied from interface:MessageBodyWorkers
Get the map of media type to list of message body writers that are compatible with a media type.- Specified by:
getWriters
in interfaceMessageBodyWorkers
- Parameters:
mediaType
- the compatible media type.- Returns:
- the map of media type to list of message body writers.
-
readersToString
public String readersToString(Map<MediaType,List<MessageBodyReader>> readers)
Description copied from interface:MessageBodyWorkers
Convert a map media type to list of message body readers to a string.- Specified by:
readersToString
in interfaceMessageBodyWorkers
- Parameters:
readers
- the map media type to list of message body readers- Returns:
- the string representation.
-
writersToString
public String writersToString(Map<MediaType,List<MessageBodyWriter>> writers)
Description copied from interface:MessageBodyWorkers
Convert a map media type to list of message body writers to a string.- Specified by:
writersToString
in interfaceMessageBodyWorkers
- Parameters:
writers
- the map media type to list of message body readers- Returns:
- the string representation.
-
getMessageBodyReader
public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> c, Type t, Annotation[] as, MediaType mediaType)
Description copied from interface:MessageBodyWorkers
Get a message body reader that matches a set of criteria.- Specified by:
getMessageBodyReader
in interfaceMessageBodyWorkers
- Type Parameters:
T
- the type of object to be read.- Parameters:
c
- the class of object to be read.t
- 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 byClass.getGenericParameterTypes
.as
- 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 byClass.getParameterAnnotations
.mediaType
- the media type of the data that will be read, this will be compared to the values ofConsumes
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.
-
getMessageBodyReader
public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> c, Type t, Annotation[] as, MediaType mediaType, PropertiesDelegate propertiesDelegate)
Description copied from interface:MessageBodyWorkers
Get a message body reader that matches a set of criteria.- Specified by:
getMessageBodyReader
in interfaceMessageBodyWorkers
- Type Parameters:
T
- the type of object to be read.- Parameters:
c
- the class of object to be read.t
- 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 byClass.getGenericParameterTypes
.as
- 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 byClass.getParameterAnnotations
.mediaType
- the media type of the data that will be read, this will be compared to the values ofConsumes
for each candidate reader and only matching readers will be queried.propertiesDelegate
- request-scoped properties delegate.- Returns:
- a MessageBodyReader that matches the supplied criteria or null if none is found.
-
getMessageBodyReaderMediaTypes
public List<MediaType> getMessageBodyReaderMediaTypes(Class<?> type, Type genericType, Annotation[] annotations)
Description copied from interface:MessageBodyWorkers
Get the list of media types supported for a Java type.- Specified by:
getMessageBodyReaderMediaTypes
in interfaceMessageBodyWorkers
- 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 byMethod.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 byMethod.getParameterAnnotations
.- Returns:
- the list of supported media types, the list is ordered as follows: a/b < a/* < *\\/*
-
getMessageBodyWriter
public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> c, Type t, Annotation[] as, MediaType mediaType)
Description copied from interface:MessageBodyWorkers
Get a message body writer that matches a set of criteria.- Specified by:
getMessageBodyWriter
in interfaceMessageBodyWorkers
- Type Parameters:
T
- the type of the object that is to be written.- Parameters:
c
- the class of object that is to be written.t
- 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 byField.getGenericType
.as
- 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 byField.getDeclaredAnnotations
.mediaType
- the media type of the data that will be written, this will be compared to the values ofProduces
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.
-
getMessageBodyWriter
public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> c, Type t, Annotation[] as, MediaType mediaType, PropertiesDelegate propertiesDelegate)
Description copied from interface:MessageBodyWorkers
Get a message body writer that matches a set of criteria.- Specified by:
getMessageBodyWriter
in interfaceMessageBodyWorkers
- Type Parameters:
T
- the type of the object that is to be written.- Parameters:
c
- the class of object that is to be written.t
- 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 byField.getGenericType
.as
- 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 byField.getDeclaredAnnotations
.mediaType
- the media type of the data that will be written, this will be compared to the values ofProduces
for each candidate writer and only matching writers will be queried.propertiesDelegate
- request-scoped properties delegate.- Returns:
- a MessageBodyReader that matches the supplied criteria or null if none is found.
-
getMessageBodyWriterMediaTypes
public List<MediaType> getMessageBodyWriterMediaTypes(Class<?> c, Type t, Annotation[] as)
Description copied from interface:MessageBodyWorkers
Get the list of media types supported for a Java type.- Specified by:
getMessageBodyWriterMediaTypes
in interfaceMessageBodyWorkers
- Parameters:
c
- the class of object that is to be written.t
- 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 byField.getGenericType
.as
- 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 byField.getDeclaredAnnotations
.- Returns:
- the list of supported media types, the list is ordered as follows: a/b < a/* < *\\/*
-
getMessageBodyWritersForType
public List<MessageBodyWriter> getMessageBodyWritersForType(Class<?> type)
Description copied from interface:MessageBodyWorkers
Get a list ofMessageBodyWriter
s that are suitable for the giventype
. The list is sorted based on the class hierarchy (most specific writers are first).- Specified by:
getMessageBodyWritersForType
in interfaceMessageBodyWorkers
- Parameters:
type
- the class of object writers are requested for.- Returns:
- the list of supported
MessageBodyWriter
s for given class.
-
getWritersModelsForType
public List<WriterModel> getWritersModelsForType(Class<?> type)
Description copied from interface:MessageBodyWorkers
Get a list ofMessageBodyWriter
models that are suitable for the giventype
. The list is sorted based on the class hierarchy (most specific writers are first).- Specified by:
getWritersModelsForType
in interfaceMessageBodyWorkers
- Parameters:
type
- the class of object writers are requested for.- Returns:
- the list of supported
MessageBodyWriter
models for given class.
-
getMessageBodyWriterMediaTypesByType
public List<MediaType> getMessageBodyWriterMediaTypesByType(Class<?> type)
Description copied from interface:MessageBodyWorkers
Get the list of media types supported for a Java type.- Specified by:
getMessageBodyWriterMediaTypesByType
in interfaceMessageBodyWorkers
- Parameters:
type
- the class of object that is to be written.- Returns:
- the list of supported media types, the list is ordered as follows: a/b < a/* < *\\/*
-
getMessageBodyReaderMediaTypesByType
public List<MediaType> getMessageBodyReaderMediaTypesByType(Class<?> type)
Description copied from interface:MessageBodyWorkers
Get the list of media types supported for a Java type.- Specified by:
getMessageBodyReaderMediaTypesByType
in interfaceMessageBodyWorkers
- Parameters:
type
- the class of object that is to be read.- Returns:
- the list of supported media types, the list is ordered as follows: a/b < a/* < *\\/*
-
getMessageBodyReadersForType
public List<MessageBodyReader> getMessageBodyReadersForType(Class<?> type)
Description copied from interface:MessageBodyWorkers
Get a list ofMessageBodyReader
s that are suitable for the giventype
. The list is sorted based on the class hierarchy (most specific readers are first).- Specified by:
getMessageBodyReadersForType
in interfaceMessageBodyWorkers
- Parameters:
type
- the class of object readers are requested for.- Returns:
- the list of supported
MessageBodyReader
s for given class.
-
getReaderModelsForType
public List<ReaderModel> getReaderModelsForType(Class<?> type)
Description copied from interface:MessageBodyWorkers
Get a list ofMessageBodyReader
models that are suitable for the giventype
. The list is sorted based on the class hierarchy (most specific readers are first).- Specified by:
getReaderModelsForType
in interfaceMessageBodyWorkers
- Parameters:
type
- the class of object readers are requested for.- Returns:
- the list of supported
MessageBodyReader
models for given class.
-
getMessageBodyWriterMediaType
public MediaType getMessageBodyWriterMediaType(Class<?> c, Type t, Annotation[] as, List<MediaType> acceptableMediaTypes)
Description copied from interface:MessageBodyWorkers
Get the most acceptable media type supported for a Java type given a set of acceptable media types.- Specified by:
getMessageBodyWriterMediaType
in interfaceMessageBodyWorkers
- Parameters:
c
- the class of object that is to be written.t
- 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 byField.getGenericType
.as
- 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 byField.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
-
readFrom
public Object readFrom(Class<?> rawType, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, PropertiesDelegate propertiesDelegate, InputStream entityStream, Iterable<ReaderInterceptor> readerInterceptors, boolean translateNce) throws WebApplicationException, IOException
Description copied from interface:MessageBodyWorkers
Reads a type from theentityStream
using interceptors. If the parameterintercept
is true thenreader interceptors
are executed before calling themessage body reader
. The appropriatemessage body reader
is chosen after the interceptor execution based on parameter passed to this method and modified by the interceptors.- Specified by:
readFrom
in interfaceMessageBodyWorkers
- 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 byMethod.getParameterAnnotations
.mediaType
- the media type of the HTTP entity.httpHeaders
- the mutable HTTP headers associated with HTTP entity.propertiesDelegate
- request-scoped properties delegate.entityStream
- theInputStream
of the HTTP entity. The stream is not closed after reading the entity.readerInterceptors
- Reader interceptor that are to be used to intercept the reading of an entity. The interceptors will be executed in the same order as given in this parameter.translateNce
- iftrue
, theNoContentException
thrown by a selected message body reader will be translated into aBadRequestException
as required by JAX-RS specification on the server side.- Returns:
- the entity that was read from the
entityStream
. - Throws:
WebApplicationException
- Thrown whenmessage body reader
fails.IOException
- Thrown when reading from theentityStream
fails.
-
writeTo
public OutputStream writeTo(Object t, Class<?> rawType, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, PropertiesDelegate propertiesDelegate, OutputStream entityStream, Iterable<WriterInterceptor> writerInterceptors) throws IOException, WebApplicationException
Description copied from interface:MessageBodyWorkers
Writers a type to theentityStream
using interceptors. If the parameterintercept
is true thenwriter interceptors
are executed before calling themessage body writer
. The appropriatemessage body writer
is chosen after the interceptor execution based on parameter passed to this method and modified by the interceptors.- Specified by:
writeTo
in interfaceMessageBodyWorkers
- Parameters:
t
- Entity to be written to the entityStreamrawType
- 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
- theOutputStream
for the HTTP entity.writerInterceptors
- Writer interceptor that are to be used to intercept the writing of an entity. The interceptors will be executed in the same order as given in this parameter.- Returns:
- Outer output stream that should be closed by the caller.
- Throws:
IOException
- Thrown when reading from theentityStream
fails.WebApplicationException
- Thrown whenmessage body reader
fails.
-
isWriteable
public static boolean isWriteable(MessageBodyWriter<?> provider, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
Safely invokesisWriteable
method on the supplied provider. Any exceptions will be logged at finer level.- Parameters:
provider
- message body writer on which theisWriteable
should be invoked.type
- the class of instance that is to be written.genericType
- the type of instance to be written, obtained either by reflection of a resource method return type or via inspection of the returned instance.GenericEntity
provides a way to specify this information at runtime.annotations
- an array of the annotations attached to the message entity instance.mediaType
- the media type of the HTTP entity.- Returns:
true
if the type is supported, otherwisefalse
.
-
isReadable
public static boolean isReadable(MessageBodyReader<?> provider, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
Safely invokesisReadable
method on the supplied provider. Any exceptions will be logged at finer level.- Parameters:
provider
- message body reader on which theisReadable
should be invoked. Safely invokesisReadable
method on the underlying provider.type
- the class of instance to be produced.genericType
- the type of instance 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 byMethod.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 byMethod.getParameterAnnotations
.mediaType
- the media type of the HTTP entity, if one is not specified in the request thenapplication/octet-stream
is used.- Returns:
true
if the type is supported, otherwisefalse
.
-
-