1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.postage.mail;
22
23 import org.apache.james.postage.configuration.MailSender;
24 import org.apache.james.postage.result.MailProcessingRecord;
25
26 import javax.activation.DataHandler;
27 import javax.mail.MessagingException;
28 import javax.mail.Multipart;
29 import javax.mail.internet.MimeBodyPart;
30 import javax.mail.internet.MimeMessage;
31 import javax.mail.internet.MimeMultipart;
32 import javax.mail.util.ByteArrayDataSource;
33
34 /***
35 * mail factory used, when no other is specified
36 */
37 public class DefaultMailFactory extends AbstractMailFactory implements MailFactory {
38
39 protected void populateMessage(MimeMessage message, MailSender mailSender, MailProcessingRecord mailProcessingRecord) throws MessagingException {
40 message.addHeader("Mime-Version", "1.0");
41 message.addHeader("Content-Type", "multipart/mixed");
42
43 Multipart multipart = new MimeMultipart("mixed");
44
45 if (mailSender.sendTextPart()) {
46 int sizeMinText = mailSender.getSizeMinText();
47 int sizeMaxText = mailSender.getSizeMaxText();
48 MimeBodyPart part = new MimeBodyPart();
49
50 int mailSize = generateRandomPartSize(sizeMinText, sizeMaxText);
51 mailProcessingRecord.setByteSendText(mailSize);
52
53 StringBuffer textBody = new StringBuffer(mailSize);
54 for (int i = 0; i < mailSize; i++) textBody.append(getRandomChar());
55
56 part.setText(textBody.toString());
57
58
59
60 multipart.addBodyPart(part);
61 }
62
63 if (mailSender.sendBinaryPart()) {
64 int sizeMinBinary = mailSender.getSizeMinBinary();
65 int sizeMaxBinary = mailSender.getSizeMaxBinary();
66 MimeBodyPart part = new MimeBodyPart();
67
68 int mailSize = generateRandomPartSize(sizeMinBinary, sizeMaxBinary);
69 mailProcessingRecord.setByteSendBinary(mailSize);
70
71 byte[] bytes = new byte[mailSize];
72 for (int i = 0; i < mailSize; i++) bytes[i] = getRandomByte();
73
74 part.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, "application/octet-stream")));
75 multipart.addBodyPart(part);
76 }
77 message.setContent(multipart);
78 }
79
80 protected Class getValidatorClass() {
81 return DefaultMailValidator.class;
82 }
83
84 }