Search in sources :

Example 1 with Viewable

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));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) Test(org.junit.Test)

Example 2 with Viewable

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);
    }
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException)

Example 3 with Viewable

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;
}
Also used : Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Flowed(com.fsck.k9.mail.internet.Viewable.Flowed) Textual(com.fsck.k9.mail.internet.Viewable.Textual) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) Html(com.fsck.k9.mail.internet.Viewable.Html) Text(com.fsck.k9.mail.internet.Viewable.Text) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Viewable

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);
}
Also used : CryptoMessageParts(com.fsck.k9.ui.crypto.MessageCryptoSplitter.CryptoMessageParts) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) WorkerThread(android.support.annotation.WorkerThread)

Example 5 with Viewable

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 :&gt; flowed line<br />not flowed line" + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, getHtmlBodyText(container.html));
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) Test(org.junit.Test)

Aggregations

Part (com.fsck.k9.mail.Part)15 ArrayList (java.util.ArrayList)14 Viewable (com.fsck.k9.mail.internet.Viewable)11 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)7 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)7 Test (org.junit.Test)7 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)6 TextBody (com.fsck.k9.mail.internet.TextBody)6 BodyPart (com.fsck.k9.mail.BodyPart)4 Alternative (com.fsck.k9.mail.internet.Viewable.Alternative)4 Html (com.fsck.k9.mail.internet.Viewable.Html)4 Text (com.fsck.k9.mail.internet.Viewable.Text)4 Body (com.fsck.k9.mail.Body)3 Multipart (com.fsck.k9.mail.Multipart)3 Flowed (com.fsck.k9.mail.internet.Viewable.Flowed)3 MessageHeader (com.fsck.k9.mail.internet.Viewable.MessageHeader)3 Textual (com.fsck.k9.mail.internet.Viewable.Textual)3 Message (com.fsck.k9.mail.Message)2 MessagingException (com.fsck.k9.mail.MessagingException)2 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)2