use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class DatabaseMailListenerTest method onPrepareError.
@Test
public void onPrepareError() throws Exception {
MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
MailListener listener = this.mocker.getComponentUnderTest();
listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
listener.onPrepareMessageError(this.message, new Exception("Error"), Collections.<String, Object>emptyMap());
verify(mailStatusStore).save(argThat(new isSameMailStatus(MailState.PREPARE_ERROR, "mywiki")), anyMap());
}
use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class DatabaseMailListenerTest method onSuccessWhenMailContentDeletionFails.
@Test
public void onSuccessWhenMailContentDeletionFails() throws Exception {
MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
MailStatus status = new MailStatus(this.batchId, this.message, MailState.PREPARE_SUCCESS);
status.setWiki("otherwiki");
when(mailStatusStore.load(this.messageId)).thenReturn(status);
MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
doThrow(new MailStoreException("error")).when(mailContentStore).delete(this.batchId, this.messageId);
MailListener listener = this.mocker.getComponentUnderTest();
listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
assertEquals("Failed to remove previously failing message [" + this.messageId + "] (batch id [" + this.batchId + "]) from the file system. Reason [MailStoreException: error].", this.logRule.getMessage(0));
}
use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class SendMailRunnable method sendMail.
/**
* Send the mail.
*
* @param item the queue item containing all the data for sending the mail
*/
protected void sendMail(SendMailQueueItem item) {
prepareContextForQueueItem(item);
MailListener listener = item.getListener();
ExtendedMimeMessage message;
try {
// Step 1: Load the message from the filesystem store
message = this.mailContentStore.load(item.getSession(), item.getBatchId(), item.getUniqueMessageId());
} catch (Exception e) {
if (listener != null) {
listener.onSendMessageFatalError(item.getUniqueMessageId(), e, Collections.<String, Object>emptyMap());
}
return;
}
try {
// TODO: explain why!
if (item.getSession() != this.currentSession || (this.count % 100) == 0) {
closeTransport();
this.currentSession = item.getSession();
this.currentTransport = this.currentSession.getTransport("smtp");
this.currentTransport.connect();
} else if (!this.currentTransport.isConnected()) {
this.currentTransport.connect();
}
// Step 3: Send the mail
// Unlike the static send method, the sendMessage method does not call the saveChanges method on the
// message; this prevent the MessageID header to be changed.
this.currentTransport.sendMessage(message, message.getAllRecipients());
this.count++;
// Step 4: Notify the user of the success if a listener has been provided
if (listener != null) {
listener.onSendMessageSuccess(message, Collections.<String, Object>emptyMap());
}
} catch (Exception e) {
// An error occurred, notify the user if a listener has been provided.
if (listener != null) {
listener.onSendMessageError(message, e, Collections.<String, Object>emptyMap());
}
}
}
use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class NotificationEmailSender method sendEmails.
/**
* Send notifications emails for specified users.
* @param fromDate only send notifications about events that happened after this date
* @param notificationUserIterator iterator for users interested in the notifications emails
* @throws JobExecutionException if error happens
*/
public void sendEmails(Date fromDate, NotificationUserIterator notificationUserIterator) throws JobExecutionException {
Map<String, Object> emailFactoryParameters = new HashMap<>();
DocumentReference templateReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), Arrays.asList("XWiki", "Notifications"), "MailTemplate");
PeriodicMimeMessageIterator periodicMimeMessageIterator = notificationMimeMessageIteratorProvider.get();
periodicMimeMessageIterator.initialize(notificationUserIterator, emailFactoryParameters, fromDate, templateReference);
Session session = this.sessionFactory.create(Collections.emptyMap());
MailListener mailListener = mailListenerProvider.get();
// Pass it to the message sender to send it asynchronously.
mailSender.sendAsynchronously(periodicMimeMessageIterator, session, mailListener);
}
Aggregations