org.abstracthorizon.mercury.maildir
Class MaildirMessage

java.lang.Object
  extended by javax.mail.Message
      extended by javax.mail.internet.MimeMessage
          extended by org.abstracthorizon.mercury.maildir.util.MessageBase
              extended by org.abstracthorizon.mercury.maildir.util.LazyParsingMessage
                  extended by org.abstracthorizon.mercury.maildir.MaildirMessage
All Implemented Interfaces:
FilenameFilter, Comparable<MaildirMessage>, javax.mail.internet.MimePart, javax.mail.Part, FileProvider
Direct Known Subclasses:
UIDMaildirMessage

public class MaildirMessage
extends LazyParsingMessage
implements FilenameFilter, FileProvider, Comparable<MaildirMessage>

Maildir message representation.

Messages file name is defined in the following way:

Flags are defined on the way explained in FlagUtilities

Author:
Daniel Sendula

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.mail.internet.MimeMessage
javax.mail.internet.MimeMessage.RecipientType
 
Field Summary
protected  String baseName
          Message's base name, name without info (flags)
static int CREATE_FILE_RETRIES
          Number of retries when creating new file
protected  File file
          Message's file
protected  long fileSize
          Cached file size or -1
static String FLAGS_SEPERATOR
          Flags (info) separator
protected static String host
          Host name cache
protected  char infoSeparator
          Cached info separator
protected  boolean isNew
          Flag to show is file in new subdirectory or not
protected  MaildirFolderData maildirFolder
          Cached link to maildir folder message belongs to.
protected static Random randomGenerator
          Random number generator
 
Fields inherited from class org.abstracthorizon.mercury.maildir.util.LazyParsingMessage
inputStream, parsed
 
Fields inherited from class javax.mail.internet.MimeMessage
content, contentStream, dh, flags, headers, modified, saved
 
Fields inherited from class javax.mail.Message
expunged, folder, msgnum, session
 
Fields inherited from interface javax.mail.Part
ATTACHMENT, INLINE
 
Constructor Summary
  MaildirMessage(MaildirFolderData folder, File file, int msgnum)
          Constructor that creates message object from the file.
  MaildirMessage(MaildirFolderData folder, File file, int msgnum, boolean initialise)
          Constructor that creates message object from the file.
protected MaildirMessage(MaildirFolderData folder, javax.mail.internet.MimeMessage message, int msgnum)
          Constructor that takes new message and creates a file.
 
Method Summary
 boolean accept(File file, String name)
          Part of FilenameFilter interface.
static String baseNameFromFile(File file)
          Static method that obtains base name from the given file.
protected  void closeFile()
          Closes all SharedInputStream impementations over this file.
 int compareTo(MaildirMessage o)
          This method compares two messages by base name.
protected  void createFile(String flags)
          Creates new file for the (new) message.
protected  String createFileName(String flags)
          Creates file name, a file name composed of time, milliseconds, process id and random number, host name and flags (as part of file's info).
protected  boolean expunge()
          Expunges the message.
 String getBaseName()
          Returns cached base name
 File getFile()
          Returns message's file.
 long getFileSize()
          Return's cached file's size.
 Date getReceivedDate()
          Returns file's date or null if file doesn't exist
protected  void initialise()
          Initialises message.
protected  void parse(InputStream is)
          Calls super parse method
protected  void parseImpl()
          Check if message has already been parsed.
 void setFile(File file)
          Sets message's file.
 void setFlags(javax.mail.Flags flags, boolean set)
          Sets flags to the message.
protected  void storeMessage(javax.mail.internet.MimeMessage message)
          Stores mime message to the file.
 boolean synchronise()
          Tries to find file with same base part as this file
 
Methods inherited from class org.abstracthorizon.mercury.maildir.util.LazyParsingMessage
addFrom, addHeader, addHeaderLine, addRecipient, addRecipients, addRecipients, createInternetHeaders, getAllHeaderLines, getAllHeaders, getAllRecipients, getContent, getContentID, getContentLanguage, getContentMD5, getContentStream, getContentType, getDataHandler, getDescription, getDisposition, getEncoding, getFileName, getFrom, getHeader, getHeader, getInputStream, getLineCount, getMatchingHeaderLines, getMatchingHeaders, getMessageID, getNonMatchingHeaderLines, getNonMatchingHeaders, getRawInputStream, getRecipients, getReplyTo, getSender, getSentDate, getSize, getSubject, isExpunged, isMimeType, isParsed, match, removeHeader, reply, saveChanges, setContent, setContent, setContentID, setContentLanguage, setContentMD5, setDataHandler, setDescription, setDescription, setDisposition, setFileName, setFrom, setFrom, setHeader, setRecipient, setRecipients, setRecipients, setReplyTo, setSender, setSentDate, setSubject, setSubject, setText, setText, updateHeaders, writeTo, writeTo
 
Methods inherited from class org.abstracthorizon.mercury.maildir.util.MessageBase
setMessageNumber
 
Methods inherited from class javax.mail.internet.MimeMessage
createMimeMessage, getFlags, isSet, setText, updateMessageID
 
Methods inherited from class javax.mail.Message
getFolder, getMessageNumber, setExpunged, setFlag
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CREATE_FILE_RETRIES

public static final int CREATE_FILE_RETRIES
Number of retries when creating new file

See Also:
Constant Field Values

maildirFolder

protected MaildirFolderData maildirFolder
Cached link to maildir folder message belongs to. Note - can be empty.


file

protected File file
Message's file


baseName

protected String baseName
Message's base name, name without info (flags)


infoSeparator

protected char infoSeparator
Cached info separator


fileSize

protected long fileSize
Cached file size or -1


isNew

protected boolean isNew
Flag to show is file in new subdirectory or not


FLAGS_SEPERATOR

public static final String FLAGS_SEPERATOR
Flags (info) separator

See Also:
Constant Field Values

randomGenerator

protected static final Random randomGenerator
Random number generator


host

protected static String host
Host name cache

Constructor Detail

MaildirMessage

protected MaildirMessage(MaildirFolderData folder,
                         javax.mail.internet.MimeMessage message,
                         int msgnum)
                  throws javax.mail.MessagingException,
                         IOException
Constructor that takes new message and creates a file.

Parameters:
folder - folder message belongs to.
message - new message this message instance will be created based upon
msgnum - message number
Throws:
javax.mail.MessagingException
IOException

MaildirMessage

public MaildirMessage(MaildirFolderData folder,
                      File file,
                      int msgnum,
                      boolean initialise)
               throws javax.mail.MessagingException
Constructor that creates message object from the file.

Parameters:
folder - folder this message belongs to
file - file
msgnum - message number
initialise - should message be initialised or not
Throws:
javax.mail.MessagingException

MaildirMessage

public MaildirMessage(MaildirFolderData folder,
                      File file,
                      int msgnum)
               throws javax.mail.MessagingException
Constructor that creates message object from the file. Message is always initialised

Parameters:
folder - folder this message belongs to
file - file
msgnum - message number
Throws:
javax.mail.MessagingException
Method Detail

initialise

protected void initialise()
                   throws javax.mail.MessagingException
Initialises message. This method really just calls parse method with appropriate SharedInputStream implementation, which in turn (because of lazy implementation) just stores that input stream.

Throws:
javax.mail.MessagingException

baseNameFromFile

public static String baseNameFromFile(File file)
Static method that obtains base name from the given file.

Parameters:
file - file
Returns:
message's base name

getFile

public File getFile()
             throws IOException
Returns message's file. If file doesn't exist it tries to synchorise (reading all files from directories and checks for same basename)

Specified by:
getFile in interface FileProvider
Returns:
message's file.
Throws:
IOException

setFile

public void setFile(File file)
             throws javax.mail.MessagingException
Sets message's file.

Parameters:
file - file
Throws:
javax.mail.MessagingException - if there is a problem setting flags

getBaseName

public String getBaseName()
Returns cached base name

Returns:
cached base name

closeFile

protected void closeFile()
Closes all SharedInputStream impementations over this file.


createFileName

protected String createFileName(String flags)
                         throws javax.mail.MessagingException
Creates file name, a file name composed of time, milliseconds, process id and random number, host name and flags (as part of file's info).

Parameters:
flags - flags to be applied
Returns:
new file name
Throws:
javax.mail.MessagingException

createFile

protected void createFile(String flags)
                   throws javax.mail.MessagingException
Creates new file for the (new) message.

Parameters:
flags - flags
Throws:
javax.mail.MessagingException - if file cannot be created

storeMessage

protected void storeMessage(javax.mail.internet.MimeMessage message)
                     throws javax.mail.MessagingException
Stores mime message to the file.

Parameters:
message - message to be stored
Throws:
javax.mail.MessagingException - if file cannot be written

getFileSize

public long getFileSize()
Return's cached file's size.

Specified by:
getFileSize in interface FileProvider
Returns:
cached file's size.

synchronise

public boolean synchronise()
                    throws javax.mail.MessagingException
Tries to find file with same base part as this file

Returns:
true if it succeded
Throws:
javax.mail.MessagingException

accept

public boolean accept(File file,
                      String name)
Part of FilenameFilter interface.

Specified by:
accept in interface FilenameFilter
Parameters:
file - directory where filter is applied on
name - file name to be checked
Returns:
true if name start's with messages basename

parse

protected void parse(InputStream is)
              throws javax.mail.MessagingException
Calls super parse method

Overrides:
parse in class LazyParsingMessage
Parameters:
is - input stream
Throws:
javax.mail.MessagingException

parseImpl

protected void parseImpl()
                  throws javax.mail.MessagingException
Check if message has already been parsed. If not calls super method and closes files.

Overrides:
parseImpl in class LazyParsingMessage
Throws:
javax.mail.MessagingException

expunge

protected boolean expunge()
Expunges the message. Effectively tries to delete the file

Returns:
true if file can be deleted

getReceivedDate

public Date getReceivedDate()
                     throws javax.mail.MessagingException
Returns file's date or null if file doesn't exist

Overrides:
getReceivedDate in class javax.mail.internet.MimeMessage
Returns:
file's date.
Throws:
javax.mail.MessagingException

setFlags

public void setFlags(javax.mail.Flags flags,
                     boolean set)
              throws javax.mail.MessagingException
Sets flags to the message. This method is updating file's name.

Overrides:
setFlags in class javax.mail.internet.MimeMessage
Parameters:
flags - flags that are applied
set - are flags set or removed
Throws:
javax.mail.MessagingException

compareTo

public int compareTo(MaildirMessage o)
This method compares two messages by base name.

Specified by:
compareTo in interface Comparable<MaildirMessage>
Parameters:
o - message to be compared with
Returns:
-1, 0, 1 depending if this message has less, equal or greater base name. If supplied object is not Maildir message then -1 is returned.


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.