-
Notifications
You must be signed in to change notification settings - Fork 101
/
Multipart.java
268 lines (241 loc) · 8.54 KB
/
Multipart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package jakarta.mail;
import java.util.Vector;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import jakarta.activation.DataSource;
import jakarta.mail.util.StreamProvider;
/**
* Multipart is a container that holds multiple body parts. Multipart
* provides methods to retrieve and set its subparts. <p>
*
* Multipart also acts as the base class for the content object returned
* by most Multipart DataContentHandlers. For example, invoking getContent()
* on a DataHandler whose source is a "multipart/signed" data source may
* return an appropriate subclass of Multipart. <p>
*
* Some messaging systems provide different subtypes of Multiparts. For
* example, MIME specifies a set of subtypes that include "alternative",
* "mixed", "related", "parallel", "signed", etc. <p>
*
* Multipart is an abstract class. Subclasses provide actual implementations.
*
* @author John Mani
*/
public abstract class Multipart {
/**
* Vector of BodyPart objects.
*/
protected Vector<BodyPart> parts = new Vector<>(); // Holds BodyParts
/**
* This field specifies the content-type of this multipart
* object. It defaults to "multipart/mixed".
*/
protected String contentType = "multipart/mixed"; // Content-Type
/**
* The <code>Part</code> containing this <code>Multipart</code>,
* if known.
* @since JavaMail 1.1
*/
protected Part parent;
/**
* Instance of stream provider.
*
* @since JavaMail 2.1
*/
protected final StreamProvider streamProvider = StreamProvider.provider();
/**
* Default constructor. An empty Multipart object is created.
*/
protected Multipart() { }
/**
* Setup this Multipart object from the given MultipartDataSource. <p>
*
* The method adds the MultipartDataSource's BodyPart
* objects into this Multipart. This Multipart's contentType is
* set to that of the MultipartDataSource. <p>
*
* 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 Multipart subclass that represents
* a specific multipart subtype.
*
* @param mp Multipart datasource
* @exception MessagingException for failures
*/
protected synchronized void setMultipartDataSource(MultipartDataSource mp)
throws MessagingException {
contentType = mp.getContentType();
int count = mp.getCount();
for (int i = 0; i < count; i++)
addBodyPart(mp.getBodyPart(i));
}
/**
* Return the content-type of this Multipart. <p>
*
* This implementation just returns the value of the
* <code>contentType</code> field.
*
* @return content-type
* @see #contentType
*/
public synchronized String getContentType() {
return contentType;
}
/**
* Return the number of enclosed BodyPart objects.
*
* @return number of parts
* @exception MessagingException for failures
* @see #parts
*/
public synchronized int getCount() throws MessagingException {
if (parts == null)
return 0;
return parts.size();
}
/**
* Get the specified Part. Parts are numbered starting at 0.
*
* @param index the index of the desired Part
* @return the Part
* @exception IndexOutOfBoundsException if the given index
* is out of range.
* @exception MessagingException for other failures
*/
public synchronized BodyPart getBodyPart(int index)
throws MessagingException {
if (parts == null)
throw new IndexOutOfBoundsException("No such BodyPart");
return parts.elementAt(index);
}
/**
* Remove the specified part from the multipart message.
* Shifts all the parts after the removed part down one.
*
* @param part The part to remove
* @return true if part removed, false otherwise
* @exception MessagingException if no such Part exists
* @exception IllegalWriteException if the underlying
* implementation does not support modification
* of existing values
*/
public synchronized boolean removeBodyPart(BodyPart part)
throws MessagingException {
if (parts == null)
throw new MessagingException("No such body part");
boolean ret = parts.removeElement(part);
part.setParent(null);
return ret;
}
/**
* Remove the part at specified location (starting from 0).
* Shifts all the parts after the removed part down one.
*
* @param index Index of the part to remove
* @exception IndexOutOfBoundsException if the given index
* is out of range.
* @exception IllegalWriteException if the underlying
* implementation does not support modification
* of existing values
* @exception MessagingException for other failures
*/
public synchronized void removeBodyPart(int index)
throws MessagingException {
if (parts == null)
throw new IndexOutOfBoundsException("No such BodyPart");
BodyPart part = parts.elementAt(index);
parts.removeElementAt(index);
part.setParent(null);
}
/**
* Adds a Part to the multipart. The BodyPart is appended to
* the list of existing Parts.
*
* @param part The Part to be appended
* @exception IllegalWriteException if the underlying
* implementation does not support modification
* of existing values
* @exception MessagingException for other failures
*/
public synchronized void addBodyPart(BodyPart part)
throws MessagingException {
if (parts == null)
parts = new Vector<>();
parts.addElement(part);
part.setParent(this);
}
/**
* Adds a BodyPart at position <code>index</code>.
* If <code>index</code> is not the last one in the list,
* the subsequent parts are shifted up. If <code>index</code>
* is larger than the number of parts present, the
* BodyPart is appended to the end.
*
* @param part The BodyPart to be inserted
* @param index Location where to insert the part
* @exception IllegalWriteException if the underlying
* implementation does not support modification
* of existing values
* @exception MessagingException for other failures
*/
public synchronized void addBodyPart(BodyPart part, int index)
throws MessagingException {
if (parts == null)
parts = new Vector<>();
parts.insertElementAt(part, index);
part.setParent(this);
}
/**
* Output an appropriately encoded bytestream to the given
* OutputStream. The implementation subclass decides the
* appropriate encoding algorithm to be used. The bytestream
* is typically used for sending.
*
* @param os the stream to write to
* @exception IOException if an IO related exception occurs
* @exception MessagingException for other failures
*/
public abstract void writeTo(OutputStream os)
throws IOException, MessagingException;
/**
* Return the <code>Part</code> that contains this <code>Multipart</code>
* object, or <code>null</code> if not known.
*
* @return the parent Part
* @since JavaMail 1.1
*/
public synchronized Part getParent() {
return parent;
}
/**
* Set the parent of this <code>Multipart</code> to be the specified
* <code>Part</code>. Normally called by the <code>Message</code>
* or <code>BodyPart</code> <code>setContent(Multipart)</code> method.
* <code>parent</code> may be <code>null</code> if the
* <code>Multipart</code> is being removed from its containing
* <code>Part</code>.
*
* @param parent the parent Part
* @since JavaMail 1.1
*/
public synchronized void setParent(Part parent) {
this.parent = parent;
}
}