Class 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
    • Method Detail

      • newContentType

        public static ContentType newContentType​(String contentType)
        Creates a ContentType wrapper over a String content-type representation.
        Parameters:
        contentType - String content-type representation
        Returns:
        a ContentType wrapper over a String content-type representation
      • newContentType

        public static ContentType newContentType​(String mimeType,
                                                 String characterEncoding)
        Creates a ContentType 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 information
        characterEncoding - charset attribute to be used with the mime-type
        Returns:
        a ContentType wrapper 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 with getByteArray(). 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 passed DataChunk.
        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, 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

        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 a String.
        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