Search in sources :

Example 1 with MimeMessageFactory

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;
}
Also used : MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) AbstractIteratorMimeMessageFactory(org.xwiki.mail.internal.factory.AbstractIteratorMimeMessageFactory) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with MimeMessageFactory

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());
}
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 3 with MimeMessageFactory

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());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) ComponentManager(org.xwiki.component.manager.ComponentManager) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Example 4 with MimeMessageFactory

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;
}
Also used : MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) AbstractIteratorMimeMessageFactory(org.xwiki.mail.internal.factory.AbstractIteratorMimeMessageFactory) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with MimeMessageFactory

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;
}
Also used : MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) AbstractIteratorMimeMessageFactory(org.xwiki.mail.internal.factory.AbstractIteratorMimeMessageFactory)

Aggregations

MimeMessageFactory (org.xwiki.mail.MimeMessageFactory)6 AbstractIteratorMimeMessageFactory (org.xwiki.mail.internal.factory.AbstractIteratorMimeMessageFactory)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Session (javax.mail.Session)2 MimeMessage (javax.mail.internet.MimeMessage)2 Test (org.junit.Test)2 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2 ExtendedMimeMessage (org.xwiki.mail.ExtendedMimeMessage)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 UserIterator (com.xpn.xwiki.internal.plugin.rightsmanager.UserIterator)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 Execution (org.xwiki.context.Execution)1 ExecutionContext (org.xwiki.context.ExecutionContext)1 DocumentReferenceResolver (org.xwiki.model.reference.DocumentReferenceResolver)1