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 Activation's MimeType and JavaMail's ContentType classes, 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 Details

    • ContentType

      public ContentType(MediaType mediaType, String... parameters)
      Constructor.
      Parameters:
      mediaType - the media type
      parameters - 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 Details

    • builder

      public static ContentType.Builder builder()
      Get a new builder instance.
      Returns:
      the builder
    • toBuilder

      public ContentType.Builder toBuilder()
      Get a new builder initialized with the media type and parameters from this instance.
      Returns:
      the builder
    • 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
    • isTextual

      public boolean isTextual()
      Check if the content type is textual, i.e. if an entity with this content type should be human readable. This information may be used to select a content transfer encoding.
      Returns:
      whether the content type is textual
    • 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