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;
}
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));
}
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));
}
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);
}
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;
}
Aggregations