@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.componentManager.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.htmlMimeBodyPartFactory.create( "<p>html... <img src='cid:embeddedAttachment.png'/></p>", Collections.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)); }
@Override public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException { MimeBodyPart resultBodyPart; 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(); MimeBodyPart htmlBodyPart; if (!embeddedImageAttachments.isEmpty()) { htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments)); } else { htmlBodyPart = createHTMLBodyPart(content, false); } String alternativeText = (String) parameters.get("alternate"); if (alternativeText != null) { resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap())); } else { resultBodyPart = htmlBodyPart; } if (!normalAttachments.isEmpty()) { MimeMultipart multipart = new MimeMultipart("mixed"); multipart.addBodyPart(resultBodyPart); handleAttachments(multipart, normalAttachments); resultBodyPart = new MimeBodyPart(); resultBodyPart.setContent(multipart); } addHeaders(resultBodyPart, parameters); return resultBodyPart; }
HTMLMimeBodyPartFactory extends AbstractMimeBodyPartFactory<String> { @Override public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException { MimeBodyPart resultBodyPart; 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(); MimeBodyPart htmlBodyPart; if (!embeddedImageAttachments.isEmpty()) { htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments)); } else { htmlBodyPart = createHTMLBodyPart(content, false); } String alternativeText = (String) parameters.get("alternate"); if (alternativeText != null) { resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap())); } else { resultBodyPart = htmlBodyPart; } if (!normalAttachments.isEmpty()) { MimeMultipart multipart = new MimeMultipart("mixed"); multipart.addBodyPart(resultBodyPart); handleAttachments(multipart, normalAttachments); resultBodyPart = new MimeBodyPart(); resultBodyPart.setContent(multipart); } addHeaders(resultBodyPart, parameters); return resultBodyPart; } }
HTMLMimeBodyPartFactory extends AbstractMimeBodyPartFactory<String> { @Override public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException { MimeBodyPart resultBodyPart; 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(); MimeBodyPart htmlBodyPart; if (!embeddedImageAttachments.isEmpty()) { htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments)); } else { htmlBodyPart = createHTMLBodyPart(content, false); } String alternativeText = (String) parameters.get("alternate"); if (alternativeText != null) { resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap())); } else { resultBodyPart = htmlBodyPart; } if (!normalAttachments.isEmpty()) { MimeMultipart multipart = new MimeMultipart("mixed"); multipart.addBodyPart(resultBodyPart); handleAttachments(multipart, normalAttachments); resultBodyPart = new MimeBodyPart(); resultBodyPart.setContent(multipart); } addHeaders(resultBodyPart, parameters); return resultBodyPart; }  }
HTMLMimeBodyPartFactory extends AbstractMimeBodyPartFactory<String> { @Override public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException { MimeBodyPart resultBodyPart; 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(); MimeBodyPart htmlBodyPart; if (!embeddedImageAttachments.isEmpty()) { htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments)); } else { htmlBodyPart = createHTMLBodyPart(content, false); } String alternativeText = (String) parameters.get("alternate"); if (alternativeText != null) { resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap())); } else { resultBodyPart = htmlBodyPart; } if (!normalAttachments.isEmpty()) { MimeMultipart multipart = new MimeMultipart("mixed"); multipart.addBodyPart(resultBodyPart); handleAttachments(multipart, normalAttachments); resultBodyPart = new MimeBodyPart(); resultBodyPart.setContent(multipart); } addHeaders(resultBodyPart, parameters); return resultBodyPart; }  @Override MimeBodyPart create(String content, Map<String, Object> parameters); }
HTMLMimeBodyPartFactory extends AbstractMimeBodyPartFactory<String> { @Override public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException { MimeBodyPart resultBodyPart; 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(); MimeBodyPart htmlBodyPart; if (!embeddedImageAttachments.isEmpty()) { htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments)); } else { htmlBodyPart = createHTMLBodyPart(content, false); } String alternativeText = (String) parameters.get("alternate"); if (alternativeText != null) { resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap())); } else { resultBodyPart = htmlBodyPart; } if (!normalAttachments.isEmpty()) { MimeMultipart multipart = new MimeMultipart("mixed"); multipart.addBodyPart(resultBodyPart); handleAttachments(multipart, normalAttachments); resultBodyPart = new MimeBodyPart(); resultBodyPart.setContent(multipart); } addHeaders(resultBodyPart, parameters); return resultBodyPart; }  @Override MimeBodyPart create(String content, Map<String, Object> parameters);  }