Handle e-mails with unsupported encoding

This commit is contained in:
Philip Whitehouse 2016-11-21 12:14:23 +00:00
parent 4d61ca8f40
commit d220b29a2b
5 changed files with 32 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/*") ||
@ -59,6 +59,8 @@ public class MessageExtractor {
} else {
throw new MessagingException("Provided invalid part: " + part);
}
} catch (UnsupportedEncodingException e) {
Log.e(LOG_TAG, "Unable to getTextFromPart", e);
} catch (IOException e) {
Log.e(LOG_TAG, "Unable to getTextFromPart", e);
} catch (MessagingException e) {
@ -68,7 +70,7 @@ public class MessageExtractor {
}
private static String getTextFromTextPart(Part part, Body body, String mimeType, long textSizeLimit)
throws IOException, MessagingException {
throws IOException, MessagingException, UnsupportedEncodingException {
/*
* We've got a text part, so let's see if it needs to be processed further.
*/

View file

@ -1018,7 +1018,7 @@ public class MimeUtility {
* The ultimate goal is to get to a point where all classes retain the original data and {@code RawDataBody} can be
* merged into {@link Body}.
*/
public static InputStream decodeBody(Body body) throws MessagingException {
public static InputStream decodeBody(Body body) throws MessagingException, UnsupportedEncodingException {
InputStream inputStream;
if (body instanceof RawDataBody) {
RawDataBody rawDataBody = (RawDataBody) body;
@ -1044,7 +1044,7 @@ public class MimeUtility {
}
};
} else {
throw new UnsupportedOperationException("Encoding for RawDataBody not supported: " + encoding);
throw new UnsupportedEncodingException(encoding);
}
} else {
inputStream = body.getInputStream();

View file

@ -0,0 +1,7 @@
package com.fsck.k9.mail.internet;
public class UnsupportedEncodingException extends Exception {
public UnsupportedEncodingException(String encoding) {
super("Unsupported encoding: "+encoding);
}
}

View file

@ -128,6 +128,23 @@ public class MimeMessageParseTest {
"");
}
@Test(expected = UnsupportedEncodingException.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"));
checkAddresses(msg.getFrom(), "adam@example.org");
checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org");
streamToString(MimeUtility.decodeBody(msg.getBody()));
}
@Test
public void testMultipartSingleLayerRecurse() throws Exception {
MimeMessage msg = parseWithRecurse(toStream(

View file

@ -32,6 +32,7 @@ import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.MimeMultipart;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mail.internet.TextBody;
import com.fsck.k9.mail.internet.UnsupportedEncodingException;
import com.fsck.k9.message.MessageBuilder.Callback;
import com.fsck.k9.view.RecipientSelectView.Recipient;
import org.apache.commons.io.IOUtils;
@ -507,7 +508,7 @@ public class PgpMessageBuilderTest {
InputStream inputStream = MimeUtility.decodeBody(signatureBodyPart.getBody());
IOUtils.copy(inputStream, bos);
Assert.assertEquals(reason, expected, new String(bos.toByteArray()));
} catch (IOException | MessagingException e) {
} catch (IOException | MessagingException | UnsupportedEncodingException e) {
Assert.fail();
}
}