001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.geronimo.javamail.store.imap;
019    
020    import javax.mail.BodyPart;
021    import javax.mail.MessagingException;
022    import javax.mail.MultipartDataSource;
023    
024    import javax.mail.internet.MimePart;
025    import javax.mail.internet.MimePartDataSource;
026    
027    import org.apache.geronimo.javamail.store.imap.connection.IMAPBodyStructure;
028    
029    public class IMAPMultipartDataSource extends MimePartDataSource implements MultipartDataSource {
030        // the list of parts
031        protected BodyPart[] parts;
032    
033        IMAPMultipartDataSource(IMAPMessage message, MimePart parent, String section, IMAPBodyStructure bodyStructure) {
034            super(parent);
035    
036            parts = new BodyPart[bodyStructure.parts.length];
037            
038            // We're either created from the parent message, in which case we're the top level 
039            // of the hierarchy, or we're created from a nested message, so we need to apply the 
040            // parent numbering prefix. 
041            String sectionBase = section == null ? "" : section + "."; 
042    
043            for (int i = 0; i < parts.length; i++) {
044                // create a section id.  This is either the count (origin zero) or a subpart of the previous section.
045                parts[i] = new IMAPMimeBodyPart(message, (IMAPBodyStructure)bodyStructure.parts[i], sectionBase + (i + 1));
046            }
047        }
048    
049        public int getCount() {
050            return parts.length;
051        }
052    
053        public BodyPart getBodyPart(int index) throws MessagingException {
054            return parts[index];
055        }
056    }