use of com.fsck.k9.mail.internet.Viewable in project k-9 by k9mail.
the class MessageViewInfoExtractorTest method testMultipartDigestWithMessages.
@Test
public void testMultipartDigestWithMessages() throws Exception {
String data = "Content-Type: multipart/digest; boundary=\"bndry\"\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "Content-Type: multipart/alternative; boundary=\"bndry2\"\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text part of second message\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/html\"\r\n" + "\r\n" + "html part of second message\r\n" + "\r\n" + "--bndry2--\r\n" + "\r\n" + "--bndry--\r\n";
MimeMessage message = MimeMessage.parseMimeMessage(new ByteArrayInputStream(data.getBytes()), false);
// Extract text
List<Part> outputNonViewableParts = new ArrayList<>();
ArrayList<Viewable> outputViewableParts = new ArrayList<>();
MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
String expectedExtractedText = "Subject: (No subject)\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "\r\n" + "------------------------------------------------------------------------\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "\r\n" + "text part of second message\r\n";
String expectedHtmlText = "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>(No subject)</td></tr>" + "</table>" + "<pre class=\"k9mail\">text body of first message<br /></pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: 1px solid #000\"></p>" + "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>subject of second message</td></tr>" + "</table>" + "<pre class=\"k9mail\">text part of second message<br /></pre>";
assertEquals(4, outputViewableParts.size());
assertEquals("subject of second message", ((MessageHeader) outputViewableParts.get(2)).getMessage().getSubject());
ViewableExtractedText firstMessageExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
assertEquals(expectedExtractedText, firstMessageExtractedText.text);
assertEquals(expectedHtmlText, getHtmlBodyText(firstMessageExtractedText.html));
}
use of com.fsck.k9.mail.internet.Viewable in project k-9 by k9mail.
the class MessageExtractor method collectAttachments.
/**
* Collect attachment parts of a message.
* @return A list of parts regarded as attachments.
* @throws MessagingException In case of an error.
*/
public static List<Part> collectAttachments(Message message) throws MessagingException {
try {
List<Part> attachments = new ArrayList<>();
findViewablesAndAttachments(message, new ArrayList<Viewable>(), attachments);
return attachments;
} catch (Exception e) {
throw new MessagingException("Couldn't collect attachment parts", e);
}
}
use of com.fsck.k9.mail.internet.Viewable in project k-9 by k9mail.
the class MessageViewInfoExtractor method buildText.
private StringBuilder buildText(Viewable viewable, boolean prependDivider) {
StringBuilder text = new StringBuilder();
if (viewable instanceof Textual) {
Part part = ((Textual) viewable).getPart();
addTextDivider(text, part, prependDivider);
String t = MessageExtractor.getTextFromPart(part);
if (t == null) {
t = "";
} else if (viewable instanceof Html) {
t = HtmlConverter.htmlToText(t);
} else if (viewable instanceof Flowed) {
t = FlowedMessageUtils.deflow(t, false);
} else if (!(viewable instanceof Text)) {
throw new IllegalStateException("unhandled case!");
}
text.append(t);
} else if (viewable instanceof Alternative) {
// That's odd - an Alternative as child of an Alternative; go ahead and try to use the
// text/plain child; fall-back to the text/html part.
Alternative alternative = (Alternative) viewable;
List<Viewable> textAlternative = alternative.getText().isEmpty() ? alternative.getHtml() : alternative.getText();
boolean divider = prependDivider;
for (Viewable textViewable : textAlternative) {
text.append(buildText(textViewable, divider));
divider = true;
}
}
return text;
}
use of com.fsck.k9.mail.internet.Viewable in project k-9 by k9mail.
the class MessageViewInfoExtractor method extractMessageForView.
@WorkerThread
public MessageViewInfo extractMessageForView(Message message, @Nullable MessageCryptoAnnotations annotations) throws MessagingException {
Part rootPart;
CryptoResultAnnotation cryptoResultAnnotation;
List<Part> extraParts;
CryptoMessageParts cryptoMessageParts = MessageCryptoSplitter.split(message, annotations);
if (cryptoMessageParts != null) {
rootPart = cryptoMessageParts.contentPart;
cryptoResultAnnotation = cryptoMessageParts.contentCryptoAnnotation;
extraParts = cryptoMessageParts.extraParts;
} else {
if (annotations != null && !annotations.isEmpty()) {
Timber.e("Got message annotations but no crypto root part!");
}
rootPart = message;
cryptoResultAnnotation = null;
extraParts = null;
}
List<AttachmentViewInfo> attachmentInfos = new ArrayList<>();
ViewableExtractedText viewable = extractViewableAndAttachments(Collections.singletonList(rootPart), attachmentInfos);
List<AttachmentViewInfo> extraAttachmentInfos = new ArrayList<>();
String extraViewableText = null;
if (extraParts != null) {
ViewableExtractedText extraViewable = extractViewableAndAttachments(extraParts, extraAttachmentInfos);
extraViewableText = extraViewable.text;
}
AttachmentResolver attachmentResolver = AttachmentResolver.createFromPart(rootPart);
boolean isMessageIncomplete = !message.isSet(Flag.X_DOWNLOADED_FULL) || MessageExtractor.hasMissingParts(message);
return MessageViewInfo.createWithExtractedContent(message, isMessageIncomplete, rootPart, viewable.html, attachmentInfos, cryptoResultAnnotation, attachmentResolver, extraViewableText, extraAttachmentInfos);
}
use of com.fsck.k9.mail.internet.Viewable in project k-9 by k9mail.
the class MessageViewInfoExtractorTest method testTextPlainFormatFlowed.
@Test
public void testTextPlainFormatFlowed() throws MessagingException {
// Create text/plain body
TextBody body = new TextBody(BODY_TEXT_FLOWED);
// Create message
MimeMessage message = new MimeMessage();
MimeMessageHelper.setBody(message, body);
message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
// Extract text
List<Part> outputNonViewableParts = new ArrayList<>();
ArrayList<Viewable> outputViewableParts = new ArrayList<>();
MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
String expectedText = "K-9 Mail rocks :> flowed line\r\n" + "not flowed line";
String expectedHtml = "<pre class=\"k9mail\">" + "K-9 Mail rocks :> flowed line<br />not flowed line" + "</pre>";
assertEquals(expectedText, container.text);
assertEquals(expectedHtml, getHtmlBodyText(container.html));
}
Aggregations