use of com.fsck.k9.mailstore.BinaryMemoryBody in project k-9 by k9mail.
the class MessageExtractorTest method getTextFromPart_withPlainTextWithCharsetInContentTypeRawDataBody_shouldReturnText.
@Test
public void getTextFromPart_withPlainTextWithCharsetInContentTypeRawDataBody_shouldReturnText() throws Exception {
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; charset=UTF-8");
BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT);
part.setBody(body);
String result = MessageExtractor.getTextFromPart(part);
assertEquals("Sample text body", result);
}
use of com.fsck.k9.mailstore.BinaryMemoryBody in project k-9 by k9mail.
the class MessageExtractorTest method getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText.
@Test
public void getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText() throws Exception {
String bodyText = "<html><head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "</head><body>Sample text body</body></html>";
BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), MimeUtil.ENC_8BIT);
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html");
part.setBody(body);
String result = MessageExtractor.getTextFromPart(part);
assertNotNull(result);
assertEquals(bodyText, result);
}
use of com.fsck.k9.mailstore.BinaryMemoryBody in project k-9 by k9mail.
the class LocalFolder method loadMessagePart.
private void loadMessagePart(LocalMessage message, Map<Long, Part> partById, Cursor cursor) throws MessagingException {
long id = cursor.getLong(0);
long parentId = cursor.getLong(2);
String mimeType = cursor.getString(3);
long size = cursor.getLong(4);
byte[] header = cursor.getBlob(6);
int dataLocation = cursor.getInt(9);
String serverExtra = cursor.getString(15);
// TODO we don't currently cache much of the part data which is computed with AttachmentInfoExtractor,
// TODO might want to do that at a later point?
// String displayName = cursor.getString(5);
// int type = cursor.getInt(1);
// boolean inlineAttachment = (type == MessagePartType.HIDDEN_ATTACHMENT);
final Part part;
if (id == message.getMessagePartId()) {
part = message;
} else {
Part parentPart = partById.get(parentId);
if (parentPart == null) {
throw new IllegalStateException("Parent part not found");
}
String parentMimeType = parentPart.getMimeType();
if (MimeUtility.isMultipart(parentMimeType)) {
BodyPart bodyPart = new LocalBodyPart(getAccountUuid(), message, id, size);
((Multipart) parentPart.getBody()).addBodyPart(bodyPart);
part = bodyPart;
} else if (MimeUtility.isMessage(parentMimeType)) {
Message innerMessage = new LocalMimeMessage(getAccountUuid(), message, id);
parentPart.setBody(innerMessage);
part = innerMessage;
} else {
throw new IllegalStateException("Parent is neither a multipart nor a message");
}
parseHeaderBytes(part, header);
}
partById.put(id, part);
part.setServerExtra(serverExtra);
if (MimeUtility.isMultipart(mimeType)) {
byte[] preamble = cursor.getBlob(11);
byte[] epilogue = cursor.getBlob(12);
String boundary = cursor.getString(13);
MimeMultipart multipart = new MimeMultipart(mimeType, boundary);
part.setBody(multipart);
multipart.setPreamble(preamble);
multipart.setEpilogue(epilogue);
} else if (dataLocation == DataLocation.IN_DATABASE) {
String encoding = cursor.getString(7);
byte[] data = cursor.getBlob(10);
Body body = new BinaryMemoryBody(data, encoding);
part.setBody(body);
} else if (dataLocation == DataLocation.ON_DISK) {
String encoding = cursor.getString(7);
File file = localStore.getAttachmentFile(Long.toString(id));
if (file.exists()) {
Body body = new FileBackedBody(file, encoding);
part.setBody(body);
}
}
}
use of com.fsck.k9.mailstore.BinaryMemoryBody in project k-9 by k9mail.
the class MigrationTest method migrateTextPlain.
@Test
public void migrateTextPlain() throws Exception {
SQLiteDatabase db = createV50Database();
insertSimplePlaintextMessage(db);
db.close();
LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
LocalMessage msg = localStore.getFolder("dev").getMessage("3");
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.BODY);
localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
Assert.assertEquals("text/plain", msg.getMimeType());
Assert.assertEquals(2, msg.getId());
Assert.assertEquals(13, msg.getHeaderNames().size());
Assert.assertEquals(0, msg.getAttachmentCount());
Assert.assertEquals(1, msg.getHeader("User-Agent").length);
Assert.assertEquals("Mutt/1.5.24 (2015-08-30)", msg.getHeader("User-Agent")[0]);
Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length);
Assert.assertEquals("text/plain", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null));
Assert.assertEquals("utf-8", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "charset"));
Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody);
String msgTextContent = MessageExtractor.getTextFromPart(msg);
Assert.assertEquals("nothing special here.\r\n", msgTextContent);
}
use of com.fsck.k9.mailstore.BinaryMemoryBody in project k-9 by k9mail.
the class MigrationTest method migratePgpInlineClearsignedMessage.
@Test
public void migratePgpInlineClearsignedMessage() throws Exception {
SQLiteDatabase db = createV50Database();
insertPgpInlineClearsignedMessage(db);
db.close();
LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
LocalMessage msg = localStore.getFolder("dev").getMessage("8");
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.BODY);
localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
Assert.assertEquals(7, msg.getId());
Assert.assertEquals(12, msg.getHeaderNames().size());
Assert.assertEquals("text/plain", msg.getMimeType());
Assert.assertEquals(0, msg.getAttachmentCount());
Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody);
String msgTextContent = MessageExtractor.getTextFromPart(msg);
Assert.assertEquals(OpenPgpUtils.PARSE_RESULT_SIGNED_MESSAGE, OpenPgpUtils.parseMessage(msgTextContent));
}
Aggregations