public class ContentType extends Object
ContentType for OData library.
For more details on format and content of a ContentType see
Media Type format as defined in RFC 2616 chapter 3.7
(http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html).
media-type = type "/" subtype *( ";" parameter )
type = token
subtype = token
Especially for Accept Header as defined in
RFC 2616 chapter 14.1 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html):
Accept = "Accept" ":"
#( media-range [ accept-params ] )
media-range = ( "* /*"
| ( type "/" "*" )
| ( type "/" subtype )
) *( ";" parameter )
accept-params = ";" "q" "=" qvalue *( accept-extension )
accept-extension = ";" token [ "=" ( token | quoted-string ) ]
Especially for Content-Type Header as defined in
RFC 2616 chapter 14.7 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html):
Content-Type = "Content-Type" ":" media-type
Once created a ContentType is IMMUTABLE.| Modifier and Type | Class and Description |
|---|---|
static class |
ContentType.ODataFormat |
| Modifier and Type | Field and Description |
|---|---|
static ContentType |
APPLICATION_ATOM_SVC |
static ContentType |
APPLICATION_ATOM_SVC_CS_UTF_8 |
static ContentType |
APPLICATION_ATOM_XML |
static ContentType |
APPLICATION_ATOM_XML_CS_UTF_8 |
static ContentType |
APPLICATION_ATOM_XML_ENTRY |
static ContentType |
APPLICATION_ATOM_XML_ENTRY_CS_UTF_8 |
static ContentType |
APPLICATION_ATOM_XML_FEED |
static ContentType |
APPLICATION_ATOM_XML_FEED_CS_UTF_8 |
static ContentType |
APPLICATION_JSON |
static ContentType |
APPLICATION_JSON_CS_UTF_8 |
static ContentType |
APPLICATION_JSON_ODATA_VERBOSE |
static ContentType |
APPLICATION_OCTET_STREAM |
static ContentType |
APPLICATION_XML |
static ContentType |
APPLICATION_XML_CS_UTF_8 |
static String |
CHARSET_UTF_8 |
static ContentType |
MULTIPART_MIXED |
static String |
PARAMETER_CHARSET |
static String |
PARAMETER_ODATA |
static String |
PARAMETER_Q |
static String |
PARAMETER_TYPE |
static ContentType |
TEXT_PLAIN |
static ContentType |
TEXT_PLAIN_CS_UTF_8 |
static ContentType |
WILDCARD |
| Modifier and Type | Method and Description |
|---|---|
int |
compareWildcardCounts(ContentType otherContentType)
Compare wildcards counts/weights of both
ContentType. |
static List<ContentType> |
convert(List<String> types) |
static ContentType |
create(ContentType contentType,
String parameterKey,
String parameterValue) |
static List<ContentType> |
create(List<String> contentTypeStrings)
Create a list of
ContentType based on given input strings (contentTypes). |
static ContentType |
create(String format)
Create a
ContentType based on given input string (format). |
static ContentType |
create(String type,
String subtype)
Creates a content type from type and subtype
|
static ContentType |
create(String type,
String subtype,
Map<String,String> parameters) |
static List<ContentType> |
createAsCustom(List<String> contentTypeStrings)
Create a list of
ContentType based on given input strings (contentTypes). |
static ContentType |
createAsCustom(String format)
Create a
ContentType based on given input string (format). |
boolean |
equals(Object obj)
ContentTypes are equal
if type, subtype and all parameters have the same value.
if type and/or subtype is set to "*" (in such a case the parameters are
ignored).
|
ContentType.ODataFormat |
getODataFormat() |
Map<String,String> |
getParameters() |
String |
getSubtype() |
String |
getType() |
boolean |
hasCompatible(List<ContentType> toMatchContentTypes)
Check if a valid compatible match for this
ContentType exists in given list. |
int |
hashCode() |
boolean |
hasMatch(List<ContentType> toMatchContentTypes)
Check if a valid match for this
ContentType exists in given list. |
boolean |
hasWildcard() |
boolean |
isCompatible(ContentType obj)
ContentTypes are compatible
if type, subtype have the same value.
if type and/or subtype is set to "*"
The set parameters are always ignored (for compare with parameters see equals(Object)
). |
boolean |
isContentTypeODataTextRelated() |
static boolean |
isParseable(String format)
Validates if given
format is parseable and can be used as input for create(String) method. |
static boolean |
isParseableAsCustom(String format)
Validates if given
format is parseable and can be used as input for create(String) method. |
boolean |
isWildcard() |
ContentType |
match(List<ContentType> toMatchContentTypes)
Find best match between this
ContentType and the ContentType in the list. |
static boolean |
match(String toMatch,
ContentType... matchExamples)
Check if a valid match for given content type formated string (
toMatch) exists in given list. |
ContentType |
matchCompatible(List<ContentType> toMatchContentTypes)
Find best match between this
ContentType and the ContentType in the list ignoring all set
parameters. |
static ContentType |
parse(String format)
Parses the given input string (
format) and returns created ContentType if input was valid or
return NULL if
input was not parseable. |
ContentType |
receiveWithCharsetParameter(String defaultCharset)
Ensure that charset parameter (
PARAMETER_CHARSET) is set on returned content type
if this ContentType is a odata text related content type (@see
isContentTypeODataTextRelated()). |
static void |
sortForQParameter(List<String> toSort)
Sort given list (which must contains content type formated string) for their "q" value
as defined in RFC 2616 section 4.1 and
RFC 2616 Section 3.9.
|
String |
toContentTypeString()
Get
ContentType as string as defined in RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt - chapter 14.17:
Content-Type) |
String |
toString() |
public static final String PARAMETER_CHARSET
public static final String PARAMETER_ODATA
public static final String PARAMETER_Q
public static final String PARAMETER_TYPE
public static final String CHARSET_UTF_8
public static final ContentType WILDCARD
public static final ContentType APPLICATION_XML
public static final ContentType APPLICATION_XML_CS_UTF_8
public static final ContentType APPLICATION_ATOM_XML
public static final ContentType APPLICATION_ATOM_XML_CS_UTF_8
public static final ContentType APPLICATION_ATOM_XML_ENTRY
public static final ContentType APPLICATION_ATOM_XML_ENTRY_CS_UTF_8
public static final ContentType APPLICATION_ATOM_XML_FEED
public static final ContentType APPLICATION_ATOM_XML_FEED_CS_UTF_8
public static final ContentType APPLICATION_ATOM_SVC
public static final ContentType APPLICATION_ATOM_SVC_CS_UTF_8
public static final ContentType APPLICATION_JSON
public static final ContentType APPLICATION_JSON_ODATA_VERBOSE
public static final ContentType APPLICATION_JSON_CS_UTF_8
public static final ContentType APPLICATION_OCTET_STREAM
public static final ContentType TEXT_PLAIN
public static final ContentType TEXT_PLAIN_CS_UTF_8
public static final ContentType MULTIPART_MIXED
public static boolean isParseable(String format)
format is parseable and can be used as input for create(String) method.format - to be validated stringtrue if format is parseable otherwise falsepublic static boolean isParseableAsCustom(String format)
format is parseable and can be used as input for create(String) method.format - to be validated stringtrue if format is parseable otherwise falsepublic static ContentType create(String type, String subtype)
type - subtype - ContentType objectpublic static ContentType create(String type, String subtype, Map<String,String> parameters)
type - subtype - parameters - ContentType objectpublic static ContentType create(ContentType contentType, String parameterKey, String parameterValue)
contentType - parameterKey - parameterValue - ContentType objectpublic static ContentType create(String format)
ContentType based on given input string (format).
Supported format is Media Type format as defined in RFC 2616 chapter 3.7.
This format is used as
HTTP Accept HEADER format as defined in RFC 2616 chapter 14.1
and
HTTP Content-Type HEADER format as defined in RFC 2616 chapter 14.17format - a string in format as defined in RFC 2616 section 3.7ContentType objectIllegalArgumentException - if input string is not parseablepublic static ContentType createAsCustom(String format)
ContentType based on given input string (format).
Supported format is Media Type format as defined in RFC 2616 chapter 3.7.
and ContentType with ContentType.ODataFormat.CUSTOM.
The Media Type format can be used as
HTTP Accept HEADER format as defined in RFC 2616 chapter 14.1
and
HTTP Content-Type HEADER format as defined in RFC 2616 chapter 14.17.
The ContentType with ContentType.ODataFormat.CUSTOM can only be used as $format system query
option
(as defined
http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#47_Format_System_Query_Option_format).format - a string in format as defined in RFC 2616 section 3.7ContentType objectIllegalArgumentException - if input string is not parseablepublic static List<ContentType> create(List<String> contentTypeStrings)
ContentType based on given input strings (contentTypes).
Supported format is Media Type format as defined in RFC 2616 chapter 3.7.
This format is used as
HTTP Accept HEADER format as defined in RFC 2616 chapter 14.1
and
HTTP Content-Type HEADER format as defined in RFC 2616 chapter 14.17.
If one of the given strings can not be parsed an exception is thrown (hence no list is returned with the parseable strings).
contentTypeStrings - a list of strings in format as defined in RFC 2616 section 3.7ContentType objectIllegalArgumentException - if one of the given input string is not parseable this exceptions is thrownpublic static List<ContentType> createAsCustom(List<String> contentTypeStrings)
ContentType based on given input strings (contentTypes).
Supported format is Media Type format as defined in RFC 2616 chapter 3.7.
and ContentType with ContentType.ODataFormat.CUSTOM.
The Media Type format can be used as
HTTP Accept HEADER format as defined in RFC 2616 chapter 14.1
and
HTTP Content-Type HEADER format as defined in RFC 2616 chapter 14.17.
The ContentType with ContentType.ODataFormat.CUSTOM can only be used as $format system query
option
(as defined
http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#47_Format_System_Query_Option_format).contentTypeStrings - a list of strings in format as defined in RFC 2616 section 3.7 or
as defined
http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#47_Format_System_Query_Option_formatContentType objectIllegalArgumentException - if one of the given input string is not parseable this exceptions is thrownpublic static ContentType parse(String format)
format) and returns created ContentType if input was valid or
return NULL if
input was not parseable.
For the definition of the supported format see create(String).format - a string in format as defined in RFC 2616 section 3.7ContentType objectpublic static void sortForQParameter(List<String> toSort)
-1 is used for sorting.toSort - list which is sorted and hence re-arrangedpublic ContentType receiveWithCharsetParameter(String defaultCharset)
PARAMETER_CHARSET) is set on returned content type
if this ContentType is a odata text related content type (@see
isContentTypeODataTextRelated()).
If this ContentType has no charset parameter set a new ContentType with given
defaultCharset is created.
Otherwise if charset parameter is already set nothing is done.defaultCharset - public boolean isContentTypeODataTextRelated()
true if this ContentType is text related (in the view of OData)public String getType()
public String getSubtype()
public Map<String,String> getParameters()
ContentType as unmodifiable map.public boolean equals(Object obj)
ContentTypes are equal
type, subtype and all parameters have the same value.type and/or subtype is set to "*" (in such a case the parameters are
ignored).public boolean isCompatible(ContentType obj)
ContentTypes are compatible
type, subtype have the same value.type and/or subtype is set to "*"parameters are always ignored (for compare with parameters see equals(Object)
).true if both instances are equal (see definition above), otherwise false.public String toContentTypeString()
ContentType as string as defined in RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt - chapter 14.17:
Content-Type)ContentType objectpublic ContentType.ODataFormat getODataFormat()
public ContentType match(List<ContentType> toMatchContentTypes)
ContentType and the ContentType in the list.
If a match (this ContentType is equal to a ContentType in list) is found either this or the
ContentType from the list is returned based on which ContentType has less "**" characters set
(checked with compareWildcardCounts(ContentType).
If no match (none ContentType in list is equal to this ContentType) is found NULL is
returned.toMatchContentTypes - list of ContentTypes which are matches against this ContentTypeNULL if none content type match to this content type
instancepublic ContentType matchCompatible(List<ContentType> toMatchContentTypes)
ContentType and the ContentType in the list ignoring all set
parameters.
If a match (this ContentType is equal to a ContentType in list) is found either this or the
ContentType from the list is returned based on which ContentType has less "**" characters set
(checked with compareWildcardCounts(ContentType).
If no match (none ContentType in list is equal to this ContentType) is found NULL is
returned.toMatchContentTypes - list of ContentTypes which are matches against this ContentTypeNULL if none content type match to this content type
instancepublic boolean hasCompatible(List<ContentType> toMatchContentTypes)
ContentType exists in given list.
Compatible in this case means that all set parameters are ignored.
For more detail what a valid match is see matchCompatible(List).toMatchContentTypes - list of ContentTypes which are matches against this ContentTypetrue if a compatible content type was found in given list
or false if none compatible content type match was foundpublic boolean hasMatch(List<ContentType> toMatchContentTypes)
ContentType exists in given list.
For more detail what a valid match is see match(List).toMatchContentTypes - list of ContentTypes which are matches against this ContentTypetrue if a matching content type was found in given list
or false if none matching content type match was foundpublic int compareWildcardCounts(ContentType otherContentType)
ContentType.
The smaller ContentType has lesser weighted wildcards then the bigger ContentType.
As result this method returns this object weighted wildcards minus the given parameter object weighted wildcards.
A type wildcard is weighted with 2 and a subtype wildcard is weighted with 1.otherContentType - ContentType to be compared topublic boolean hasWildcard()
true if type or subtype of this instance is a "*".public boolean isWildcard()
true if both type and subtype of this instance are a "*".public static List<ContentType> convert(List<String> types)
public static boolean match(String toMatch, ContentType... matchExamples)
toMatch) exists in given list.
Therefore the given content type formated string (toMatch) is converted into a ContentType
with a simple create(String) call (during which an exception can occur).
For more detail in general see hasMatch(List) and for what a valid match is see match(List).toMatch - content type formated string (toMatch) for which is checked if a match exists in given
listmatchExamples - list of ContentTypes which are matches against content type formated string
(toMatch)true if a matching content type was found in given list
or false if none matching content type match was foundCopyright © 2020. All rights reserved.