public class MimeMultipart extends Object
A MimeMultipart is obtained from a MimeBodyPart whose primary type
is "multipart" (by invoking the part's getContent()
method)
or it can be created by a client as part of creating a new MimeMessage.
The default multipart subtype is "mixed". The other multipart subtypes, such as "alternative", "related", and so on, can be implemented as subclasses of MimeMultipart with additional methods to implement the additional semantics of that type of multipart content. The intent is that service providers, mail JavaBean writers and mail clients will write many such subclasses and their Command Beans, and will install them into the JavaBeans Activation Framework, so that any JavaMail implementation and its clients can transparently find and use these classes. Thus, a MIME multipart handler is treated just like any other type handler, thereby decoupling the process of providing multipart handlers from the JavaMail API. Lacking these additional MimeMultipart subclasses, all subtypes of MIME multipart data appear as MimeMultipart objects.
An application can directly construct a MIME multipart object of any
subtype by using the MimeMultipart(String subtype)
constructor. For example, to create a "multipart/alternative" object,
use new MimeMultipart("alternative")
.
Modifier and Type | Field and Description |
---|---|
protected ContentType |
contentType
This field specifies the content-type of this multipart
object.
|
protected DataSource |
ds
The DataSource supplying our InputStream.
|
protected static boolean |
ignoreMissingEndBoundary |
protected MimeBodyPart |
parent
The
MimeBodyPart containing this MimeMultipart ,
if known. |
protected boolean |
parsed
Have we parsed the data from our InputStream yet?
Defaults to true; set to false when our constructor is
given a DataSource with an InputStream that we need to
parse.
|
protected FinalArrayList<MimeBodyPart> |
parts
Vector of MimeBodyPart objects.
|
Constructor and Description |
---|
MimeMultipart()
Default constructor.
|
MimeMultipart(DataSource ds,
ContentType ct)
Constructs a MimeMultipart object and its bodyparts from the
given DataSource.
|
MimeMultipart(String subtype)
Construct a MimeMultipart object of the given subtype.
|
Modifier and Type | Method and Description |
---|---|
void |
addBodyPart(MimeBodyPart part)
Adds a MimeBodyPart to the multipart.
|
void |
addBodyPart(MimeBodyPart part,
int index)
Adds a MimeBodyPart at position
index . |
protected InternetHeaders |
createInternetHeaders(InputStream is)
Create and return an InternetHeaders object that loads the
headers from the given InputStream.
|
protected MimeBodyPart |
createMimeBodyPart(InputStream is)
Create and return a MimeBodyPart object to represent a
body part parsed from the InputStream.
|
protected MimeBodyPart |
createMimeBodyPart(InternetHeaders headers,
byte[] content,
int len)
Create and return a MimeBodyPart object to represent a
body part parsed from the InputStream.
|
MimeBodyPart |
getBodyPart(int index)
Get the specified MimeBodyPart.
|
MimeBodyPart |
getBodyPart(String CID)
Get the MimeBodyPart referred to by the given ContentID (CID).
|
ContentType |
getContentType()
Return the content-type of this MimeMultipart.
|
int |
getCount()
Return the number of enclosed MimeBodyPart objects.
|
protected void |
parse()
Parse the InputStream from our DataSource, constructing the
appropriate MimeBodyParts.
|
void |
removeBodyPart(int index)
Remove the part at specified location (starting from 0).
|
boolean |
removeBodyPart(MimeBodyPart part)
Remove the specified part from the multipart message.
|
protected void |
setMultipartDataSource(MultipartDataSource mp)
Setup this MimeMultipart object from the given MultipartDataSource.
|
void |
setSubType(String subtype)
Set the subtype.
|
protected void |
updateHeaders()
Update headers.
|
void |
writeTo(OutputStream os)
Iterates through all the parts and outputs each Mime part
separated by a boundary.
|
protected DataSource ds
protected boolean parsed
protected FinalArrayList<MimeBodyPart> parts
protected ContentType contentType
protected MimeBodyPart parent
MimeBodyPart
containing this MimeMultipart
,
if known.protected static final boolean ignoreMissingEndBoundary
public MimeMultipart()
contentType
field. MimeBodyParts may be added later.
public MimeMultipart(String subtype)
contentType
field. MimeBodyParts may be added later.
subtype
- subtype.public MimeMultipart(DataSource ds, ContentType ct) throws MessagingException
This constructor handles as a special case the situation where the
given DataSource is a MultipartDataSource object.
Otherwise, the DataSource is assumed to provide a MIME multipart
byte stream. The parsed
flag is set to false. When
the data for the body parts are needed, the parser extracts the
"boundary" parameter from the content type of this DataSource,
skips the 'preamble' and reads bytes till the terminating
boundary and creates MimeBodyParts for each part of the stream.
ds
- DataSource, can be a MultipartDataSourcect
- This must be the same information as DataSource.getContentType()
.
All the callers of this method seem to have this object handy, so
for performance reason this method accepts it. Can be null.MessagingException
- in case of errorpublic void setSubType(String subtype)
subtype
- Subtypepublic int getCount() throws MessagingException
MessagingException
- in case of error.public MimeBodyPart getBodyPart(int index) throws MessagingException
index
- the index of the desired MimeBodyPart.MessagingException
- if no such MimeBodyPart existspublic MimeBodyPart getBodyPart(String CID) throws MessagingException
CID
- the ContentID of the desired partMessagingException
- if no such MimeBodyPart exists.protected void updateHeaders() throws MessagingException
updateHeaders
method on each of its
children BodyParts. Note that the boundary parameter is already set up when a new and empty MimeMultipart object is created.
This method is called when the saveChanges
method is invoked on the Message object containing this
MimeMultipart. This is typically done as part of the Message
send process, however note that a client is free to call
it any number of times. So if the header updating process is
expensive for a specific MimeMultipart subclass, then it
might itself want to track whether its internal state actually
did change, and do the header updating only if necessary.
MessagingException
- in case of error.public void writeTo(OutputStream os) throws IOException, MessagingException
os
- output stream.IOException
- if an I/O Error occurs.MessagingException
- in case of error.protected void parse() throws MessagingException
parsed
flag is
set to true, and if true on entry nothing is done. This
method is called by all other methods that need data for
the body parts, to make sure the data has been parsed.MessagingException
- in case of error.protected InternetHeaders createInternetHeaders(InputStream is) throws MessagingException
is
- the InputStream to read the headers from.MessagingException
- in case of error.protected MimeBodyPart createMimeBodyPart(InternetHeaders headers, byte[] content, int len)
headers
- the headers for the body part.content
- the content of the body part.len
- the content length.protected MimeBodyPart createMimeBodyPart(InputStream is) throws MessagingException
is
- InputStream containing the body part.MessagingException
- in case of error.protected void setMultipartDataSource(MultipartDataSource mp) throws MessagingException
The method adds the MultipartDataSource's MimeBodyPart objects into this MimeMultipart. This MimeMultipart's contentType is set to that of the MultipartDataSource.
This method is typically used in those cases where one has a multipart data source that has already been pre-parsed into the individual body parts (for example, an IMAP datasource), but needs to create an appropriate MimeMultipart subclass that represents a specific multipart subtype.
mp
- MimeMultipart datasourceMessagingException
- in case of error.public ContentType getContentType()
This implementation just returns the value of the
contentType
field.
contentType
public boolean removeBodyPart(MimeBodyPart part) throws MessagingException
part
- The part to removeMessagingException
- if no such MimeBodyPart existspublic void removeBodyPart(int index)
index
- Index of the part to removeIndexOutOfBoundsException
- if the given index
is out of range.public void addBodyPart(MimeBodyPart part)
part
- The MimeBodyPart to be appendedpublic void addBodyPart(MimeBodyPart part, int index)
index
.
If index
is not the last one in the list,
the subsequent parts are shifted up. If index
is larger than the number of parts present, the
MimeBodyPart is appended to the end.part
- The MimeBodyPart to be insertedindex
- Location where to insert the partCopyright © 2021 JBoss by Red Hat. All rights reserved.