Package com.google.api.client.http
Class HttpMediaType
- java.lang.Object
-
- com.google.api.client.http.HttpMediaType
-
public final class HttpMediaType extends java.lang.Object
HTTP Media-type as specified in the HTTP RFC ( {@link "http://tools.ietf.org/html/rfc2616#section-3.7"}).Implementation is not thread-safe.
- Since:
- 1.10
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
cachedBuildResult
The last build result ornull
.private static java.util.regex.Pattern
FULL_MEDIA_TYPE_REGEX
The pattern matching the full HTTP media type string.private static java.util.regex.Pattern
PARAMETER_REGEX
The pattern matching a single parameter (key, value) at a time.private java.util.SortedMap<java.lang.String,java.lang.String>
parameters
Additional parameters to the media type, for example"charset=utf-8"
.private java.lang.String
subType
The sub type of the media type, for example"plain"
.private static java.util.regex.Pattern
TOKEN_REGEX
Matches a valid token which might be used as a type, key parameter or key value.private java.lang.String
type
The main type of the media type, for example"text"
.private static java.util.regex.Pattern
TYPE_REGEX
Matches a valid media type or '*' (examples: "text" or "*").
-
Constructor Summary
Constructors Constructor Description HttpMediaType(java.lang.String mediaType)
Creates aHttpMediaType
by parsing the specified media type string.HttpMediaType(java.lang.String type, java.lang.String subType)
Initializes theHttpMediaType
by setting the specified media type.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
build()
Builds the full media type string which can be passed in the Content-Type header.void
clearParameters()
Removes all set parameters from this media type.boolean
equals(java.lang.Object obj)
boolean
equalsIgnoreParameters(HttpMediaType mediaType)
Returnstrue
if the specified media type has both the same type and subtype, orfalse
if they don't match or the media type isnull
.static boolean
equalsIgnoreParameters(java.lang.String mediaTypeA, java.lang.String mediaTypeB)
Returnstrue
if the two specified media types have the same type and subtype, or if both types arenull
.private HttpMediaType
fromString(java.lang.String combinedType)
Sets the full media type by parsing a full content-type string, for example"text/plain; foo=bar"
.java.nio.charset.Charset
getCharsetParameter()
Returns the specified charset ornull
if unset.java.lang.String
getParameter(java.lang.String name)
Returns the value of the specified parameter ornull
if not found.java.util.Map<java.lang.String,java.lang.String>
getParameters()
Returns an unmodifiable map of all specified parameters.java.lang.String
getSubType()
Returns the sub media type, for example"plain"
when using"text"
.java.lang.String
getType()
Returns the main media type, for example"text"
, ornull
for '*'.int
hashCode()
(package private) static boolean
matchesToken(java.lang.String value)
Returns whether the given value matches the regular expression for "token" as specified in RFC 2616 section 2.2.private static java.lang.String
quoteString(java.lang.String unquotedString)
HttpMediaType
removeParameter(java.lang.String name)
Removes the specified media parameter.HttpMediaType
setCharsetParameter(java.nio.charset.Charset charset)
Sets the charset parameter of the media type.HttpMediaType
setParameter(java.lang.String name, java.lang.String value)
Sets the media parameter to the specified value.HttpMediaType
setSubType(java.lang.String subType)
Sets the sub media type, for example"plain"
when using"text"
.HttpMediaType
setType(java.lang.String type)
Sets the (main) media type, for example"text"
.java.lang.String
toString()
-
-
-
Field Detail
-
TYPE_REGEX
private static final java.util.regex.Pattern TYPE_REGEX
Matches a valid media type or '*' (examples: "text" or "*").
-
TOKEN_REGEX
private static final java.util.regex.Pattern TOKEN_REGEX
Matches a valid token which might be used as a type, key parameter or key value.
-
FULL_MEDIA_TYPE_REGEX
private static final java.util.regex.Pattern FULL_MEDIA_TYPE_REGEX
The pattern matching the full HTTP media type string.
-
PARAMETER_REGEX
private static final java.util.regex.Pattern PARAMETER_REGEX
The pattern matching a single parameter (key, value) at a time.
-
type
private java.lang.String type
The main type of the media type, for example"text"
.
-
subType
private java.lang.String subType
The sub type of the media type, for example"plain"
.
-
parameters
private final java.util.SortedMap<java.lang.String,java.lang.String> parameters
Additional parameters to the media type, for example"charset=utf-8"
.
-
cachedBuildResult
private java.lang.String cachedBuildResult
The last build result ornull
.
-
-
Constructor Detail
-
HttpMediaType
public HttpMediaType(java.lang.String type, java.lang.String subType)
Initializes theHttpMediaType
by setting the specified media type.- Parameters:
type
- main media type, for example"text"
subType
- sub media type, for example"plain"
-
HttpMediaType
public HttpMediaType(java.lang.String mediaType)
Creates aHttpMediaType
by parsing the specified media type string.- Parameters:
mediaType
- full media type string, for example"text/plain; charset=utf-8"
-
-
Method Detail
-
setType
public HttpMediaType setType(java.lang.String type)
Sets the (main) media type, for example"text"
.- Parameters:
type
- main/major media type
-
getType
public java.lang.String getType()
Returns the main media type, for example"text"
, ornull
for '*'.
-
setSubType
public HttpMediaType setSubType(java.lang.String subType)
Sets the sub media type, for example"plain"
when using"text"
.- Parameters:
subType
- sub media type
-
getSubType
public java.lang.String getSubType()
Returns the sub media type, for example"plain"
when using"text"
.
-
fromString
private HttpMediaType fromString(java.lang.String combinedType)
Sets the full media type by parsing a full content-type string, for example"text/plain; foo=bar"
.This method will not clear existing parameters. Use
clearParameters()
if this behavior is required.- Parameters:
combinedType
- full media type in the"maintype/subtype; key=value"
format.
-
setParameter
public HttpMediaType setParameter(java.lang.String name, java.lang.String value)
Sets the media parameter to the specified value.- Parameters:
name
- case-insensitive name of the parametervalue
- value of the parameter ornull
to remove
-
getParameter
public java.lang.String getParameter(java.lang.String name)
Returns the value of the specified parameter ornull
if not found.- Parameters:
name
- name of the parameter
-
removeParameter
public HttpMediaType removeParameter(java.lang.String name)
Removes the specified media parameter.- Parameters:
name
- parameter to remove
-
clearParameters
public void clearParameters()
Removes all set parameters from this media type.
-
getParameters
public java.util.Map<java.lang.String,java.lang.String> getParameters()
Returns an unmodifiable map of all specified parameters. Parameter names will be stored in lower-case in this map.
-
matchesToken
static boolean matchesToken(java.lang.String value)
Returns whether the given value matches the regular expression for "token" as specified in RFC 2616 section 2.2.
-
quoteString
private static java.lang.String quoteString(java.lang.String unquotedString)
-
build
public java.lang.String build()
Builds the full media type string which can be passed in the Content-Type header.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equalsIgnoreParameters
public boolean equalsIgnoreParameters(HttpMediaType mediaType)
Returnstrue
if the specified media type has both the same type and subtype, orfalse
if they don't match or the media type isnull
.
-
equalsIgnoreParameters
public static boolean equalsIgnoreParameters(java.lang.String mediaTypeA, java.lang.String mediaTypeB)
Returnstrue
if the two specified media types have the same type and subtype, or if both types arenull
.
-
setCharsetParameter
public HttpMediaType setCharsetParameter(java.nio.charset.Charset charset)
Sets the charset parameter of the media type.- Parameters:
charset
- new value for the charset parameter ornull
to remove
-
getCharsetParameter
public java.nio.charset.Charset getCharsetParameter()
Returns the specified charset ornull
if unset.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-