use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class TextPartFinder method findTextPartInMultipartAlternative.
private Part findTextPartInMultipartAlternative(Multipart multipart) {
Part htmlPart = null;
for (BodyPart bodyPart : multipart.getBodyParts()) {
String mimeType = bodyPart.getMimeType();
Body body = bodyPart.getBody();
if (body instanceof Multipart) {
Part candidatePart = findFirstTextPart(bodyPart);
if (candidatePart != null) {
if (isSameMimeType(candidatePart.getMimeType(), "text/html")) {
htmlPart = candidatePart;
} else {
return candidatePart;
}
}
} else if (isSameMimeType(mimeType, "text/plain")) {
return bodyPart;
} else if (isSameMimeType(mimeType, "text/html") && htmlPart == null) {
htmlPart = bodyPart;
}
}
if (htmlPart != null) {
return htmlPart;
}
return null;
}
use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class MessageViewInfoExtractorTest method testShouldSanitizeOutputHtml.
@Test
public void testShouldSanitizeOutputHtml() throws MessagingException {
// Create text/plain body
TextBody body = new TextBody(BODY_TEXT);
// Create message
MimeMessage message = new MimeMessage();
MimeMessageHelper.setBody(message, body);
message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
// Prepare fixture
HtmlProcessor htmlProcessor = mock(HtmlProcessor.class);
MessageViewInfoExtractor messageViewInfoExtractor = new MessageViewInfoExtractor(null, htmlProcessor, new TestCoreResourceProvider());
String value = "--sanitized html--";
when(htmlProcessor.processForDisplay(anyString())).thenReturn(value);
// Extract text
List<Part> outputNonViewableParts = new ArrayList<>();
ArrayList<Viewable> outputViewableParts = new ArrayList<>();
MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
ViewableExtractedText viewableExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
assertSame(value, viewableExtractedText.html);
}
use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class MessageViewInfoExtractorTest method testSimpleHtmlMessage.
@Test
public void testSimpleHtmlMessage() throws MessagingException {
String bodyText = "<strong>K-9 Mail</strong> rocks :>";
// Create text/plain body
TextBody body = new TextBody(bodyText);
// Create message
MimeMessage message = new MimeMessage();
message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html");
MimeMessageHelper.setBody(message, body);
// Extract text
ArrayList<Viewable> outputViewableParts = new ArrayList<>();
MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, null);
assertEquals(outputViewableParts.size(), 1);
ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
assertEquals(BODY_TEXT, container.text);
assertEquals(bodyText, container.html);
}
use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class MessageBuilderTest method build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder.
@Test
public void build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder() {
MessageBuilder messageBuilder = createHtmlMessageBuilder();
messageBuilder.buildAsync(callback);
MimeMessage message = getMessageFromCallback();
assertEquals(MimeMultipart.class, message.getBody().getClass());
assertEquals("multipart/alternative", ((MimeMultipart) message.getBody()).getMimeType());
List<BodyPart> parts = ((MimeMultipart) message.getBody()).getBodyParts();
// RFC 2046 - 5.1.4. - Best type is last displayable
assertEquals("text/plain", parts.get(0).getMimeType());
assertEquals("text/html", parts.get(1).getMimeType());
}
use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.
the class MessageCryptoStructureDetectorTest method findPrimaryCryptoPart_withMultipartAlternativeContainingPgpInline.
@Test
public void findPrimaryCryptoPart_withMultipartAlternativeContainingPgpInline() throws Exception {
List<Part> outputExtraParts = new ArrayList<>();
BodyPart pgpInlinePart = bodypart("text/plain", PGP_INLINE_DATA);
Message message = messageFromBody(multipart("alternative", pgpInlinePart, bodypart("text/html")));
Part cryptoPart = MessageCryptoStructureDetector.findPrimaryEncryptedOrSignedPart(message, outputExtraParts);
assertSame(pgpInlinePart, cryptoPart);
}
Aggregations