001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package javax.mail;
021    
022    import java.io.IOException;
023    import java.io.OutputStream;
024    import java.util.Vector;
025    
026    /**
027     * A container for multiple {@link BodyPart BodyParts}.
028     *
029     * @version $Rev: 467553 $ $Date: 2006-10-25 00:01:51 -0400 (Wed, 25 Oct 2006) $
030     */
031    public abstract class Multipart {
032        /**
033         * Vector of sub-parts.
034         */
035        protected Vector parts = new Vector();
036    
037        /**
038         * The content type of this multipart object; defaults to "multipart/mixed"
039         */
040        protected String contentType = "multipart/mixed";
041    
042        /**
043         * The Part that contains this multipart.
044         */
045        protected Part parent;
046    
047        protected Multipart() {
048        }
049    
050        /**
051         * Initialize this multipart object from the supplied data source.
052         * This adds any {@link BodyPart BodyParts} into this object and initializes the content type.
053         *
054         * @param mds the data source
055         * @throws MessagingException
056         */
057        protected void setMultipartDataSource(MultipartDataSource mds) throws MessagingException {
058            parts.clear();
059            contentType = mds.getContentType();
060            int size = mds.getCount();
061            for (int i = 0; i < size; i++) {
062                parts.add(mds.getBodyPart(i));
063            }
064        }
065    
066        /**
067         * Return the content type.
068         *
069         * @return the content type
070         */
071        public String getContentType() {
072            return contentType;
073        }
074    
075        /**
076         * Return the number of enclosed parts
077         *
078         * @return the number of parts
079         * @throws MessagingException
080         */
081        public int getCount() throws MessagingException {
082            return parts.size();
083        }
084    
085        /**
086         * Get the specified part; numbering starts at zero.
087         *
088         * @param index the part to get
089         * @return the part
090         * @throws MessagingException
091         */
092        public BodyPart getBodyPart(int index) throws MessagingException {
093            return (BodyPart) parts.get(index);
094        }
095    
096        /**
097         * Remove the supplied part from the list.
098         *
099         * @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    }