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");
}
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);
}
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]);
}
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));
}
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);
}
Aggregations