Search in sources :

Example 41 with Body

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

the class BinaryTempFileBody method setEncoding.

public void setEncoding(String encoding) throws MessagingException {
    if (mEncoding != null && mEncoding.equalsIgnoreCase(encoding)) {
        return;
    }
    // The encoding changed, so we need to convert the message
    if (!MimeUtil.ENC_8BIT.equalsIgnoreCase(mEncoding)) {
        throw new RuntimeException("Can't convert from encoding: " + mEncoding);
    }
    try {
        File newFile = File.createTempFile("body", null, mTempDirectory);
        final OutputStream out = new FileOutputStream(newFile);
        try {
            OutputStream wrappedOut;
            if (MimeUtil.ENC_QUOTED_PRINTABLE.equals(encoding)) {
                wrappedOut = new QuotedPrintableOutputStream(out, false);
            } else if (MimeUtil.ENC_BASE64.equals(encoding)) {
                wrappedOut = new Base64OutputStream(out);
            } else {
                throw new RuntimeException("Target encoding not supported: " + encoding);
            }
            InputStream in = getInputStream();
            try {
                IOUtils.copy(in, wrappedOut);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(wrappedOut);
            }
        } finally {
            IOUtils.closeQuietly(out);
        }
        mFile = newFile;
        mEncoding = encoding;
    } catch (IOException e) {
        throw new MessagingException("Unable to convert body", e);
    }
}
Also used : QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) MessagingException(com.fsck.k9.mail.MessagingException) FileInputStream(java.io.FileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Base64OutputStream(com.fsck.k9.mail.filter.Base64OutputStream) FileOutputStream(java.io.FileOutputStream) QuotedPrintableOutputStream(org.apache.james.mime4j.codec.QuotedPrintableOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Base64OutputStream(com.fsck.k9.mail.filter.Base64OutputStream) File(java.io.File)

Example 42 with Body

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

the class MessageExtractor method findAttachments.

/**
 * Traverse the MIME tree and add everything that's not a known text part to 'attachments'.
 *
 * @param multipart
 *         The {@link Multipart} to start from.
 * @param knownTextParts
 *         A set of known text parts we don't want to end up in 'attachments'.
 * @param attachments
 *         A list that will receive the parts that are considered attachments.
 */
private static void findAttachments(Multipart multipart, Set<Part> knownTextParts, @NonNull List<Part> attachments) {
    for (Part part : multipart.getBodyParts()) {
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            findAttachments(innerMultipart, knownTextParts, attachments);
        } else if (!knownTextParts.contains(part)) {
            attachments.add(part);
        }
    }
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) Body(com.fsck.k9.mail.Body)

Example 43 with Body

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

the class DefaultBodyFactory method createBody.

public Body createBody(String contentTransferEncoding, String contentType, InputStream inputStream) throws IOException {
    if (contentTransferEncoding != null) {
        contentTransferEncoding = MimeUtility.getHeaderParameter(contentTransferEncoding, null);
    }
    final BinaryTempFileBody tempBody;
    if (MimeUtil.isMessage(contentType)) {
        tempBody = new BinaryTempFileMessageBody(contentTransferEncoding);
    } else {
        tempBody = new BinaryTempFileBody(contentTransferEncoding);
    }
    OutputStream outputStream = tempBody.getOutputStream();
    try {
        copyData(inputStream, outputStream);
    } finally {
        outputStream.close();
    }
    return tempBody;
}
Also used : BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) OutputStream(java.io.OutputStream) BinaryTempFileMessageBody(com.fsck.k9.mail.internet.BinaryTempFileMessageBody)

Example 44 with Body

use of com.fsck.k9.mail.Body 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 dir=\"auto\" 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 dir=\"auto\" 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, firstMessageExtractedText.html);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 45 with Body

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

the class WebDavFolderTest method folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies.

@Test
public void folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies() throws MessagingException, IOException, URISyntaxException {
    setupStoreForMessageFetching();
    when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMap())).thenReturn(mockDataSet);
    List<WebDavMessage> messages = setup25MessagesToFetch();
    when(mockHttpClient.executeOverride(any(HttpUriRequest.class), nullable(HttpContext.class))).thenAnswer(new Answer<HttpResponse>() {

        @Override
        public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
            HttpResponse httpResponse = mock(HttpResponse.class);
            StatusLine statusLine = mock(StatusLine.class);
            when(httpResponse.getStatusLine()).thenReturn(statusLine);
            when(statusLine.getStatusCode()).thenReturn(200);
            BasicHttpEntity httpEntity = new BasicHttpEntity();
            String body = "";
            httpEntity.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
            when(httpResponse.getEntity()).thenReturn(httpEntity);
            return httpResponse;
        }
    });
    FetchProfile profile = new FetchProfile();
    profile.add(FetchProfile.Item.FLAGS);
    profile.add(FetchProfile.Item.BODY);
    folder.fetch(messages, profile, listener, MAX_DOWNLOAD_SIZE);
    verify(listener, times(25)).messageStarted(any(String.class), anyInt(), anyInt());
    verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), anyInt());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) FetchProfile(com.fsck.k9.mail.FetchProfile) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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