com.openexchange.mail.mime
Class ContentType

java.lang.Object
  extended by com.openexchange.mail.mime.ParameterizedHeader
      extended by com.openexchange.mail.mime.ContentType
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<ParameterizedHeader>
Direct Known Subclasses:
ContentType.UnmodifiableContentType

public class ContentType
extends ParameterizedHeader

ContentType.UnmodifiableContentType - Parses value of MIME header Content-Type

Author:
Thorben Betten
See Also:
Serialized Form

Nested Class Summary
static class ContentType.UnmodifiableContentType
          ContentType.UnmodifiableContentType - An unmodifiable content type.
 
Field Summary
static ContentType DEFAULT_CONTENT_TYPE
          The (unmodifiable) default content type: text/plain; charset=us-ascii
 
Constructor Summary
ContentType()
          Initializes a new ContentType.UnmodifiableContentType
ContentType(java.lang.String contentType)
          Initializes a new ContentType.UnmodifiableContentType
 
Method Summary
 int compareTo(ParameterizedHeader other)
           
 boolean containsCharsetParameter()
           
 boolean containsNameParameter()
           
 boolean equals(java.lang.Object obj)
           
 java.lang.String getBaseType()
          Gets this content type's base type without any parameters appended; e.g.
static java.lang.String getBaseType(java.lang.String mimeType)
          Detects the base type of given MIME type
 java.lang.String getCharsetParameter()
           
 java.lang.String getNameParameter()
           
 java.lang.String getPrimaryType()
           
 java.lang.String getSubType()
           
 int hashCode()
           
 boolean isMimeType(java.lang.String pattern)
          Checks if Content-Type's base type matches given wildcard pattern (e.g text/plain, text/* or text/htm*)
static boolean isMimeType(java.lang.String mimeType, java.lang.String pattern)
          Checks if given MIME type's base type matches given wildcard pattern (e.g text/plain, text/* or text/htm*)
static java.lang.String prepareContentTypeString(java.lang.String contentType)
          Parses and prepares specified content-type string for being inserted into a MIME part's headers.
static java.lang.String prepareContentTypeString(java.lang.String contentType, java.lang.String name)
          Parses and prepares specified content-type string for being inserted into a MIME part's headers.
 void reset()
          Resets this ContentType.UnmodifiableContentType instance.
 ContentType setBaseType(java.lang.String baseType)
          Sets base type (e.g. text/plain)
 ContentType setCharsetParameter(java.lang.String charset)
          Sets "charset" parameter
 void setContentType(ContentType contentType)
          Applies given content type to this content type
 void setContentType(java.lang.String contentType)
          Sets the content type to specified content type string; e.g.
 ContentType setNameParameter(java.lang.String filename)
          Sets "name" parameter
 ContentType setPrimaryType(java.lang.String primaryType)
          Sets primary type
 ContentType setSubType(java.lang.String subType)
          Sets sub-type
 boolean startsWith(java.lang.String prefix)
          Checks if Content-Type's base type ignore-case starts with specified prefix.
 java.lang.String toString()
           
 java.lang.String toString(boolean skipEmptyParams)
          Returns a RFC2045 style (ASCII-only) string representation of this content type.
 
Methods inherited from class com.openexchange.mail.mime.ParameterizedHeader
addParameter, containsParameter, getParameter, getParameterNames, removeParameter, setParameter
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CONTENT_TYPE

public static final ContentType DEFAULT_CONTENT_TYPE
The (unmodifiable) default content type: text/plain; charset=us-ascii

Constructor Detail

ContentType

public ContentType()
Initializes a new ContentType.UnmodifiableContentType


ContentType

public ContentType(java.lang.String contentType)
            throws MailException
Initializes a new ContentType.UnmodifiableContentType

Parameters:
contentType - The content type
Throws:
MailException - If content type cannot be parsed
Method Detail

reset

public void reset()
Resets this ContentType.UnmodifiableContentType instance.


compareTo

public int compareTo(ParameterizedHeader other)
Specified by:
compareTo in interface java.lang.Comparable<ParameterizedHeader>
Overrides:
compareTo in class ParameterizedHeader

hashCode

public int hashCode()
Overrides:
hashCode in class ParameterizedHeader

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class ParameterizedHeader

setContentType

public void setContentType(ContentType contentType)
Applies given content type to this content type

Parameters:
contentType - The content type to apply

getPrimaryType

public java.lang.String getPrimaryType()
Returns:
primary type

setPrimaryType

public ContentType setPrimaryType(java.lang.String primaryType)
Sets primary type

Returns:
This content type with new primary type applied

getSubType

public java.lang.String getSubType()
Returns:
sub-type

setSubType

public ContentType setSubType(java.lang.String subType)
Sets sub-type

Returns:
This content type with new sub-type applied

getBaseType

public java.lang.String getBaseType()
Gets this content type's base type without any parameters appended; e.g. "text/plain".

Returns:
The base type

setBaseType

public ContentType setBaseType(java.lang.String baseType)
                        throws MailException
Sets base type (e.g. text/plain)

Returns:
This content type with new base type applied
Throws:
MailException

setCharsetParameter

public ContentType setCharsetParameter(java.lang.String charset)
Sets "charset" parameter

Parameters:
charset - The charset parameter value; e.g. "UTF-8"
Returns:
This content type with new "charset" parameter applied

getCharsetParameter

public java.lang.String getCharsetParameter()
Returns:
the "charset" value or null if not present

containsCharsetParameter

public boolean containsCharsetParameter()
Returns:
true if "charset" parameter is present, false otherwise

setNameParameter

public ContentType setNameParameter(java.lang.String filename)
Sets "name" parameter

Parameters:
filename - The name parameter
Returns:
This content type with new "name" parameter applied

getNameParameter

public java.lang.String getNameParameter()
Returns:
the "name" value or null if not present

containsNameParameter

public boolean containsNameParameter()
Returns:
true if "name" parameter is present, false otherwise

setContentType

public void setContentType(java.lang.String contentType)
                    throws MailException
Sets the content type to specified content type string; e.g. "text/plain; charset=US-ASCII"

Parameters:
contentType - The content type string
Throws:
MailException - If specified content type string cannot be parsed

isMimeType

public boolean isMimeType(java.lang.String pattern)
Checks if Content-Type's base type matches given wildcard pattern (e.g text/plain, text/* or text/htm*)

Returns:
true if Content-Type's base type matches given pattern, false otherwise

startsWith

public boolean startsWith(java.lang.String prefix)
Checks if Content-Type's base type ignore-case starts with specified prefix.

Parameters:
prefix - The prefix
Returns:
true if Content-Type's base type ignore-case starts with specified prefix; otherwise false
Throws:
java.lang.IllegalArgumentException - If specified prefix is null

prepareContentTypeString

public static java.lang.String prepareContentTypeString(java.lang.String contentType)
                                                 throws MailException
Parses and prepares specified content-type string for being inserted into a MIME part's headers.

Parameters:
contentType - The content-type string to process
Returns:
Prepared content-type string ready for being inserted into a MIME part's headers.
Throws:
MailException - If parsing content-type string fails

prepareContentTypeString

public static java.lang.String prepareContentTypeString(java.lang.String contentType,
                                                        java.lang.String name)
                                                 throws MailException
Parses and prepares specified content-type string for being inserted into a MIME part's headers.

Parameters:
contentType - The content-type string to process
name - The optional name parameter to set if no "name" parameter is present in specified content-type string; pass null to ignore
Returns:
Prepared content-type string ready for being inserted into a MIME part's headers.
Throws:
MailException - If parsing content-type string fails

isMimeType

public static boolean isMimeType(java.lang.String mimeType,
                                 java.lang.String pattern)
                          throws MailException
Checks if given MIME type's base type matches given wildcard pattern (e.g text/plain, text/* or text/htm*)

Parameters:
mimeType - The MIME type
pattern - The pattern
Returns:
true if pattern matches; otherwise false
Throws:
MailException - If an invalid MIME type is detected

getBaseType

public static java.lang.String getBaseType(java.lang.String mimeType)
                                    throws MailException
Detects the base type of given MIME type

Parameters:
mimeType - The MIME type
Returns:
the base type
Throws:
MailException - If an invalid MIME type is detected

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(boolean skipEmptyParams)
Returns a RFC2045 style (ASCII-only) string representation of this content type.

Parameters:
skipEmptyParams - true to skip empty parameters; otherwise false
Returns:
A RFC2045 style (ASCII-only) string representation of this content type