Merge pull request #1805 from philipwhiuk/unsupportedEncodingHandling

Handle e-mails with unsupported encoding better
This commit is contained in:
cketti 2016-12-05 18:05:16 +01:00 committed by GitHub
commit 4855d48ba3
5 changed files with 31 additions and 5 deletions

View file

@ -47,7 +47,7 @@ public class MessageExtractor {
if ((part != null) && (part.getBody() != null)) {
final Body body = part.getBody();
if (body instanceof TextBody) {
return ((TextBody)body).getRawText();
return ((TextBody) body).getRawText();
}
final String mimeType = part.getMimeType();
if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*") ||

View file

@ -1044,7 +1044,7 @@ public class MimeUtility {
}
};
} else {
throw new UnsupportedOperationException("Encoding for RawDataBody not supported: " + encoding);
throw new UnsupportedContentTransferEncodingException(encoding);
}
} else {
inputStream = body.getInputStream();

View file

@ -0,0 +1,9 @@
package com.fsck.k9.mail.internet;
import com.fsck.k9.mail.MessagingException;
public class UnsupportedContentTransferEncodingException extends MessagingException {
public UnsupportedContentTransferEncodingException(String encoding) {
super("Unsupported encoding: "+encoding);
}
}

View file

@ -72,13 +72,15 @@ public class MessageExtractorTest {
assertNull(result);
}
@Test(expected = UnsupportedOperationException.class)
public void getTextFromPart_withUnknownEncoding_shouldThrowRuntimeException() throws Exception {
@Test
public void getTextFromPart_withUnknownEncoding_shouldReturnNull() throws Exception {
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain");
BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), "unknown encoding");
part.setBody(body);
MessageExtractor.getTextFromPart(part);
String result = MessageExtractor.getTextFromPart(part);
assertNull(result);
}
@Test

View file

@ -128,6 +128,21 @@ public class MimeMessageParseTest {
"");
}
@Test(expected = UnsupportedContentTransferEncodingException.class)
public void testSinglePartUnknownEncoding_throwsUnsupportedEncodingException() throws Exception {
MimeMessage msg = parseWithoutRecurse(toStream(
"From: <adam@example.org>\r\n" +
"To: <eva@example.org>\r\n" +
"Subject: Testmail\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-type: text/plain\r\n" +
"Content-Transfer-Encoding: utf-8\r\n" +
"\r\n" +
"dGhpcyBpcyBzb21lIG1vcmUgdGVzdCB0ZXh0Lg==\r\n"));
MimeUtility.decodeBody(msg.getBody());
}
@Test
public void testMultipartSingleLayerRecurse() throws Exception {
MimeMessage msg = parseWithRecurse(toStream(