Search in sources :

Example 16 with Alternative

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;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Body(com.fsck.k9.mail.Body) Nullable(android.support.annotation.Nullable)

Example 17 with Alternative

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());
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Test(org.junit.Test)

Example 18 with Alternative

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());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 19 with Alternative

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());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 20 with Alternative

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;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) ArrayList(java.util.ArrayList) Text(com.fsck.k9.mail.internet.Viewable.Text) Body(com.fsck.k9.mail.Body)

Aggregations

Part (com.fsck.k9.mail.Part)20 BodyPart (com.fsck.k9.mail.BodyPart)18 Test (org.junit.Test)18 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)9 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)9 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)9 ArrayList (java.util.ArrayList)9 Multipart (com.fsck.k9.mail.Multipart)8 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)7 Message (com.fsck.k9.mail.Message)6 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)6 Body (com.fsck.k9.mail.Body)5 Viewable (com.fsck.k9.mail.internet.Viewable)4 Alternative (com.fsck.k9.mail.internet.Viewable.Alternative)4 Html (com.fsck.k9.mail.internet.Viewable.Html)4 Text (com.fsck.k9.mail.internet.Viewable.Text)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 FetchProfile (com.fsck.k9.mail.FetchProfile)3 Flowed (com.fsck.k9.mail.internet.Viewable.Flowed)3 MessageHeader (com.fsck.k9.mail.internet.Viewable.MessageHeader)3