View Javadoc

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