Class ContentType
- java.lang.Object
-
- org.apache.olingo.odata2.core.commons.ContentType
-
public class ContentType extends Object
Internally usedContentTypefor OData library. For more details on format and content of aContentTypeseeMedia Typeformat as defined inRFC 2616 chapter 3.7 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html).
Especially formedia-type = type "/" subtype *( ";" parameter ) type = token subtype = tokenAcceptHeader as defined inRFC 2616 chapter 14.1 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html):
Especially forAccept = "Accept" ":" #( media-range [ accept-params ] ) media-range = ( "* /*" | ( type "/" "*" ) | ( type "/" subtype ) ) *( ";" parameter ) accept-params = ";" "q" "=" qvalue *( accept-extension ) accept-extension = ";" token [ "=" ( token | quoted-string ) ]Content-TypeHeader as defined inRFC 2616 chapter 14.7 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html):
Once created aContent-Type = "Content-Type" ":" media-typeContentTypeis IMMUTABLE.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classContentType.ODataFormat
-
Field Summary
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareWildcardCounts(ContentType otherContentType)Compare wildcards counts/weights of bothContentType.static List<ContentType>convert(List<String> types)static ContentTypecreate(String format)Create aContentTypebased on given input string (format).static ContentTypecreate(String type, String subtype)Creates a content type from type and subtypestatic ContentTypecreate(String type, String subtype, Map<String,String> parameters)static List<ContentType>create(List<String> contentTypeStrings)Create a list ofContentTypebased on given input strings (contentTypes).static ContentTypecreate(ContentType contentType, String parameterKey, String parameterValue)static ContentTypecreateAsCustom(String format)Create aContentTypebased on given input string (format).static List<ContentType>createAsCustom(List<String> contentTypeStrings)Create a list ofContentTypebased on given input strings (contentTypes).booleanequals(Object obj)ContentTypes are equal iftype,subtypeand allparametershave the same value. iftypeand/orsubtypeis set to "*" (in such a case theparametersare ignored).ContentType.ODataFormatgetODataFormat()Map<String,String>getParameters()StringgetSubtype()StringgetType()booleanhasCompatible(List<ContentType> toMatchContentTypes)Check if a valid compatible match for thisContentTypeexists in given list.inthashCode()booleanhasMatch(List<ContentType> toMatchContentTypes)Check if a valid match for thisContentTypeexists in given list.booleanhasWildcard()booleanisCompatible(ContentType obj)ContentTypes are compatible iftype,subtypehave the same value. iftypeand/orsubtypeis set to "*" The setparametersare always ignored (for compare with parameters seeequals(Object)).booleanisContentTypeODataTextRelated()static booleanisParseable(String format)Validates if givenformatis parseable and can be used as input forcreate(String)method.static booleanisParseableAsCustom(String format)Validates if givenformatis parseable and can be used as input forcreate(String)method.booleanisWildcard()static booleanmatch(String toMatch, ContentType... matchExamples)Check if a valid match for given content type formated string (toMatch) exists in given list.ContentTypematch(List<ContentType> toMatchContentTypes)Find best match between thisContentTypeand theContentTypein the list.ContentTypematchCompatible(List<ContentType> toMatchContentTypes)Find best match between thisContentTypeand theContentTypein the list ignoring all set parameters.static ContentTypeparse(String format)Parses the given input string (format) and returns createdContentTypeif input was valid or returnNULLif input was not parseable.ContentTypereceiveWithCharsetParameter(String defaultCharset)Ensure that charset parameter (PARAMETER_CHARSET) is set on returned content type if thisContentTypeis aodata text relatedcontent type (@seeisContentTypeODataTextRelated()).static voidsortForQParameter(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.StringtoContentTypeString()GetContentTypeas string as defined in RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt - chapter 14.17: Content-Type)StringtoString()
-
-
-
Field Detail
-
PARAMETER_CHARSET
public static final String PARAMETER_CHARSET
- See Also:
- Constant Field Values
-
PARAMETER_ODATA
public static final String PARAMETER_ODATA
- See Also:
- Constant Field Values
-
PARAMETER_Q
public static final String PARAMETER_Q
- See Also:
- Constant Field Values
-
PARAMETER_TYPE
public static final String PARAMETER_TYPE
- See Also:
- Constant Field Values
-
CHARSET_UTF_8
public static final String CHARSET_UTF_8
- See Also:
- Constant Field Values
-
WILDCARD
public static final ContentType WILDCARD
-
APPLICATION_XML
public static final ContentType APPLICATION_XML
-
APPLICATION_XML_CS_UTF_8
public static final ContentType APPLICATION_XML_CS_UTF_8
-
APPLICATION_ATOM_XML
public static final ContentType APPLICATION_ATOM_XML
-
APPLICATION_ATOM_XML_CS_UTF_8
public static final ContentType APPLICATION_ATOM_XML_CS_UTF_8
-
APPLICATION_ATOM_XML_ENTRY
public static final ContentType APPLICATION_ATOM_XML_ENTRY
-
APPLICATION_ATOM_XML_ENTRY_CS_UTF_8
public static final ContentType APPLICATION_ATOM_XML_ENTRY_CS_UTF_8
-
APPLICATION_ATOM_XML_FEED
public static final ContentType APPLICATION_ATOM_XML_FEED
-
APPLICATION_ATOM_XML_FEED_CS_UTF_8
public static final ContentType APPLICATION_ATOM_XML_FEED_CS_UTF_8
-
APPLICATION_ATOM_SVC
public static final ContentType APPLICATION_ATOM_SVC
-
APPLICATION_ATOM_SVC_CS_UTF_8
public static final ContentType APPLICATION_ATOM_SVC_CS_UTF_8
-
APPLICATION_JSON
public static final ContentType APPLICATION_JSON
-
APPLICATION_JSON_ODATA_VERBOSE
public static final ContentType APPLICATION_JSON_ODATA_VERBOSE
-
APPLICATION_JSON_CS_UTF_8
public static final ContentType APPLICATION_JSON_CS_UTF_8
-
APPLICATION_OCTET_STREAM
public static final ContentType APPLICATION_OCTET_STREAM
-
TEXT_PLAIN
public static final ContentType TEXT_PLAIN
-
TEXT_PLAIN_CS_UTF_8
public static final ContentType TEXT_PLAIN_CS_UTF_8
-
MULTIPART_MIXED
public static final ContentType MULTIPART_MIXED
-
TEXT_JAVASCRIPT
public static final ContentType TEXT_JAVASCRIPT
-
TEXT_JAVASCRIPT_UTF_8
public static final ContentType TEXT_JAVASCRIPT_UTF_8
-
-
Method Detail
-
isParseable
public static boolean isParseable(String format)
Validates if givenformatis parseable and can be used as input forcreate(String)method.- Parameters:
format- to be validated string- Returns:
trueif format is parseable otherwisefalse
-
isParseableAsCustom
public static boolean isParseableAsCustom(String format)
Validates if givenformatis parseable and can be used as input forcreate(String)method.- Parameters:
format- to be validated string- Returns:
trueif format is parseable otherwisefalse
-
create
public static ContentType create(String type, String subtype)
Creates a content type from type and subtype- Parameters:
type- Tiposubtype- Subtipo- Returns:
- a new
ContentTypeobject
-
create
public static ContentType create(String type, String subtype, Map<String,String> parameters)
- Parameters:
type- Tiposubtype- Subtipoparameters- Parametros- Returns:
- a new
ContentTypeobject
-
create
public static ContentType create(ContentType contentType, String parameterKey, String parameterValue)
- Parameters:
contentType- TipoparameterKey- ChaveparameterValue- Valor- Returns:
- a new
ContentTypeobject
-
create
public static ContentType create(String format)
Create aContentTypebased on given input string (format). Supported format isMedia Typeformat as defined inRFC 2616 chapter 3.7. This format is used asHTTP Accept HEADERformat as defined inRFC 2616 chapter 14.1andHTTP Content-Type HEADERformat as defined inRFC 2616 chapter 14.17- Parameters:
format- a string in format as defined inRFC 2616 section 3.7- Returns:
- a new
ContentTypeobject - Throws:
IllegalArgumentException- if input string is not parseable
-
createAsCustom
public static ContentType createAsCustom(String format)
Create aContentTypebased on given input string (format). Supported format isMedia Typeformat as defined inRFC 2616 chapter 3.7. andContentTypewithContentType.ODataFormat.CUSTOM. TheMedia Typeformat can be used asHTTP Accept HEADERformat as defined inRFC 2616 chapter 14.1andHTTP Content-Type HEADERformat as defined inRFC 2616 chapter 14.17. TheContentTypewithContentType.ODataFormat.CUSTOMcan only be used as$formatsystem query option (as defined http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#47_Format_System_Query_Option_format).- Parameters:
format- a string in format as defined inRFC 2616 section 3.7- Returns:
- a new
ContentTypeobject - Throws:
IllegalArgumentException- if input string is not parseable
-
create
public static List<ContentType> create(List<String> contentTypeStrings)
Create a list ofContentTypebased on given input strings (contentTypes). Supported format isMedia Typeformat as defined inRFC 2616 chapter 3.7. This format is used asHTTP Accept HEADERformat as defined inRFC 2616 chapter 14.1andHTTP Content-Type HEADERformat as defined inRFC 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).
- Parameters:
contentTypeStrings- a list of strings in format as defined inRFC 2616 section 3.7- Returns:
- a list of new
ContentTypeobject - Throws:
IllegalArgumentException- if one of the given input string is not parseable this exceptions is thrown
-
createAsCustom
public static List<ContentType> createAsCustom(List<String> contentTypeStrings)
Create a list ofContentTypebased on given input strings (contentTypes). Supported format isMedia Typeformat as defined inRFC 2616 chapter 3.7. andContentTypewithContentType.ODataFormat.CUSTOM. TheMedia Typeformat can be used asHTTP Accept HEADERformat as defined inRFC 2616 chapter 14.1andHTTP Content-Type HEADERformat as defined inRFC 2616 chapter 14.17. TheContentTypewithContentType.ODataFormat.CUSTOMcan only be used as$formatsystem query option (as defined http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#47_Format_System_Query_Option_format).- Parameters:
contentTypeStrings- a list of strings in format as defined inRFC 2616 section 3.7or as defined http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/#47_Format_System_Query_Option_format- Returns:
- a list of new
ContentTypeobject - Throws:
IllegalArgumentException- if one of the given input string is not parseable this exceptions is thrown
-
parse
public static ContentType parse(String format)
Parses the given input string (format) and returns createdContentTypeif input was valid or returnNULLif input was not parseable. For the definition of the supported format seecreate(String).- Parameters:
format- a string in format as defined inRFC 2616 section 3.7- Returns:
- a new
ContentTypeobject
-
sortForQParameter
public 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. Attention: For invalid values a "q" value from-1is used for sorting.- Parameters:
toSort- list which is sorted and hence re-arranged
-
receiveWithCharsetParameter
public ContentType receiveWithCharsetParameter(String defaultCharset)
Ensure that charset parameter (PARAMETER_CHARSET) is set on returned content type if thisContentTypeis aodata text relatedcontent type (@seeisContentTypeODataTextRelated()). IfthisContentTypehas no charset parameter set a newContentTypewith givendefaultCharsetis created. Otherwise if charset parameter is already set nothing is done.- Parameters:
defaultCharset- Charset- Returns:
- ContentType
-
isContentTypeODataTextRelated
public boolean isContentTypeODataTextRelated()
- Returns:
trueif thisContentTypeis text related (in the view of OData)
-
getType
public String getType()
-
getSubtype
public String getSubtype()
-
getParameters
public Map<String,String> getParameters()
- Returns:
- parameters of this
ContentTypeas unmodifiable map.
-
equals
public boolean equals(Object obj)
ContentTypes are equal- if
type,subtypeand allparametershave the same value. - if
typeand/orsubtypeis set to "*" (in such a case theparametersare ignored).
- if
-
isCompatible
public boolean isCompatible(ContentType obj)
ContentTypes are compatible- if
type,subtypehave the same value. - if
typeand/orsubtypeis set to "*"
parametersare always ignored (for compare with parameters seeequals(Object)).- Parameters:
obj- Objeto- Returns:
trueif both instances are equal (see definition above), otherwisefalse.
- if
-
toContentTypeString
public String toContentTypeString()
GetContentTypeas string as defined in RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt - chapter 14.17: Content-Type)- Returns:
- string representation of
ContentTypeobject
-
getODataFormat
public ContentType.ODataFormat getODataFormat()
-
match
public ContentType match(List<ContentType> toMatchContentTypes)
Find best match between thisContentTypeand theContentTypein the list. If a match (thisContentTypeis equal to aContentTypein list) is found either this or theContentTypefrom the list is returned based on whichContentTypehas less "**" characters set (checked withcompareWildcardCounts(ContentType). If no match (noneContentTypein list is equal to thisContentType) is foundNULLis returned.- Parameters:
toMatchContentTypes- list ofContentTypes which are matches against thisContentType- Returns:
- best matched content type in list or
NULLif none content type match to this content type instance
-
matchCompatible
public ContentType matchCompatible(List<ContentType> toMatchContentTypes)
Find best match between thisContentTypeand theContentTypein the list ignoring all set parameters. If a match (thisContentTypeis equal to aContentTypein list) is found either this or theContentTypefrom the list is returned based on whichContentTypehas less "**" characters set (checked withcompareWildcardCounts(ContentType). If no match (noneContentTypein list is equal to thisContentType) is foundNULLis returned.- Parameters:
toMatchContentTypes- list ofContentTypes which are matches against thisContentType- Returns:
- best matched content type in list or
NULLif none content type match to this content type instance
-
hasCompatible
public boolean hasCompatible(List<ContentType> toMatchContentTypes)
Check if a valid compatible match for thisContentTypeexists in given list. Compatible in this case means that all set parameters are ignored. For more detail what a valid match is seematchCompatible(List).- Parameters:
toMatchContentTypes- list ofContentTypes which are matches against thisContentType- Returns:
trueif a compatible content type was found in given list orfalseif none compatible content type match was found
-
hasMatch
public boolean hasMatch(List<ContentType> toMatchContentTypes)
Check if a valid match for thisContentTypeexists in given list. For more detail what a valid match is seematch(List).- Parameters:
toMatchContentTypes- list ofContentTypes which are matches against thisContentType- Returns:
trueif a matching content type was found in given list orfalseif none matching content type match was found
-
compareWildcardCounts
public int compareWildcardCounts(ContentType otherContentType)
Compare wildcards counts/weights of bothContentType. The smallerContentTypehas lesser weighted wildcards then the biggerContentType. As result this method returns this object weighted wildcards minus the given parameter object weighted wildcards. A type wildcard is weighted with2and a subtype wildcard is weighted with1.- Parameters:
otherContentType-ContentTypeto be compared to- Returns:
- this object weighted wildcards minus the given parameter object weighted wildcards.
-
hasWildcard
public boolean hasWildcard()
- Returns:
trueiftypeorsubtypeof this instance is a "*".
-
isWildcard
public boolean isWildcard()
- Returns:
trueif bothtypeandsubtypeof this instance are a "*".
-
convert
public static List<ContentType> convert(List<String> types)
-
match
public static boolean match(String toMatch, ContentType... matchExamples)
Check if a valid match for given content type formated string (toMatch) exists in given list. Therefore the given content type formated string (toMatch) is converted into aContentTypewith a simplecreate(String)call (during which an exception can occur). For more detail in general seehasMatch(List)and for what a valid match is seematch(List).- Parameters:
toMatch- content type formated string (toMatch) for which is checked if a match exists in given listmatchExamples- list ofContentTypes which are matches against content type formated string (toMatch)- Returns:
trueif a matching content type was found in given list orfalseif none matching content type match was found
-
-