Search in sources :

Example 1 with UpdateableMailStatusResult

use of org.xwiki.mail.internal.UpdateableMailStatusResult in project xwiki-platform by xwiki.

the class PrepareMailRunnable method prepareMail.

/**
 * Prepare the messages to send, persist them and put them on the Mail Sender Queue.
 *
 * @param item the queue item containing all the data for sending the mail
 * @throws org.xwiki.context.ExecutionContextException when the XWiki Context fails to be set up
 */
protected void prepareMail(PrepareMailQueueItem item) throws ExecutionContextException {
    Iterator<? extends MimeMessage> messageIterator = item.getMessages().iterator();
    MailListener listener = item.getListener();
    if (listener != null) {
        listener.onPrepareBegin(item.getBatchId(), Collections.<String, Object>emptyMap());
    }
    // Count the total number of messages to process
    long messageCounter = 0;
    try {
        boolean shouldStop = false;
        while (!shouldStop) {
            // Note that we need to have the hasNext() call after the context is ready since the implementation can
            // need a valid XWiki Context.
            prepareContext(item.getContext());
            try {
                if (messageIterator.hasNext()) {
                    MimeMessage mimeMessage = messageIterator.next();
                    prepareSingleMail(mimeMessage, item);
                    messageCounter++;
                } else {
                    shouldStop = true;
                }
            } finally {
                removeContext();
            }
        }
    } catch (Exception e) {
        if (listener != null) {
            listener.onPrepareFatalError(e, Collections.<String, Object>emptyMap());
        }
    } finally {
        if (listener != null) {
            MailStatusResult result = listener.getMailStatusResult();
            // so that waiting process have a chance to see an end.
            if (result instanceof UpdateableMailStatusResult) {
                ((UpdateableMailStatusResult) result).setTotalSize(messageCounter);
            }
            listener.onPrepareEnd(Collections.<String, Object>emptyMap());
        }
    }
}
Also used : MailListener(org.xwiki.mail.MailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MessagingException(javax.mail.MessagingException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 2 with UpdateableMailStatusResult

use of org.xwiki.mail.internal.UpdateableMailStatusResult in project xwiki-platform by xwiki.

the class SendMailRunnableTest method sendMailWhenSendingFails.

@Test
public void sendMailWhenSendingFails() throws Exception {
    // Create a Session with an invalid host so that it generates an error
    Properties properties = new Properties();
    properties.setProperty("mail.smtp.host", "xwiki-unknown");
    Session session = Session.getDefaultInstance(properties);
    MimeMessage msg1 = new MimeMessage(session);
    msg1.setText("Content1");
    ExtendedMimeMessage message1 = new ExtendedMimeMessage(msg1);
    String id1 = message1.getUniqueMessageId();
    MimeMessage msg2 = new MimeMessage(session);
    msg2.setText("Content2");
    ExtendedMimeMessage message2 = new ExtendedMimeMessage(msg2);
    String id2 = message2.getUniqueMessageId();
    MemoryMailListener listener = this.mocker.getInstance(MailListener.class, "memory");
    String batchId = UUID.randomUUID().toString();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    ((UpdateableMailStatusResult) listener.getMailStatusResult()).setTotalSize(2);
    SendMailQueueItem item1 = new SendMailQueueItem(id1, session, listener, batchId, "xwiki");
    SendMailQueueItem item2 = new SendMailQueueItem(id2, session, listener, batchId, "xwiki");
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    // Simulate loading the message from the content store
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(session, batchId, id1)).thenReturn(message1);
    when(contentStore.load(session, batchId, id2)).thenReturn(message2);
    // Send 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    // This is the real test: we verify that there's been an error while sending each email.
    Iterator<MailStatus> statuses = listener.getMailStatusResult().getByState(MailState.SEND_ERROR);
    int errorCount = 0;
    while (statuses.hasNext()) {
        MailStatus status = statuses.next();
        // Note: I would have liked to assert the exact message but it seems there can be different ones returned.
        // During my tests I got 2 different ones:
        // "UnknownHostException: xwiki-unknown"
        // "ConnectException: Connection refused"
        // Thus for now I only assert that there's an error set, but not its content.
        assertTrue(status.getErrorSummary() != null);
        errorCount++;
    }
    assertEquals(2, errorCount);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailContentStore(org.xwiki.mail.MailContentStore) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 3 with UpdateableMailStatusResult

use of org.xwiki.mail.internal.UpdateableMailStatusResult in project xwiki-platform by xwiki.

the class SendMailRunnableTest method sendMailWhenMailRetrievalFails.

@Test
public void sendMailWhenMailRetrievalFails() throws Exception {
    // Create a Session with an invalid host so that it generates an error
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage msg1 = new MimeMessage(session);
    msg1.setText("Content1");
    ExtendedMimeMessage message1 = new ExtendedMimeMessage(msg1);
    String id1 = message1.getUniqueMessageId();
    MimeMessage msg2 = new MimeMessage(session);
    msg2.setText("Content2");
    ExtendedMimeMessage message2 = new ExtendedMimeMessage(msg2);
    String id2 = message2.getUniqueMessageId();
    MemoryMailListener listener = this.mocker.getInstance(MailListener.class, "memory");
    String batchId = UUID.randomUUID().toString();
    listener.onPrepareBegin(batchId, Collections.<String, Object>emptyMap());
    ((UpdateableMailStatusResult) listener.getMailStatusResult()).setTotalSize(2);
    listener.onPrepareMessageSuccess(message1, Collections.<String, Object>emptyMap());
    SendMailQueueItem item1 = new SendMailQueueItem(id1, session, listener, batchId, "xwiki");
    listener.onPrepareMessageSuccess(message2, Collections.<String, Object>emptyMap());
    SendMailQueueItem item2 = new SendMailQueueItem(id2, session, listener, batchId, "xwiki");
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    // Simulate loading the message from the content store
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(session, batchId, id1)).thenThrow(new MailStoreException("Store failure on message 1"));
    when(contentStore.load(session, batchId, id2)).thenThrow(new MailStoreException("Store failure on message 2"));
    // Send 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    // This is the real test: we verify that there's been an error while sending each email.
    Iterator<MailStatus> statuses = listener.getMailStatusResult().getByState(MailState.SEND_FATAL_ERROR);
    int errorCount = 0;
    while (statuses.hasNext()) {
        MailStatus status = statuses.next();
        // Note: I would have liked to assert the exact message but it seems there can be different ones returned.
        // During my tests I got 2 different ones:
        // "UnknownHostException: xwiki-unknown"
        // "ConnectException: Connection refused"
        // Thus for now I only assert that there's an error set, but not its content.
        assertEquals("MailStoreException: Store failure on message " + ++errorCount, status.getErrorSummary());
    }
    assertEquals(2, errorCount);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) Test(org.junit.Test)

Example 4 with UpdateableMailStatusResult

use of org.xwiki.mail.internal.UpdateableMailStatusResult in project xwiki-platform by xwiki.

the class PrepareMailRunnableTest method prepareMailWhenIteratorFails.

@Test
public void prepareMailWhenIteratorFails() throws Exception {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    final MimeMessage message1 = new MimeMessage(session);
    message1.setText("Content1");
    MimeMessage message2 = new MimeMessage(session);
    message2.setText("Content2");
    String batchId1 = UUID.randomUUID().toString();
    String batchId2 = UUID.randomUUID().toString();
    ExecutionContext context1 = new ExecutionContext();
    XWikiContext xContext1 = new XWikiContext();
    xContext1.setWikiId("wiki1");
    context1.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext1);
    ExecutionContext context2 = new ExecutionContext();
    XWikiContext xContext2 = new XWikiContext();
    xContext2.setWikiId("wiki2");
    context2.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext2);
    MemoryMailListener listener1 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item1 = new PrepareMailQueueItem(new Iterable<MimeMessage>() {

        @Override
        public Iterator<MimeMessage> iterator() {
            return new Iterator<MimeMessage>() {

                int index = 0;

                @Override
                public boolean hasNext() {
                    return true;
                }

                @Override
                public MimeMessage next() {
                    if (index++ == 0) {
                        return message1;
                    }
                    throw new RuntimeException("Iterator failure");
                }

                @Override
                public void remove() {
                }
            };
        }
    }, session, listener1, batchId1, context1);
    MemoryMailListener listener2 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item2 = new PrepareMailQueueItem(Arrays.asList(message2), session, listener2, batchId2, context2);
    MailQueueManager prepareMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, PrepareMailQueueItem.class));
    MailQueueManager sendMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            MimeMessage message = (MimeMessage) args[1];
            message.saveChanges();
            return null;
        }
    }).when(contentStore).save(any(String.class), any(ExtendedMimeMessage.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            SendMailQueueItem item = (SendMailQueueItem) args[0];
            ((UpdateableMailStatusResult) item.getListener().getMailStatusResult()).incrementCurrentSize();
            return null;
        }
    }).when(sendMailQueueManager).addToQueue(any(SendMailQueueItem.class));
    // Prepare 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    prepareMailQueueManager.addToQueue(item1);
    prepareMailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener1.getMailStatusResult().waitTillProcessed(10000L);
        listener2.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    MailStatusResult result1 = listener1.getMailStatusResult();
    MailStatusResult result2 = listener2.getMailStatusResult();
    // Despite the errors, both process should be ended with known total number of mails
    assertTrue(result1.isProcessed());
    assertTrue(result2.isProcessed());
    // This is the real test: we verify that there's been an error while sending each email.
    assertNotNull(listener1.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertNotNull(listener2.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertFalse(listener1.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertFalse(listener2.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertEquals("Failure during preparation phase of thread [" + batchId1 + "]", logRule.getMessage(0));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Iterator(java.util.Iterator) MailContentStore(org.xwiki.mail.MailContentStore) XWikiContext(com.xpn.xwiki.XWikiContext) ExecutionContext(org.xwiki.context.ExecutionContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) Session(javax.mail.Session) Test(org.junit.Test)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)4 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)4 UpdateableMailStatusResult (org.xwiki.mail.internal.UpdateableMailStatusResult)4 Properties (java.util.Properties)3 Session (javax.mail.Session)3 Test (org.junit.Test)3 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)3 MailContentStore (org.xwiki.mail.MailContentStore)3 MemoryMailListener (org.xwiki.mail.internal.MemoryMailListener)3 MailStatus (org.xwiki.mail.MailStatus)2 MailStatusResult (org.xwiki.mail.MailStatusResult)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 Iterator (java.util.Iterator)1 MessagingException (javax.mail.MessagingException)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ExecutionContext (org.xwiki.context.ExecutionContext)1 ExecutionContextException (org.xwiki.context.ExecutionContextException)1 MailListener (org.xwiki.mail.MailListener)1 MailStoreException (org.xwiki.mail.MailStoreException)1