Search in sources :

Example 1 with Attachment

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

the class AttachmentMimeBodyPartFactoryTest method createAttachmentBodyPart.

@Test
public void createAttachmentBodyPart() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    Attachment attachment = mock(Attachment.class);
    when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
    when(attachment.getFilename()).thenReturn("image.png");
    when(attachment.getMimeType()).thenReturn("image/png");
    MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, Collections.<String, Object>emptyMap());
    assertEquals("<image.png>", part.getContentID());
    // JavaMail adds some extra params to the content-type header
    // (e.g. image/png; name=image.png) , we just verify the content type that we passed.
    assertTrue(part.getContentType().startsWith("image/png"));
    // We verify that the Content-Disposition has the correct file namr
    assertTrue(part.getFileName().matches("image\\.png"));
    assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}
Also used : Environment(org.xwiki.environment.Environment) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Test(org.junit.Test)

Example 2 with Attachment

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

the class AttachmentMimeBodyPartFactoryTest method createAttachmentBodyPartWithHeader.

@Test
public void createAttachmentBodyPartWithHeader() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    Attachment attachment = mock(Attachment.class);
    when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
    when(attachment.getFilename()).thenReturn("image.png");
    when(attachment.getMimeType()).thenReturn("image/png");
    Map<String, Object> parameters = Collections.singletonMap("headers", (Object) Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable"));
    MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, parameters);
    assertEquals("<image.png>", part.getContentID());
    // JavaMail adds some extra params to the content-type header
    // (e.g. image/png; name=image.png) , we just verify the content type that we passed.
    assertTrue(part.getContentType().startsWith("image/png"));
    // We verify that the Content-Disposition has the correct file namr
    assertTrue(part.getFileName().matches("image\\.png"));
    assertArrayEquals(new String[] { "quoted-printable" }, part.getHeader("Content-Transfer-Encoding"));
    assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}
Also used : Environment(org.xwiki.environment.Environment) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Test(org.junit.Test)

Example 3 with Attachment

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

the class AttachmentMimeBodyPartFactoryTest method createAttachmentBodyPartWhenWriteError.

@Test
public void createAttachmentBodyPartWhenWriteError() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    Attachment attachment = mock(Attachment.class);
    when(attachment.getFilename()).thenReturn("image.png");
    when(attachment.getContent()).thenThrow(new RuntimeException("error"));
    try {
        this.mocker.getComponentUnderTest().create(attachment, Collections.<String, Object>emptyMap());
        fail("Should have thrown an exception here!");
    } catch (MessagingException expected) {
        assertEquals("Failed to save attachment [image.png] to the file system", expected.getMessage());
    }
}
Also used : MessagingException(javax.mail.MessagingException) Environment(org.xwiki.environment.Environment) Attachment(com.xpn.xwiki.api.Attachment) File(java.io.File) Test(org.junit.Test)

Example 4 with Attachment

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

the class HTMLMimeBodyPartFactory method create.

@Override
public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException {
    MimeBodyPart resultBodyPart;
    // Separate normal attachment from embedded image attachments
    List<Attachment> allAttachments = (List<Attachment>) parameters.get("attachments");
    Pair<List<Attachment>, List<Attachment>> attachmentPairs = separateAttachments(content, allAttachments);
    List<Attachment> embeddedImageAttachments = attachmentPairs.getLeft();
    List<Attachment> normalAttachments = attachmentPairs.getRight();
    // Step 1: Handle the HTML section of the mail.
    MimeBodyPart htmlBodyPart;
    if (!embeddedImageAttachments.isEmpty()) {
        htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments));
    } else {
        // Create the HTML body part of the email
        htmlBodyPart = createHTMLBodyPart(content, false);
    }
    // Step 2: Handle the optional alternative text
    String alternativeText = (String) parameters.get("alternate");
    if (alternativeText != null) {
        resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap()));
    } else {
        // No alternative text, just add the HTML body part to the Multipart
        resultBodyPart = htmlBodyPart;
    }
    // part. Note: If there are attachments we need to wrap our body part inside a "mixed" Multipart.
    if (!normalAttachments.isEmpty()) {
        MimeMultipart multipart = new MimeMultipart("mixed");
        multipart.addBodyPart(resultBodyPart);
        handleAttachments(multipart, normalAttachments);
        resultBodyPart = new MimeBodyPart();
        resultBodyPart.setContent(multipart);
    }
    // Handle headers passed as parameter
    addHeaders(resultBodyPart, parameters);
    return resultBodyPart;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Attachment(com.xpn.xwiki.api.Attachment) ArrayList(java.util.ArrayList) List(java.util.List) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 5 with Attachment

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

the class AbstractTemplateMimeBodyPartFactory method create.

@Override
public MimeBodyPart create(DocumentReference documentReference, Map<String, Object> parameters) throws MessagingException {
    Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables");
    Object localeValue = parameters.get("language");
    String textContent = getTemplateManager().evaluate(documentReference, "text", velocityVariables, localeValue);
    String htmlContent = getTemplateManager().evaluate(documentReference, "html", velocityVariables, localeValue);
    Map<String, Object> htmlParameters = new HashMap<>();
    htmlParameters.put("alternate", textContent);
    // Handle attachments:
    // - if the user has passed an "attachments" property with a list of attachment then add them
    // - if the user has set the "includeTemplateAttachments" property then add all attachments found in the
    // template document too
    List<Attachment> attachments = new ArrayList<>();
    List<Attachment> parameterAttachments = (List<Attachment>) parameters.get(ATTACHMENT_PROPERTY_NAME);
    if (parameterAttachments != null) {
        attachments.addAll(parameterAttachments);
    }
    Boolean includeTemplateAttachments = (Boolean) parameters.get(INCLUDE_TEMPLATE_ATTACHMENTS_PROPERTY_NAME);
    if (includeTemplateAttachments != null && includeTemplateAttachments) {
        try {
            List<XWikiAttachment> xwikiAttachments = ((XWikiDocument) this.bridge.getDocumentInstance(documentReference)).getAttachmentList();
            attachments.addAll(this.attachmentConverter.convert(xwikiAttachments));
        } catch (Exception e) {
            throw new MessagingException(String.format("Failed to include attachments from the Mail Template [%s]", documentReference), e);
        }
    }
    if (!attachments.isEmpty()) {
        htmlParameters.put(ATTACHMENT_PROPERTY_NAME, attachments);
    }
    return this.htmlBodyPartFactory.create(htmlContent, htmlParameters);
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) MessagingException(javax.mail.MessagingException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

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