use of com.fsck.k9.mail.internet.Viewable.Alternative in project k-9 by k9mail.
the class TextPartFinder method findFirstTextPart.
@Nullable
public Part findFirstTextPart(@NonNull Part part) {
String mimeType = part.getMimeType();
Body body = part.getBody();
if (body instanceof Multipart) {
Multipart multipart = (Multipart) body;
if (isSameMimeType(mimeType, "multipart/alternative")) {
return findTextPartInMultipartAlternative(multipart);
} else {
return findTextPartInMultipart(multipart);
}
} else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) {
return part;
}
return null;
}
use of com.fsck.k9.mail.internet.Viewable.Alternative in project k-9 by k9mail.
the class MessageBuilderTest method build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder.
@Test
public void build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder() {
MessageBuilder messageBuilder = createHtmlMessageBuilder();
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("text/html", parts.get(1).getMimeType());
}
use of com.fsck.k9.mail.internet.Viewable.Alternative in project k-9 by k9mail.
the class MigrationTest method migrateMixedWithAttachments.
@Test
public void migrateMixedWithAttachments() throws Exception {
SQLiteDatabase db = createV50Database();
insertMixedWithAttachments(db);
db.close();
LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
LocalMessage msg = localStore.getFolder("dev").getMessage("4");
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.BODY);
localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
Assert.assertEquals(3, msg.getId());
Assert.assertEquals(8, msg.getHeaderNames().size());
Assert.assertEquals("multipart/mixed", msg.getMimeType());
Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length);
Assert.assertEquals("multipart/mixed", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null));
Assert.assertEquals("----5D6OUTIYLNN2X63O0R2M0V53TOUAQP", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "boundary"));
Assert.assertEquals(2, msg.getAttachmentCount());
Multipart body = (Multipart) msg.getBody();
Assert.assertEquals(3, body.getCount());
Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType());
LocalBodyPart attachmentPart = (LocalBodyPart) body.getBodyPart(1);
Assert.assertEquals("image/png", attachmentPart.getMimeType());
Assert.assertEquals("2", attachmentPart.getServerExtra());
Assert.assertEquals("attachment", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), null));
Assert.assertEquals("k9small.png", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), "filename"));
Assert.assertEquals("2250", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), "size"));
FileBackedBody attachmentBody = (FileBackedBody) attachmentPart.getBody();
Assert.assertEquals(2250, attachmentBody.getSize());
Assert.assertEquals(MimeUtil.ENC_BINARY, attachmentBody.getEncoding());
Assert.assertEquals("application/whatevs", body.getBodyPart(2).getMimeType());
Assert.assertNull(body.getBodyPart(2).getBody());
}
use of com.fsck.k9.mail.internet.Viewable.Alternative in project k-9 by k9mail.
the class MigrationTest method migratePgpMimeSignedMessage.
@Test
public void migratePgpMimeSignedMessage() throws Exception {
SQLiteDatabase db = createV50Database();
insertPgpMimeSignedMessage(db);
db.close();
LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
LocalMessage msg = localStore.getFolder("dev").getMessage("5");
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.BODY);
localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
Assert.assertEquals(4, msg.getId());
Assert.assertEquals(8, msg.getHeaderNames().size());
Assert.assertEquals("multipart/mixed", msg.getMimeType());
Assert.assertEquals(2, msg.getAttachmentCount());
Multipart body = (Multipart) msg.getBody();
Assert.assertEquals(3, body.getCount());
Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType());
Assert.assertEquals("image/png", body.getBodyPart(1).getMimeType());
Assert.assertEquals("application/pgp-signature", body.getBodyPart(2).getMimeType());
}
use of com.fsck.k9.mail.internet.Viewable.Alternative 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<Viewable>();
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;
}
Aggregations