use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class Emailer method splitEmail.
protected List<SendingMessage> splitEmail(EmailInfo info, @Nullable Integer attemptsCount, @Nullable Date deadline) {
List<SendingMessage> sendingMessageList = new ArrayList<>();
String[] splitAddresses = info.getAddresses().split("[,;]");
for (String address : splitAddresses) {
address = address.trim();
if (StringUtils.isNotBlank(address)) {
SendingMessage sendingMessage = convertToSendingMessage(address, info.getFrom(), info.getCaption(), info.getBody(), info.getHeaders(), info.getAttachments(), attemptsCount, deadline);
sendingMessageList.add(sendingMessage);
}
}
return sendingMessageList;
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class Emailer method migrateEmailsToFileStorage.
@Override
public void migrateEmailsToFileStorage(List<SendingMessage> messages) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (SendingMessage msg : messages) {
migrateMessage(em, msg);
}
tx.commit();
}
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class EmailerTest method testMigration.
@Test
public void testMigration() throws Exception {
emailerConfig.setFileStorageUsed(false);
byte[] expectedBytes = new byte[] { 1, 2, 3, 4, 6 };
EmailAttachment fileAttachment = new EmailAttachment(expectedBytes, "invoice.pdf");
String body = "Hi! This is test email. Bye.";
EmailInfo emailInfo = new EmailInfo("test@example.com", "Test", body);
emailInfo.setAttachments(new EmailAttachment[] { fileAttachment });
List<SendingMessage> messages = emailer.sendEmailAsync(emailInfo);
SendingMessage msg;
SendingAttachment attachment;
// check DB storage
msg = reload(messages.get(0), "sendingMessage.loadFromQueue");
attachment = msg.getAttachments().get(0);
assertNotNull(msg.getContentText());
assertNull(msg.getContentTextFile());
assertNotNull(attachment.getContent());
assertNull(attachment.getContentFile());
emailer.migrateEmailsToFileStorage(Lists.newArrayList(msg));
emailer.migrateAttachmentsToFileStorage(Lists.newArrayList(attachment));
// check file storage
msg = reload(msg, "sendingMessage.loadFromQueue");
attachment = msg.getAttachments().get(0);
assertNull(msg.getContentText());
assertNotNull(msg.getContentTextFile());
assertEquals(body, emailer.loadContentText(msg));
assertNull(attachment.getContent());
assertNotNull(attachment.getContentFile());
FileStorageAPI fileStorage = AppBeans.get(FileStorageAPI.NAME);
byte[] actualBytes = fileStorage.loadFile(attachment.getContentFile());
assertByteArrayEquals(expectedBytes, actualBytes);
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class EmailerTest method doTestSeveralRecipients.
private void doTestSeveralRecipients(boolean useFs) throws MessagingException {
emailerConfig.setFileStorageUsed(useFs);
testMailSender.clearBuffer();
String body = "Test Email Body";
// 3 recipients
String recipients = "misha@example.com,kolya@example.com;tanya@example.com;";
EmailInfo myInfo = new EmailInfo(recipients, "Test", body);
List<SendingMessage> messages = emailer.sendEmailAsync(myInfo);
assertEquals(3, messages.size());
assertTrue(testMailSender.isEmpty());
emailer.processQueuedEmails();
Set<String> recipientSet = new HashSet<>();
// check
assertEquals(3, testMailSender.getBufferSize());
for (int i = 0; i < 3; i++) {
MimeMessage msg = testMailSender.fetchSentEmail();
Address[] msgRecipients = msg.getAllRecipients();
assertEquals(1, msgRecipients.length);
recipientSet.add(msgRecipients[0].toString());
}
assertTrue(recipientSet.contains("misha@example.com"));
assertTrue(recipientSet.contains("kolya@example.com"));
assertTrue(recipientSet.contains("tanya@example.com"));
}
use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.
the class EmailerTest method doTestSentFromSecondAttempt.
private void doTestSentFromSecondAttempt(boolean useFs) {
emailerConfig.setFileStorageUsed(useFs);
testMailSender.clearBuffer();
String body = "Test Email Body";
EmailInfo myInfo = new EmailInfo("recipient@example.com", "Test", body);
List<SendingMessage> messages = emailer.sendEmailAsync(myInfo, 2, getDeadlineWhichDoesntMatter());
assertEquals(1, messages.size());
// not sent yet
assertTrue(testMailSender.isEmpty());
SendingMessage sendingMsg = reload(messages.get(0));
assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
// will fail
testMailSender.failPlease();
try {
// try once
emailer.processQueuedEmails();
sendingMsg = reload(sendingMsg);
assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
} finally {
testMailSender.workNormallyPlease();
}
// success now
emailer.processQueuedEmails();
sendingMsg = reload(sendingMsg);
assertEquals(SendingStatus.SENT, sendingMsg.getStatus());
assertEquals(2, sendingMsg.getAttemptsCount().intValue());
}
Aggregations