public final class ContentType extends Object
To create a new content type application/json without character
set parameter:
ContentType ct = new ContentType("application", "json");
// Prints out "application/json"
System.out.println(ct.toString());
With a character set parameter application/json; charset=UTF-8:
ContentType ct = new ContentType("application", "json", new ContentType.Parameter("charset", "UTF-8"));
// Prints out "application/json; charset=UTF-8"
System.out.println(ct.toString());
To parse a content type:
try {
ContentType.parse("application/json; charset=UTF-8");
} catch (java.text.ParseException e) {
System.err.println(e.getMessage());
}
See RFC 2045, section 5.1.
| Modifier and Type | Class and Description |
|---|---|
static class |
ContentType.Parameter
Optional content type parameter, for example
charset=UTF-8. |
| Modifier and Type | Field and Description |
|---|---|
static ContentType |
APPLICATION_JOSE
Content type
application/jose; charset=UTF-8. |
static ContentType |
APPLICATION_JSON
Content type
application/json; charset=UTF-8. |
static ContentType |
APPLICATION_JWT
Content type
application/jwt; charset=UTF-8. |
static ContentType |
APPLICATION_PDF
Content type
application/pdf. |
static ContentType |
APPLICATION_URLENCODED
Content type
application/x-www-form-urlencoded; charset=UTF-8. |
static ContentType |
IMAGE_APNG
Content type
image/apng. |
static ContentType |
IMAGE_AVIF
Content type
image/avif. |
static ContentType |
IMAGE_GIF
Content type
image/gif. |
static ContentType |
IMAGE_JPEG
Content type
image/jpeg. |
static ContentType |
IMAGE_PNG
Content type
image/png. |
static ContentType |
IMAGE_SVG_XML
Content type
image/svg+xml. |
static ContentType |
IMAGE_WEBP
Content type
image/webp. |
static ContentType |
TEXT_PLAIN
Content type
text/plain; charset=UTF-8. |
| Constructor and Description |
|---|
ContentType(String baseType,
String subType,
Charset charset)
Creates a new content type with the specified character set.
|
ContentType(String baseType,
String subType,
ContentType.Parameter... param)
Creates a new content type.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object o) |
String |
getBaseType()
Returns the base type.
|
List<ContentType.Parameter> |
getParameters()
Returns the optional parameters.
|
String |
getSubType()
Returns the subtype.
|
String |
getType()
Returns the type.
|
int |
hashCode() |
boolean |
matches(ContentType other)
Returns
true if the types and subtypes match. |
static ContentType |
parse(String s)
Parses a content type from the specified string.
|
String |
toString() |
public static final ContentType APPLICATION_JSON
application/json; charset=UTF-8.public static final ContentType APPLICATION_JOSE
application/jose; charset=UTF-8.public static final ContentType APPLICATION_JWT
application/jwt; charset=UTF-8.public static final ContentType APPLICATION_URLENCODED
application/x-www-form-urlencoded; charset=UTF-8.public static final ContentType TEXT_PLAIN
text/plain; charset=UTF-8.public static final ContentType IMAGE_APNG
image/apng.public static final ContentType IMAGE_AVIF
image/avif.public static final ContentType IMAGE_GIF
image/gif.public static final ContentType IMAGE_JPEG
image/jpeg.public static final ContentType IMAGE_PNG
image/png.public static final ContentType IMAGE_SVG_XML
image/svg+xml.public static final ContentType IMAGE_WEBP
image/webp.public static final ContentType APPLICATION_PDF
application/pdf.public ContentType(String baseType, String subType, ContentType.Parameter... param)
baseType - The type. E.g. "application" from
"application/json".Must not be null or
empty.subType - The subtype. E.g. "json" from "application/json".
Must not be null or empty.param - Optional parameters.public ContentType(String baseType, String subType, Charset charset)
baseType - The base type. E.g. "application" from
"application/json".Must not be null or
empty.subType - The subtype. E.g. "json" from "application/json".
Must not be null or empty.charset - The character set to use for the charset
parameter. Must not be null.public String getBaseType()
public String getSubType()
public String getType()
public List<ContentType.Parameter> getParameters()
public boolean matches(ContentType other)
true if the types and subtypes match. The
parameters, if any, are ignored.other - The other content type, null if not specified.true if the types and subtypes match, else
false.public static ContentType parse(String s) throws ParseException
s - The string to parse.ParseException - If parsing failed or the string is
null or empty.Copyright © 2021 Connect2id Ltd.. All rights reserved.