java.lang.Object
org.glassfish.grizzly.http.util.ContentType
- Direct Known Subclasses:
ContentType.SettableContentType
This class serves as a Content-Type holder, plus it implements useful utility methods to work with content-type.
- Author:
- Alexey Stashok
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
Composes a content-type array, based on the mime-type represented by a byte array and a charset attribute value, represented by aString
.get()
int
Used in conjunction withgetByteArray()
.byte[]
static String
getCharsetFromContentType
(String contentType) Parse the character encoding from the specified content type header.Returns the mime-type part of the content-type (the part without charset attribute).boolean
boolean
isSet()
static ContentType
newContentType
(String contentType) Creates aContentType
wrapper over aString
content-type representation.static ContentType
newContentType
(String mimeType, String characterEncoding) Creates aContentType
wrapper over the passed mime-type and character encoding.prepare()
Prepare the ContentType for the serialization.static byte[]
removeCharset
(byte[] contentType) Removes the charset attribute from the content-type represented by an array.protected void
reset()
Resets the ContentType state.void
Serializes this ContentType value into a passedDataChunk
.protected void
Sets the content type.protected void
set
(ContentType contentType) Sets the content type.protected void
setCharacterEncoding
(String charset) Sets the the character encoding (the content-type's charset attribute value).protected void
setMimeType
(String mimeType) Sets the mime-type part of the content-type (the part without charset attribute).toString()
-
Method Details
-
newSettableContentType
- Returns:
ContentType.SettableContentType
, the mutableContentType
representation.
-
newContentType
Creates aContentType
wrapper over aString
content-type representation.- Parameters:
contentType
-String
content-type representation- Returns:
- a
ContentType
wrapper over aString
content-type representation
-
newContentType
Creates aContentType
wrapper over the passed mime-type and character encoding.- Parameters:
mimeType
-String
mimeType-type representation (like "text/plain", "text/html", etc), which doesn't contain charset informationcharacterEncoding
- charset attribute to be used with the mime-type- Returns:
- a
ContentType
wrapper over the passed mime-type and character encoding
-
prepare
Prepare the ContentType for the serialization. This method might be particularly useful if we use the same ContentType over and over for different responses, so that the ContentType will not have to be parsed and prepared for each response separately.- Returns:
- this ContentType
-
isSet
public boolean isSet()- Returns:
- true if either mime-type or character encoding is set, or false otherwise
-
isMimeTypeSet
public boolean isMimeTypeSet()- Returns:
- true if mime-type is set, or false otherwise
-
getMimeType
Returns the mime-type part of the content-type (the part without charset attribute).- Returns:
- the mime-type part of the content-type (the part without charset attribute)
-
setMimeType
Sets the mime-type part of the content-type (the part without charset attribute).- Parameters:
mimeType
- the mime-type part of the content-type (the part without charset attribute)
-
getCharacterEncoding
- Returns:
- the character encoding (the content-type's charset attribute value)
-
setCharacterEncoding
Sets the the character encoding (the content-type's charset attribute value).- Parameters:
charset
- the character encoding (the content-type's charset attribute value)
-
getArrayLen
public int getArrayLen()Used in conjunction withgetByteArray()
. The array returned by the aforementioned method may be larger than the data contained therein. This method will return the proper data length.- Returns:
- the data length within the array returned by
getByteArray()
-
getByteArray
public byte[] getByteArray()- Returns:
- the byte array representation of the content-type
-
get
- Returns:
- the content type of this HTTP message.
-
set
Sets the content type. This method must preserve any charset that may already have been set via a call to request/response.setContentType(), request/response.setLocale(), or request/response.setCharacterEncoding().- Parameters:
contentType
- the content type
-
set
Sets the content type. This method must preserve any charset that may already have been set via a call to request/response.setContentType(), request/response.setLocale(), or request/response.setCharacterEncoding(). This method copies the passed contentType state into this ContentType.- Parameters:
contentType
- the content type
-
serializeToDataChunk
Serializes this ContentType value into a passedDataChunk
.- Parameters:
dc
-DataChunk
-
reset
protected void reset()Resets the ContentType state. -
toString
-
getCharsetFromContentType
Parse the character encoding from the specified content type header. If the content type is null, or there is no explicit character encoding,null
is returned.- Parameters:
contentType
- a content type header- Returns:
- the contentType's charset attribute value
-
removeCharset
public static byte[] removeCharset(byte[] contentType) Removes the charset attribute from the content-type represented by an array. The returned array will be completely independent of the source one.- Parameters:
contentType
- the content-type represented by an array- Returns:
- a new array, which represents the same content-type as a given one, but without charset attribute
-
compose
Composes a content-type array, based on the mime-type represented by a byte array and a charset attribute value, represented by aString
.- Parameters:
mimeType
- a mime-type part of the content-type (doesn't contain charset attribute)charset
- charset attribute value- Returns:
- a content-type array, composed of the mime-type represented by a byte array and a charset attribute value,
represented by a
String
-