Class ContentType


  • public final class ContentType
    extends Object
    Content (media) type.

    To create a new content type application/json without character set parameter:

     ContentType ct = new ContentType("application", "json");
    
     // Prints out "application/json"
     System.out.println(ct.toString());
     

    With a character set parameter application/json; charset=UTF-8:

     ContentType ct = new ContentType("application", "json", new ContentType.Parameter("charset", "UTF-8"));
    
     // Prints out "application/json; charset=UTF-8"
     System.out.println(ct.toString());
     

    To parse a content type:

     try {
             ContentType.parse("application/json; charset=UTF-8");
     } catch (java.text.ParseException e) {
             System.err.println(e.getMessage());
     }
     

    See RFC 2045, section 5.1.

    See RFC 6838, section 4.2.8.

    Author:
    vd
    • Constructor Detail

      • ContentType

        public ContentType​(String baseType,
                           String subType,
                           ContentType.Parameter... param)
        Creates a new content type.
        Parameters:
        baseType - The type. E.g. "application" from "application/json".Must not be null or empty.
        subType - The subtype. E.g. "json" from "application/json". Must not be null or empty.
        param - Optional parameters.
      • ContentType

        public ContentType​(String baseType,
                           String subType,
                           Charset charset)
        Creates a new content type with the specified character set.
        Parameters:
        baseType - The base type. E.g. "application" from "application/json".Must not be null or empty.
        subType - The subtype. E.g. "json" from "application/json". Must not be null or empty.
        charset - The character set to use for the charset parameter. Must not be null.
    • Method Detail

      • getBaseType

        public String getBaseType()
        Returns the base type. E.g. "application" from "application/json".
        Returns:
        The base type.
      • getSubType

        public String getSubType()
        Returns the subtype. E.g. "json" from "application/json".
        Returns:
        The subtype.
      • getBaseSubType

        public String getBaseSubType()
        Returns the base sub type. E.g. "entity-statement" from "application/entity-statement+jwt".
        Returns:
        The base sub type or the sub type if a suffix is not present.
      • getSubTypeSuffix

        public String getSubTypeSuffix()
        Returns the sub type suffix. E.g. "jwt" from "application/entity-statement+jwt".
        Returns:
        The sub type suffix, null none.
      • hasSubTypeSuffix

        public boolean hasSubTypeSuffix​(String suffix)
        Returns true if this content type has the specified sub type suffix.
        Parameters:
        suffix - The sub type suffix, null if not specified.
        Returns:
        true if the sub type has the specified suffix, else false.
      • getType

        public String getType()
        Returns the type. E.g. "application/json".
        Returns:
        The type, any optional parameters are omitted.
      • matches

        public boolean matches​(ContentType other)
        Returns true if the types and subtypes match. The parameters, if any, are ignored.
        Parameters:
        other - The other content type, null if not specified.
        Returns:
        true if the types and subtypes match, else false.
      • parse

        public static ContentType parse​(String s)
                                 throws ParseException
        Parses a content type from the specified string.
        Parameters:
        s - The string to parse.
        Returns:
        The content type.
        Throws:
        ParseException - If parsing failed or the string is null or empty.