1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.mail.internet;
21
22 import java.io.BufferedInputStream;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.io.PushbackInputStream;
29
30 import javax.activation.DataSource;
31 import javax.mail.BodyPart;
32 import javax.mail.MessagingException;
33 import javax.mail.Multipart;
34 import javax.mail.MultipartDataSource;
35
36 import org.apache.geronimo.mail.util.SessionUtil;
37
38
39
40
41 public class MimeMultipart extends Multipart {
42 private static final String MIME_IGNORE_MISSING_BOUNDARY = "mail.mime.multipart.ignoremissingendboundary";
43
44
45
46
47 protected DataSource ds;
48
49
50
51 protected boolean parsed = true;
52
53
54 private transient ContentType type;
55
56
57 private boolean complete = true;
58
59
60 private String preamble = null;
61
62
63
64
65 public MimeMultipart() {
66 this("mixed");
67 }
68
69
70
71
72
73
74 public MimeMultipart(String subtype) {
75 type = new ContentType("multipart", subtype, null);
76 type.setParameter("boundary", getBoundary());
77 contentType = type.toString();
78 }
79
80
81
82
83
84
85
86 public MimeMultipart(DataSource dataSource) throws MessagingException {
87 ds = dataSource;
88 if (dataSource instanceof MultipartDataSource) {
89 super.setMultipartDataSource((MultipartDataSource) dataSource);
90 parsed = true;
91 } else {
92 type = new ContentType(ds.getContentType());
93 contentType = type.toString();
94 parsed = false;
95 }
96 }
97
98 public void setSubType(String subtype) throws MessagingException {
99 type.setSubType(subtype);
100 contentType = type.toString();
101 }
102
103 public int getCount() throws MessagingException {
104 parse();
105 return super.getCount();
106 }
107
108 public synchronized BodyPart getBodyPart(int part) throws MessagingException {
109 parse();
110 return super.getBodyPart(part);
111 }
112
113 public BodyPart getBodyPart(String cid) throws MessagingException {
114 parse();
115 for (int i = 0; i < parts.size(); i++) {
116 MimeBodyPart bodyPart = (MimeBodyPart) parts.get(i);
117 if (cid.equals(bodyPart.getContentID())) {
118 return bodyPart;
119 }
120 }
121 return null;
122 }
123
124 protected void updateHeaders() throws MessagingException {
125 parse();
126 for (int i = 0; i < parts.size(); i++) {
127 MimeBodyPart bodyPart = (MimeBodyPart) parts.get(i);
128 bodyPart.updateHeaders();
129 }
130 }
131
132 private static byte[] dash = { '-', '-' };
133 private static byte[] crlf = { 13, 10 };
134
135 public void writeTo(OutputStream out) throws IOException, MessagingException {
136 parse();
137 String boundary = type.getParameter("boundary");
138 byte[] bytes = boundary.getBytes();
139
140 if (preamble != null) {
141 byte[] preambleBytes = preamble.getBytes();
142
143 out.write(preambleBytes);
144 out.write(crlf);
145 }
146
147 for (int i = 0; i < parts.size(); i++) {
148 BodyPart bodyPart = (BodyPart) parts.get(i);
149 out.write(dash);
150 out.write(bytes);
151 out.write(crlf);
152 bodyPart.writeTo(out);
153 out.write(crlf);
154 }
155 out.write(dash);
156 out.write(bytes);
157 out.write(dash);
158 out.write(crlf);
159 out.flush();
160 }
161
162 protected void parse() throws MessagingException {
163 if (parsed) {
164 return;
165 }
166 try {
167 ContentType cType = new ContentType(contentType);
168 byte[] boundary = ("--" + cType.getParameter("boundary")).getBytes();
169 InputStream is = new BufferedInputStream(ds.getInputStream());
170 PushbackInputStream pushbackInStream = new PushbackInputStream(is,
171 (boundary.length + 2));
172 readTillFirstBoundary(pushbackInStream, boundary);
173 while (pushbackInStream.available()>0){
174 MimeBodyPartInputStream partStream;
175 partStream = new MimeBodyPartInputStream(pushbackInStream,
176 boundary);
177 addBodyPart(new MimeBodyPart(partStream));
178
179
180 if (!partStream.boundaryFound) {
181 if (!SessionUtil.getBooleanProperty(MIME_IGNORE_MISSING_BOUNDARY, true)) {
182 throw new MessagingException("Missing Multi-part end boundary");
183 }
184 complete = false;
185 }
186 }
187 } catch (Exception e){
188 throw new MessagingException(e.toString(),e);
189 }
190 parsed = true;
191 }
192
193
194
195
196
197
198
199
200
201
202 private boolean readTillFirstBoundary(PushbackInputStream pushbackInStream, byte[] boundary) throws MessagingException {
203 ByteArrayOutputStream preambleStream = new ByteArrayOutputStream();
204
205 try {
206 while (pushbackInStream.available() > 0) {
207 int value = pushbackInStream.read();
208 if ((byte) value == boundary[0]) {
209 int boundaryIndex = 0;
210 while (pushbackInStream.available() > 0 && (boundaryIndex < boundary.length)
211 && ((byte) value == boundary[boundaryIndex])) {
212 value = pushbackInStream.read();
213 if (value == -1)
214 throw new MessagingException(
215 "Unexpected End of Stream while searching for first Mime Boundary");
216 boundaryIndex++;
217 }
218 if (boundaryIndex == boundary.length) {
219 pushbackInStream.read();
220
221
222 byte[] preambleBytes = preambleStream.toByteArray();
223 if (preambleBytes.length > 0) {
224 preamble = new String(preambleBytes);
225 }
226 return true;
227 }
228 else {
229
230
231 preambleStream.write(boundary, 0, boundaryIndex);
232 preambleStream.write((byte)value);
233 }
234 }
235 else {
236
237 preambleStream.write((byte)value);
238 }
239 }
240 } catch (IOException ioe) {
241 throw new MessagingException(ioe.toString(), ioe);
242 }
243 return false;
244 }
245
246 protected InternetHeaders createInternetHeaders(InputStream in) throws MessagingException {
247 return new InternetHeaders(in);
248 }
249
250 protected MimeBodyPart createMimeBodyPart(InternetHeaders headers, byte[] data) throws MessagingException {
251 return new MimeBodyPart(headers, data);
252 }
253
254 protected MimeBodyPart createMimeBodyPart(InputStream in) throws MessagingException {
255 return new MimeBodyPart(in);
256 }
257
258
259 private static int part;
260
261 private synchronized static String getBoundary() {
262 int i;
263 synchronized(MimeMultipart.class) {
264 i = part++;
265 }
266 StringBuffer buf = new StringBuffer(64);
267 buf.append("----=_Part_").append(i).append('_').append((new Object()).hashCode()).append('.').append(System.currentTimeMillis());
268 return buf.toString();
269 }
270
271 private class MimeBodyPartInputStream extends InputStream {
272 PushbackInputStream inStream;
273 public boolean boundaryFound = false;
274 byte[] boundary;
275
276 public MimeBodyPartInputStream(PushbackInputStream inStream,
277 byte[] boundary) {
278 super();
279 this.inStream = inStream;
280 this.boundary = boundary;
281 }
282
283 public int read() throws IOException {
284 if (boundaryFound) {
285 return -1;
286 }
287
288 int value = inStream.read();
289
290
291
292 if (value == 13) {
293 value = inStream.read();
294 if (value != 10) {
295 inStream.unread(value);
296 return 13;
297 } else {
298 value = inStream.read();
299 if ((byte) value != boundary[0]) {
300 inStream.unread(value);
301 inStream.unread(10);
302 return 13;
303 }
304 }
305 } else if ((byte) value != boundary[0]) {
306 return value;
307 }
308
309
310 int boundaryIndex = 0;
311 while ((boundaryIndex < boundary.length)
312 && ((byte) value == boundary[boundaryIndex])) {
313 value = inStream.read();
314 boundaryIndex++;
315 }
316 if (boundaryIndex == boundary.length) {
317 boundaryFound = true;
318
319 if (inStream.read() == '-' && value == '-') {
320
321
322 inStream.read();
323 inStream.read();
324 }
325 return -1;
326 }
327
328
329 if (value != -1) {
330 inStream.unread(value);
331 }
332 inStream.unread(boundary, 1, boundaryIndex - 1);
333 return boundary[0];
334 }
335 }
336
337
338
339
340
341
342
343
344 public boolean isComplete() throws MessagingException {
345
346 parse();
347 return complete;
348 }
349
350
351
352
353
354
355
356
357
358
359 public String getPreamble() throws MessagingException {
360 parse();
361 return preamble;
362 }
363
364
365
366
367
368
369
370
371
372
373 public void setPreamble(String preamble) throws MessagingException {
374 this.preamble = preamble;
375 }
376 }