KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > Multipart


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)Multipart.java 1.14 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail;
29
30 import java.util.Vector JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import javax.activation.DataSource JavaDoc;
35
36 /**
37  * Multipart is a container that holds multiple body parts. Multipart
38  * provides methods to retrieve and set its subparts. <p>
39  *
40  * Multipart also acts as the base class for the content object returned
41  * by most Multipart DataContentHandlers. For example, invoking getContent()
42  * on a DataHandler whose source is a "multipart/signed" data source may
43  * return an appropriate subclass of Multipart. <p>
44  *
45  * Some messaging systems provide different subtypes of Multiparts. For
46  * example, MIME specifies a set of subtypes that include "alternative",
47  * "mixed", "related", "parallel", "signed", etc. <p>
48  *
49  * Multipart is an abstract class. Subclasses provide actual implementations.
50  *
51  * @version 1.14, 05/08/29
52  * @author John Mani
53  */

54
55 public abstract class Multipart {
56
57     /**
58      * Vector of BodyPart objects.
59      */

60     protected Vector JavaDoc parts = new Vector JavaDoc(); // Holds BodyParts
61

62     /**
63      * This field specifies the content-type of this multipart
64      * object. It defaults to "multipart/mixed".
65      */

66     protected String JavaDoc contentType = "multipart/mixed" ; // Content-Type
67

68     /**
69      * The <code>Part</code> containing this <code>Multipart</code>,
70      * if known.
71      * @since JavaMail 1.1
72      */

73     protected Part JavaDoc parent;
74
75     /**
76      * Default constructor. An empty Multipart object is created.
77      */

78     protected Multipart() { }
79
80     /**
81      * Setup this Multipart object from the given MultipartDataSource. <p>
82      *
83      * The method adds the MultipartDataSource's BodyPart
84      * objects into this Multipart. This Multipart's contentType is
85      * set to that of the MultipartDataSource. <p>
86      *
87      * This method is typically used in those cases where one
88      * has a multipart data source that has already been pre-parsed into
89      * the individual body parts (for example, an IMAP datasource), but
90      * needs to create an appropriate Multipart subclass that represents
91      * a specific multipart subtype.
92      *
93      * @param mp Multipart datasource
94      */

95     protected void setMultipartDataSource(MultipartDataSource JavaDoc mp)
96             throws MessagingException JavaDoc {
97     contentType = mp.getContentType();
98
99     int count = mp.getCount();
100     for (int i = 0; i < count; i++)
101         addBodyPart(mp.getBodyPart(i));
102     }
103
104     /**
105      * Return the content-type of this Multipart. <p>
106      *
107      * This implementation just returns the value of the
108      * <code>contentType</code> field.
109      *
110      * @return content-type
111      * @see #contentType
112      */

113     public String JavaDoc getContentType() {
114     return contentType;
115     }
116
117     /**
118      * Return the number of enclosed BodyPart objects. <p>
119      *
120      * @return number of parts
121      * @see #parts
122      */

123     public int getCount() throws MessagingException JavaDoc {
124     if (parts == null)
125         return 0;
126
127     return parts.size();
128     }
129
130     /**
131      * Get the specified Part. Parts are numbered starting at 0.
132      *
133      * @param index the index of the desired Part
134      * @return the Part
135      * @exception IndexOutOfBoundsException if the given index
136      * is out of range.
137      * @exception MessagingException
138      */

139     public BodyPart JavaDoc getBodyPart(int index) throws MessagingException JavaDoc {
140     if (parts == null)
141         throw new IndexOutOfBoundsException JavaDoc("No such BodyPart");
142
143     return (BodyPart JavaDoc)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      * @exception MessagingException if no such Part exists
153      * @exception IllegalWriteException if the underlying
154      * implementation does not support modification
155      * of existing values
156      */

157     public boolean removeBodyPart(BodyPart JavaDoc part) throws MessagingException JavaDoc {
158     if (parts == null)
159         throw new MessagingException JavaDoc("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 MessagingException
172      * @exception IndexOutOfBoundsException if the given index
173      * is out of range.
174      * @exception IllegalWriteException if the underlying
175      * implementation does not support modification
176      * of existing values
177      */

178     public void removeBodyPart(int index) throws MessagingException JavaDoc {
179     if (parts == null)
180         throw new IndexOutOfBoundsException JavaDoc("No such BodyPart");
181
182     BodyPart JavaDoc part = (BodyPart JavaDoc)parts.elementAt(index);
183     parts.removeElementAt(index);
184     part.setParent(null);
185     }
186
187     /**
188      * Adds a Part to the multipart. The BodyPart is appended to
189      * the list of existing Parts.
190      *
191      * @param part The Part to be appended
192      * @exception MessagingException
193      * @exception IllegalWriteException if the underlying
194      * implementation does not support modification
195      * of existing values
196      */

197     public synchronized void addBodyPart(BodyPart JavaDoc part)
198         throws MessagingException JavaDoc {
199     if (parts == null)
200         parts = new Vector JavaDoc();
201
202     parts.addElement(part);
203     part.setParent(this);
204     }
205
206     /**
207      * Adds a BodyPart at position <code>index</code>.
208      * If <code>index</code> is not the last one in the list,
209      * the subsequent parts are shifted up. If <code>index</code>
210      * is larger than the number of parts present, the
211      * BodyPart is appended to the end.
212      *
213      * @param part The BodyPart to be inserted
214      * @param index Location where to insert the part
215      * @exception MessagingException
216      * @exception IllegalWriteException if the underlying
217      * implementation does not support modification
218      * of existing values
219      */

220     public synchronized void addBodyPart(BodyPart JavaDoc part, int index)
221                 throws MessagingException JavaDoc {
222     if (parts == null)
223         parts = new Vector JavaDoc();
224
225     parts.insertElementAt(part, index);
226     part.setParent(this);
227     }
228
229     /**
230      * Output an appropriately encoded bytestream to the given
231      * OutputStream. The implementation subclass decides the
232      * appropriate encoding algorithm to be used. The bytestream
233      * is typically used for sending.
234      *
235      * @exception IOException if an IO related exception occurs
236      * @exception MessagingException
237      */

238     public abstract void writeTo(OutputStream JavaDoc os)
239         throws IOException JavaDoc, MessagingException JavaDoc;
240
241     /**
242      * Return the <code>Part</code> that contains this <code>Multipart</code>
243      * object, or <code>null</code> if not known.
244      * @since JavaMail 1.1
245      */

246     public Part JavaDoc getParent() {
247     return parent;
248     }
249
250     /**
251      * Set the parent of this <code>Multipart</code> to be the specified
252      * <code>Part</code>. Normally called by the <code>Message</code>
253      * or <code>BodyPart</code> <code>setContent(Multipart)</code> method.
254      * <code>parent</code> may be <code>null</code> if the
255      * <code>Multipart</code> is being removed from its containing
256      * <code>Part</code>.
257      * @since JavaMail 1.1
258      */

259     public void setParent(Part JavaDoc parent) {
260     this.parent = parent;
261     }
262 }
263
Popular Tags