org.apache.axiom.mime

## Class ContentType

- java.lang.Object
- - org.apache.axiom.mime.ContentType

-     public final class ContentType
      extends Object

  Represents the (parsed) value of a `Content-Type` header as defined by RFC 2045.

  The relevant productions from RFC 2045 and RFC 822 are:

       content := "Content-Type" ":" type "/" subtype *(";" parameter)
       parameter := attribute "=" value
       attribute := token
       value := token / quoted-string
       token := 1*<any (US-ASCII) CHAR except SPACE, CTLs, or tspecials>
       tspecials := "(" / ")" / "<" / ">" / "@" / "," / ";" / ":" / "\" / <"> / "/" / "[" / "]" / "?" / "="
       quoted-string := <"> *(qtext/quoted-pair) <">
       qtext := <any CHAR excepting <">, "\" & CR, and including linear-white-space>
       quoted-pair := "\" CHAR

  This class is similar to `MimeType` and JavaMail's `ContentType` class, but the following differences exist:

  - This class is more lenient than (certain implementations of) `MimeType`. It will accept content types that are not strictly valid, but that are commonly found. E.g. it will accept content types with an extra semicolon at the end.
  - This class is immutable.
  - This class makes a distinction between a media type (which is defined by a primary type and a sub type and represented by a `MediaType` object) and a content type, which is defined by a media type and a set of parameters.

  Another reason for the existence of this class is to avoid a dependency on JavaMail.

  Note that this class doesn't override `Object.equals(Object)` because there is no meaningful way to compare content types with parameters.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ContentType(MediaType mediaType, String[] parameters)` Constructor.
    `ContentType(String type)` Constructor that parses a `Content-Type` header value.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `MediaType` | `getMediaType()` Get the media type this content type refers to.
    `String` | `getParameter(String name)` Get the specified parameter value.
    `String` | `toString()` Create a string representation of this content type suitable as the value for a `Content-Type` header as specified by RFC 2045.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### ContentType

          public ContentType(MediaType mediaType,
                             String[] parameters)

      Constructor.
      Parameters:
        `mediaType` - the media type
        `parameters` - an array specifying the parameters as name/value pairs (with even entries representing the parameter names, and odd entries the corresponding values)

    - #### ContentType

          public ContentType(String type)
                      throws ParseException

      Constructor that parses a `Content-Type` header value.
      Parameters:
        `type` - the value of the `Content-Type` header conforming to RFC 2045
      Throws:
        `ParseException` - if the value is invalid and could not be parsed

  - ### Method Detail

    - #### getMediaType

          public MediaType getMediaType()

      Get the media type this content type refers to.
      Returns:
        the media type

    - #### getParameter

          public String getParameter(String name)

      Get the specified parameter value.
      Parameters:
        `name` - the parameter name
      Returns:
        the parameter value, or `null` if no parameter with the given name was found

    - #### toString

          public String toString()

      Create a string representation of this content type suitable as the value for a `Content-Type` header as specified by RFC 2045. Note that this method serializes all parameter values as quoted strings, even values that could be represented as tokens. This is compatible with R1109 in WS-I Basic Profile 1.2 and 2.0.
      Overrides:
        `toString` in class `Object`
      Returns:
        the string representation of this content type

Copyright © The Apache Software Foundation. All Rights Reserved.