Skip to content

Package: Multipart

Multipart

nameinstructionbranchcomplexitylinemethod
Multipart()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
addBodyPart(BodyPart)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
addBodyPart(BodyPart, int)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getBodyPart(int)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getContentType()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getCount()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getParent()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
removeBodyPart(BodyPart)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
removeBodyPart(int)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
setMultipartDataSource(MultipartDataSource)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
setParent(Part)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

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