org.apache.geronimo.javamail.store.imap.connection
Class IMAPCommand

java.lang.Object
  extended by org.apache.geronimo.javamail.store.imap.connection.IMAPCommand

public class IMAPCommand
extends Object

Utility class for building up what might be complex arguments to a command. This includes the ability to directly write out binary arrays of data and have them constructed as IMAP literals.


Field Summary
static char[] encodingTable
           
protected  boolean needWhiteSpace
           
protected  DataOutputStream out
           
protected  List segments
           
protected  ByteArrayOutputStream sink
           
protected  String tag
           
protected static int tagCounter
           
 
Constructor Summary
IMAPCommand()
          Create an empty command.
IMAPCommand(String command)
          Create a command with an initial command string.
 
Method Summary
 void append(String value)
          Directly append a value to the buffer without attempting to insert whitespace or figure out any format encodings.
 void appendACL(ACL acl)
          Append an ACL value to a command.
 void appendACL(ACL acl, String modifier)
          Append an ACL value to a command.
protected  void appendAnd(AndTerm term, String charset)
          append IMAP search term information from an AndTerm item.
 void appendAtom(byte[] value)
          Append an atom to the command buffer.
 void appendAtom(String value)
          Append an atom value to the command.
protected  void appendBody(BodyTerm term, String charset)
          append IMAP search term information from a BodyTerm item.
 void appendBodySection(String partName)
          Append a body section specification to a command string.
 void appendBodySection(String section, String partName)
          Append a body section specification to a command string.
 void appendDate(Date d)
          Format a date into the form required for IMAP commands.
 void appendEncodedString(String value)
          Append a string value to a command buffer, with encoding.
 void appendFetchProfile(FetchProfile profile)
          Append a FetchProfile information to an IMAPCommand that's to be issued.
protected  void appendFlag(FlagTerm term, String charset)
          append IMAP search term information from a FlagTerm item.
 void appendFlags(Flags flags)
          Append a set of flags to a command buffer.
protected  void appendFrom(FromStringTerm term, String charset)
          append IMAP search term information from a FromStringTerm item.
protected  void appendFrom(FromTerm term, String charset)
          append IMAP search term information from a FromTerm item.
protected  void appendHeader(HeaderTerm term, String charset)
          append IMAP search term information from a HeaderTerm item.
 void appendInteger(int value)
          Append an integer value to the command, converting the integer into string form.
 void appendLiteral(byte[] value)
          Append an IMAP literal values to the command.
 void appendLiteral(ByteArrayOutputStream value)
          Append literal data to the command where the literal sourcd is a ByteArrayOutputStream.
protected  void appendLiteralHeader(int size)
          Add a literal header to the buffer.
 void appendLong(long value)
          Append a long value to the command, converting the integer into string form.
protected  void appendMessageID(MessageIDTerm term, String charset)
          append IMAP search term information from a MessageIDTerm item.
protected  void appendNot(NotTerm term, String charset)
          append IMAP search term information from a NotTerm item.
protected  void appendOr(OrTerm term, String charset)
          append IMAP search term information from an OrTerm item.
 void appendQuota(Quota quota)
          Append a quota specification to an IMAP command.
 void appendQuotaResource(Quota.Resource resource)
          Append a Quota.Resource element to an IMAP command.
 void appendQuotedString(byte[] value)
          Write out a string of literal data, taking into account the need to escape both '"' and '\' characters.
 void appendQuotedString(String value)
          Append a string value to a command buffer.
protected  void appendReceivedDate(ReceivedDateTerm term, String charset)
          append IMAP search term information from a ReceivedDateTerm item.
protected  void appendRecipient(RecipientStringTerm term, String charset)
          append IMAP search term information from a RecipientStringTerm item.
protected  void appendRecipient(RecipientTerm term, String charset)
          append IMAP search term information from a RecipientTerm item.
 void appendSearchDate(Date d)
          Format a date into the form required for IMAP search commands.
 void appendSearchTerm(SearchTerm term, String charset)
          append an IMAP search sequence from a SearchTerm.
protected  void appendSentDate(SentDateTerm term, String charset)
          append IMAP search term information from a SendDateTerm item.
protected  void appendSize(SizeTerm term, String charset)
          append IMAP search term information from a SizeTerm item.
 void appendString(byte[] value)
          Append a value in a byte array to a command buffer.
 void appendString(String value)
          Append a string value to a command buffer.
 void appendString(String value, String charset)
          Append a string value to a command buffer.
protected  void appendSubject(SubjectTerm term, String charset)
          append IMAP search term information from a SubjectTerm item.
static boolean checkSearchEncoding(SearchTerm term)
          Run the tree of search terms, checking for problems with the terms that may require specifying a CHARSET modifier on a SEARCH command sent to the server.
static boolean checkSearchEncoding(SearchTerm[] terms)
          Run an array of search term items to check each one for ASCII encoding problems.
static boolean checkStringEncoding(String s)
          Check a string to see if this can be processed using just 7-bit ASCII.
protected  void conditionalWhitespace()
          Add a whitespace character to the command if the previous token was a type that required a white space character to mark the boundary.
protected static int encode(byte[] buffer, int count, StringBuffer result)
          Encode a single buffer of characters.
 String encode(String original)
          Encode a string using the modified UTF-7 encoding.
 void endList()
          Write out the end of the list.
 String getTag()
           
protected  String recipientType(Message.RecipientType type)
          Translate a recipient type into it's string name equivalent.
 void startList()
          Mark the start of a list value being written to the command.
 void writeTo(OutputStream outStream, IMAPConnection connection)
          Write all of the command data to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

encodingTable

public static final char[] encodingTable

needWhiteSpace

protected boolean needWhiteSpace

out

protected DataOutputStream out

sink

protected ByteArrayOutputStream sink

segments

protected List segments

tag

protected String tag

tagCounter

protected static int tagCounter
Constructor Detail

IMAPCommand

public IMAPCommand()
Create an empty command.


IMAPCommand

public IMAPCommand(String command)
Create a command with an initial command string.

Parameters:
command - The command string used to start this command.
Method Detail

getTag

public String getTag()

writeTo

public void writeTo(OutputStream outStream,
                    IMAPConnection connection)
             throws IOException,
                    MessagingException
Write all of the command data to the stream. This includes the leading tag data.

Parameters:
outStream -
connection -
Throws:
IOException
MessagingException

append

public void append(String value)
Directly append a value to the buffer without attempting to insert whitespace or figure out any format encodings.

Parameters:
value - The value to append.

appendString

public void appendString(String value)
Append a string value to a command buffer. This sorts out what form the string needs to be appended in (LITERAL, QUOTEDSTRING, or ATOM).

Parameters:
target - The target buffer for appending the string.
value - The value to append.

appendQuotedString

public void appendQuotedString(String value)
Append a string value to a command buffer. This always appends as a QUOTEDSTRING

Parameters:
value - The value to append.

appendEncodedString

public void appendEncodedString(String value)
Append a string value to a command buffer, with encoding. This sorts out what form the string needs to be appended in (LITERAL, QUOTEDSTRING, or ATOM).

Parameters:
target - The target buffer for appending the string.
value - The value to append.

encode

public String encode(String original)
Encode a string using the modified UTF-7 encoding.

Parameters:
original - The original string.
Returns:
The original string encoded with modified UTF-7 encoding.

encode

protected static int encode(byte[] buffer,
                            int count,
                            StringBuffer result)
Encode a single buffer of characters. This buffer will have between 0 and 4 bytes to encode.

Parameters:
buffer - The buffer to encode.
count - The number of characters in the buffer.
result - The accumulator for appending the result.
Returns:
The remaining number of bytes remaining in the buffer (return 0 unless the count was 4 at the beginning).

appendString

public void appendString(String value,
                         String charset)
                  throws MessagingException
Append a string value to a command buffer. This sorts out what form the string needs to be appended in (LITERAL, QUOTEDSTRING, or ATOM).

Parameters:
target - The target buffer for appending the string.
value - The value to append.
Throws:
MessagingException

appendString

public void appendString(byte[] value)
Append a value in a byte array to a command buffer. This sorts out what form the string needs to be appended in (LITERAL, QUOTEDSTRING, or ATOM).

Parameters:
target - The target buffer for appending the string.
value - The value to append.

appendInteger

public void appendInteger(int value)
Append an integer value to the command, converting the integer into string form.

Parameters:
value - The value to append.

appendLong

public void appendLong(long value)
Append a long value to the command, converting the integer into string form.

Parameters:
value - The value to append.

appendAtom

public void appendAtom(String value)
Append an atom value to the command. Atoms are directly appended without using literal encodings.

Parameters:
value - The value to append.

appendAtom

public void appendAtom(byte[] value)
Append an atom to the command buffer. Atoms are directly appended without using literal encodings. White space is accounted for with the append operation.

Parameters:
value - The value to append.

appendLiteral

public void appendLiteral(byte[] value)
Append an IMAP literal values to the command. literals are written using a header with the length specified, followed by a CRLF sequence, followed by the literal data.

Parameters:
value - The literal data to write.

appendLiteralHeader

protected void appendLiteralHeader(int size)
Add a literal header to the buffer. The literal header is the literal length enclosed in a "{n}" pair, followed by a CRLF sequence.

Parameters:
size - The size of the literal value.

appendLiteral

public void appendLiteral(ByteArrayOutputStream value)
Append literal data to the command where the literal sourcd is a ByteArrayOutputStream.

Parameters:
value - The source of the literal data.

appendQuotedString

public void appendQuotedString(byte[] value)
Write out a string of literal data, taking into account the need to escape both '"' and '\' characters.

Parameters:
value - The bytes of the string to write.

startList

public void startList()
Mark the start of a list value being written to the command. A list is a sequences of different tokens enclosed in "(" ")" pairs. Lists can be nested.


endList

public void endList()
Write out the end of the list.


conditionalWhitespace

protected void conditionalWhitespace()
Add a whitespace character to the command if the previous token was a type that required a white space character to mark the boundary.


appendBodySection

public void appendBodySection(String section,
                              String partName)
Append a body section specification to a command string. Body section specifications are of the form "[section]".

Parameters:
section - The section numeric identifier.
partName - The name of the body section we want (e.g. "TEST", "HEADERS").

appendBodySection

public void appendBodySection(String partName)
Append a body section specification to a command string. Body section specifications are of the form "[section]".

Parameters:
partName - The partname we require.

appendFlags

public void appendFlags(Flags flags)
Append a set of flags to a command buffer.

Parameters:
flags - The flag set to append.

appendDate

public void appendDate(Date d)
Format a date into the form required for IMAP commands.

Parameters:
d - The source Date.

appendSearchDate

public void appendSearchDate(Date d)
Format a date into the form required for IMAP search commands.

Parameters:
d - The source Date.

appendSearchTerm

public void appendSearchTerm(SearchTerm term,
                             String charset)
                      throws MessagingException
append an IMAP search sequence from a SearchTerm. SearchTerms terms can be complex sets of terms in a tree form, so this may involve some recursion to completely translate.

Parameters:
term - The search term we're processing.
charset - The charset we need to use when generating the sequence.
Throws:
MessagingException

appendFlag

protected void appendFlag(FlagTerm term,
                          String charset)
append IMAP search term information from a FlagTerm item.

Parameters:
term - The source FlagTerm
charset - target charset for the search information (can be null).
out - The target command buffer.

appendAnd

protected void appendAnd(AndTerm term,
                         String charset)
                  throws MessagingException
append IMAP search term information from an AndTerm item.

Parameters:
term - The source AndTerm
charset - target charset for the search information (can be null).
out - The target command buffer.
Throws:
MessagingException

appendOr

protected void appendOr(OrTerm term,
                        String charset)
                 throws MessagingException
append IMAP search term information from an OrTerm item.

Parameters:
term - The source OrTerm
charset - target charset for the search information (can be null).
out - The target command buffer.
Throws:
MessagingException

appendNot

protected void appendNot(NotTerm term,
                         String charset)
                  throws MessagingException
append IMAP search term information from a NotTerm item.

Parameters:
term - The source NotTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendFrom

protected void appendFrom(FromTerm term,
                          String charset)
                   throws MessagingException
append IMAP search term information from a FromTerm item.

Parameters:
term - The source FromTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendFrom

protected void appendFrom(FromStringTerm term,
                          String charset)
                   throws MessagingException
append IMAP search term information from a FromStringTerm item.

Parameters:
term - The source FromStringTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendRecipient

protected void appendRecipient(RecipientTerm term,
                               String charset)
                        throws MessagingException
append IMAP search term information from a RecipientTerm item.

Parameters:
term - The source RecipientTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendRecipient

protected void appendRecipient(RecipientStringTerm term,
                               String charset)
                        throws MessagingException
append IMAP search term information from a RecipientStringTerm item.

Parameters:
term - The source RecipientStringTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

recipientType

protected String recipientType(Message.RecipientType type)
                        throws MessagingException
Translate a recipient type into it's string name equivalent.

Parameters:
type - The source recipient type
Returns:
A string name matching the recipient type.
Throws:
MessagingException

appendHeader

protected void appendHeader(HeaderTerm term,
                            String charset)
                     throws MessagingException
append IMAP search term information from a HeaderTerm item.

Parameters:
term - The source HeaderTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendSubject

protected void appendSubject(SubjectTerm term,
                             String charset)
                      throws MessagingException
append IMAP search term information from a SubjectTerm item.

Parameters:
term - The source SubjectTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendBody

protected void appendBody(BodyTerm term,
                          String charset)
                   throws MessagingException
append IMAP search term information from a BodyTerm item.

Parameters:
term - The source BodyTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendSize

protected void appendSize(SizeTerm term,
                          String charset)
                   throws MessagingException
append IMAP search term information from a SizeTerm item.

Parameters:
term - The source SizeTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendMessageID

protected void appendMessageID(MessageIDTerm term,
                               String charset)
                        throws MessagingException
append IMAP search term information from a MessageIDTerm item.

Parameters:
term - The source MessageIDTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendSentDate

protected void appendSentDate(SentDateTerm term,
                              String charset)
                       throws MessagingException
append IMAP search term information from a SendDateTerm item.

Parameters:
term - The source SendDateTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

appendReceivedDate

protected void appendReceivedDate(ReceivedDateTerm term,
                                  String charset)
                           throws MessagingException
append IMAP search term information from a ReceivedDateTerm item.

Parameters:
term - The source ReceivedDateTerm
charset - target charset for the search information (can be null).
Throws:
MessagingException

checkSearchEncoding

public static boolean checkSearchEncoding(SearchTerm term)
Run the tree of search terms, checking for problems with the terms that may require specifying a CHARSET modifier on a SEARCH command sent to the server.

Parameters:
term - The term to check.
Returns:
True if there are 7-bit problems, false if the terms contain only 7-bit ASCII characters.

checkSearchEncoding

public static boolean checkSearchEncoding(SearchTerm[] terms)
Run an array of search term items to check each one for ASCII encoding problems.

Parameters:
terms - The array of terms to check.
Returns:
True if any of the search terms contains a 7-bit ASCII problem, false otherwise.

checkStringEncoding

public static boolean checkStringEncoding(String s)
Check a string to see if this can be processed using just 7-bit ASCII.

Parameters:
s - The string to check
Returns:
true if the string contains characters outside the 7-bit ascii range, false otherwise.

appendFetchProfile

public void appendFetchProfile(FetchProfile profile)
                        throws MessagingException
Append a FetchProfile information to an IMAPCommand that's to be issued.

Parameters:
profile - The fetch profile we're using.
Throws:
MessagingException

appendACL

public void appendACL(ACL acl)
Append an ACL value to a command. The ACL is the writes string name, followed by the rights value. This version uses no +/- modifier.

Parameters:
acl - The ACL to append.

appendACL

public void appendACL(ACL acl,
                      String modifier)
Append an ACL value to a command. The ACL is the writes string name, followed by the rights value. A +/- modifier can be added to the // result.

Parameters:
acl - The ACL to append.
modifier - The modifer string (can be null).

appendQuota

public void appendQuota(Quota quota)
Append a quota specification to an IMAP command.

Parameters:
quota - The quota value to append.

appendQuotaResource

public void appendQuotaResource(Quota.Resource resource)
Append a Quota.Resource element to an IMAP command. This converts as the resoure name, the usage value and limit value).

Parameters:
resource - The resource element we're appending.


Copyright © 2003-2009 The Apache Software Foundation. All Rights Reserved.