Search in sources :

Example 21 with Attachment

use of com.xpn.xwiki.api.Attachment 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 22 with Attachment

use of com.xpn.xwiki.api.Attachment 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 23 with Attachment

use of com.xpn.xwiki.api.Attachment 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 24 with Attachment

use of com.xpn.xwiki.api.Attachment in project xwiki-platform by xwiki.

the class HTMLMimeBodyPartFactory method separateAttachments.

/**
 * Separate embedded images from attachments list.
 *
 * @return the embedded attachments on the left and the normal attachments on the right of the Pair
 */
private Pair<List<Attachment>, List<Attachment>> separateAttachments(String content, List<Attachment> attachments) {
    if (attachments == null) {
        return new ImmutablePair<>(Collections.<Attachment>emptyList(), Collections.<Attachment>emptyList());
    }
    // Copy all attachments in the list of attachments to add to the email. We'll then remove the attachments
    // that are embedded from the list below.
    List<Attachment> normalAttachments = new ArrayList<>(attachments);
    // Find images used with src="cid:" in the email HTML part
    Matcher matcher = CID_PATTERN.matcher(content);
    List<String> embeddedImageNames = new ArrayList<>();
    while (matcher.find()) {
        embeddedImageNames.add(matcher.group(2));
    }
    // Loop over the attachments of the email, add images used from the HTML to the list of attachments to be
    // embedded with the HTML part, add the other attachments to the list of attachments to be attached to the
    // email.
    List<Attachment> embeddedImageAttachments = new ArrayList<>();
    for (Attachment attachment : attachments) {
        if (embeddedImageNames.contains(attachment.getFilename())) {
            embeddedImageAttachments.add(attachment);
            normalAttachments.remove(attachment);
        }
    }
    return new ImmutablePair<>(embeddedImageAttachments, normalAttachments);
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Attachment(com.xpn.xwiki.api.Attachment)

Example 25 with Attachment

use of com.xpn.xwiki.api.Attachment in project xwiki-platform by xwiki.

the class AbstractMimeMessageIterator method handleAvatars.

private void handleAvatars() {
    Set<DocumentReference> userAvatars = new HashSet<>();
    for (CompositeEvent event : currentEvents) {
        userAvatars.addAll(event.getUsers());
    }
    Collection<Attachment> attachments = getAttachments();
    for (DocumentReference userAvatar : userAvatars) {
        try {
            attachments.add(userAvatarAttachmentExtractor.getUserAvatar(userAvatar, 32));
        } catch (Exception e) {
            logger.warn("Failed to add the avatar of [{}] in the email.", userAvatar, e);
        }
    }
}
Also used : Attachment(com.xpn.xwiki.api.Attachment) CompositeEvent(org.xwiki.notifications.CompositeEvent) DocumentReference(org.xwiki.model.reference.DocumentReference) NotificationException(org.xwiki.notifications.NotificationException) AddressException(javax.mail.internet.AddressException) HashSet(java.util.HashSet)

Aggregations

Attachment (com.xpn.xwiki.api.Attachment)38 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)24 Test (org.junit.Test)21 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)15 Document (com.xpn.xwiki.api.Document)14 DocumentReference (org.xwiki.model.reference.DocumentReference)14 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)8 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)6 MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)6 XWikiContext (com.xpn.xwiki.XWikiContext)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 InputStream (java.io.InputStream)4 XWiki (com.xpn.xwiki.XWiki)3 XWikiAttachmentContent (com.xpn.xwiki.doc.XWikiAttachmentContent)3 File (java.io.File)3 IOException (java.io.IOException)3