Search in sources :

Example 91 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageExtractor method findTextPart.

/**
 * Search the children of a {@link Multipart} for {@code text/plain} parts.
 *
 * @param multipart The {@code Multipart} to search through.
 * @param directChild If {@code true}, this method will return after the first {@code text/plain} was
 *         found.
 *
 * @return A list of {@link Text} viewables.
 *
 * @throws MessagingException
 *          In case of an error.
 */
private static List<Viewable> findTextPart(Multipart multipart, boolean directChild) throws MessagingException {
    List<Viewable> viewables = new ArrayList<>();
    for (Part part : multipart.getBodyParts()) {
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            /*
                 * Recurse to find text parts. Since this is a multipart that is a child of a
                 * multipart/alternative we don't want to stop after the first text/plain part
                 * we find. This will allow to get all text parts for constructions like this:
                 *
                 * 1. multipart/alternative
                 * 1.1. multipart/mixed
                 * 1.1.1. text/plain
                 * 1.1.2. text/plain
                 * 1.2. text/html
                 */
            List<Viewable> textViewables = findTextPart(innerMultipart, false);
            if (!textViewables.isEmpty()) {
                viewables.addAll(textViewables);
                if (directChild) {
                    break;
                }
            }
        } else if (isPartTextualBody(part) && isSameMimeType(part.getMimeType(), "text/plain")) {
            Text text = new Text(part);
            viewables.add(text);
            if (directChild) {
                break;
            }
        }
    }
    return viewables;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) ArrayList(java.util.ArrayList) Text(com.fsck.k9.mail.internet.Viewable.Text) Body(com.fsck.k9.mail.Body)

Example 92 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageExtractor method getTextFromPart.

public static String getTextFromPart(Part part, long textSizeLimit) {
    if (part == null) {
        throw new IllegalArgumentException("Argument 'part' must not be null");
    }
    try {
        Body body = part.getBody();
        if (body == null) {
            Timber.v("No body present for this message part");
            return null;
        }
        if (body instanceof TextBody) {
            TextBody textBody = (TextBody) body;
            return textBody.getRawText();
        }
        String mimeType = part.getMimeType();
        if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*") || part.isMimeType("application/pgp")) {
            return getTextFromTextPart(part, body, mimeType, textSizeLimit);
        }
        Timber.w("Provided non-text part: %s", mimeType);
    } catch (IOException | MessagingException e) {
        Timber.e(e, "Unable to getTextFromPart");
    }
    return null;
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) IOException(java.io.IOException) Body(com.fsck.k9.mail.Body)

Example 93 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageExtractor method findHtmlPart.

/**
 * Search the children of a {@link Multipart} for {@code text/html} parts.
 * Every part that is not a {@code text/html} we want to display, we add to 'attachments'.
 *
 * @param multipart The {@code Multipart} to search through.
 * @param knownTextParts A set of {@code text/plain} parts that shouldn't be added to 'attachments'.
 * @param outputNonViewableParts A list that will receive the parts that are considered attachments.
 * @param directChild If {@code true}, this method will add all {@code text/html} parts except the first
 *         found to 'attachments'.
 *
 * @return A list of {@link Text} viewables.
 *
 * @throws MessagingException In case of an error.
 */
private static List<Viewable> findHtmlPart(Multipart multipart, Set<Part> knownTextParts, @Nullable List<Part> outputNonViewableParts, boolean directChild) throws MessagingException {
    boolean saveNonViewableParts = outputNonViewableParts != null;
    List<Viewable> viewables = new ArrayList<>();
    boolean partFound = false;
    for (Part part : multipart.getBodyParts()) {
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            if (directChild && partFound) {
                if (saveNonViewableParts) {
                    // We already found our text/html part. Now we're only looking for attachments.
                    findAttachments(innerMultipart, knownTextParts, outputNonViewableParts);
                }
            } else {
                /*
                     * Recurse to find HTML parts. Since this is a multipart that is a child of a
                     * multipart/alternative we don't want to stop after the first text/html part
                     * we find. This will allow to get all text parts for constructions like this:
                     *
                     * 1. multipart/alternative
                     * 1.1. text/plain
                     * 1.2. multipart/mixed
                     * 1.2.1. text/html
                     * 1.2.2. text/html
                     * 1.3. image/jpeg
                     */
                List<Viewable> htmlViewables = findHtmlPart(innerMultipart, knownTextParts, outputNonViewableParts, false);
                if (!htmlViewables.isEmpty()) {
                    partFound = true;
                    viewables.addAll(htmlViewables);
                }
            }
        } else if (!(directChild && partFound) && isPartTextualBody(part) && isSameMimeType(part.getMimeType(), "text/html")) {
            Html html = new Html(part);
            viewables.add(html);
            partFound = true;
        } else if (!knownTextParts.contains(part)) {
            if (saveNonViewableParts) {
                // Only add this part as attachment if it's not a viewable text/plain part found earlier
                outputNonViewableParts.add(part);
            }
        }
    }
    return viewables;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) ArrayList(java.util.ArrayList) Html(com.fsck.k9.mail.internet.Viewable.Html) Body(com.fsck.k9.mail.Body)

Example 94 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MimeMessageParseTest method checkLeafParts.

private static void checkLeafParts(MimeMessage msg, String... expectedParts) throws Exception {
    List<String> actual = new ArrayList<>();
    for (Body leaf : getLeafParts(msg.getBody())) {
        actual.add(streamToString(MimeUtility.decodeBody(leaf)));
    }
    assertEquals(Arrays.asList(expectedParts), actual);
}
Also used : ArrayList(java.util.ArrayList) Body(com.fsck.k9.mail.Body)

Example 95 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageExtractorTest method getTextFromPart_withHtmlWithCharsetInContentTypeRawDataBody_shouldReturnHtmlText.

@Test
public void getTextFromPart_withHtmlWithCharsetInContentTypeRawDataBody_shouldReturnHtmlText() throws Exception {
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html; charset=UTF-8");
    BinaryMemoryBody body = new BinaryMemoryBody("<html><body>Sample text body</body></html>".getBytes(), MimeUtil.ENC_8BIT);
    part.setBody(body);
    String result = MessageExtractor.getTextFromPart(part);
    assertEquals("<html><body>Sample text body</body></html>", result);
}
Also used : BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) Test(org.junit.Test)

Aggregations

Body (com.fsck.k9.mail.Body)44 BodyPart (com.fsck.k9.mail.BodyPart)35 Multipart (com.fsck.k9.mail.Multipart)32 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)32 Part (com.fsck.k9.mail.Part)29 Test (org.junit.Test)29 TextBody (com.fsck.k9.mail.internet.TextBody)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)21 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)19 ArrayList (java.util.ArrayList)16 MessagingException (com.fsck.k9.mail.MessagingException)14 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Message (com.fsck.k9.mail.Message)9 OutputStream (java.io.OutputStream)9 Stack (java.util.Stack)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)7 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)7 InputStream (java.io.InputStream)7