Class HttpMediaType

java.lang.Object
com.google.api.client.http.HttpMediaType

public final class HttpMediaType extends Object
HTTP Media-type as specified in the HTTP RFC.

Implementation is not thread-safe.

Since:
1.10
  • Field Details

    • TYPE_REGEX

      private static final Pattern TYPE_REGEX
      Matches a valid media type or '*' (examples: "text" or "*").
    • TOKEN_REGEX

      private static final Pattern TOKEN_REGEX
      Matches a valid token which might be used as a type, key parameter or key value.
    • FULL_MEDIA_TYPE_REGEX

      private static final Pattern FULL_MEDIA_TYPE_REGEX
      The pattern matching the full HTTP media type string.
    • PARAMETER_REGEX

      private static final Pattern PARAMETER_REGEX
      The pattern matching a single parameter (key, value) at a time.
    • type

      private String type
      The main type of the media type, for example "text".
    • subType

      private String subType
      The sub type of the media type, for example "plain".
    • parameters

      private final SortedMap<String,String> parameters
      Additional parameters to the media type, for example "charset=utf-8".
    • cachedBuildResult

      private String cachedBuildResult
      The last build result or null.
  • Constructor Details

    • HttpMediaType

      public HttpMediaType(String type, String subType)
      Initializes the HttpMediaType by setting the specified media type.
      Parameters:
      type - main media type, for example "text"
      subType - sub media type, for example "plain"
    • HttpMediaType

      public HttpMediaType(String mediaType)
      Creates a HttpMediaType by parsing the specified media type string.
      Parameters:
      mediaType - full media type string, for example "text/plain; charset=utf-8"
  • Method Details

    • setType

      public HttpMediaType setType(String type)
      Sets the (main) media type, for example "text".
      Parameters:
      type - main/major media type
    • getType

      public String getType()
      Returns the main media type, for example "text", or null for '*'.
    • setSubType

      public HttpMediaType setSubType(String subType)
      Sets the sub media type, for example "plain" when using "text".
      Parameters:
      subType - sub media type
    • getSubType

      public String getSubType()
      Returns the sub media type, for example "plain" when using "text".
    • fromString

      private HttpMediaType fromString(String combinedType)
      Sets the full media type by parsing a full content-type string, for example "text/plain; foo=bar".

      This method will not clear existing parameters. Use clearParameters() if this behavior is required.

      Parameters:
      combinedType - full media type in the "maintype/subtype; key=value" format.
    • setParameter

      public HttpMediaType setParameter(String name, String value)
      Sets the media parameter to the specified value.
      Parameters:
      name - case-insensitive name of the parameter
      value - value of the parameter or null to remove
    • getParameter

      public String getParameter(String name)
      Returns the value of the specified parameter or null if not found.
      Parameters:
      name - name of the parameter
    • removeParameter

      public HttpMediaType removeParameter(String name)
      Removes the specified media parameter.
      Parameters:
      name - parameter to remove
    • clearParameters

      public void clearParameters()
      Removes all set parameters from this media type.
    • getParameters

      public Map<String,String> getParameters()
      Returns an unmodifiable map of all specified parameters. Parameter names will be stored in lower-case in this map.
    • matchesToken

      static boolean matchesToken(String value)
      Returns whether the given value matches the regular expression for "token" as specified in RFC 2616 section 2.2.
    • quoteString

      private static String quoteString(String unquotedString)
    • build

      public String build()
      Builds the full media type string which can be passed in the Content-Type header.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equalsIgnoreParameters

      public boolean equalsIgnoreParameters(HttpMediaType mediaType)
      Returns true if the specified media type has both the same type and subtype, or false if they don't match or the media type is null.
    • equalsIgnoreParameters

      public static boolean equalsIgnoreParameters(String mediaTypeA, String mediaTypeB)
      Returns true if the two specified media types have the same type and subtype, or if both types are null.
    • setCharsetParameter

      public HttpMediaType setCharsetParameter(Charset charset)
      Sets the charset parameter of the media type.
      Parameters:
      charset - new value for the charset parameter or null to remove
    • getCharsetParameter

      public Charset getCharsetParameter()
      Returns the specified charset or null if unset.
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object