1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package javax.mail;
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Vector;
25
26 /**
27 * A container for multiple {@link BodyPart BodyParts}.
28 *
29 * @version $Rev: 467553 $ $Date: 2006-10-25 00:01:51 -0400 (Wed, 25 Oct 2006) $
30 */
31 public abstract class Multipart {
32 /**
33 * Vector of sub-parts.
34 */
35 protected Vector parts = new Vector();
36
37 /**
38 * The content type of this multipart object; defaults to "multipart/mixed"
39 */
40 protected String contentType = "multipart/mixed";
41
42 /**
43 * The Part that contains this multipart.
44 */
45 protected Part parent;
46
47 protected Multipart() {
48 }
49
50 /**
51 * Initialize this multipart object from the supplied data source.
52 * This adds any {@link BodyPart BodyParts} into this object and initializes the content type.
53 *
54 * @param mds the data source
55 * @throws MessagingException
56 */
57 protected void setMultipartDataSource(MultipartDataSource mds) throws MessagingException {
58 parts.clear();
59 contentType = mds.getContentType();
60 int size = mds.getCount();
61 for (int i = 0; i < size; i++) {
62 parts.add(mds.getBodyPart(i));
63 }
64 }
65
66 /**
67 * Return the content type.
68 *
69 * @return the content type
70 */
71 public String getContentType() {
72 return contentType;
73 }
74
75 /**
76 * Return the number of enclosed parts
77 *
78 * @return the number of parts
79 * @throws MessagingException
80 */
81 public int getCount() throws MessagingException {
82 return parts.size();
83 }
84
85 /**
86 * Get the specified part; numbering starts at zero.
87 *
88 * @param index the part to get
89 * @return the part
90 * @throws MessagingException
91 */
92 public BodyPart getBodyPart(int index) throws MessagingException {
93 return (BodyPart) parts.get(index);
94 }
95
96 /**
97 * Remove the supplied part from the list.
98 *
99 * @param part the part to remove
100 * @return true if the part was removed
101 * @throws MessagingException
102 */
103 public boolean removeBodyPart(BodyPart part) throws MessagingException {
104 return parts.remove(part);
105 }
106
107 /**
108 * Remove the specified part; all others move down one
109 *
110 * @param index the part to remove
111 * @throws MessagingException
112 */
113 public void removeBodyPart(int index) throws MessagingException {
114 parts.remove(index);
115 }
116
117 /**
118 * Add a part to the end of the list.
119 *
120 * @param part the part to add
121 * @throws MessagingException
122 */
123 public void addBodyPart(BodyPart part) throws MessagingException {
124 parts.add(part);
125 }
126
127 /**
128 * Insert a part into the list at a designated point; all subsequent parts move down
129 *
130 * @param part the part to add
131 * @param pos the index of the new part
132 * @throws MessagingException
133 */
134 public void addBodyPart(BodyPart part, int pos) throws MessagingException {
135 parts.add(pos, part);
136 }
137
138 /**
139 * Encode and write this multipart to the supplied OutputStream; the encoding
140 * used is determined by the implementation.
141 *
142 * @param out the stream to write to
143 * @throws IOException
144 * @throws MessagingException
145 */
146 public abstract void writeTo(OutputStream out) throws IOException, MessagingException;
147
148 /**
149 * Return the Part containing this Multipart object or null if unknown.
150 *
151 * @return this Multipart's parent
152 */
153 public Part getParent() {
154 return parent;
155 }
156
157 /**
158 * Set the parent of this Multipart object
159 *
160 * @param part this object's parent
161 */
162 public void setParent(Part part) {
163 parent = part;
164 }
165
166 }