Package org.apache.http.entity
Class ContentType
- java.lang.Object
-
- org.apache.http.entity.ContentType
-
- All Implemented Interfaces:
java.io.Serializable
@Contract(threading=IMMUTABLE) public final class ContentType extends java.lang.Object implements java.io.Serializable
Content type information consisting of a MIME type and an optional charset.This class makes no attempts to verify validity of the MIME type. The input parameters of the
create(String, String)method, however, may not contain characters<">, <;>, <,>reserved by the HTTP specification.- Since:
- 4.2
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ContentTypeAPPLICATION_ATOM_XMLstatic ContentTypeAPPLICATION_FORM_URLENCODEDstatic ContentTypeAPPLICATION_JSONstatic ContentTypeAPPLICATION_OCTET_STREAMstatic ContentTypeAPPLICATION_SVG_XMLstatic ContentTypeAPPLICATION_XHTML_XMLstatic ContentTypeAPPLICATION_XMLstatic ContentTypeDEFAULT_BINARYstatic ContentTypeDEFAULT_TEXTstatic ContentTypeMULTIPART_FORM_DATAstatic ContentTypeTEXT_HTMLstatic ContentTypeTEXT_PLAINstatic ContentTypeTEXT_XMLstatic ContentTypeWILDCARD
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ContentTypecreate(java.lang.String mimeType)Creates a new instance ofContentTypewithout a charset.static ContentTypecreate(java.lang.String mimeType, java.lang.String charset)Creates a new instance ofContentType.static ContentTypecreate(java.lang.String mimeType, java.nio.charset.Charset charset)Creates a new instance ofContentType.static ContentTypecreate(java.lang.String mimeType, NameValuePair... params)Creates a new instance ofContentTypewith the given parameters.static ContentTypeget(HttpEntity entity)ExtractsContent-Typevalue fromHttpEntityexactly as specified by theContent-Typeheader of the entity.static ContentTypegetByMimeType(java.lang.String mimeType)ReturnsContent-Typefor the given MIME type.java.nio.charset.CharsetgetCharset()static ContentTypegetLenient(HttpEntity entity)ExtractsContent-Typevalue fromHttpEntity.static ContentTypegetLenientOrDefault(HttpEntity entity)ExtractsContent-Typevalue fromHttpEntityor returns the default valueDEFAULT_TEXTif not explicitly specified or incorrect (could not be parsed).java.lang.StringgetMimeType()static ContentTypegetOrDefault(HttpEntity entity)ExtractsContent-Typevalue fromHttpEntityor returns the default valueDEFAULT_TEXTif not explicitly specified.java.lang.StringgetParameter(java.lang.String name)static ContentTypeparse(java.lang.String s)Parses textual representation ofContent-Typevalue.java.lang.StringtoString()Generates textual representation of this content type which can be used as the value of aContent-Typeheader.ContentTypewithCharset(java.lang.String charset)Creates a new instance with this MIME type and the given Charset name.ContentTypewithCharset(java.nio.charset.Charset charset)Creates a new instance with this MIME type and the given Charset.ContentTypewithParameters(NameValuePair... params)Creates a new instance with this MIME type and the given parameters.
-
-
-
Field Detail
-
APPLICATION_ATOM_XML
public static final ContentType APPLICATION_ATOM_XML
-
APPLICATION_FORM_URLENCODED
public static final ContentType APPLICATION_FORM_URLENCODED
-
APPLICATION_JSON
public static final ContentType APPLICATION_JSON
-
APPLICATION_OCTET_STREAM
public static final ContentType APPLICATION_OCTET_STREAM
-
APPLICATION_SVG_XML
public static final ContentType APPLICATION_SVG_XML
-
APPLICATION_XHTML_XML
public static final ContentType APPLICATION_XHTML_XML
-
APPLICATION_XML
public static final ContentType APPLICATION_XML
-
MULTIPART_FORM_DATA
public static final ContentType MULTIPART_FORM_DATA
-
TEXT_HTML
public static final ContentType TEXT_HTML
-
TEXT_PLAIN
public static final ContentType TEXT_PLAIN
-
TEXT_XML
public static final ContentType TEXT_XML
-
WILDCARD
public static final ContentType WILDCARD
-
DEFAULT_TEXT
public static final ContentType DEFAULT_TEXT
-
DEFAULT_BINARY
public static final ContentType DEFAULT_BINARY
-
-
Method Detail
-
getMimeType
public java.lang.String getMimeType()
-
getCharset
public java.nio.charset.Charset getCharset()
-
getParameter
public java.lang.String getParameter(java.lang.String name)
- Since:
- 4.3
-
toString
public java.lang.String toString()
Generates textual representation of this content type which can be used as the value of aContent-Typeheader.- Overrides:
toStringin classjava.lang.Object
-
create
public static ContentType create(java.lang.String mimeType, java.nio.charset.Charset charset)
Creates a new instance ofContentType.- Parameters:
mimeType- MIME type. It may not benullor empty. It may not contain characters<">, <;>, <,>reserved by the HTTP specification.charset- charset.- Returns:
- content type
-
create
public static ContentType create(java.lang.String mimeType)
Creates a new instance ofContentTypewithout a charset.- Parameters:
mimeType- MIME type. It may not benullor empty. It may not contain characters<">, <;>, <,>reserved by the HTTP specification.- Returns:
- content type
-
create
public static ContentType create(java.lang.String mimeType, java.lang.String charset) throws java.nio.charset.UnsupportedCharsetException
Creates a new instance ofContentType.- Parameters:
mimeType- MIME type. It may not benullor empty. It may not contain characters<">, <;>, <,>reserved by the HTTP specification.charset- charset. It may not contain characters<">, <;>, <,>reserved by the HTTP specification. This parameter is optional.- Returns:
- content type
- Throws:
java.nio.charset.UnsupportedCharsetException- Thrown when the named charset is not available in this instance of the Java virtual machine
-
create
public static ContentType create(java.lang.String mimeType, NameValuePair... params) throws java.nio.charset.UnsupportedCharsetException
Creates a new instance ofContentTypewith the given parameters.- Parameters:
mimeType- MIME type. It may not benullor empty. It may not contain characters<">, <;>, <,>reserved by the HTTP specification.params- parameters.- Returns:
- content type
- Throws:
java.nio.charset.UnsupportedCharsetException- Since:
- 4.4
-
parse
public static ContentType parse(java.lang.String s) throws ParseException, java.nio.charset.UnsupportedCharsetException
Parses textual representation ofContent-Typevalue.- Parameters:
s- text- Returns:
- content type
- Throws:
ParseException- if the given text does not represent a validContent-Typevalue.java.nio.charset.UnsupportedCharsetException- Thrown when the named charset is not available in this instance of the Java virtual machine
-
get
public static ContentType get(HttpEntity entity) throws ParseException, java.nio.charset.UnsupportedCharsetException
ExtractsContent-Typevalue fromHttpEntityexactly as specified by theContent-Typeheader of the entity. Returnsnullif not specified.- Parameters:
entity- HTTP entity- Returns:
- content type
- Throws:
ParseException- if the given text does not represent a validContent-Typevalue.java.nio.charset.UnsupportedCharsetException- Thrown when the named charset is not available in this instance of the Java virtual machine
-
getLenient
public static ContentType getLenient(HttpEntity entity)
ExtractsContent-Typevalue fromHttpEntity. Returnsnullif not specified or incorrect (could not be parsed)..- Parameters:
entity- HTTP entity- Returns:
- content type
- Since:
- 4.4
-
getOrDefault
public static ContentType getOrDefault(HttpEntity entity) throws ParseException, java.nio.charset.UnsupportedCharsetException
ExtractsContent-Typevalue fromHttpEntityor returns the default valueDEFAULT_TEXTif not explicitly specified.- Parameters:
entity- HTTP entity- Returns:
- content type
- Throws:
ParseException- if the given text does not represent a validContent-Typevalue.java.nio.charset.UnsupportedCharsetException- Thrown when the named charset is not available in this instance of the Java virtual machine
-
getLenientOrDefault
public static ContentType getLenientOrDefault(HttpEntity entity) throws ParseException, java.nio.charset.UnsupportedCharsetException
ExtractsContent-Typevalue fromHttpEntityor returns the default valueDEFAULT_TEXTif not explicitly specified or incorrect (could not be parsed).- Parameters:
entity- HTTP entity- Returns:
- content type
- Throws:
ParseExceptionjava.nio.charset.UnsupportedCharsetException- Since:
- 4.4
-
getByMimeType
public static ContentType getByMimeType(java.lang.String mimeType)
ReturnsContent-Typefor the given MIME type.- Parameters:
mimeType- MIME type- Returns:
- content type or
nullif not known. - Since:
- 4.5
-
withCharset
public ContentType withCharset(java.nio.charset.Charset charset)
Creates a new instance with this MIME type and the given Charset.- Parameters:
charset- charset- Returns:
- a new instance with this MIME type and the given Charset.
- Since:
- 4.3
-
withCharset
public ContentType withCharset(java.lang.String charset)
Creates a new instance with this MIME type and the given Charset name.- Parameters:
charset- name- Returns:
- a new instance with this MIME type and the given Charset name.
- Throws:
java.nio.charset.UnsupportedCharsetException- Thrown when the named charset is not available in this instance of the Java virtual machine- Since:
- 4.3
-
withParameters
public ContentType withParameters(NameValuePair... params) throws java.nio.charset.UnsupportedCharsetException
Creates a new instance with this MIME type and the given parameters.- Parameters:
params-- Returns:
- a new instance with this MIME type and the given parameters.
- Throws:
java.nio.charset.UnsupportedCharsetException- Since:
- 4.4
-
-