public class

ContentType

extends Object
implements Serializable
java.lang.Object
   ↳ com.google.gdata.util.ContentType

Class Overview

Simple class for parsing and generating Content-Type header values, per RFC 2045 (MIME) and 2616 (HTTP 1.1).

Summary

Constants
String ATTR_CHARSET Name of the attribute that contains the encoding character set for the content type.
String DEFAULT_CHARSET The UTF-8 charset encoding is used by default for all text and xml based MIME types.
String STAR Special "*" character to match any type or subtype.
Fields
public static final ContentType ANY Wildcard content type that will match any MIME type
public static final ContentType APPLICATION_XML A ContentType constant that describes the application/xml content type.
public static final ContentType ATOM A ContentType constant that describes the base unqualified Atom content type.
public static final ContentType ATOM_ENTRY A ContentType constant that describes the qualified Atom entry content type.
public static final ContentType ATOM_FEED A ContentType constant that describes the qualified Atom feed content type.
public static final ContentType ATOM_SERVICE A ContentType constant that describes the Atom Service content type.
private static Pattern ATTR_PATTERN
public static final ContentType GDATA_ERROR A ContentType constant that describes the GData error content type.
public static final ContentType JAVASCRIPT A ContentType constant that describes the Javascript content type.
public static final ContentType JSON A ContentType constant that describes the JSON content type.
public static final ContentType MESSAGE_RFC822 A ContentType constant that indicates that the body contains an encapsulated message, with the syntax of an RFC 822 email message.
public static final ContentType MULTIPART_RELATED A ContentType constant that describes the MIME multipart/related content type.
public static final ContentType OPENSEARCH A ContentType constant that describes the OpenSearch description document
public static final ContentType RSS A ContentType constant that describes the RSS channel/item content type.
public static final ContentType TEXT_HTML A ContentType constant that describes the generic text/html content type.
public static final ContentType TEXT_PLAIN A ContentType constant that describes the generic text/plain content type.
public static final ContentType TEXT_XML A ContentType constant that describes the generic text/xml content type.
private static String TOKEN
private static Pattern TOKEN_PATTERN
private static Pattern TYPE_PATTERN
private HashMap<String, String> attributes
private boolean inferredCharset true if parsed input didn't contain charset encoding info
private boolean locked If set to true, the object is immutable.
private String subType
private String type
Public Constructors
ContentType()
Constructs a new instance with default media type
ContentType(String typeHeader)
Constructs a new instance from a content-type header value parsing the MIME content type (RFC2045) format.
Public Methods
boolean equals(Object o)
static ContentType getAtomEntry()
Returns the ContentType that should be used in contexts that expect an Atom entry.
static ContentType getAtomFeed()
Returns the ContentType that should be used in contexts that expect an Atom feed.
String getAttribute(String name)
Returns the additional attribute by name of the content type.
Map<String, String> getAttributes()
Returns the additional attributes of the content type.
static ContentType getBestContentType(String acceptHeader, List<ContentType> actualContentTypes)
Determines the best "Content-Type" header to use in a servlet response based on the "Accept" header from a servlet request.
String getCharset()
String getMediaType()
Returns the full media type
String getSubType()
String getType()
int hashCode()
ContentType lock()
Makes the object immutable and returns it.
boolean match(ContentType acceptedContentType)
Returns whether this content type is match by the content type found in the "Accept" header field of an HTTP request.
void setSubType(String subType)
void setType(String type)
String toString()
Generates the Content-Type value
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String ATTR_CHARSET

Name of the attribute that contains the encoding character set for the content type.

See Also
Constant Value: "charset"

private static final String DEFAULT_CHARSET

The UTF-8 charset encoding is used by default for all text and xml based MIME types.

Constant Value: "charset=UTF-8"

private static final String STAR

Special "*" character to match any type or subtype.

Constant Value: "*"

Fields

public static final ContentType ANY

Wildcard content type that will match any MIME type

public static final ContentType APPLICATION_XML

A ContentType constant that describes the application/xml content type.

public static final ContentType ATOM

A ContentType constant that describes the base unqualified Atom content type.

public static final ContentType ATOM_ENTRY

A ContentType constant that describes the qualified Atom entry content type.

See Also

public static final ContentType ATOM_FEED

A ContentType constant that describes the qualified Atom feed content type.

See Also

public static final ContentType ATOM_SERVICE

A ContentType constant that describes the Atom Service content type.

private static Pattern ATTR_PATTERN

public static final ContentType GDATA_ERROR

A ContentType constant that describes the GData error content type.

public static final ContentType JAVASCRIPT

A ContentType constant that describes the Javascript content type.

public static final ContentType JSON

A ContentType constant that describes the JSON content type.

public static final ContentType MESSAGE_RFC822

A ContentType constant that indicates that the body contains an encapsulated message, with the syntax of an RFC 822 email message.

public static final ContentType MULTIPART_RELATED

A ContentType constant that describes the MIME multipart/related content type.

public static final ContentType OPENSEARCH

A ContentType constant that describes the OpenSearch description document

public static final ContentType RSS

A ContentType constant that describes the RSS channel/item content type.

public static final ContentType TEXT_HTML

A ContentType constant that describes the generic text/html content type.

public static final ContentType TEXT_PLAIN

A ContentType constant that describes the generic text/plain content type.

public static final ContentType TEXT_XML

A ContentType constant that describes the generic text/xml content type.

private static String TOKEN

private static Pattern TOKEN_PATTERN

private static Pattern TYPE_PATTERN

private HashMap<String, String> attributes

private boolean inferredCharset

true if parsed input didn't contain charset encoding info

private boolean locked

If set to true, the object is immutable.

private String subType

private String type

Public Constructors

public ContentType ()

Constructs a new instance with default media type

public ContentType (String typeHeader)

Constructs a new instance from a content-type header value parsing the MIME content type (RFC2045) format. If the type is null, then media type and charset will be initialized to default values.

Parameters
typeHeader Content type value in RFC2045 header format.

Public Methods

public boolean equals (Object o)

Parameters
o

public static ContentType getAtomEntry ()

Returns the ContentType that should be used in contexts that expect an Atom entry.

public static ContentType getAtomFeed ()

Returns the ContentType that should be used in contexts that expect an Atom feed.

public String getAttribute (String name)

Returns the additional attribute by name of the content type.

Parameters
name Attribute name

public Map<String, String> getAttributes ()

Returns the additional attributes of the content type.

public static ContentType getBestContentType (String acceptHeader, List<ContentType> actualContentTypes)

Determines the best "Content-Type" header to use in a servlet response based on the "Accept" header from a servlet request.

Parameters
acceptHeader "Accept" header value from a servlet request (not null)
actualContentTypes Actual content types in descending order of preference (non-empty, and each entry is of the form "type/subtype" without the wildcard char '*') or null if no "Accept" header was specified
Returns
  • the best content type to use (or null on no match).

public String getCharset ()

public String getMediaType ()

Returns the full media type

public String getSubType ()

public String getType ()

public int hashCode ()

public ContentType lock ()

Makes the object immutable and returns it. This should at least be used when keeping a ContentType instance as a static.

public boolean match (ContentType acceptedContentType)

Returns whether this content type is match by the content type found in the "Accept" header field of an HTTP request.

For atom content type, this method will check the optional attribute 'type'. If the type attribute is set in both this and acceptedContentType, then they must be the same. That is, application/atom+xml will match both application/atom+xml;type=feed and application/atom+xml;type=entry, but application/atom+xml;type=entry will not match application/atom+xml;type=feed.a

Parameters
acceptedContentType Content type found in the "Accept" header field of an HTTP request

public void setSubType (String subType)

Parameters
subType

public void setType (String type)

Parameters
type

public String toString ()

Generates the Content-Type value