001 /** 002 * 003 * Copyright 2003-2006 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 javax.mail; 019 020 import java.io.IOException; 021 import java.io.OutputStream; 022 import java.util.Vector; 023 024 /** 025 * A container for multiple {@link BodyPart BodyParts}. 026 * 027 * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $ 028 */ 029 public abstract class Multipart { 030 /** 031 * Vector of sub-parts. 032 */ 033 protected Vector parts = new Vector(); 034 035 /** 036 * The content type of this multipart object; defaults to "multipart/mixed" 037 */ 038 protected String contentType = "multipart/mixed"; 039 040 /** 041 * The Part that contains this multipart. 042 */ 043 protected Part parent; 044 045 protected Multipart() { 046 } 047 048 /** 049 * Initialize this multipart object from the supplied data source. 050 * This adds any {@link BodyPart BodyParts} into this object and initializes the content type. 051 * 052 * @param mds the data source 053 * @throws MessagingException 054 */ 055 protected void setMultipartDataSource(MultipartDataSource mds) throws MessagingException { 056 parts.clear(); 057 contentType = mds.getContentType(); 058 int size = mds.getCount(); 059 for (int i = 0; i < size; i++) { 060 parts.add(mds.getBodyPart(i)); 061 } 062 } 063 064 /** 065 * Return the content type. 066 * 067 * @return the content type 068 */ 069 public String getContentType() { 070 return contentType; 071 } 072 073 /** 074 * Return the number of enclosed parts 075 * 076 * @return the number of parts 077 * @throws MessagingException 078 */ 079 public int getCount() throws MessagingException { 080 return parts.size(); 081 } 082 083 /** 084 * Get the specified part; numbering starts at zero. 085 * 086 * @param index the part to get 087 * @return the part 088 * @throws MessagingException 089 */ 090 public BodyPart getBodyPart(int index) throws MessagingException { 091 return (BodyPart) parts.get(index); 092 } 093 094 /** 095 * Remove the supplied part from the list. 096 * 097 * @param part the part to remove 098 * @return true if the part was removed 099 * @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 }