Search in sources :

Example 6 with MimeBodyPartFactory

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

the class ScriptMimeMessage method addPart.

/**
 * Add some content to the mail to be sent. Can be called several times to add different content type to the mail.
 *
 * @return the Mime Body Part object that was added. Returning it allows script to make modifications to that body
 *         part after it's been set (get/set some headers, etc)
 * @param mimeType the mime type of the content parameter
 * @param content the content to include in the mail. The type depends on the mimetype. For example for a mime
 *        type of {@code text/plain} or {@code text/html}, the content should be a String, for a mime type of
 *        {@code xwiki/template} it should be a {@link org.xwiki.model.reference.DocumentReference}, etc.
 *        Also accepts a {@link BodyPart} object, in which case, the mime type and parameters are not used.
 * @param parameters the list of extra parameters. This is used for example to pass alternate content for the mail
 *        using the {@code alternate} key in the HTML Mime Body Part Factory. Mail headers can also be passed using
 *        the {@code headers} key with a {@code Map<String, String>} value containing header keys
 *        and values.
 */
public BodyPart addPart(String mimeType, Object content, Map<String, Object> parameters) {
    BodyPart bodyPart;
    try {
        if (!(content instanceof BodyPart)) {
            MimeBodyPartFactory factory = getBodyPartFactory(mimeType, content.getClass());
            // Pass the mime type in the parameters so that generic Mime Body Part factories can use it.
            // Note that if the user has already passed a "mimetype" parameter then we don't override it!
            Map<String, Object> enhancedParameters = new HashMap<>();
            enhancedParameters.put("mimetype", mimeType);
            enhancedParameters.putAll(parameters);
            bodyPart = factory.create(content, enhancedParameters);
        } else {
            bodyPart = (BodyPart) content;
        }
        Multipart multipart = getMultipart();
        multipart.addBodyPart(bodyPart);
    } catch (Exception e) {
        // Save the exception for reporting through the script services's getError() API
        setError(e);
        bodyPart = null;
    }
    return bodyPart;
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) HashMap(java.util.HashMap) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 7 with MimeBodyPartFactory

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

the class HTMLMimeBodyPartFactoryTest method createWhenHTMLAndAlternateTextAndEmbeddedImagesAndNormalAttachments.

@Test
public void createWhenHTMLAndAlternateTextAndEmbeddedImagesAndNormalAttachments() throws Exception {
    Attachment normalAttachment = mock(Attachment.class, "normalAttachment");
    when(normalAttachment.getFilename()).thenReturn("attachment1.png");
    Attachment embeddedAttachment = mock(Attachment.class, "embeddedAttachment");
    when(embeddedAttachment.getFilename()).thenReturn("embeddedAttachment.png");
    MimeBodyPart embeddedAttachmentBodyPart = mock(MimeBodyPart.class);
    MimeBodyPartFactory attachmentBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, Attachment.class), "xwiki/attachment");
    when(attachmentBodyPartFactory.create(same(embeddedAttachment), any(Map.class))).thenReturn(embeddedAttachmentBodyPart);
    MimeBodyPart normalAttachmentBodyPart = mock(MimeBodyPart.class);
    when(attachmentBodyPartFactory.create(same(normalAttachment), any(Map.class))).thenReturn(normalAttachmentBodyPart);
    MimeBodyPart textBodyPart = mock(MimeBodyPart.class);
    MimeBodyPartFactory defaultBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class));
    when(defaultBodyPartFactory.create(eq("some text"), any(Map.class))).thenReturn(textBodyPart);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("attachments", Arrays.asList(normalAttachment, embeddedAttachment));
    parameters.put("alternate", "some text");
    MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>html... <img src='cid:embeddedAttachment.png'/></p>", parameters);
    MimeMultipart multipart = (MimeMultipart) bodyPart.getContent();
    assertEquals(2, multipart.getCount());
    MimeMultipart alternateMultipart = (MimeMultipart) multipart.getBodyPart(0).getContent();
    assertEquals(2, alternateMultipart.getCount());
    assertSame(textBodyPart, alternateMultipart.getBodyPart(0));
    MimeMultipart relatedMultipart = (MimeMultipart) alternateMultipart.getBodyPart(1).getContent();
    assertEquals(2, relatedMultipart.getCount());
    assertEquals("<p>html... <img src='cid:embeddedAttachment.png'/></p>", relatedMultipart.getBodyPart(0).getContent());
    assertSame(embeddedAttachmentBodyPart, relatedMultipart.getBodyPart(1));
    assertSame(normalAttachmentBodyPart, multipart.getBodyPart(1));
}
Also used : HashMap(java.util.HashMap) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with MimeBodyPartFactory

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

the class TemplateMimeBodyPartFactoryTest method createWithAttachment.

@Test
public void createWithAttachment() throws Exception {
    MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html");
    Attachment attachment = mock(Attachment.class);
    List<Attachment> attachments = Collections.singletonList(attachment);
    Map<String, Object> bodyPartParameters = new HashMap<>();
    bodyPartParameters.put("velocityVariables", new HashMap<String, String>());
    bodyPartParameters.put("attachments", attachments);
    this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters);
    Map<String, Object> htmlParameters = new HashMap<>();
    htmlParameters.put("alternate", "Hello John Doe, john@doe.com");
    htmlParameters.put("attachments", attachments);
    verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", htmlParameters);
}
Also used : HashMap(java.util.HashMap) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 9 with MimeBodyPartFactory

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

the class TemplateMimeBodyPartFactoryTest method createWithAttachmentAndTemplateAttachmentsWhenError.

@Test
public void createWithAttachmentAndTemplateAttachmentsWhenError() throws Exception {
    MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html");
    Attachment attachment1 = mock(Attachment.class, "attachment1");
    Map<String, Object> bodyPartParameters = new HashMap<>();
    bodyPartParameters.put("velocityVariables", new HashMap<String, String>());
    bodyPartParameters.put("attachments", Collections.singletonList(attachment1));
    bodyPartParameters.put("includeTemplateAttachments", true);
    // Mock the retrieval and conversion of attachments from the Template document
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    when(dab.getDocumentInstance(this.documentReference)).thenThrow(new Exception("error"));
    try {
        this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters);
        fail("Should have thrown an exception here");
    } catch (MessagingException expected) {
        assertEquals("Failed to include attachments from the Mail Template [wiki:space.page]", expected.getMessage());
    }
    verifyNoMoreInteractions(htmlMimeBodyPartFactory);
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MessagingException(javax.mail.MessagingException) Test(org.junit.Test)

Example 10 with MimeBodyPartFactory

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

the class TemplateMimeMessageFactoryTest method setUp.

@Before
public void setUp() throws Exception {
    this.templateReference = new DocumentReference("templatewiki", "templatespace", "templatepage");
    MailTemplateManager mailTemplateManager = this.mocker.getInstance(MailTemplateManager.class);
    when(mailTemplateManager.evaluate(same(this.templateReference), eq("subject"), any(), any())).thenReturn("XWiki news");
    MimeBodyPartFactory<DocumentReference> templateBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, DocumentReference.class), "xwiki/template");
    this.mimeBodyPart = mock(MimeBodyPart.class);
    when(templateBodyPartFactory.create(same(this.templateReference), any())).thenReturn(this.mimeBodyPart);
}
Also used : MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MimeBodyPart(javax.mail.internet.MimeBodyPart) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Aggregations

MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)10 HashMap (java.util.HashMap)9 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)9 Test (org.junit.Test)8 Attachment (com.xpn.xwiki.api.Attachment)6 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 Map (java.util.Map)4 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)3 MessagingException (javax.mail.MessagingException)2 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 IOException (java.io.IOException)1 BodyPart (javax.mail.BodyPart)1 Multipart (javax.mail.Multipart)1 Before (org.junit.Before)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1