use of org.xwiki.mail.MimeMessageFactory in project xwiki-platform by xwiki.
the class GroupMimeMessageFactory method createMessage.
@Override
public Iterator<MimeMessage> createMessage(Object groupReferenceObject, Map<String, Object> parameters) throws MessagingException {
DocumentReference groupReference = getTypedSource(groupReferenceObject, DocumentReference.class);
// We verify that we have both a Factory hint specified but also the source for the Factory.
validateParameters(parameters, HINT, SOURCE);
// Extract from the passed parameters the MimeMessageFactory to use to create a single mail
String factoryHint = (String) parameters.get(HINT);
MimeMessageFactory factory = getInternalMimeMessageFactory(factoryHint);
GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, this.componentManagerProvider.get());
return iterator;
}
use of org.xwiki.mail.MimeMessageFactory 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.MimeMessageFactory in project xwiki-platform by xwiki.
the class UsersMimeMessageIteratorTest method createMessage.
@Test
public void createMessage() throws Exception {
DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
List<DocumentReference> userReferences = Arrays.asList(userReference1, userReference2, userReference3);
Session session = Session.getInstance(new Properties());
MimeMessageFactory factory = new MimeMessageFactory() {
@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(userReference1), any(DocumentReference.class), eq("email"))).thenReturn("john@doe.com");
when(accessBridge.getProperty(eq(userReference2), any(DocumentReference.class), eq("email"))).thenReturn("jane@doe.com");
when(accessBridge.getProperty(eq(userReference3), any(DocumentReference.class), eq("email"))).thenReturn("jannie@doe.com");
ComponentManager componentManager = mock(ComponentManager.class);
when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
UsersMimeMessageIterator iterator = new UsersMimeMessageIterator(userReferences, 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.MimeMessageFactory in project xwiki-platform by xwiki.
the class UsersMimeMessageFactory method createMessage.
@Override
public Iterator<MimeMessage> createMessage(Object userReferencesObject, Map<String, Object> parameters) throws MessagingException {
List<DocumentReference> userReferences = getTypedSource(userReferencesObject, List.class);
// We verify that we have both a Factory hint specified but also the source for the Factory.
validateParameters(parameters, HINT, SOURCE);
// Extract from the passed parameters the MimeMessageFactory to use to create a single mail
String factoryHint = (String) parameters.get(HINT);
MimeMessageFactory factory = getInternalMimeMessageFactory(factoryHint);
UsersMimeMessageIterator iterator = new UsersMimeMessageIterator(userReferences, factory, parameters, this.componentManagerProvider.get());
return iterator;
}
use of org.xwiki.mail.MimeMessageFactory in project xwiki-platform by xwiki.
the class UsersAndGroupsMimeMessageFactory method createMessage.
@Override
public Iterator<MimeMessage> createMessage(Object sourceObject, Map<String, Object> parameters) throws MessagingException {
Map<String, Object> source = getTypedSource(sourceObject, Map.class);
// We verify that we have both a Factory hint specified but also the source for the Factory.
validateParameters(parameters, HINT, SOURCE);
// Extract from the passed parameters the MimeMessageFactory to use to create a single mail
String factoryHint = (String) parameters.get(HINT);
MimeMessageFactory factory = getInternalMimeMessageFactory(factoryHint);
UsersAndGroupsMimeMessageIterator iterator = new UsersAndGroupsMimeMessageIterator(source, factory, parameters, this.explicitDocumentReferenceResolver, this.execution);
return iterator;
}
Aggregations