Package org.apache.hc.core5.http
Class ContentType
- java.lang.Object
-
- org.apache.hc.core5.http.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_NDJSONPublic constant media type forapplication/x-ndjson.static ContentTypeAPPLICATION_OCTET_STREAMstatic ContentTypeAPPLICATION_PDFPublic constant media type forapplication/pdf.static ContentTypeAPPLICATION_PROBLEM_JSONPublic constant media type forapplication/problem+json.static ContentTypeAPPLICATION_PROBLEM_XMLPublic constant media type forapplication/problem+xml.static ContentTypeAPPLICATION_RSS_XMLPublic constant media type forapplication/rss+xml.static ContentTypeAPPLICATION_SOAP_XMLstatic ContentTypeAPPLICATION_SVG_XMLstatic ContentTypeAPPLICATION_XHTML_XMLstatic ContentTypeAPPLICATION_XMLprivate java.nio.charset.Charsetcharsetprivate static java.lang.StringCHARSETParam that representcharsetconstant.private static java.util.Map<java.lang.String,ContentType>CONTENT_TYPE_MAPDeprecated.To be removed in 6.0static ContentTypeDEFAULT_BINARYstatic ContentTypeDEFAULT_TEXTprivate static NameValuePair[]EMPTY_NAME_VALUE_PAIR_ARRAYAn empty immutableNameValuePairarray.static ContentTypeIMAGE_BMPstatic ContentTypeIMAGE_GIFstatic ContentTypeIMAGE_JPEGstatic ContentTypeIMAGE_PNGstatic ContentTypeIMAGE_SVGstatic ContentTypeIMAGE_TIFFstatic ContentTypeIMAGE_WEBPprivate java.lang.StringmimeTypestatic ContentTypeMULTIPART_FORM_DATAstatic ContentTypeMULTIPART_MIXEDPublic constant media type formultipart/mixed.static ContentTypeMULTIPART_RELATEDPublic constant media type formultipart/related.private NameValuePair[]paramsprivate static longserialVersionUIDstatic ContentTypeTEXT_EVENT_STREAMPublic constant media type fortext/event-stream.static ContentTypeTEXT_HTMLstatic ContentTypeTEXT_MARKDOWNPublic constant media type fortext/markdown.static ContentTypeTEXT_PLAINstatic ContentTypeTEXT_XMLstatic ContentTypeWILDCARD
-
Constructor Summary
Constructors Constructor Description ContentType(java.lang.String mimeType, java.nio.charset.Charset charset)ContentType(java.lang.String mimeType, java.nio.charset.Charset charset, NameValuePair[] params)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated 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.private static ContentTypecreate(java.lang.String mimeType, NameValuePair[] params, boolean strict)private static ContentTypecreate(HeaderElement helem, boolean strict)static ContentTypegetByMimeType(java.lang.String mimeType)Deprecated.Do not use.java.nio.charset.CharsetgetCharset()java.nio.charset.CharsetgetCharset(java.nio.charset.Charset defaultCharset)Gets this Charset if it's non-null, otherwise, return the givendefaultCharset.static java.nio.charset.CharsetgetCharset(ContentType contentType, java.nio.charset.Charset defaultCharset)Gets a ContentType's Charset if neither are null, otherwise, return the givendefaultCharset.java.lang.StringgetMimeType()java.lang.StringgetParameter(java.lang.String name)booleanisSameMimeType(ContentType contentType)static ContentTypeparse(java.lang.CharSequence s)Parses textual representation ofContent-Typevalue.private static ContentTypeparse(java.lang.CharSequence s, boolean strict)static ContentTypeparseLenient(java.lang.CharSequence s)Parses textual representation ofContent-Typevalue ignoring invalid charsets.java.lang.StringtoString()Generates textual representation of this content type which can be used as the value of aContent-Typeheader.private static booleanvalid(java.lang.String s)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
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
CHARSET
private static final java.lang.String CHARSET
Param that representcharsetconstant.- See Also:
- Constant Field Values
-
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_NDJSON
public static final ContentType APPLICATION_NDJSON
Public constant media type forapplication/x-ndjson.- Since:
- 5.1
-
APPLICATION_OCTET_STREAM
public static final ContentType APPLICATION_OCTET_STREAM
-
APPLICATION_PDF
public static final ContentType APPLICATION_PDF
Public constant media type forapplication/pdf.- Since:
- 5.1
-
APPLICATION_SOAP_XML
public static final ContentType APPLICATION_SOAP_XML
-
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
-
APPLICATION_PROBLEM_JSON
public static final ContentType APPLICATION_PROBLEM_JSON
Public constant media type forapplication/problem+json.- Since:
- 5.1
- See Also:
- Problem Details for HTTP APIs, 6.1. application/problem+json
-
APPLICATION_PROBLEM_XML
public static final ContentType APPLICATION_PROBLEM_XML
Public constant media type forapplication/problem+xml.- Since:
- 5.1
- See Also:
- Problem Details for HTTP APIs, 6.2. application/problem+xml
-
APPLICATION_RSS_XML
public static final ContentType APPLICATION_RSS_XML
Public constant media type forapplication/rss+xml.- Since:
- 5.1
-
IMAGE_BMP
public static final ContentType IMAGE_BMP
-
IMAGE_GIF
public static final ContentType IMAGE_GIF
-
IMAGE_JPEG
public static final ContentType IMAGE_JPEG
-
IMAGE_PNG
public static final ContentType IMAGE_PNG
-
IMAGE_SVG
public static final ContentType IMAGE_SVG
-
IMAGE_TIFF
public static final ContentType IMAGE_TIFF
-
IMAGE_WEBP
public static final ContentType IMAGE_WEBP
-
MULTIPART_FORM_DATA
public static final ContentType MULTIPART_FORM_DATA
-
MULTIPART_MIXED
public static final ContentType MULTIPART_MIXED
Public constant media type formultipart/mixed.- Since:
- 5.1
-
MULTIPART_RELATED
public static final ContentType MULTIPART_RELATED
Public constant media type formultipart/related.- Since:
- 5.1
-
TEXT_HTML
public static final ContentType TEXT_HTML
-
TEXT_MARKDOWN
public static final ContentType TEXT_MARKDOWN
Public constant media type fortext/markdown.- Since:
- 5.1
-
TEXT_PLAIN
public static final ContentType TEXT_PLAIN
-
TEXT_XML
public static final ContentType TEXT_XML
-
TEXT_EVENT_STREAM
public static final ContentType TEXT_EVENT_STREAM
Public constant media type fortext/event-stream.- Since:
- 5.1
- See Also:
- Server-Sent Events W3C recommendation
-
WILDCARD
public static final ContentType WILDCARD
-
EMPTY_NAME_VALUE_PAIR_ARRAY
private static final NameValuePair[] EMPTY_NAME_VALUE_PAIR_ARRAY
An empty immutableNameValuePairarray.
-
CONTENT_TYPE_MAP
@Deprecated private static final java.util.Map<java.lang.String,ContentType> CONTENT_TYPE_MAP
Deprecated.To be removed in 6.0
-
DEFAULT_TEXT
public static final ContentType DEFAULT_TEXT
-
DEFAULT_BINARY
public static final ContentType DEFAULT_BINARY
-
mimeType
private final java.lang.String mimeType
-
charset
private final java.nio.charset.Charset charset
-
params
private final NameValuePair[] params
-
-
Constructor Detail
-
ContentType
ContentType(java.lang.String mimeType, java.nio.charset.Charset charset)
-
ContentType
ContentType(java.lang.String mimeType, java.nio.charset.Charset charset, NameValuePair[] params)
-
-
Method Detail
-
getMimeType
public java.lang.String getMimeType()
-
getCharset
public java.nio.charset.Charset getCharset()
-
getCharset
public java.nio.charset.Charset getCharset(java.nio.charset.Charset defaultCharset)
Gets this Charset if it's non-null, otherwise, return the givendefaultCharset.- Parameters:
defaultCharset- A default Charset.- Returns:
- this Charset if it's non-null, or the given
defaultCharset. - Since:
- 5.2
-
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
-
valid
private static boolean valid(java.lang.String s)
-
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
private static ContentType create(HeaderElement helem, boolean strict)
-
create
private static ContentType create(java.lang.String mimeType, NameValuePair[] params, boolean strict)
-
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.CharSequence s) throws java.nio.charset.UnsupportedCharsetException
Parses textual representation ofContent-Typevalue.- Parameters:
s- text- Returns:
- content type
Content-Typevalue or null. - Throws:
java.nio.charset.UnsupportedCharsetException- Thrown when the named charset is not available in this instance of the Java virtual machine
-
parseLenient
public static ContentType parseLenient(java.lang.CharSequence s) throws java.nio.charset.UnsupportedCharsetException
Parses textual representation ofContent-Typevalue ignoring invalid charsets.- Parameters:
s- text- Returns:
- content type
Content-Typevalue or null. - Throws:
java.nio.charset.UnsupportedCharsetException- Thrown when the named charset is not available in this instance of the Java virtual machine
-
parse
private static ContentType parse(java.lang.CharSequence s, boolean strict) throws java.nio.charset.UnsupportedCharsetException
- Throws:
java.nio.charset.UnsupportedCharsetException
-
getByMimeType
@Deprecated public static ContentType getByMimeType(java.lang.String mimeType)
Deprecated.Do not use. This method was made public by mistake.ReturnsContent-Typefor the given MIME type.- Parameters:
mimeType- MIME type- Returns:
- content type or
nullif not known. - Since:
- 4.5
-
getCharset
public static java.nio.charset.Charset getCharset(ContentType contentType, java.nio.charset.Charset defaultCharset)
Gets a ContentType's Charset if neither are null, otherwise, return the givendefaultCharset.- Parameters:
contentType- the ContentType to test and query.defaultCharset- a default Charset.- Returns:
- the ContentType's Charset if neither are null, otherwise, return the given
defaultCharset. - Since:
- 5.2
-
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- parameters.- Returns:
- a new instance with this MIME type and the given parameters.
- Throws:
java.nio.charset.UnsupportedCharsetException- Since:
- 4.4
-
isSameMimeType
public boolean isSameMimeType(ContentType contentType)
-
-