Search in sources :

Example 6 with Attachment

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

the class DefaultAttachmentConverter method convert.

@Override
public List<Attachment> convert(List<XWikiAttachment> attachments) {
    XWikiContext xwikiContext = getXWikiContext();
    List<Attachment> attachmentList = new ArrayList<>();
    for (XWikiAttachment attachment : attachments) {
        attachmentList.add(new Attachment(new Document(attachment.getDoc(), xwikiContext), attachment, xwikiContext));
    }
    return attachmentList;
}
Also used : ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document)

Example 7 with Attachment

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

the class HTMLMimeBodyPartFactoryTest method createWhenHTMLAndEmbeddedImagesAndNormalAttachments.

@Test
public void createWhenHTMLAndEmbeddedImagesAndNormalAttachments() 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 bodyPart = this.mocker.getComponentUnderTest().create("<p>html... <img src='cid:embeddedAttachment.png'/></p>", Collections.<String, Object>singletonMap("attachments", Arrays.asList(normalAttachment, embeddedAttachment)));
    MimeMultipart multipart = (MimeMultipart) bodyPart.getContent();
    assertEquals(2, multipart.getCount());
    MimeMultipart htmlMultipart = (MimeMultipart) multipart.getBodyPart(0).getContent();
    assertEquals(2, htmlMultipart.getCount());
    assertEquals("<p>html... <img src='cid:embeddedAttachment.png'/></p>", htmlMultipart.getBodyPart(0).getContent());
    assertSame(embeddedAttachmentBodyPart, htmlMultipart.getBodyPart(1));
    assertSame(normalAttachmentBodyPart, multipart.getBodyPart(1));
}
Also used : 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 Attachment

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

the class HTMLMimeBodyPartFactoryTest method createWhenHTMLAndEmbeddedImages.

@Test
public void createWhenHTMLAndEmbeddedImages() throws Exception {
    Attachment attachment = mock(Attachment.class);
    MimeBodyPart attachmentBodyPart = mock(MimeBodyPart.class);
    MimeBodyPartFactory attachmentBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, Attachment.class), "xwiki/attachment");
    when(attachmentBodyPartFactory.create(same(attachment), any(Map.class))).thenReturn(attachmentBodyPart);
    MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>some html</p>", Collections.<String, Object>singletonMap("attachments", Arrays.asList(attachment)));
    MimeMultipart multipart = ((MimeMultipart) bodyPart.getContent());
    assertEquals(2, multipart.getCount());
    assertEquals("<p>some html</p>", multipart.getBodyPart(0).getContent());
    assertSame(attachmentBodyPart, multipart.getBodyPart(1));
}
Also used : 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 9 with Attachment

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

the class TemplateMimeBodyPartFactoryTest method createWithAttachmentAndTemplateAttachments.

@Test
public void createWithAttachmentAndTemplateAttachments() 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);
    XWikiDocument xwikiDocument = mock(XWikiDocument.class);
    when(dab.getDocumentInstance(this.documentReference)).thenReturn(xwikiDocument);
    XWikiAttachment xwikiAttachment = mock(XWikiAttachment.class);
    when(xwikiDocument.getAttachmentList()).thenReturn(Collections.singletonList(xwikiAttachment));
    AttachmentConverter attachmentConverter = this.mocker.getInstance(AttachmentConverter.class);
    Attachment attachment2 = mock(Attachment.class, "attachment2");
    when(attachmentConverter.convert(Collections.singletonList(xwikiAttachment))).thenReturn(Collections.singletonList(attachment2));
    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", Arrays.asList(attachment1, attachment2));
    verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", htmlParameters);
}
Also used : HashMap(java.util.HashMap) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 10 with Attachment

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

the class MailSenderPlugin method createMimeMultipart.

/**
 * Creates a Multipart MIME Message (multiple content-types within the same message) from an existing mail
 *
 * @param mail The original Mail
 * @return The Multipart MIME message
 */
public Multipart createMimeMultipart(Mail mail, XWikiContext context) throws MessagingException, XWikiException, IOException {
    Multipart multipart;
    List<Attachment> rawAttachments = mail.getAttachments() != null ? mail.getAttachments() : new ArrayList<Attachment>();
    if (mail.getHtmlPart() == null && mail.getAttachments() != null) {
        multipart = new MimeMultipart("mixed");
        // Create the text part of the email
        BodyPart textPart = new MimeBodyPart();
        textPart.setContent(mail.getTextPart(), "text/plain; charset=" + EMAIL_ENCODING);
        multipart.addBodyPart(textPart);
        // Add attachments to the main multipart
        for (Attachment attachment : rawAttachments) {
            multipart.addBodyPart(createAttachmentBodyPart(attachment, context));
        }
    } else {
        multipart = new MimeMultipart("mixed");
        List<Attachment> attachments = new ArrayList<Attachment>();
        List<Attachment> embeddedImages = new ArrayList<Attachment>();
        // Create the text part of the email
        BodyPart textPart;
        textPart = new MimeBodyPart();
        textPart.setText(mail.getTextPart());
        // Create the HTML part of the email, define the html as a multipart/related in case there are images
        Multipart htmlMultipart = new MimeMultipart("related");
        BodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(mail.getHtmlPart(), "text/html; charset=" + EMAIL_ENCODING);
        htmlPart.setHeader("Content-Disposition", "inline");
        htmlPart.setHeader("Content-Transfer-Encoding", "quoted-printable");
        htmlMultipart.addBodyPart(htmlPart);
        // Find images used with src="cid:" in the email HTML part
        Pattern cidPattern = Pattern.compile("src=('|\")cid:([^'\"]*)('|\")", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher matcher = cidPattern.matcher(mail.getHtmlPart());
        List<String> foundEmbeddedImages = new ArrayList<String>();
        while (matcher.find()) {
            foundEmbeddedImages.add(matcher.group(2));
        }
        // email.
        for (Attachment attachment : rawAttachments) {
            if (foundEmbeddedImages.contains(attachment.getFilename())) {
                embeddedImages.add(attachment);
            } else {
                attachments.add(attachment);
            }
        }
        // Add the images to the HTML multipart (they should be hidden from the mail reader attachment list)
        for (Attachment image : embeddedImages) {
            htmlMultipart.addBodyPart(createAttachmentBodyPart(image, context));
        }
        // Wrap the HTML and text parts in an alternative body part and add it to the main multipart
        Multipart alternativePart = new MimeMultipart("alternative");
        BodyPart alternativeMultipartWrapper = new MimeBodyPart();
        BodyPart htmlMultipartWrapper = new MimeBodyPart();
        alternativePart.addBodyPart(textPart);
        htmlMultipartWrapper.setContent(htmlMultipart);
        alternativePart.addBodyPart(htmlMultipartWrapper);
        alternativeMultipartWrapper.setContent(alternativePart);
        multipart.addBodyPart(alternativeMultipartWrapper);
        // Add attachments to the main multipart
        for (Attachment attachment : attachments) {
            multipart.addBodyPart(createAttachmentBodyPart(attachment, context));
        }
    }
    return multipart;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Pattern(java.util.regex.Pattern) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart)

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