public abstract class ProviderBase<THIS extends ProviderBase<THIS,MAPPER,EP_CONFIG,MAPPER_CONFIG>,MAPPER extends ObjectMapper,EP_CONFIG extends EndpointConfigBase<EP_CONFIG>,MAPPER_CONFIG extends MapperConfiguratorBase<MAPPER_CONFIG,MAPPER>> extends Object implements MessageBodyReader<Object>, MessageBodyWriter<Object>, Versioned
Modifier and Type | Field and Description |
---|---|
protected boolean |
_cfgCheckCanDeserialize
Whether we want to actually check that Jackson has
a deserializer for given type.
|
protected boolean |
_cfgCheckCanSerialize
Whether we want to actually check that Jackson has
a serializer for given type.
|
protected HashMap<ClassKey,Boolean> |
_cfgCustomUntouchables
Map that contains overrides to default list of untouchable
types:
true meaning that entry is untouchable,
false that is is not. |
protected Class<?> |
_defaultReadView
View to use for reading if none defined for the end point.
|
protected Class<?> |
_defaultWriteView
View to use for writing if none defined for the end point.
|
protected int |
_jaxRSFeatures
Feature flags set.
|
protected MAPPER_CONFIG |
_mapperConfig
Helper object used for encapsulating configuration aspects
of
ObjectMapper |
protected AtomicReference<IOException> |
_noContentExceptionRef |
protected LRUMap<AnnotationBundleKey,EP_CONFIG> |
_readers
Cache for resolved endpoint configurations when reading JSON data
|
static Class<?>[] |
_unreadableClasses |
static HashSet<ClassKey> |
_untouchables |
static Class<?>[] |
_unwritableClasses |
protected LRUMap<AnnotationBundleKey,EP_CONFIG> |
_writers
Cache for resolved endpoint configurations when writing JSON data
|
protected static String |
CLASS_NAME_NO_CONTENT_EXCEPTION |
static Class<?>[] |
DEFAULT_UNREADABLES
These are classes that we never use for reading
(never try to deserialize instances of these types).
|
protected static HashSet<ClassKey> |
DEFAULT_UNTOUCHABLES
Looks like we need to worry about accidental
data binding for types we shouldn't be handling.
|
static Class<?>[] |
DEFAULT_UNWRITABLES
These are classes that we never use for writing
(never try to serialize instances of these types).
|
static String |
HEADER_CONTENT_TYPE_OPTIONS
This header is useful on Windows, trying to deal with potential XSS attacks.
|
protected static int |
JAXRS_FEATURE_DEFAULTS |
protected NoContentExceptionSupplier |
noContentExceptionSupplier |
Modifier | Constructor and Description |
---|---|
protected |
ProviderBase()
Deprecated.
|
protected |
ProviderBase(MAPPER_CONFIG mconfig) |
Modifier and Type | Method and Description |
---|---|
protected static void |
_addSuperTypes(Class<?> cls,
Class<?> endBefore,
Collection<Class<?>> result,
boolean addClassItself) |
protected EP_CONFIG |
_configForReading(MAPPER mapper,
Annotation[] annotations,
Class<?> defaultView) |
protected abstract EP_CONFIG |
_configForReading(ObjectReader reader,
Annotation[] annotations) |
protected EP_CONFIG |
_configForWriting(MAPPER mapper,
Annotation[] annotations,
Class<?> defaultView) |
protected abstract EP_CONFIG |
_configForWriting(ObjectWriter writer,
Annotation[] annotations) |
protected static boolean |
_containedIn(Class<?> mainType,
HashSet<ClassKey> set) |
protected JsonGenerator |
_createGenerator(ObjectWriter writer,
OutputStream rawStream,
JsonEncoding enc)
Overridable helper method called to create a
JsonGenerator for writing
contents into given raw OutputStream . |
protected IOException |
_createNoContentException() |
protected JsonParser |
_createParser(ObjectReader reader,
InputStream rawStream)
Overridable helper method called to create a
JsonParser for reading
contents of given raw InputStream . |
protected EP_CONFIG |
_endpointForReading(Class<Object> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String,String> httpHeaders)
Overridable helper method that will basically fetch representation of the
endpoint that can be used to get
ObjectReader to use for deserializing
content |
protected EP_CONFIG |
_endpointForWriting(Object value,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders) |
protected Boolean |
_findCustomUntouchable(Class<?> mainType) |
protected boolean |
_isIgnorableForReading(ClassKey typeKey)
Overridable helper method called to check whether given type is a known
"ignorable type" (in context of reading), values of which are not bound
from content.
|
protected boolean |
_isIgnorableForWriting(ClassKey typeKey)
Overridable helper method called to check whether given type is a known
"ignorable type" (in context of reading), values of which
can not be written out.
|
protected boolean |
_isSpecialReadable(Class<?> type)
Overridable helper method used to allow handling of somewhat special
types for reading
|
protected abstract MAPPER |
_locateMapperViaProvider(Class<?> type,
MediaType mediaType)
Helper method that is called if no mapper has been explicitly configured.
|
protected void |
_modifyHeaders(Object value,
Class<?> type,
Type genericType,
Annotation[] annotations,
MultivaluedMap<String,Object> httpHeaders,
EP_CONFIG endpoint)
Overridable method used for adding optional response headers before
serializing response object.
|
void |
addUntouchable(Class<?> type)
Method for marking specified type as "untouchable", meaning that provider
will not try to read or write values of this type (or its subtypes).
|
void |
checkCanDeserialize(boolean state)
Method for defining whether actual detection for existence of
a deserializer for type should be done when
isReadable(java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
is called. |
void |
checkCanSerialize(boolean state)
Method for defining whether actual detection for existence of
a serializer for type should be done when
isWriteable(java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
is called. |
THIS |
configure(DeserializationFeature f,
boolean state) |
THIS |
configure(JaxRSFeature feature,
boolean state) |
THIS |
configure(JsonGenerator.Feature f,
boolean state) |
THIS |
configure(JsonParser.Feature f,
boolean state) |
THIS |
configure(SerializationFeature f,
boolean state) |
THIS |
disable(DeserializationFeature f) |
THIS |
disable(JaxRSFeature feature) |
THIS |
disable(JaxRSFeature first,
JaxRSFeature... f2) |
THIS |
disable(JsonGenerator.Feature f) |
THIS |
disable(JsonParser.Feature f) |
THIS |
disable(SerializationFeature f) |
THIS |
enable(DeserializationFeature f) |
THIS |
enable(JaxRSFeature feature) |
THIS |
enable(JaxRSFeature first,
JaxRSFeature... f2) |
THIS |
enable(JsonGenerator.Feature f) |
THIS |
enable(JsonParser.Feature f) |
THIS |
enable(SerializationFeature f) |
protected JsonEncoding |
findEncoding(MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders)
Helper method to use for determining desired output encoding.
|
protected static List<Class<?>> |
findSuperTypes(Class<?> cls,
Class<?> endBefore) |
protected static List<Class<?>> |
findSuperTypes(Class<?> cls,
Class<?> endBefore,
List<Class<?>> result) |
long |
getSize(Object value,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType)
Method that JAX-RS container calls to try to figure out
serialized length of given value.
|
protected abstract boolean |
hasMatchingMediaType(MediaType mediaType)
Helper method used to check whether given media type
is supported by this provider.
|
protected boolean |
hasMatchingMediaTypeForReading(MediaType mediaType)
Helper method used to check whether given media type
is supported by this provider for read operations
(when binding input data such as POST body).
|
protected boolean |
hasMatchingMediaTypeForWriting(MediaType mediaType)
Helper method used to check whether given media type
is supported by this provider for writing operations,
such as when converting response object to response
body of request (like GET or POST).
|
boolean |
isEnabled(JaxRSFeature f) |
boolean |
isReadable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType)
Method that JAX-RS container calls to try to check whether
values of given type (and media type) can be deserialized by
this provider.
|
boolean |
isWriteable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType)
Method that JAX-RS container calls to try to check whether
given value (of specified type) can be serialized by
this provider.
|
MAPPER |
locateMapper(Class<?> type,
MediaType mediaType)
Method called to locate
ObjectMapper to use for serialization
and deserialization. |
Object |
readFrom(Class<Object> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String,String> httpHeaders,
InputStream entityStream)
Method that JAX-RS container calls to deserialize given value.
|
void |
removeUntouchable(Class<?> type)
Method for removing definition of specified type as untouchable:
usually only
|
void |
setAnnotationsToUse(Annotations[] annotationsToUse)
Method for configuring which annotation sets to use (including none).
|
THIS |
setDefaultReadView(Class<?> view)
Method for specifying JSON View to use for reading content
when end point does not have explicit View annotations.
|
THIS |
setDefaultView(Class<?> view)
Method for specifying JSON View to use for reading and writing content
when end point does not have explicit View annotations.
|
THIS |
setDefaultWriteView(Class<?> view)
Method for specifying JSON View to use for reading content
when end point does not have explicit View annotations.
|
void |
setMapper(MAPPER m)
Method that can be used to directly define
ObjectMapper to use
for serialization and deserialization; if null, will use the standard
provider discovery from context instead. |
void |
writeTo(Object value,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders,
OutputStream entityStream)
Method that JAX-RS container calls to serialize given value.
|
public static final String HEADER_CONTENT_TYPE_OPTIONS
protected static final String CLASS_NAME_NO_CONTENT_EXCEPTION
protected final NoContentExceptionSupplier noContentExceptionSupplier
protected static final HashSet<ClassKey> DEFAULT_UNTOUCHABLES
(why ClassKey? since plain old Class has no hashCode() defined, lookups are painfully slow)
public static final Class<?>[] DEFAULT_UNREADABLES
public static final Class<?>[] DEFAULT_UNWRITABLES
protected static final int JAXRS_FEATURE_DEFAULTS
protected final MAPPER_CONFIG extends MapperConfiguratorBase<MAPPER_CONFIG,MAPPER> _mapperConfig
ObjectMapper
protected HashMap<ClassKey,Boolean> _cfgCustomUntouchables
true
meaning that entry is untouchable,
false
that is is not.protected boolean _cfgCheckCanSerialize
protected boolean _cfgCheckCanDeserialize
protected int _jaxRSFeatures
protected Class<?> _defaultReadView
protected Class<?> _defaultWriteView
public static final Class<?>[] _unreadableClasses
public static final Class<?>[] _unwritableClasses
protected final LRUMap<AnnotationBundleKey,EP_CONFIG extends EndpointConfigBase<EP_CONFIG>> _readers
protected final LRUMap<AnnotationBundleKey,EP_CONFIG extends EndpointConfigBase<EP_CONFIG>> _writers
protected final AtomicReference<IOException> _noContentExceptionRef
protected ProviderBase(MAPPER_CONFIG mconfig)
@Deprecated protected ProviderBase()
public void checkCanDeserialize(boolean state)
isReadable(java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
is called.public void checkCanSerialize(boolean state)
isWriteable(java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)
is called.public void addUntouchable(Class<?> type)
type
- Type to consider untouchable; can be any kind of class,
including abstract class or interface. No instance of this type
(including subtypes, i.e. types assignable to this type) will
be read or written by providerpublic void removeUntouchable(Class<?> type)
public void setAnnotationsToUse(Annotations[] annotationsToUse)
annotationsToUse
- Ordered list of annotation sets to use; if null,
defaultpublic void setMapper(MAPPER m)
ObjectMapper
to use
for serialization and deserialization; if null, will use the standard
provider discovery from context instead. Default setting is null.public THIS setDefaultReadView(Class<?> view)
public THIS setDefaultWriteView(Class<?> view)
public THIS setDefaultView(Class<?> view)
setDefaultReadView(view);
setDefaultWriteView(view);
public THIS configure(JaxRSFeature feature, boolean state)
public THIS enable(JaxRSFeature feature)
public THIS enable(JaxRSFeature first, JaxRSFeature... f2)
public THIS disable(JaxRSFeature feature)
public THIS disable(JaxRSFeature first, JaxRSFeature... f2)
public boolean isEnabled(JaxRSFeature f)
public THIS configure(DeserializationFeature f, boolean state)
public THIS enable(DeserializationFeature f)
public THIS disable(DeserializationFeature f)
public THIS configure(SerializationFeature f, boolean state)
public THIS enable(SerializationFeature f)
public THIS disable(SerializationFeature f)
public THIS enable(JsonParser.Feature f)
public THIS enable(JsonGenerator.Feature f)
public THIS disable(JsonParser.Feature f)
public THIS disable(JsonGenerator.Feature f)
public THIS configure(JsonParser.Feature f, boolean state)
public THIS configure(JsonGenerator.Feature f, boolean state)
protected boolean hasMatchingMediaTypeForReading(MediaType mediaType)
Default implementation simply calls hasMatchingMediaType(javax.ws.rs.core.MediaType)
.
protected boolean hasMatchingMediaTypeForWriting(MediaType mediaType)
Default implementation simply calls hasMatchingMediaType(javax.ws.rs.core.MediaType)
.
protected abstract boolean hasMatchingMediaType(MediaType mediaType)
protected abstract MAPPER _locateMapperViaProvider(Class<?> type, MediaType mediaType)
protected EP_CONFIG _configForReading(MAPPER mapper, Annotation[] annotations, Class<?> defaultView)
protected EP_CONFIG _configForWriting(MAPPER mapper, Annotation[] annotations, Class<?> defaultView)
protected abstract EP_CONFIG _configForReading(ObjectReader reader, Annotation[] annotations)
protected abstract EP_CONFIG _configForWriting(ObjectWriter writer, Annotation[] annotations)
public long getSize(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
getSize
in interface MessageBodyWriter<Object>
value
- the instance to writetype
- the class of instance that is to be written.genericType
- the type of instance to be written. 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.public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
hasMatchingMediaType(javax.ws.rs.core.MediaType)
); then verify
that type is not one of "untouchable" types (types we will never
automatically handle), and finally that there is a serializer
for type (iff checkCanSerialize(boolean)
has been called with
true argument -- otherwise assumption is there will be a handler)isWriteable
in interface MessageBodyWriter<Object>
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.true
if the type is supported, otherwise false
.public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream) throws IOException
writeTo
in interface MessageBodyWriter<Object>
value
- the instance to write.type
- the class of instance that is to be written.genericType
- the type of instance to be written. 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.httpHeaders
- a mutable map of the HTTP message headers.entityStream
- the OutputStream
for the HTTP entity. The
implementation should not close the output stream.IOException
- if an IO error arises.protected JsonEncoding findEncoding(MediaType mediaType, MultivaluedMap<String,Object> httpHeaders)
protected void _modifyHeaders(Object value, Class<?> type, Type genericType, Annotation[] annotations, MultivaluedMap<String,Object> httpHeaders, EP_CONFIG endpoint) throws IOException
IOException
protected JsonGenerator _createGenerator(ObjectWriter writer, OutputStream rawStream, JsonEncoding enc) throws IOException
JsonGenerator
for writing
contents into given raw OutputStream
.IOException
protected EP_CONFIG _endpointForWriting(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders)
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
hasMatchingMediaType(javax.ws.rs.core.MediaType)
);
then verify
that type is not one of "untouchable" types (types we will never
automatically handle), and finally that there is a deserializer
for type (iff checkCanDeserialize(boolean)
has been called with
true argument -- otherwise assumption is there will be a handler)isReadable
in interface MessageBodyReader<Object>
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 by
Method.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
Method.getParameterAnnotations
.mediaType
- the media type of the HTTP entity, if one is not
specified in the request then application/octet-stream
is
used.true
if the type is supported, otherwise false
.public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, InputStream entityStream) throws IOException
readFrom
in interface MessageBodyReader<Object>
type
- the type that is to be read from the entity stream.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 by
Method.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
Method.getParameterAnnotations
.mediaType
- the media type of the HTTP entity.httpHeaders
- the read-only HTTP headers associated with HTTP entity.entityStream
- the InputStream
of the HTTP entity. The
caller is responsible for ensuring that the input stream ends when the
entity has been consumed. The implementation should not close the input
stream.NoContentException
in case no zero-length entity representation is
defined for the supported Java type.IOException
- if an IO error arises. In case the entity input stream is empty
and the reader is not able to produce a Java representation for
a zero-length entity, NoContentException
is expected to
be thrown.protected JsonParser _createParser(ObjectReader reader, InputStream rawStream) throws IOException
JsonParser
for reading
contents of given raw InputStream
.
May return null to indicate that Stream is empty; that is, contains no
content.IOException
protected EP_CONFIG _endpointForReading(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders)
ObjectReader
to use for deserializing
contentpublic MAPPER locateMapper(Class<?> type, MediaType mediaType)
ObjectMapper
to use for serialization
and deserialization. Exact logic depends on setting of
JaxRSFeature.DYNAMIC_OBJECT_MAPPER_LOOKUP
.
If JaxRSFeature.DYNAMIC_OBJECT_MAPPER_LOOKUP
is disabled (default
setting unless changed), behavior is as follows:
setMapper(MAPPER)
(or non-null instance passed in constructor), that
will be used.
ContextResolver
mechanism, if it has been properly configured
to access it (by JAX-RS runtime).
ObjectMapper
instance (one constructed with default constructor
and not modified in any way)
If JaxRSFeature.DYNAMIC_OBJECT_MAPPER_LOOKUP
is enabled, steps
1 and 2 are reversed, such that JAX-RS ContextResolver
is first used, and only if none is defined will configured mapper be used.
type
- Class of object being serialized or deserialized;
not checked at this point, since it is assumed that unprocessable
classes have been already weeded out,
but will be passed to ContextResolver
as is.mediaType
- Declared media type for the instance to process:
not used by this method,
but will be passed to ContextResolver
as is.protected boolean _isSpecialReadable(Class<?> type)
protected boolean _isIgnorableForReading(ClassKey typeKey)
protected boolean _isIgnorableForWriting(ClassKey typeKey)
protected IOException _createNoContentException()
protected static List<Class<?>> findSuperTypes(Class<?> cls, Class<?> endBefore, List<Class<?>> result)
protected static void _addSuperTypes(Class<?> cls, Class<?> endBefore, Collection<Class<?>> result, boolean addClassItself)
Copyright © 2019 JBoss by Red Hat. All rights reserved.