|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object SK.gnome.dwarf.mail.mime.MimePartBuilder
Provides a MimePart builder.
This class implements the MimePart
interface and can be used to create MIME parts
with a very little effort.
All methods which set MIME headers handle non-ASCII strings by encoding them as per RFC 2047.
Performance note: this class stores the part content in an internal byte buffer, therefore it is not suitable for a frequent building of large binary parts.
Field Summary | |
protected boolean |
allow8bit
Whether the 8bit transfer encoding is permitted. |
static java.lang.String |
DEFAULT_CHARSET
The default MIME charset for encoding headers as per RFC 2047. |
protected java.lang.String |
charset
The actual MIME charset for encoding headers as per RFC 2047. |
Constructor Summary | |
MimePartBuilder()
Creates a new MimePartBuilder. |
|
MimePartBuilder(boolean allow8bit)
Creates a new MimePartBuilder with 8bit transfer enabled. |
|
MimePartBuilder(boolean allow8bit,
java.lang.String charset)
Creates a new MimePartBuilder. |
|
MimePartBuilder(java.lang.String charset)
Creates a new MimePartBuilder with the given charset. |
Method Summary | |
void |
addHeader(java.lang.String name,
java.lang.String value)
Adds the given header. |
void |
addMimePart(MimePartBuilder part)
Adds the given MIME part. |
java.io.InputStream |
getContentInputStream()
Returns the content input stream. |
int |
getContentSize()
Returns the content size. |
java.io.InputStream |
getDecodedInputStream()
Returns the decoded content input stream. |
java.lang.String[] |
getHeader(java.lang.String name)
Returns all values of the given header. |
java.lang.String |
getHeader(java.lang.String name,
java.lang.String delimiter)
Returns all values of the given header, delimited by the given character sequence. |
java.util.Iterator |
getHeaderLines()
Returns all header lines. |
java.util.Iterator |
getHeaderNames()
Returns all header names. |
int |
getLineCount()
Returns the line count of the body. |
java.util.Iterator |
getMatchingHeaderLines(java.lang.String[] names)
Returns matching header lines. |
MimePart |
getMimePart(int number)
Returns the nested MIME part. |
int |
getMimePartCount()
Returns the number of nested MIME parts. |
java.util.Iterator |
getNonMatchingHeaderLines(java.lang.String[] names)
Returns non matching header lines. |
java.io.InputStream |
getRawInputStream()
Returns the raw input stream. |
int |
getRawSize()
Returns the raw size of this part. |
boolean |
isMimeType(java.lang.String type)
Whether this MIME part if of the given type. |
void |
removeHeader(java.lang.String name)
Removes the given header. |
void |
removeMimePart(int number)
Removes the given MIME part. |
void |
setAddressHeader(java.lang.String name,
java.lang.String value)
Sets the given address header. |
void |
setAddressHeader(java.lang.String name,
java.lang.String[] value)
Sets the given address header. |
void |
setContent(java.io.InputStream in,
java.lang.String type,
java.lang.String encoding)
Sets the body content with the given MIME content type and transfer encoding. |
void |
setContent(java.lang.String text)
Sets the body content. |
void |
setContent(java.lang.String text,
java.lang.String charset)
Sets the body content with the given charset. |
void |
setContentDescription(java.lang.String description)
Sets the "Content-Description" header. |
void |
setContentLanguage(java.lang.String[] languages)
Sets the "Content-Language" header. |
void |
setContentTransferEncoding(java.lang.String encoding)
Sets the "Content-Transfer-Encoding" header. |
void |
setContentType(java.lang.String type)
Sets the "Content-Type" header. |
void |
setHeader(java.lang.String name,
java.lang.String value)
Sets the given header. |
void |
setName(java.lang.String name)
Sets the MIME part name. |
void |
writeTo(java.io.OutputStream out)
Writes the entire MIME part to the given output stream. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String DEFAULT_CHARSET
The default charset is "utf-8".
protected java.lang.String charset
protected boolean allow8bit
Constructor Detail |
public MimePartBuilder() throws java.io.IOException, MimeException
public MimePartBuilder(boolean allow8bit) throws java.io.IOException, MimeException
If the allow8bit parameter is true, the part will be created assuming that the MTA is capable to transfer 8bit content. If it is false, the content will be always converted to pure 7bit ASCII format.
allow8bit
- whether to enable 8bit contentpublic MimePartBuilder(java.lang.String charset) throws java.io.IOException, MimeException
The charset parameter specifies the MIME charset used to encode the headers as per RFC 2047.
charset
- the MIME charsetcharset
public MimePartBuilder(boolean allow8bit, java.lang.String charset) throws java.io.IOException, MimeException
If the allow8bit parameter is true, the part will be created assuming that the MTA is capable to transfer 8bit content. If it is false, the content will be always converted to pure 7bit ASCII format.
The charset parameter specifies the MIME charset used to encode the headers as per RFC 2047.
allow8bit
- whether to enable 8bit contentcharset
- the MIME charsetcharset
Method Detail |
public java.util.Iterator getHeaderNames() throws MimeException
MimePart
getHeaderNames
in interface MimePart
MimeException
- if a MIME syntax error occuredpublic java.lang.String[] getHeader(java.lang.String name) throws MimeException
MimePart
getHeader
in interface MimePart
name
- the header name
MimeException
- if a MIME syntax error occuredpublic java.lang.String getHeader(java.lang.String name, java.lang.String delimiter) throws MimeException
MimePart
getHeader
in interface MimePart
name
- the header namedelimiter
- the value delimiter
MimeException
- if a MIME syntax error occuredpublic java.util.Iterator getHeaderLines() throws MimeException
MimePart
The header lines are returned in the exact form as they appear in the MIME part.
getHeaderLines
in interface MimePart
MimeException
- if a MIME syntax error occuredpublic java.util.Iterator getMatchingHeaderLines(java.lang.String[] names) throws MimeException
MimePart
The header lines are returned in the exact form as they appear in the MIME part.
getMatchingHeaderLines
in interface MimePart
names
- the header names
MimeException
- if a MIME syntax error occuredpublic java.util.Iterator getNonMatchingHeaderLines(java.lang.String[] names) throws MimeException
MimePart
The header lines are returned in the exact form as they appear in the MIME part.
getNonMatchingHeaderLines
in interface MimePart
names
- the header names
MimeException
- if a MIME syntax error occuredpublic boolean isMimeType(java.lang.String type) throws MimeException
MimePart
The type argument must be in the form of "type/subtype" pair. If wildcard character "*" is used as the subtype, the method will test only the main type. The type matching is case insensitive.
isMimeType
in interface MimePart
type
- the mime type
MimeException
- if a MIME syntax error occuredpublic int getRawSize() throws MimeException
MimePart
Returns the size of the whole MIME part including the header.
getRawSize
in interface MimePart
MimeException
- if a MIME syntax error occuredpublic int getContentSize() throws MimeException
MimePart
Returns the size of content-encoded MIME part body.
getContentSize
in interface MimePart
MimeException
- if a MIME syntax error occuredpublic int getLineCount() throws java.io.IOException, MimeException
MimePart
Returns the total line count of the content-encoded MIME part body.
getLineCount
in interface MimePart
MimeException
- if a MIME syntax error occured
java.io.IOException
- if an I/O error occuredpublic java.io.InputStream getRawInputStream() throws java.io.IOException, MimeException
MimePart
Returns the input stream of the whole MIME part including the header.
getRawInputStream
in interface MimePart
MimeException
- if a MIME syntax error occured
java.io.IOException
- if an I/O error occuredpublic java.io.InputStream getContentInputStream() throws java.io.IOException, MimeException
MimePart
Returns the input stream of the content-encoded MIME part body.
getContentInputStream
in interface MimePart
java.io.IOException
- if an I/O error occured
MimeException
- if a MIME syntax error occuredpublic java.io.InputStream getDecodedInputStream() throws java.io.IOException, MimeException
MimePart
Returns the input stream of the content-decoded MIME part body.
getDecodedInputStream
in interface MimePart
java.io.IOException
- if an I/O error occured
MimeException
- if a MIME syntax error occuredpublic int getMimePartCount() throws MimeException
MimePart
Returns the total number of nested MIME parts. The parts are numbered from zero. Only parts of type "multipart/*" or "message/rfc822" are regarded as multipart. If the part if of type "message/rfc822", it has only one directly nested part - the RFC822 message itself, which may in turn contain another MIME parts, if it is a multipart message.
getMimePartCount
in interface MimePart
MimeException
- if a MIME syntax error occuredpublic MimePart getMimePart(int number) throws MimeException
MimePart
getMimePart
in interface MimePart
number
- the part number
MimeException
- if a MIME syntax error occuredpublic void setName(java.lang.String name)
The name is used to set the corresponding parameter of the Content-Type header.
name
- the part namepublic void setHeader(java.lang.String name, java.lang.String value) throws MailException
It sets the header with the given name to the given value. All other occurences of the header are removed.
If the given value is a non-ASCII string, it is first encoded as per RFC 2047 using
the actual charset
.
name
- the header namevalue
- the header value
MailException
- if an error occuredpublic void setAddressHeader(java.lang.String name, java.lang.String value) throws MailException
It sets the header with the given name to the given value. All other occurences of the header are removed. The value must be a comma-separated list of valid RFC 822 addresses.
The addresses are automatically encoded as per RFC 2047 using the actual charset
.
This method calls setHeader(String, String)
to set the header value.
name
- the header namevalue
- comma-separated list of addresses
MailException
- if an error occuredpublic void setAddressHeader(java.lang.String name, java.lang.String[] value) throws MailException
It sets the header with the given name to the given value. All other occurences of the header are removed. The value must be an array of valid RFC 822 addresses.
The addresses are automatically encoded as per RFC 2047 using the actual charset
.
This method calls setHeader(String, String)
to set the header value.
name
- the header namevalue
- array of addresses
MailException
- if an error occuredpublic void addHeader(java.lang.String name, java.lang.String value) throws MailException
It adds the header with the given name and value. All other occurences of the headers are left untouched.
If the given value is a non-ASCII string, it is first encoded as per RFC 2047 using the actual charset.
name
- the header namevalue
- the header value
MailException
- if an error occuredpublic void removeHeader(java.lang.String name)
It removes all occurences of the given header.
name
- the header namepublic void setContentDescription(java.lang.String description) throws MailException
description
- the description
MailException
- if an error occuredpublic void setContentLanguage(java.lang.String[] languages) throws MailException
languages
- the languages
MailException
- if an error occuredpublic void setContentType(java.lang.String type) throws MailException
It the part's name is not null, the corresponding parameter of the Content-Type header is set also.
If this part contains another parts, the boundary parameter is automatically added to the Content-Type header value, too.
type
- the MIME content type
MailException
- if an error occuredpublic void setContentTransferEncoding(java.lang.String encoding) throws MailException
The currently supported encodings are: 7bit, 8bit, quoted-printable, base64 and uuencode.
encoding
- the encoding
MailException
- if an error occuredpublic void setContent(java.lang.String text) throws java.io.IOException, MailException
It sets the text body by calling setContent(String, String)
with
the null charset argument.
text
- the body text
java.io.IOException
- if an I/O error occured
MailException
- if an error occuredpublic void setContent(java.lang.String text, java.lang.String charset) throws java.io.IOException, MailException
It sets the text body by encoding it first as necessary according to the given
MIME charset. If the charset parameter is null, a default "us-ascii"
or DEFAULT_CHARSET
is choosen according to the given text.
This method sets also the Content-Type and Content-Transfer-Encoding headers, overriding the existing values.
text
- the body textcharset
- the MIME charset or null
java.io.IOException
- if an I/O error occured
MailException
- if an error occuredpublic void setContent(java.io.InputStream in, java.lang.String type, java.lang.String encoding) throws java.io.IOException, MailException
It sets the body by encoding the octets first as necessary according to the given transfer encoding. The currently supported encodings are: 7bit, 8bit, quoted-printable, base64 and uuencode.
This method sets the Content-Type and Content-Transfer-Encoding headers by overriding the existing ones.
in
- the input streamtype
- the MIME content typeencoding
- the transfer encoding
java.io.IOException
- if an I/O error occured
MailException
- if an error occuredpublic void addMimePart(MimePartBuilder part) throws MailException
Please note that this method does not modify the Content-Type header, therefore it must be set manually to one of the MIME multipart types after all the nested MIME parts have been added.
part
- the MIME part
MailException
public void removeMimePart(int number)
number
- the part numberpublic void writeTo(java.io.OutputStream out) throws java.io.IOException, MimeException
out
- the output stream to write to
java.io.IOException
- if an error occured
MimeException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |