Search in sources :

Example 16 with Body

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

the class MessageExtractorTest method getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText.

@Test
public void getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText() throws Exception {
    String bodyText = "<html><head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "</head><body>Sample text body</body></html>";
    BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), MimeUtil.ENC_8BIT);
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html");
    part.setBody(body);
    String result = MessageExtractor.getTextFromPart(part);
    assertNotNull(result);
    assertEquals(bodyText, result);
}
Also used : BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) Test(org.junit.Test)

Example 17 with Body

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

the class MessageExtractorTest method getTextFromPart_withPlainTextWithCharsetInContentTypeRawDataBody_shouldReturnText.

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

Example 18 with Body

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

the class MessageExtractorTest method getTextFromPart_withTextBody_shouldReturnText.

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

Example 19 with Body

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

the class MessageExtractorTest method getTextFromPart_withUnknownEncoding_shouldReturnUnmodifiedBodyContents.

@Test
public void getTextFromPart_withUnknownEncoding_shouldReturnUnmodifiedBodyContents() throws Exception {
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain");
    String bodyText = "Sample text body";
    BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), "unknown encoding");
    part.setBody(body);
    String result = MessageExtractor.getTextFromPart(part);
    assertEquals(bodyText, result);
}
Also used : BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) Test(org.junit.Test)

Example 20 with Body

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

the class PgpMessageBuilder method createOpenPgpDataSourceFromBodyPart.

@NonNull
private OpenPgpDataSource createOpenPgpDataSourceFromBodyPart(final MimeBodyPart bodyPart, final boolean writeBodyContentOnly) throws MessagingException {
    return new OpenPgpDataSource() {

        @Override
        public void writeTo(OutputStream os) throws IOException {
            try {
                if (writeBodyContentOnly) {
                    Body body = bodyPart.getBody();
                    InputStream inputStream = body.getInputStream();
                    IOUtils.copy(inputStream, os);
                } else {
                    bodyPart.writeTo(os);
                }
            } catch (MessagingException e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) InputStream(java.io.InputStream) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) IOException(java.io.IOException) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) NonNull(androidx.annotation.NonNull)

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