com.openexchange.mail.json.writer
Class MessageWriter

java.lang.Object
  extended by com.openexchange.mail.json.writer.MessageWriter

public final class MessageWriter
extends java.lang.Object

MessageWriter - Writes MailMessage instances as JSON strings

Author:
Thorben Betten

Nested Class Summary
static interface MessageWriter.MailFieldWriter
           
 
Method Summary
static long addUserTimezone(long time, java.util.TimeZone timeZone)
          Adds the user time zone offset to given date time
static org.json.JSONArray getAddressesAsArray(javax.mail.internet.InternetAddress[] addrs)
          Convert an array of InternetAddress instances into a JSON-Array conforming to: [["The Personal", "someone@somewhere.com"], ...]
static MessageWriter.MailFieldWriter[] getHeaderFieldWriter(java.lang.String[] headers)
          Gets writers for specified header names.
static MessageWriter.MailFieldWriter[] getMailFieldWriter(MailListField[] fields)
          Generates appropriate field writers for given mail fields
static org.json.JSONObject writeMailMessage(int accountId, MailMessage mail, DisplayMode displayMode, com.openexchange.session.Session session, UserSettingMail settings)
          Writes whole mail as a JSON object.
static org.json.JSONObject writeMailMessage(int accountId, MailMessage mail, DisplayMode displayMode, com.openexchange.session.Session session, UserSettingMail settings, java.util.Collection<com.openexchange.groupware.AbstractOXException> warnings)
          Writes whole mail as a JSON object.
static org.json.JSONObject writeRawMailMessage(int accountId, MailMessage mail)
          Writes raw mail as a JSON object.
static org.json.JSONObject writeStructure(int accountId, MailMessage mail, long maxSize)
          Writes specified mail's structure as a JSON object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

writeStructure

public static org.json.JSONObject writeStructure(int accountId,
                                                 MailMessage mail,
                                                 long maxSize)
                                          throws MailException
Writes specified mail's structure as a JSON object.

Optionally a prepared version can be returned, this includes following actions:

  1. Header names are inserted to JSON lower-case
  2. Mail-safe encoded header values as per RFC 2047 are decoded;
    e.g. To: =?iso-8859-1?q?Keld_J=F8rn?= <keld@xyz.dk>
  3. Address headers are delivered as JSON objects with a "personal" and an "address" field
  4. Parameterized headers are delivered as JSON objects with a "type" and a "params" field

Parameters:
accountId - The mail's account ID
mail - The mail to write
maxSize - The allowed max. size
Returns:
The structure as a JSON object
Throws:
MailException - If writing structure fails

writeMailMessage

public static org.json.JSONObject writeMailMessage(int accountId,
                                                   MailMessage mail,
                                                   DisplayMode displayMode,
                                                   com.openexchange.session.Session session,
                                                   UserSettingMail settings)
                                            throws MailException
Writes whole mail as a JSON object.

Parameters:
accountId - The account ID
mail - The mail to write
displayMode - The display mode
session - The session
settings - The user's mail settings used for writing message; if null the settings are going to be fetched from storage, thus no request-specific preparations will take place.
warnings - A container for possible warnings
Returns:
The written JSON object
Throws:
MailException - If writing message fails

writeMailMessage

public static org.json.JSONObject writeMailMessage(int accountId,
                                                   MailMessage mail,
                                                   DisplayMode displayMode,
                                                   com.openexchange.session.Session session,
                                                   UserSettingMail settings,
                                                   java.util.Collection<com.openexchange.groupware.AbstractOXException> warnings)
                                            throws MailException
Writes whole mail as a JSON object.

Parameters:
accountId - The account ID
mail - The mail to write
displayMode - The display mode
session - The session
settings - The user's mail settings used for writing message; if null the settings are going to be fetched from storage, thus no request-specific preparations will take place.
warnings - A container for possible warnings
Returns:
The written JSON object
Throws:
MailException - If writing message fails

writeRawMailMessage

public static org.json.JSONObject writeRawMailMessage(int accountId,
                                                      MailMessage mail)
                                               throws MailException
Writes raw mail as a JSON object.

Parameters:
accountId - The account ID
mail - The mail to write
Returns:
The written JSON object or null if message's text body parts exceed max. size
Throws:
MailException - If writing message fails

getMailFieldWriter

public static MessageWriter.MailFieldWriter[] getMailFieldWriter(MailListField[] fields)
Generates appropriate field writers for given mail fields

Parameters:
fields - The mail fields to write
Returns:
Appropriate field writers as an array of MessageWriter.MailFieldWriter

getHeaderFieldWriter

public static MessageWriter.MailFieldWriter[] getHeaderFieldWriter(java.lang.String[] headers)
Gets writers for specified header names.

Parameters:
headers - The header names
Returns:
The writers for specified header names

addUserTimezone

public static long addUserTimezone(long time,
                                   java.util.TimeZone timeZone)
Adds the user time zone offset to given date time

Parameters:
time - The date time
timeZone - The time zone
Returns:
The time with added time zone offset

getAddressesAsArray

public static org.json.JSONArray getAddressesAsArray(javax.mail.internet.InternetAddress[] addrs)
Convert an array of InternetAddress instances into a JSON-Array conforming to:
 [["The Personal", "someone@somewhere.com"], ...]