Search in sources :

Example 11 with MailListener

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);
}
Also used : MailListener(org.xwiki.mail.MailListener) NotificationUserIterator(org.xwiki.notifications.notifiers.internal.email.NotificationUserIterator) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session)

Example 12 with 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);
    }
}
Also used : MailListener(org.xwiki.mail.MailListener) HashMap(java.util.HashMap) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) XWikiException(com.xpn.xwiki.XWikiException) WatchListException(org.xwiki.watchlist.internal.api.WatchListException) MimeMessage(javax.mail.internet.MimeMessage) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session)

Example 13 with MailListener

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());
}
Also used : MailListener(org.xwiki.mail.MailListener) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Session(javax.mail.Session) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 14 with MailListener

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));
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 15 with MailListener

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());
}
Also used : MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Aggregations

MailListener (org.xwiki.mail.MailListener)19 Test (org.junit.Test)11 MailStatusStore (org.xwiki.mail.MailStatusStore)8 Session (javax.mail.Session)7 MimeMessage (javax.mail.internet.MimeMessage)6 MailStatus (org.xwiki.mail.MailStatus)6 MailStoreException (org.xwiki.mail.MailStoreException)6 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)4 MailContentStore (org.xwiki.mail.MailContentStore)4 MessagingException (javax.mail.MessagingException)3 InternetAddress (javax.mail.internet.InternetAddress)3 ExecutionContextException (org.xwiki.context.ExecutionContextException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 HashMap (java.util.HashMap)2 MailSender (org.xwiki.mail.MailSender)2 MemoryMailListener (org.xwiki.mail.internal.MemoryMailListener)2 XWikiException (com.xpn.xwiki.XWikiException)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1