Search in sources :

Example 11 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class ImapResponseParser method parseLiteral.

/**
 * A "{" has been read. Read the rest of the size string, the space and then notify the callback with an
 * {@code InputStream}.
 */
private Object parseLiteral() throws IOException {
    expect('{');
    int size = Integer.parseInt(readStringUntil('}'));
    expect('\r');
    expect('\n');
    if (size == 0) {
        return "";
    }
    if (response.getCallback() != null) {
        FixedLengthInputStream fixed = new FixedLengthInputStream(inputStream, size);
        Exception callbackException = null;
        Object result = null;
        try {
            result = response.getCallback().foundLiteral(response, fixed);
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            callbackException = e;
        }
        boolean someDataWasRead = fixed.available() != size;
        if (someDataWasRead) {
            if (result == null && callbackException == null) {
                throw new AssertionError("Callback consumed some data but returned no result");
            }
            fixed.skipRemaining();
        }
        if (callbackException != null) {
            if (exception == null) {
                exception = callbackException;
            }
            return "EXCEPTION";
        }
        if (result != null) {
            return result;
        }
    }
    byte[] data = new byte[size];
    int read = 0;
    while (read != size) {
        int count = inputStream.read(data, read, size - read);
        if (count == -1) {
            throw new IOException("parseLiteral(): end of stream reached");
        }
        read += count;
    }
    return new String(data, "US-ASCII");
}
Also used : FixedLengthInputStream(com.fsck.k9.mail.filter.FixedLengthInputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 12 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method buildWithException_shouldThrow.

@Test
public void buildWithException_shouldThrow() throws MessagingException {
    MessageBuilder messageBuilder = new SimpleMessageBuilder(messageIdGenerator, boundaryGenerator, resourceProvider) {

        @Override
        protected void buildMessageInternal() {
            queueMessageBuildException(new MessagingException("expected error"));
        }
    };
    messageBuilder.buildAsync(callback);
    verify(callback).onMessageBuildException(any(MessagingException.class));
    verifyNoMoreInteractions(callback);
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 13 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method build_usingHtmlFormatWithInlineAttachment_shouldUseMultipartAlternativeInCorrectOrder.

@Test
public void build_usingHtmlFormatWithInlineAttachment_shouldUseMultipartAlternativeInCorrectOrder() throws Exception {
    String contentId = "contentId";
    String attachmentMimeType = "image/png";
    Map<String, Attachment> inlineAttachments = new HashMap<>();
    inlineAttachments.put(contentId, createAttachmentWithContent(attachmentMimeType, "1x1.png", "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC"));
    MessageBuilder messageBuilder = createHtmlMessageBuilder().setInlineAttachments(inlineAttachments);
    messageBuilder.buildAsync(callback);
    MimeMessage message = getMessageFromCallback();
    assertEquals(MimeMultipart.class, message.getBody().getClass());
    assertEquals("multipart/alternative", ((MimeMultipart) message.getBody()).getMimeType());
    List<BodyPart> parts = ((MimeMultipart) message.getBody()).getBodyParts();
    // RFC 2046 - 5.1.4. - Best type is last displayable
    assertEquals("text/plain", parts.get(0).getMimeType());
    assertEquals("multipart/related", parts.get(1).getMimeType());
    List<BodyPart> partWithInlineAttachment = ((MimeMultipart) parts.get(1).getBody()).getBodyParts();
    assertEquals("text/html", partWithInlineAttachment.get(0).getMimeType());
    assertEquals(attachmentMimeType, partWithInlineAttachment.get(1).getMimeType());
    String[] attachmentHeaders = partWithInlineAttachment.get(1).getHeader(MimeHeader.HEADER_CONTENT_ID);
    assertEquals(1, attachmentHeaders.length);
    assertEquals(contentId, attachmentHeaders[0]);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) HashMap(java.util.HashMap) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 14 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method build_shouldSucceed.

@Test
public void build_shouldSucceed() throws Exception {
    MessageBuilder messageBuilder = createSimpleMessageBuilder();
    messageBuilder.buildAsync(callback);
    MimeMessage message = getMessageFromCallback();
    assertEquals("text/plain", message.getMimeType());
    assertEquals(TEST_SUBJECT, message.getSubject());
    assertEquals(TEST_IDENTITY_ADDRESS, message.getFrom()[0]);
    assertArrayEquals(TEST_REPLY_TO, message.getReplyTo());
    assertArrayEquals(TEST_TO, message.getRecipients(RecipientType.TO));
    assertArrayEquals(TEST_CC, message.getRecipients(RecipientType.CC));
    assertArrayEquals(TEST_BCC, message.getRecipients(RecipientType.BCC));
    assertEquals(MESSAGE_HEADERS + MESSAGE_CONTENT, getMessageContents(message));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 15 with Callback

use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.

the class MessageBuilderTest method build_detachAndReattach_shouldSucceed.

@Test
public void build_detachAndReattach_shouldSucceed() throws MessagingException {
    MessageBuilder messageBuilder = createSimpleMessageBuilder();
    Callback anotherCallback = mock(Callback.class);
    Robolectric.getBackgroundThreadScheduler().pause();
    messageBuilder.buildAsync(callback);
    messageBuilder.detachCallback();
    Robolectric.getBackgroundThreadScheduler().unPause();
    messageBuilder.reattachCallback(anotherCallback);
    verifyNoMoreInteractions(callback);
    verify(anotherCallback).onMessageBuildSuccess(any(MimeMessage.class), eq(false));
    verifyNoMoreInteractions(anotherCallback);
}
Also used : Callback(com.fsck.k9.message.MessageBuilder.Callback) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)14 Callback (com.fsck.k9.message.MessageBuilder.Callback)14 MessagingException (com.fsck.k9.mail.MessagingException)11 RobolectricTest (com.fsck.k9.RobolectricTest)10 PendingIntent (android.app.PendingIntent)9 Intent (android.content.Intent)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 OutputStream (java.io.OutputStream)9 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)9 ComposeCryptoStatus (com.fsck.k9.activity.compose.ComposeCryptoStatus)7 Recipient (com.fsck.k9.view.RecipientSelectView.Recipient)5 BodyPart (com.fsck.k9.mail.BodyPart)4 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)4 IOException (java.io.IOException)3 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Activity (android.app.Activity)1