use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class LiveNotificationEmailSender method sendEmails.
/**
* Send live notification e-mails regarding the given event for the users that are concerned by this event and that
* have enabled live notifications.
* @param event the event that should be sent to the users
*/
public void sendEmails(CompositeEvent event) {
DocumentReference templateReference = new DocumentReference(this.wikiDescriptorManager.getCurrentWikiId(), Arrays.asList("XWiki", "Notifications"), "MailTemplate");
// Get a list of users that have enabled the live e-mail notifications.
NotificationUserIterator notificationUserIterator = this.notificationUserIteratorProvider.get();
notificationUserIterator.initialize(NotificationEmailInterval.LIVE);
LiveMimeMessageIterator liveNotificationMessageIterator = this.liveMimeMessageIteratorProvider.get();
liveNotificationMessageIterator.initialize(notificationUserIterator, new HashMap<>(), event, templateReference);
Session session = this.sessionFactory.create(Collections.emptyMap());
MailListener mailListener = mailListenerProvider.get();
// Pass it to the message sender to send it asynchronously.
mailSender.sendAsynchronously(liveNotificationMessageIterator, session, mailListener);
}
use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class DefaultWatchListNotifier method sendNotification.
@Override
public void sendNotification(Collection<String> subscribers, List<WatchListEvent> events, Map<String, Object> notificationData) throws WatchListException {
try {
// FIXME: Temporary, until we move to all references.
List<DocumentReference> subscriberReferences = getSubscriberReferences(subscribers);
// Source
Map<String, Object> source = new HashMap<>();
source.put(EventsAndSubscribersSource.SUBSCRIBERS_PARAMETER, subscriberReferences);
source.put(EventsAndSubscribersSource.EVENTS_PARAMETER, events);
// Parameters
Map<String, Object> parameters = new HashMap<>();
parameters.put(WatchListEventMimeMessageFactory.HINT_PARAMETER, "template");
parameters.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, notificationData.get(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER));
parameters.put(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER, notificationData.get(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER));
parameters.put(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER, notificationData.get(WatchListEventMimeMessageFactory.ATTACH_AUTHOR_AVATARS_PARAMETER));
Map<String, Object> templateFactoryParameters = getTemplateFactoryParameters(notificationData);
parameters.put(WatchListEventMimeMessageFactory.PARAMETERS_PARAMETER, templateFactoryParameters);
// Create the message iterator and the other mail sender parameters.
Iterator<MimeMessage> messageIterator = messageFactory.createMessage(source, parameters);
Session session = this.sessionFactory.create(Collections.<String, String>emptyMap());
MailListener mailListener = mailListenerProvider.get();
// Pass it to the message sender to send it asynchronously.
// FIXME: !? There must be a better way instead of using IteratorIterable.
mailSender.sendAsynchronously(new IteratorIterable<MimeMessage>(messageIterator), session, mailListener);
} catch (Exception e) {
throw new WatchListException(String.format("Failed to send notification to subscribers [%s]", subscribers), e);
}
}
use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class JavaIntegrationTest method sendMailWithCustomMessageId.
@Test
public void sendMailWithCustomMessageId() throws Exception {
Session session = Session.getInstance(this.configuration.getAllProperties());
MimeMessage message = new MimeMessage(session) {
@Override
protected void updateMessageID() throws MessagingException {
if (getMessageID() == null) {
super.updateMessageID();
}
}
};
message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
message.setText("Test message Id support");
message.setHeader("Message-ID", "<custom@domain>");
message.setSubject("subject");
MailListener memoryMailListener = this.componentManager.getInstance(MailListener.class, "memory");
this.sender.sendAsynchronously(Arrays.asList(message), session, memoryMailListener);
// Verify that the mails have been received (wait maximum 30 seconds).
this.mail.waitForIncomingEmail(30000L, 1);
MimeMessage[] messages = this.mail.getReceivedMessages();
assertEquals("<custom@domain>", messages[0].getMessageID());
}
use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class DatabaseMailListenerTest method onPrepareWhenSaveFails.
@Test
public void onPrepareWhenSaveFails() throws Exception {
MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
doThrow(new MailStoreException("error")).when(mailStatusStore).save(any(MailStatus.class), anyMap());
MailListener listener = this.mocker.getComponentUnderTest();
listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
listener.onPrepareMessageSuccess(this.message, Collections.<String, Object>emptyMap());
ArgumentCaptor<MailStatus> statusCapture = ArgumentCaptor.forClass(MailStatus.class);
verify(mailStatusStore).save(statusCapture.capture(), anyMap());
assertEquals("Failed to save mail status [messageId = [" + this.messageId + "], batchId = [" + this.batchId + "], state = [prepare_success], date = [" + statusCapture.getValue().getDate() + "], " + "recipients = [<null>], type = [type], wiki = [mywiki]] to the database", this.logRule.getMessage(0));
}
use of org.xwiki.mail.MailListener in project xwiki-platform by xwiki.
the class DatabaseMailListenerTest method onSuccessWhenStatusLoadFails.
@Test
public void onSuccessWhenStatusLoadFails() throws Exception {
MailStatusStore mailStatusStore = this.mocker.getInstance(MailStatusStore.class, "database");
when(mailStatusStore.load(this.messageId)).thenThrow(new MailStoreException("error"));
MailListener listener = this.mocker.getComponentUnderTest();
listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
listener.onSendMessageSuccess(this.message, Collections.<String, Object>emptyMap());
assertEquals("Error when looking for a previous mail status for message [" + this.messageId + "] of batch [" + batchId + "] and state [send_success].", this.logRule.getMessage(0));
assertEquals("Forcing a new mail status for message [" + this.messageId + "] of batch [" + batchId + "] to send_success state.", this.logRule.getMessage(1));
// Verify that save and delete happened
verify(mailStatusStore).save(any(MailStatus.class), anyMap());
MailContentStore mailContentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
verify(mailContentStore).delete(any(), any());
}
Aggregations