Class ContentType

java.lang.Object
org.apache.http.entity.ContentType
All Implemented Interfaces:
Serializable

@Contract(threading=IMMUTABLE) public final class ContentType extends Object implements Serializable
Content type information consisting of a MIME type and an optional charset.

This class makes no attempts to verify validity of the MIME type. The input parameters of the create(String, String) method, however, may not contain characters <">, <;>, <,> reserved by the HTTP specification.

Since:
4.2
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • APPLICATION_ATOM_XML

      public static final ContentType APPLICATION_ATOM_XML
    • APPLICATION_FORM_URLENCODED

      public static final ContentType APPLICATION_FORM_URLENCODED
    • APPLICATION_JSON

      public static final ContentType APPLICATION_JSON
    • APPLICATION_OCTET_STREAM

      public static final ContentType APPLICATION_OCTET_STREAM
    • APPLICATION_SOAP_XML

      public static final ContentType APPLICATION_SOAP_XML
    • APPLICATION_SVG_XML

      public static final ContentType APPLICATION_SVG_XML
    • APPLICATION_XHTML_XML

      public static final ContentType APPLICATION_XHTML_XML
    • APPLICATION_XML

      public static final ContentType APPLICATION_XML
    • IMAGE_BMP

      public static final ContentType IMAGE_BMP
    • IMAGE_GIF

      public static final ContentType IMAGE_GIF
    • IMAGE_JPEG

      public static final ContentType IMAGE_JPEG
    • IMAGE_PNG

      public static final ContentType IMAGE_PNG
    • IMAGE_SVG

      public static final ContentType IMAGE_SVG
    • IMAGE_TIFF

      public static final ContentType IMAGE_TIFF
    • IMAGE_WEBP

      public static final ContentType IMAGE_WEBP
    • MULTIPART_FORM_DATA

      public static final ContentType MULTIPART_FORM_DATA
    • TEXT_HTML

      public static final ContentType TEXT_HTML
    • TEXT_PLAIN

      public static final ContentType TEXT_PLAIN
    • TEXT_XML

      public static final ContentType TEXT_XML
    • WILDCARD

      public static final ContentType WILDCARD
    • CONTENT_TYPE_MAP

      private static final Map<String,ContentType> CONTENT_TYPE_MAP
    • DEFAULT_TEXT

      public static final ContentType DEFAULT_TEXT
    • DEFAULT_BINARY

      public static final ContentType DEFAULT_BINARY
    • mimeType

      private final String mimeType
    • charset

      private final Charset charset
    • params

      private final NameValuePair[] params
  • Constructor Details

  • Method Details

    • getMimeType

      public String getMimeType()
    • getCharset

      public Charset getCharset()
    • getParameter

      public String getParameter(String name)
      Since:
      4.3
    • toString

      public String toString()
      Generates textual representation of this content type which can be used as the value of a Content-Type header.
      Overrides:
      toString in class Object
    • valid

      private static boolean valid(String s)
    • create

      public static ContentType create(String mimeType, Charset charset)
      Creates a new instance of ContentType.
      Parameters:
      mimeType - MIME type. It may not be null or empty. It may not contain characters <">, <;>, <,> reserved by the HTTP specification.
      charset - charset.
      Returns:
      content type
    • create

      public static ContentType create(String mimeType)
      Creates a new instance of ContentType without a charset.
      Parameters:
      mimeType - MIME type. It may not be null or empty. It may not contain characters <">, <;>, <,> reserved by the HTTP specification.
      Returns:
      content type
    • create

      public static ContentType create(String mimeType, String charset) throws UnsupportedCharsetException
      Creates a new instance of ContentType.
      Parameters:
      mimeType - MIME type. It may not be null or empty. It may not contain characters <">, <;>, <,> reserved by the HTTP specification.
      charset - charset. It may not contain characters <">, <;>, <,> reserved by the HTTP specification. This parameter is optional.
      Returns:
      content type
      Throws:
      UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
    • create

      private static ContentType create(HeaderElement helem, boolean strict)
    • create

      private static ContentType create(String mimeType, NameValuePair[] params, boolean strict)
    • create

      public static ContentType create(String mimeType, NameValuePair... params) throws UnsupportedCharsetException
      Creates a new instance of ContentType with the given parameters.
      Parameters:
      mimeType - MIME type. It may not be null or empty. It may not contain characters <">, <;>, <,> reserved by the HTTP specification.
      params - parameters.
      Returns:
      content type
      Throws:
      UnsupportedCharsetException
      Since:
      4.4
    • parse

      Parses textual representation of Content-Type value.
      Parameters:
      s - text
      Returns:
      content type
      Throws:
      ParseException - if the given text does not represent a valid Content-Type value.
      UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
    • get

      Extracts Content-Type value from HttpEntity exactly as specified by the Content-Type header of the entity. Returns null if not specified.
      Parameters:
      entity - HTTP entity
      Returns:
      content type
      Throws:
      ParseException - if the given text does not represent a valid Content-Type value.
      UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
    • getLenient

      public static ContentType getLenient(HttpEntity entity)
      Extracts Content-Type value from HttpEntity. Returns null if not specified or incorrect (could not be parsed)..
      Parameters:
      entity - HTTP entity
      Returns:
      content type
      Since:
      4.4
    • getOrDefault

      public static ContentType getOrDefault(HttpEntity entity) throws ParseException, UnsupportedCharsetException
      Extracts Content-Type value from HttpEntity or returns the default value DEFAULT_TEXT if not explicitly specified.
      Parameters:
      entity - HTTP entity
      Returns:
      content type
      Throws:
      ParseException - if the given text does not represent a valid Content-Type value.
      UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
    • getLenientOrDefault

      public static ContentType getLenientOrDefault(HttpEntity entity) throws ParseException, UnsupportedCharsetException
      Extracts Content-Type value from HttpEntity or returns the default value DEFAULT_TEXT if not explicitly specified or incorrect (could not be parsed).
      Parameters:
      entity - HTTP entity
      Returns:
      content type
      Throws:
      ParseException
      UnsupportedCharsetException
      Since:
      4.4
    • getByMimeType

      public static ContentType getByMimeType(String mimeType)
      Returns Content-Type for the given MIME type.
      Parameters:
      mimeType - MIME type
      Returns:
      content type or null if not known.
      Since:
      4.5
    • withCharset

      public ContentType withCharset(Charset charset)
      Creates a new instance with this MIME type and the given Charset.
      Parameters:
      charset - charset
      Returns:
      a new instance with this MIME type and the given Charset.
      Since:
      4.3
    • withCharset

      public ContentType withCharset(String charset)
      Creates a new instance with this MIME type and the given Charset name.
      Parameters:
      charset - name
      Returns:
      a new instance with this MIME type and the given Charset name.
      Throws:
      UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
      Since:
      4.3
    • withParameters

      public ContentType withParameters(NameValuePair... params) throws UnsupportedCharsetException
      Creates a new instance with this MIME type and the given parameters.
      Parameters:
      params -
      Returns:
      a new instance with this MIME type and the given parameters.
      Throws:
      UnsupportedCharsetException
      Since:
      4.4