Package org.glassfish.grizzly.http.util
Class ContentType
- java.lang.Object
-
- org.glassfish.grizzly.http.util.ContentType
-
- Direct Known Subclasses:
ContentType.SettableContentType
public class ContentType extends Object
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 Modifier and Type Class Description static classContentType.SettableContentTypeMutableContentTypeobject.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static byte[]compose(byte[] mimeType, String charset)Composes a content-type array, based on the mime-type represented by a byte array and a charset attribute value, represented by aString.Stringget()intgetArrayLen()Used in conjunction withgetByteArray().byte[]getByteArray()StringgetCharacterEncoding()static StringgetCharsetFromContentType(String contentType)Parse the character encoding from the specified content type header.StringgetMimeType()Returns the mime-type part of the content-type (the part without charset attribute).booleanisMimeTypeSet()booleanisSet()static ContentTypenewContentType(String contentType)Creates aContentTypewrapper over aStringcontent-type representation.static ContentTypenewContentType(String mimeType, String characterEncoding)Creates aContentTypewrapper over the passed mime-type and character encoding.static ContentType.SettableContentTypenewSettableContentType()ContentTypeprepare()Prepare the ContentType for the serialization.static byte[]removeCharset(byte[] contentType)Removes the charset attribute from the content-type represented by an array.protected voidreset()Resets the ContentType state.voidserializeToDataChunk(DataChunk dc)Serializes this ContentType value into a passedDataChunk.protected voidset(String contentType)Sets the content type.protected voidset(ContentType contentType)Sets the content type.protected voidsetCharacterEncoding(String charset)Sets the the character encoding (the content-type's charset attribute value).protected voidsetMimeType(String mimeType)Sets the mime-type part of the content-type (the part without charset attribute).StringtoString()
-
-
-
Method Detail
-
newSettableContentType
public static ContentType.SettableContentType newSettableContentType()
- Returns:
ContentType.SettableContentType, the mutableContentTyperepresentation.
-
newContentType
public static ContentType newContentType(String contentType)
Creates aContentTypewrapper over aStringcontent-type representation.- Parameters:
contentType-Stringcontent-type representation- Returns:
- a
ContentTypewrapper over aStringcontent-type representation
-
newContentType
public static ContentType newContentType(String mimeType, String characterEncoding)
Creates aContentTypewrapper over the passed mime-type and character encoding.- Parameters:
mimeType-StringmimeType-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
ContentTypewrapper over the passed mime-type and character encoding
-
prepare
public ContentType 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
public String 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
protected void setMimeType(String mimeType)
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
public String getCharacterEncoding()
- Returns:
- the character encoding (the content-type's charset attribute value)
-
setCharacterEncoding
protected void setCharacterEncoding(String charset)
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
public String get()
- Returns:
- the content type of this HTTP message.
-
set
protected void set(String contentType)
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
protected void set(ContentType contentType)
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
public void serializeToDataChunk(DataChunk dc)
Serializes this ContentType value into a passedDataChunk.- Parameters:
dc-DataChunk
-
reset
protected void reset()
Resets the ContentType state.
-
getCharsetFromContentType
public static String getCharsetFromContentType(String contentType)
Parse the character encoding from the specified content type header. If the content type is null, or there is no explicit character encoding,nullis 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
public static byte[] compose(byte[] mimeType, String charset)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
-
-