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)));
}
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;
}
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());
}
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));
}
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();
}
Aggregations