Class ContentType

java.lang.Object
uk.ac.starlink.util.ContentType

public class ContentType extends Object
Represents a Content Type (MIME type) string. Most of the work is done by the {link #parseContentType} factory method.

This takes care of things like optional whitespace and case folding, so for instance if ctypeTxt has the value

    APPLICATION / X-VOTABLE+XML ; content=datalink; CHARSET="iso\-8859\-1"
 
then
    ContentType ctype = CointentType.parse(ctypeTxt);
    assert ctype.matches("application", "x-votable+xml");
    assert ctype.getParameter("charset").equals("iso-8859-1");
 
Since:
17 Nov 2017
Author:
Mark Taylor
See Also:
  • Constructor Details

    • ContentType

      public ContentType(String type, String subtype)
      Constructs a ContentType from type and subtype strings. Case is normalised (to lower case).
      Parameters:
      type - type part
      subtype - subtype part
    • ContentType

      public ContentType(String type, String subtype, Map<String,String> params)
      Constructs a ContentType from its constituent parts. Case is normalised (to lower case) for the case-insensitive parts, that is type, subtype and parameter names.
      Parameters:
      type - type part
      subtype - subtype part
      params - map of parameters
  • Method Details

    • getType

      public String getType()
      Returns the Type part of this content type.
      Returns:
      type
    • getSubtype

      public String getSubtype()
      Returns the Subtype part of this content type.
      Returns:
      subtype
    • getParameters

      public Map<String,String> getParameters()
      Returns the parameter name/value pairs of this content type. The parameter names (keys of the returned map) will always be in lower case.
      Returns:
      name/value pairs as an ordered map
    • matches

      public boolean matches(String type, String subtype)
      Indicates whether the type and subtype match a given pair.
      Parameters:
      type - required type part (case-insensitive)
      subtype - required subtype part (case-insensitive)
      Returns:
      true iff type and subtype match those of this content-type
    • getParameter

      public String getParameter(String paramName)
      Returns the value of a parameter of this content type.
      Parameters:
      paramName - parameter name (case-insensitive)
      Returns:
      parameter value, or null if no such parameter
    • hashCode

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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

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

      public static ContentType parseContentType(String txt)
      Parses a Content-Type (MIME type) string in accordance with the syntax rules in RFC2045. Such strings look something like "type/subtype[;p1=v1;p2=v2...]". It may not be completely bulletproof, but should do a fairly good job of the parse. However, it makes no attempt to restrict the type or subtype to IANA-approved values, and it may parse some strings which are not strictly legal.

      Null is returned if the string cannot be parsed.

      Parameters:
      txt - content-type string of the approximate form type/subtype(;param=value)*
      Returns:
      ContentType object if txt can be parsed, otherwise null
      See Also:
    • main

      public static void main(String[] args)
      Parses a single content-type string supplied on the command line, and prints a representation of the parsed form on standard output.