Class MimeMultipart
- java.lang.Object
-
- com.sun.xml.messaging.saaj.packaging.mime.internet.MimeMultipart
-
- Direct Known Subclasses:
BMMimeMultipart
,MimePullMultipart
public class MimeMultipart extends Object
The MimeMultipart class is an implementation that uses MIME conventions for the multipart data.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, usenew MimeMultipart("alternative")
.- Version:
- 1.31, 03/01/29
- Author:
- John Mani, Bill Shannon, Max Spivak
-
-
Field Summary
Fields Modifier and Type Field Description protected ContentType
contentType
This field specifies the content-type of this multipart object.protected jakarta.activation.DataSource
ds
The DataSource supplying our InputStream.protected static boolean
ignoreMissingEndBoundary
protected MimeBodyPart
parent
TheMimeBodyPart
containing thisMimeMultipart
, 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 Summary
Constructors Constructor Description MimeMultipart()
Default constructor.MimeMultipart(jakarta.activation.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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBodyPart(MimeBodyPart part)
Adds a MimeBodyPart to the multipart.void
addBodyPart(MimeBodyPart part, int index)
Adds a MimeBodyPart at positionindex
.protected InternetHeaders
createInternetHeaders(InputStream is)
Create and return an InternetHeaders object that loads the headers from the given 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.protected MimeBodyPart
createMimeBodyPart(InputStream is)
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.
-
-
-
Field Detail
-
ds
protected jakarta.activation.DataSource ds
The DataSource supplying our InputStream.
-
parsed
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.
-
parts
protected FinalArrayList<MimeBodyPart> parts
Vector of MimeBodyPart objects.
-
contentType
protected ContentType contentType
This field specifies the content-type of this multipart object. It defaults to "multipart/mixed".
-
parent
protected MimeBodyPart parent
TheMimeBodyPart
containing thisMimeMultipart
, if known.- Since:
- JavaMail 1.1
-
ignoreMissingEndBoundary
protected static final boolean ignoreMissingEndBoundary
-
-
Constructor Detail
-
MimeMultipart
public MimeMultipart()
Default constructor. An empty MimeMultipart object is created. Its content type is set to "multipart/mixed". A unique boundary string is generated and this string is setup as the "boundary" parameter for thecontentType
field.MimeBodyParts may be added later.
-
MimeMultipart
public MimeMultipart(String subtype)
Construct a MimeMultipart object of the given subtype. A unique boundary string is generated and this string is setup as the "boundary" parameter for thecontentType
field.MimeBodyParts may be added later.
- Parameters:
subtype
- subtype.
-
MimeMultipart
public MimeMultipart(jakarta.activation.DataSource ds, ContentType ct) throws MessagingException
Constructs a MimeMultipart object and its bodyparts from the given DataSource.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.- Parameters:
ds
- DataSource, can be a MultipartDataSourcect
- This must be the same information asDataSource.getContentType()
. All the callers of this method seem to have this object handy, so for performance reason this method accepts it. Can be null.- Throws:
MessagingException
- in case of error
-
-
Method Detail
-
setSubType
public void setSubType(String subtype)
Set the subtype. This method should be invoked only on a new MimeMultipart object created by the client. The default subtype of such a multipart object is "mixed".- Parameters:
subtype
- Subtype
-
getCount
public int getCount() throws MessagingException
Return the number of enclosed MimeBodyPart objects.- Returns:
- number of parts.
- Throws:
MessagingException
- in case of error.
-
getBodyPart
public MimeBodyPart getBodyPart(int index) throws MessagingException
Get the specified MimeBodyPart. BodyParts are numbered starting at 0.- Parameters:
index
- the index of the desired MimeBodyPart.- Returns:
- the MimeBodyPart.
- Throws:
MessagingException
- if no such MimeBodyPart exists
-
getBodyPart
public MimeBodyPart getBodyPart(String CID) throws MessagingException
Get the MimeBodyPart referred to by the given ContentID (CID). Returns null if the part is not found.- Parameters:
CID
- the ContentID of the desired part- Returns:
- the MimeBodyPart
- Throws:
MessagingException
- if no such MimeBodyPart exists.
-
updateHeaders
protected void updateHeaders() throws MessagingException
Update headers. The default implementation here just calls theupdateHeaders
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.- Throws:
MessagingException
- in case of error.
-
writeTo
public void writeTo(OutputStream os) throws IOException, MessagingException
Iterates through all the parts and outputs each Mime part separated by a boundary.- Parameters:
os
- output stream.- Throws:
IOException
- if an I/O Error occurs.MessagingException
- in case of error.
-
parse
protected void parse() throws MessagingException
Parse the InputStream from our DataSource, constructing the appropriate MimeBodyParts. Theparsed
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.- Throws:
MessagingException
- in case of error.- Since:
- JavaMail 1.2
-
createInternetHeaders
protected InternetHeaders createInternetHeaders(InputStream is) throws MessagingException
Create and return an InternetHeaders object that loads the headers from the given InputStream. Subclasses can override this method to return a subclass of InternetHeaders, if necessary. This implementation simply constructs and returns an InternetHeaders object.- Parameters:
is
- the InputStream to read the headers from.- Returns:
- headers.
- Throws:
MessagingException
- in case of error.- Since:
- JavaMail 1.2
-
createMimeBodyPart
protected MimeBodyPart createMimeBodyPart(InternetHeaders headers, byte[] content, int len)
Create and return a MimeBodyPart object to represent a body part parsed from the InputStream. Subclasses can override this method to return a subclass of MimeBodyPart, if necessary. This implementation simply constructs and returns a MimeBodyPart object.- Parameters:
headers
- the headers for the body part.content
- the content of the body part.len
- the content length.- Returns:
- MimeBodyPart
- Since:
- JavaMail 1.2
-
createMimeBodyPart
protected MimeBodyPart createMimeBodyPart(InputStream is) throws MessagingException
Create and return a MimeBodyPart object to represent a body part parsed from the InputStream. Subclasses can override this method to return a subclass of MimeBodyPart, if necessary. This implementation simply constructs and returns a MimeBodyPart object.- Parameters:
is
- InputStream containing the body part.- Returns:
- MimeBodyPart.
- Throws:
MessagingException
- in case of error.- Since:
- JavaMail 1.2
-
setMultipartDataSource
protected void setMultipartDataSource(MultipartDataSource mp) throws MessagingException
Setup this MimeMultipart object from the given MultipartDataSource.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.
- Parameters:
mp
- MimeMultipart datasource- Throws:
MessagingException
- in case of error.
-
getContentType
public ContentType getContentType()
Return the content-type of this MimeMultipart.This implementation just returns the value of the
contentType
field.- Returns:
- content-type
- See Also:
contentType
-
removeBodyPart
public boolean removeBodyPart(MimeBodyPart part) throws MessagingException
Remove the specified part from the multipart message. Shifts all the parts after the removed part down one.- Parameters:
part
- The part to remove- Returns:
- true if part removed, false otherwise
- Throws:
MessagingException
- if no such MimeBodyPart exists
-
removeBodyPart
public void removeBodyPart(int index)
Remove the part at specified location (starting from 0). Shifts all the parts after the removed part down one.- Parameters:
index
- Index of the part to remove- Throws:
IndexOutOfBoundsException
- if the given index is out of range.
-
addBodyPart
public void addBodyPart(MimeBodyPart part)
Adds a MimeBodyPart to the multipart. The MimeBodyPart is appended to the list of existing Parts.- Parameters:
part
- The MimeBodyPart to be appended
-
addBodyPart
public void addBodyPart(MimeBodyPart part, int index)
Adds a MimeBodyPart at positionindex
. Ifindex
is not the last one in the list, the subsequent parts are shifted up. Ifindex
is larger than the number of parts present, the MimeBodyPart is appended to the end.- Parameters:
part
- The MimeBodyPart to be insertedindex
- Location where to insert the part
-
-