Skip to content

Package: Multipart

Multipart

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