Class ContentType


  • public class ContentType
    extends Object
    Internally used 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.
    • Field Detail

      • 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 given format is parseable and can be used as input for create(String) method.
        Parameters:
        format - to be validated string
        Returns:
        true if format is parseable otherwise false
      • isParseableAsCustom

        public static boolean isParseableAsCustom​(String format)
        Validates if given format is parseable and can be used as input for create(String) method.
        Parameters:
        format - to be validated string
        Returns:
        true if format is parseable otherwise false
      • create

        public static ContentType create​(String type,
                                         String subtype)
        Creates a content type from type and subtype
        Parameters:
        type - Tipo
        subtype - Subtipo
        Returns:
        a new ContentType object
      • create

        public static ContentType create​(String type,
                                         String subtype,
                                         Map<String,​String> parameters)
        Parameters:
        type - Tipo
        subtype - Subtipo
        parameters - Parametros
        Returns:
        a new ContentType object
      • create

        public static ContentType create​(ContentType contentType,
                                         String parameterKey,
                                         String parameterValue)
        Parameters:
        contentType - Tipo
        parameterKey - Chave
        parameterValue - Valor
        Returns:
        a new ContentType object
      • create

        public static ContentType create​(String format)
        Create a 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.17
        Parameters:
        format - a string in format as defined in RFC 2616 section 3.7
        Returns:
        a new ContentType object
        Throws:
        IllegalArgumentException - if input string is not parseable
      • createAsCustom

        public static ContentType createAsCustom​(String format)
        Create a 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).
        Parameters:
        format - a string in format as defined in RFC 2616 section 3.7
        Returns:
        a new ContentType object
        Throws:
        IllegalArgumentException - if input string is not parseable
      • create

        public static List<ContentType> create​(List<String> contentTypeStrings)
        Create a list of 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).

        Parameters:
        contentTypeStrings - a list of strings in format as defined in RFC 2616 section 3.7
        Returns:
        a list of new ContentType object
        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 of 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).
        Parameters:
        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_format
        Returns:
        a list of new ContentType object
        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 created ContentType if input was valid or return NULL if input was not parseable. For the definition of the supported format see create(String).
        Parameters:
        format - a string in format as defined in RFC 2616 section 3.7
        Returns:
        a new ContentType object
      • 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 -1 is 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 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.
        Parameters:
        defaultCharset - Charset
        Returns:
        ContentType
      • isContentTypeODataTextRelated

        public boolean isContentTypeODataTextRelated()
        Returns:
        true if this ContentType is text related (in the view of OData)
      • getType

        public String getType()
      • getSubtype

        public String getSubtype()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • equals

        public 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).
        Overrides:
        equals in class Object
        Returns:
        true if both instances are equal (see definition above), otherwise false.
      • isCompatible

        public 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) ).
        Parameters:
        obj - Objeto
        Returns:
        true if both instances are equal (see definition above), otherwise false.
      • toContentTypeString

        public String toContentTypeString()
        Get ContentType as string as defined in RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt - chapter 14.17: Content-Type)
        Returns:
        string representation of ContentType object
      • hasCompatible

        public boolean hasCompatible​(List<ContentType> toMatchContentTypes)
        Check if a valid compatible match for this 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).
        Parameters:
        toMatchContentTypes - list of ContentTypes which are matches against this ContentType
        Returns:
        true if a compatible content type was found in given list or false if none compatible content type match was found
      • hasMatch

        public boolean hasMatch​(List<ContentType> toMatchContentTypes)
        Check if a valid match for this ContentType exists in given list. For more detail what a valid match is see match(List).
        Parameters:
        toMatchContentTypes - list of ContentTypes which are matches against this ContentType
        Returns:
        true if a matching content type was found in given list or false if none matching content type match was found
      • compareWildcardCounts

        public int compareWildcardCounts​(ContentType otherContentType)
        Compare wildcards counts/weights of both 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.
        Parameters:
        otherContentType - ContentType to be compared to
        Returns:
        this object weighted wildcards minus the given parameter object weighted wildcards.
      • hasWildcard

        public boolean hasWildcard()
        Returns:
        true if type or subtype of this instance is a "*".
      • isWildcard

        public boolean isWildcard()
        Returns:
        true if both type and subtype of this instance are a "*".
      • 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 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).
        Parameters:
        toMatch - content type formated string (toMatch) for which is checked if a match exists in given list
        matchExamples - list of ContentTypes which are matches against content type formated string (toMatch)
        Returns:
        true if a matching content type was found in given list or false if none matching content type match was found