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