Search in sources :

Example 6 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class PrepareMailRunnable method prepareSingleMail.

private void prepareSingleMail(MimeMessage mimeMessage, PrepareMailQueueItem item) {
    MailListener listener = item.getListener();
    // Step 1: Try to complete message with From and Bcc from configuration if needed
    completeMessage(mimeMessage);
    // Ensure mimeMessage to be extended
    ExtendedMimeMessage message = ExtendedMimeMessage.wrap(mimeMessage);
    // Note: Message identifier is stabilized at this step by the serialization process
    try {
        this.mailContentStore.save(item.getBatchId(), message);
    } catch (Exception e) {
        // An error occurred, notify the user if a listener has been provided
        if (listener != null) {
            listener.onPrepareMessageError(message, e, Collections.<String, Object>emptyMap());
        }
        return;
    }
    // Step 3: Notify the user that the MimeMessage is prepared
    if (listener != null) {
        listener.onPrepareMessageSuccess(message, Collections.<String, Object>emptyMap());
    }
    // Step 4: Put the MimeMessage id on the Mail Send Queue for sending
    // Extract the wiki id from the context
    this.sendMailQueueManager.addToQueue(new SendMailQueueItem(message.getUniqueMessageId(), item.getSession(), listener, item.getBatchId(), extractWikiId(item)));
}
Also used : MailListener(org.xwiki.mail.MailListener) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MessagingException(javax.mail.MessagingException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 7 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class UsersMimeMessageIterator method createMessageInternal.

@Override
protected ExtendedMimeMessage createMessageInternal() throws MessagingException {
    ExtendedMimeMessage mimeMessage;
    DocumentReference userReference = users.get(this.position);
    // If the user has no email address then return a null Mime Message so that it's skipped
    Object emailObject = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), "XWiki", "XWikiUsers"), "email");
    if (emailObject != null) {
        String email = emailObject.toString();
        Map<String, Object> parameters = (Map<String, Object>) this.parameters.get("parameters");
        mimeMessage = ExtendedMimeMessage.wrap(this.factory.createMessage(this.parameters.get("source"), parameters));
        mimeMessage.addRecipient(Message.RecipientType.TO, InternetAddress.parse(email)[0]);
    } else {
        getLogger().warn("User [{}] has no email defined. Email has not been sent to that user.", userReference);
        mimeMessage = null;
    }
    return mimeMessage;
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 8 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class GroupMimeMessageIteratorTest method createMessage.

@Test
public void createMessage() throws Exception {
    DocumentReference groupReference = new DocumentReference("xwiki", "XWiki", "Marketing");
    DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
    DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
    DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
    Session session = Session.getInstance(new Properties());
    MimeMessageFactory<MimeMessage> factory = new MimeMessageFactory<MimeMessage>() {

        @Override
        public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
            return new ExtendedMimeMessage();
        }
    };
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("parameters", Collections.EMPTY_MAP);
    parameters.put("session", session);
    DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
    when(accessBridge.getProperty(eq(groupReference), any(), eq(0), eq("member"))).thenReturn("XWiki.JohnDoe");
    when(accessBridge.getProperty(eq(groupReference), any(), eq(1), eq("member"))).thenReturn("XWiki.JaneDoe");
    when(accessBridge.getProperty(eq(groupReference), any(), eq(2), eq("member"))).thenReturn("XWiki.JonnieDoe");
    when(accessBridge.getProperty(eq(userReference1), any(), eq("email"))).thenReturn("john@doe.com");
    when(accessBridge.getProperty(eq(userReference2), any(), eq("email"))).thenReturn("jane@doe.com");
    when(accessBridge.getProperty(eq(userReference3), any(), eq("email"))).thenReturn("jannie@doe.com");
    Execution execution = mock(Execution.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
    XWiki xwiki = mock(XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    XWikiDocument document = mock(XWikiDocument.class);
    when(xwiki.getDocument(eq(groupReference), eq(xwikiContext))).thenReturn(document);
    BaseObject object = mock(BaseObject.class);
    when(document.getXObjects(any(EntityReference.class))).thenReturn(Arrays.asList(object, object, object));
    DocumentReferenceResolver<String> resolver = (DocumentReferenceResolver<String>) mock(DocumentReferenceResolver.class);
    when(resolver.resolve("XWiki.JohnDoe")).thenReturn(userReference1);
    when(resolver.resolve("XWiki.JaneDoe")).thenReturn(userReference2);
    when(resolver.resolve("XWiki.JonnieDoe")).thenReturn(userReference3);
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
    when(componentManager.getInstance(eq(Execution.class))).thenReturn(execution);
    when(componentManager.getInstance(eq(DocumentReferenceResolver.TYPE_STRING), eq("current"))).thenReturn(resolver);
    GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, componentManager);
    assertTrue(iterator.hasNext());
    MimeMessage message1 = iterator.next();
    assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message2 = iterator.next();
    assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message3 = iterator.next();
    assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
    assertFalse(iterator.hasNext());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Properties(java.util.Properties) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager) BaseObject(com.xpn.xwiki.objects.BaseObject) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Example 9 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class DatabaseMailResenderTest method resendAsynchronouslySeveralMessagesWhenMailContentStoreLoadingFailsForFirstMessage.

@Test
public void resendAsynchronouslySeveralMessagesWhenMailContentStoreLoadingFailsForFirstMessage() throws Exception {
    Map filterMap = Collections.singletonMap("state", "prepare_%");
    MailStatus status1 = new MailStatus();
    status1.setBatchId("batch1");
    status1.setMessageId("message1");
    MailStatus status2 = new MailStatus();
    status2.setBatchId("batch2");
    status2.setMessageId("message2");
    List<MailStatus> statuses = new ArrayList<>();
    statuses.add(status1);
    statuses.add(status2);
    MailStatusStore statusStore = this.mocker.getInstance(MailStatusStore.class, "database");
    when(statusStore.load(filterMap, 0, 0, null, true)).thenReturn(statuses);
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(any(), eq("batch1"), eq("message1"))).thenThrow(new MailStoreException("error1"));
    ExtendedMimeMessage message2 = new ExtendedMimeMessage();
    when(contentStore.load(any(), eq("batch2"), eq("message2"))).thenReturn(message2);
    MailSender sender = this.mocker.getInstance(MailSender.class);
    this.mocker.getComponentUnderTest().resendAsynchronously(filterMap, 0, 0);
    // The test is here
    verify(this.mocker.getMockedLogger()).warn("Failed to load mail content for batchId [{}], messageId [{}]", "batch1", "message1");
    verify(sender).sendAsynchronously(eq(Arrays.asList(message2)), any(), any(MailListener.class));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailListener(org.xwiki.mail.MailListener) MailStatusStore(org.xwiki.mail.MailStatusStore) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) ArrayList(java.util.ArrayList) MailSender(org.xwiki.mail.MailSender) Map(java.util.Map) MailStatus(org.xwiki.mail.MailStatus) Test(org.junit.Test)

Example 10 with ExtendedMimeMessage

use of org.xwiki.mail.ExtendedMimeMessage in project xwiki-platform by xwiki.

the class DatabaseMailResenderTest method resendAsynchronouslySingleMesssage.

@Test
public void resendAsynchronouslySingleMesssage() throws Exception {
    MailListener listener = this.mocker.registerMockComponent(MailListener.class, "database");
    ExtendedMimeMessage message = new ExtendedMimeMessage();
    String batchId = UUID.randomUUID().toString();
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    when(contentStore.load(any(), eq(batchId), eq("messageId"))).thenReturn(message);
    MailSender sender = this.mocker.getInstance(MailSender.class);
    when(sender.sendAsynchronously(eq(Arrays.asList(message)), any(), any(MailListener.class))).thenReturn(new DefaultMailResult(batchId));
    this.mocker.getComponentUnderTest().resendAsynchronously(batchId, "messageId");
    // The test is here
    verify(sender).sendAsynchronously(eq(Arrays.asList(message)), any(), same(listener));
    verify(listener).getMailStatusResult();
}
Also used : MailListener(org.xwiki.mail.MailListener) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailContentStore(org.xwiki.mail.MailContentStore) MailSender(org.xwiki.mail.MailSender) Test(org.junit.Test)

Aggregations

ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)21 Test (org.junit.Test)13 Map (java.util.Map)7 MailStatus (org.xwiki.mail.MailStatus)6 File (java.io.File)5 MailContentStore (org.xwiki.mail.MailContentStore)5 MailListener (org.xwiki.mail.MailListener)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 Session (javax.mail.Session)4 MimeMessage (javax.mail.internet.MimeMessage)4 ArrayList (java.util.ArrayList)3 MailSender (org.xwiki.mail.MailSender)3 MailStoreException (org.xwiki.mail.MailStoreException)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 HashMap (java.util.HashMap)2 MessagingException (javax.mail.MessagingException)2 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2