com.openexchange.mail.dataobjects
Class MailPart

java.lang.Object
  extended by com.openexchange.mail.dataobjects.MailPart
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
DataMailPart, InfostoreDocumentMailPart, MailMessage, MIMEMailPart, MIMEMultipartMailPart, NestedMessageMailPart, ReferencedMailPart, TextBodyMailPart, UploadFileMailPart, UUEncodedAttachmentMailPart

public abstract class MailPart
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

MailPart - Abstract super class for all MailPart subclasses.

It's main purpose is to provide access to common part headers and part's content.

Author:
Thorben Betten
See Also:
Serialized Form

Field Summary
static int NO_ENCLOSED_PARTS
          The integer constant returned by getEnclosedCount() if mail part's content type does not match multipart/* and therefore does not hold any enclosed parts.
 
Method Summary
 void addHeader(java.lang.String name, java.lang.String value)
          Adds a header
 void addHeaders(HeaderCollection headers)
          Adds a header collection
 java.lang.Object clone()
           
 boolean containsContentDisposition()
           
 boolean containsContentId()
           
 boolean containsContentType()
           
 boolean containsFileName()
           
 boolean containsHeader(java.lang.String name)
          Checks for a header entry for specified header
 boolean containsHeaders()
           
 boolean containsMsgref()
           
 boolean containsSequenceId()
           
 boolean containsSize()
           
abstract  java.lang.Object getContent()
          Returns the part's content as a Java object dependent on underlying implementation.
 ContentDisposition getContentDisposition()
          Gets the disposition
 java.lang.String getContentId()
          Gets the contentId
 ContentType getContentType()
          Gets the content type
abstract  javax.activation.DataHandler getDataHandler()
          Returns an appropriate DataHandler for this mail part.
abstract  int getEnclosedCount()
          Gets the number of enclosed mail parts.
abstract  MailPart getEnclosedMailPart(int index)
          Gets the mail part located at given index.
 java.lang.String getFileName()
          Gets the fileName
 java.lang.String getFirstHeader(java.lang.String name)
          Gets the first header for specified header name.
 java.lang.String[] getHeader(java.lang.String name)
          Gets all the values for the specified header.
 java.lang.String getHeader(java.lang.String name, char delimiter)
          Gets all the headers for this header name, returned as a single String, with headers separated by the delimiter.
 java.lang.String getHeader(java.lang.String name, java.lang.String delimiter)
          Gets all the headers for this header name, returned as a single String, with headers separated by the delimiter.
 HeaderCollection getHeaders()
          Gets a read-only version of this part's headers
 java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> getHeadersIterator()
          Gets an instance of Iterator to iterate all headers
 int getHeadersSize()
          Gets the number of headers
abstract  java.io.InputStream getInputStream()
          Returns an input stream for this part.
 java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> getMatchingHeaders(java.lang.String[] matchingHeaders)
          Gets an iterator for matching headers
 MailPath getMsgref()
          Gets the message reference
 java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> getNonMatchingHeaders(java.lang.String[] nonMatchingHeaders)
          Gets an iterator for non-matching headers
 java.lang.String getSequenceId()
          Gets the sequenceId
 long getSize()
          Gets the size
 java.lang.String getSource()
          Gets the mail part's source
 byte[] getSourceBytes()
          Gets a newly allocated byte array containing the mail part's source bytes
 boolean hasEnclosedParts()
          Checks if part's MIME type is multipart/*
 boolean hasHeaders(java.lang.String... names)
          Checks if this part contains all of specified headers.
abstract  void loadContent()
          Ensures that the part's content is loaded, thus this part is independent of the original.
abstract  void prepareForCaching()
          Prepares this mail part to be put into cache; meaning to release all kept resources
 void removeContentDisposition()
          Removes the disposition
 void removeContentId()
          Removes the contentId
 void removeContentType()
          Removes the content type
 void removeFileName()
          Removes the fileName
 void removeHeader(java.lang.String name)
          Removes the header if present
 void removeHeaders()
          Removes the headers
 void removeMsgref()
          Removes the message reference
 void removeSequenceId()
          Removes the sequenceId
 void removeSize()
          Removes the size
 void setContentDisposition(ContentDisposition disposition)
          Sets the disposition
 void setContentDisposition(java.lang.String disposition)
          Sets the disposition
 void setContentId(java.lang.String contentId)
          Sets the contentId
 void setContentType(ContentType contentType)
          Sets the content type
 void setContentType(java.lang.String contentType)
          Parses and sets the content type
 void setFileName(java.lang.String fileName)
          Sets the fileName
 void setMsgref(MailPath msgref)
          Sets the message reference
 void setSequenceId(java.lang.String sequenceId)
          Sets the sequenceId
 void setSize(long size)
          Sets the size
 void writeTo(java.io.OutputStream out)
          Writes complete part's data into given output stream
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_ENCLOSED_PARTS

public static final int NO_ENCLOSED_PARTS
The integer constant returned by getEnclosedCount() if mail part's content type does not match multipart/* and therefore does not hold any enclosed parts.

See Also:
Constant Field Values
Method Detail

getContentType

public ContentType getContentType()
Gets the content type

Returns:
the content type

containsContentType

public boolean containsContentType()
Returns:
true if content type is set; otherwise false

removeContentType

public void removeContentType()
Removes the content type


setContentType

public void setContentType(ContentType contentType)
Sets the content type

Parameters:
contentType - the contentType to set

setContentType

public void setContentType(java.lang.String contentType)
                    throws MailException
Parses and sets the content type

Parameters:
contentType - the content type to parse
Throws:
MailException - If content type is invalid or could not be parsed

getContentDisposition

public ContentDisposition getContentDisposition()
Gets the disposition

Returns:
the disposition

containsContentDisposition

public boolean containsContentDisposition()
Returns:
true if disposition is set; otherwise false

removeContentDisposition

public void removeContentDisposition()
Removes the disposition


setContentDisposition

public void setContentDisposition(java.lang.String disposition)
                           throws MailException
Sets the disposition

Parameters:
disposition - the disposition to set
Throws:
MailException - If content disposition is invalid or could not be parsed

setContentDisposition

public void setContentDisposition(ContentDisposition disposition)
Sets the disposition

Parameters:
disposition - the disposition to set

getFileName

public java.lang.String getFileName()
Gets the fileName

Returns:
the fileName

containsFileName

public boolean containsFileName()
Returns:
true if fileName is set; otherwise false

removeFileName

public void removeFileName()
Removes the fileName


setFileName

public void setFileName(java.lang.String fileName)
Sets the fileName

Parameters:
fileName - the fileName to set

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Adds a header

Parameters:
name - The header name
value - The header value

addHeaders

public void addHeaders(HeaderCollection headers)
Adds a header collection

Parameters:
headers - The header collection

containsHeaders

public boolean containsHeaders()
Returns:
true if headers is set; otherwise false

removeHeaders

public void removeHeaders()
Removes the headers


getHeadersSize

public int getHeadersSize()
Gets the number of headers

Returns:
The number of headers

getHeadersIterator

public java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> getHeadersIterator()
Gets an instance of Iterator to iterate all headers

Returns:
An instance of Iterator to iterate all headers
See Also:
getHeadersSize()

containsHeader

public boolean containsHeader(java.lang.String name)
Checks for a header entry for specified header

Parameters:
name - The header name
Returns:
true if a header entry exists for specified header; otherwise false

getHeader

public java.lang.String[] getHeader(java.lang.String name)
Gets all the values for the specified header. Returns null if no headers with the specified name exist.

Parameters:
name - The header name
Returns:
The header values or null

getFirstHeader

public java.lang.String getFirstHeader(java.lang.String name)
Gets the first header for specified header name.

This is a convenience method that invokes getHeader(String, String) with the latter parameter set to null.

Parameters:
name - The header name
Returns:
The header's first value or null

getHeader

public java.lang.String getHeader(java.lang.String name,
                                  java.lang.String delimiter)
Gets all the headers for this header name, returned as a single String, with headers separated by the delimiter. If the delimiter is null, only the first header is returned. Returns null if no headers with the specified name exist.

Parameters:
name - The header name
delimiter - The delimiter
Returns:
The header values as a single String or null

getHeader

public java.lang.String getHeader(java.lang.String name,
                                  char delimiter)
Gets all the headers for this header name, returned as a single String, with headers separated by the delimiter. If the delimiter is '\0', only the first header is returned. Returns null if no headers with the specified name exist.

Parameters:
name - The header name
delimiter - The delimiter character
Returns:
The header values as a single String or null

getHeaders

public HeaderCollection getHeaders()
Gets a read-only version of this part's headers

Returns:
A read-only version of this part's headers

getNonMatchingHeaders

public java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> getNonMatchingHeaders(java.lang.String[] nonMatchingHeaders)
Gets an iterator for non-matching headers

Parameters:
nonMatchingHeaders - The non-matching headers
Returns:
An iterator for non-matching headers

getMatchingHeaders

public java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.String>> getMatchingHeaders(java.lang.String[] matchingHeaders)
Gets an iterator for matching headers

Parameters:
matchingHeaders - The matching headers
Returns:
An iterator for matching headers or null if not exists

removeHeader

public void removeHeader(java.lang.String name)
Removes the header if present

Parameters:
name - The header name

hasHeaders

public boolean hasHeaders(java.lang.String... names)
Checks if this part contains all of specified headers.

Parameters:
names - The names of the headers to check
Returns:
true if this part contains all of specified headers; otherwise false

getSize

public long getSize()
Gets the size

Returns:
the size

containsSize

public boolean containsSize()
Returns:
true if size is set; otherwise false

removeSize

public void removeSize()
Removes the size


setSize

public void setSize(long size)
Sets the size

Parameters:
size - the size to set

getContentId

public java.lang.String getContentId()
Gets the contentId

Returns:
the contentId

containsContentId

public boolean containsContentId()
Returns:
true if contentId is set; otherwise false

removeContentId

public void removeContentId()
Removes the contentId


setContentId

public void setContentId(java.lang.String contentId)
Sets the contentId

Parameters:
contentId - the contentId to set

getSequenceId

public java.lang.String getSequenceId()
Gets the sequenceId

Returns:
the sequenceId

containsSequenceId

public boolean containsSequenceId()
Returns:
true if sequenceId is set; otherwise false

removeSequenceId

public void removeSequenceId()
Removes the sequenceId


setSequenceId

public void setSequenceId(java.lang.String sequenceId)
Sets the sequenceId

Parameters:
sequenceId - the sequenceId to set

getMsgref

public MailPath getMsgref()
Gets the message reference

Returns:
the message reference

containsMsgref

public boolean containsMsgref()
Returns:
true if message reference is set; otherwise false

removeMsgref

public void removeMsgref()
Removes the message reference


setMsgref

public void setMsgref(MailPath msgref)
Sets the message reference

Parameters:
msgref - the message reference to set

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

hasEnclosedParts

public boolean hasEnclosedParts()
                         throws MailException
Checks if part's MIME type is multipart/*

Returns:
true if part holds enclosed parts; otherwise false
Throws:
MailException - If check fails

getContent

public abstract java.lang.Object getContent()
                                     throws MailException
Returns the part's content as a Java object dependent on underlying implementation.
This method is not applicable if part's MIME type is multipart/*

Returns:
The content as a Java object or null if not applicable
Throws:
MailException - If content cannot be returned as a Java object

getDataHandler

public abstract javax.activation.DataHandler getDataHandler()
                                                     throws MailException
Returns an appropriate DataHandler for this mail part.
This method is not applicable if part's MIME type is multipart/*

Returns:
an appropriate DataHandler or null if not applicable
Throws:
MailException - If an appropriate DataHandler cannot be returned

getInputStream

public abstract java.io.InputStream getInputStream()
                                            throws MailException
Returns an input stream for this part.
This method is not applicable if part's MIME type is multipart/*

Returns:
An input stream for this part or null if not applicable
Throws:
MailException - If no input stream could be returned

getEnclosedCount

public abstract int getEnclosedCount()
                              throws MailException
Gets the number of enclosed mail parts.
This method is only applicable if part's MIME type is multipart/*

Returns:
The number of enclosed mail parts or NO_ENCLOSED_PARTS if not applicable
Throws:
MailException
See Also:
NO_ENCLOSED_PARTS

getEnclosedMailPart

public abstract MailPart getEnclosedMailPart(int index)
                                      throws MailException
Gets the mail part located at given index.
This method is only applicable if part's MIME type is multipart/*

Parameters:
index - The index of desired mail part or null if not applicable
Returns:
The mail part
Throws:
MailException

loadContent

public abstract void loadContent()
                          throws MailException
Ensures that the part's content is loaded, thus this part is independent of the original.

This method is intended for mailing systems that read the contents stepwise on demand. If dealing with such a mail part with its underlying connection closed, the part's content is no more accessible. Otherwise this method may be implemented with an empty body.

Moreover the loaded content is no more discarded when prepareForCaching() is invoked.

Throws:
MailException - If loading part's content fails

writeTo

public void writeTo(java.io.OutputStream out)
             throws MailException
Writes complete part's data into given output stream

Parameters:
out - The output stream to write to
Throws:
MailException - If writing to output stream fails

getSource

public java.lang.String getSource()
                           throws MailException
Gets the mail part's source

Returns:
The mail part's source
Throws:
MailException - If mail part's source cannot be returned

getSourceBytes

public byte[] getSourceBytes()
                      throws MailException
Gets a newly allocated byte array containing the mail part's source bytes

Returns:
The mail part's source bytes
Throws:
MailException - If mail part's source cannot be returned

prepareForCaching

public abstract void prepareForCaching()
Prepares this mail part to be put into cache; meaning to release all kept resources