don't throw MessagingExceptions for getting headers from parts

This commit is contained in:
Vincent Breitmoser 2016-07-27 19:21:48 +02:00
parent 2b5064ea55
commit f87a9cad49
20 changed files with 120 additions and 173 deletions

View file

@ -77,7 +77,7 @@ public abstract class Message implements Part, CompositeBody {
public abstract String getSubject();
public abstract void setSubject(String subject) throws MessagingException;
public abstract void setSubject(String subject);
public Date getInternalDate() {
return mInternalDate;
@ -89,14 +89,13 @@ public abstract class Message implements Part, CompositeBody {
public abstract Date getSentDate();
public abstract void setSentDate(Date sentDate, boolean hideTimeZone) throws MessagingException;
public abstract void setSentDate(Date sentDate, boolean hideTimeZone);
public abstract Address[] getRecipients(RecipientType type);
public abstract void setRecipients(RecipientType type, Address[] addresses)
throws MessagingException;
public abstract void setRecipients(RecipientType type, Address[] addresses);
public void setRecipient(RecipientType type, Address address) throws MessagingException {
public void setRecipient(RecipientType type, Address address) {
setRecipients(type, new Address[] {
address
});
@ -104,40 +103,40 @@ public abstract class Message implements Part, CompositeBody {
public abstract Address[] getFrom();
public abstract void setFrom(Address from) throws MessagingException;
public abstract void setFrom(Address from);
public abstract Address[] getReplyTo();
public abstract void setReplyTo(Address[] from) throws MessagingException;
public abstract void setReplyTo(Address[] from);
public abstract String getMessageId() throws MessagingException;
public abstract String getMessageId();
public abstract void setInReplyTo(String inReplyTo) throws MessagingException;
public abstract void setInReplyTo(String inReplyTo);
public abstract String[] getReferences() throws MessagingException;
public abstract String[] getReferences();
public abstract void setReferences(String references) throws MessagingException;
public abstract void setReferences(String references);
@Override
public abstract Body getBody();
@Override
public abstract void addHeader(String name, String value) throws MessagingException;
public abstract void addHeader(String name, String value);
@Override
public abstract void addRawHeader(String name, String raw) throws MessagingException;
public abstract void addRawHeader(String name, String raw);
@Override
public abstract void setHeader(String name, String value) throws MessagingException;
public abstract void setHeader(String name, String value);
@NonNull
@Override
public abstract String[] getHeader(String name) throws MessagingException;
public abstract String[] getHeader(String name);
public abstract Set<String> getHeaderNames() throws MessagingException;
public abstract Set<String> getHeaderNames();
@Override
public abstract void removeHeader(String name) throws MessagingException;
public abstract void removeHeader(String name);
@Override
public abstract void setBody(Body body);

View file

@ -9,13 +9,13 @@ import android.support.annotation.NonNull;
public interface Part {
void addHeader(String name, String value) throws MessagingException;
void addHeader(String name, String value);
void addRawHeader(String name, String raw) throws MessagingException;
void addRawHeader(String name, String raw);
void removeHeader(String name) throws MessagingException;
void removeHeader(String name);
void setHeader(String name, String value) throws MessagingException;
void setHeader(String name, String value);
Body getBody();
@ -29,7 +29,7 @@ public interface Part {
* Returns an array of headers of the given name. The array may be empty.
*/
@NonNull
String[] getHeader(String name) throws MessagingException;
String[] getHeader(String name);
boolean isMimeType(String mimeType);

View file

@ -48,7 +48,7 @@ class JisSupport {
}
private static String getJisVariantFromMailerHeaders(Message message) throws MessagingException {
private static String getJisVariantFromMailerHeaders(Message message) {
String[] mailerHeaders = message.getHeader("X-Mailer");
if (mailerHeaders.length == 0)
return null;

View file

@ -7,12 +7,9 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.net.MailTo;
import android.util.Log;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
/**
@ -40,7 +37,7 @@ public class ListHeaders {
public static Address[] getListPostAddresses(Message message) {
String[] headerValues = getHeaderValues(message, LIST_POST_HEADER);
String[] headerValues = message.getHeader(LIST_POST_HEADER);
if (headerValues.length < 1) {
return new Address[0];
}
@ -56,15 +53,6 @@ public class ListHeaders {
return listPostAddresses.toArray(new Address[listPostAddresses.size()]);
}
private static String[] getHeaderValues(Message message, String headerName) {
try {
return message.getHeader(headerName);
} catch (MessagingException e) {
Log.e(K9MailLib.LOG_TAG, "Unable to get " + headerName + " header", e);
return new String[0];
}
}
private static Address extractAddress(String headerValue) {
if (headerValue == null || headerValue.isEmpty()) {
return null;

View file

@ -49,7 +49,7 @@ public class MimeBodyPart extends BodyPart {
}
@Override
public void addHeader(String name, String value) throws MessagingException {
public void addHeader(String name, String value) {
mHeader.addHeader(name, value);
}
@ -65,12 +65,12 @@ public class MimeBodyPart extends BodyPart {
@NonNull
@Override
public String[] getHeader(String name) throws MessagingException {
public String[] getHeader(String name) {
return mHeader.getHeader(name);
}
@Override
public void removeHeader(String name) throws MessagingException {
public void removeHeader(String name) {
mHeader.removeHeader(name);
}

View file

@ -140,7 +140,7 @@ public class MimeMessage extends Message {
* @param sentDate
* @throws com.fsck.k9.mail.MessagingException
*/
public void addSentDate(Date sentDate, boolean hideTimeZone) throws MessagingException {
public void addSentDate(Date sentDate, boolean hideTimeZone) {
if (mDateFormat == null) {
mDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
}
@ -154,7 +154,7 @@ public class MimeMessage extends Message {
}
@Override
public void setSentDate(Date sentDate, boolean hideTimeZone) throws MessagingException {
public void setSentDate(Date sentDate, boolean hideTimeZone) {
removeHeader("Date");
addSentDate(sentDate, hideTimeZone);
}
@ -225,7 +225,7 @@ public class MimeMessage extends Message {
}
@Override
public void setRecipients(RecipientType type, Address[] addresses) throws MessagingException {
public void setRecipients(RecipientType type, Address[] addresses) {
if (type == RecipientType.TO) {
if (addresses == null || addresses.length == 0) {
removeHeader("To");
@ -251,7 +251,7 @@ public class MimeMessage extends Message {
this.mBcc = addresses;
}
} else {
throw new MessagingException("Unrecognized recipient type.");
throw new IllegalStateException("Unrecognized recipient type.");
}
}
@ -264,7 +264,7 @@ public class MimeMessage extends Message {
}
@Override
public void setSubject(String subject) throws MessagingException {
public void setSubject(String subject) {
setHeader("Subject", subject);
}
@ -281,7 +281,7 @@ public class MimeMessage extends Message {
}
@Override
public void setFrom(Address from) throws MessagingException {
public void setFrom(Address from) {
if (from != null) {
setHeader("From", from.toEncodedString());
this.mFrom = new Address[] {
@ -301,7 +301,7 @@ public class MimeMessage extends Message {
}
@Override
public void setReplyTo(Address[] replyTo) throws MessagingException {
public void setReplyTo(Address[] replyTo) {
if (replyTo == null || replyTo.length == 0) {
removeHeader("Reply-to");
mReplyTo = null;
@ -312,7 +312,7 @@ public class MimeMessage extends Message {
}
@Override
public String getMessageId() throws MessagingException {
public String getMessageId() {
if (mMessageId == null) {
mMessageId = getFirstHeader("Message-ID");
}
@ -340,18 +340,18 @@ public class MimeMessage extends Message {
setMessageId(messageId);
}
public void setMessageId(String messageId) throws MessagingException {
public void setMessageId(String messageId) {
setHeader("Message-ID", messageId);
mMessageId = messageId;
}
@Override
public void setInReplyTo(String inReplyTo) throws MessagingException {
public void setInReplyTo(String inReplyTo) {
setHeader("In-Reply-To", inReplyTo);
}
@Override
public String[] getReferences() throws MessagingException {
public String[] getReferences() {
if (mReferences == null) {
mReferences = getHeader("References");
}
@ -359,7 +359,7 @@ public class MimeMessage extends Message {
}
@Override
public void setReferences(String references) throws MessagingException {
public void setReferences(String references) {
/*
* Make sure the References header doesn't exceed the maximum header
* line length and won't get (Q-)encoded later. Otherwise some clients
@ -410,7 +410,7 @@ public class MimeMessage extends Message {
}
@Override
public void addHeader(String name, String value) throws MessagingException {
public void addHeader(String name, String value) {
mHeader.addHeader(name, value);
}
@ -420,23 +420,23 @@ public class MimeMessage extends Message {
}
@Override
public void setHeader(String name, String value) throws MessagingException {
public void setHeader(String name, String value) {
mHeader.setHeader(name, value);
}
@NonNull
@Override
public String[] getHeader(String name) throws MessagingException {
public String[] getHeader(String name) {
return mHeader.getHeader(name);
}
@Override
public void removeHeader(String name) throws MessagingException {
public void removeHeader(String name) {
mHeader.removeHeader(name);
}
@Override
public Set<String> getHeaderNames() throws MessagingException {
public Set<String> getHeaderNames() {
return mHeader.getHeaderNames();
}
@ -482,7 +482,7 @@ public class MimeMessage extends Message {
}
private class MimeMessageBuilder implements ContentHandler {
private final LinkedList<Object> stack = new LinkedList<Object>();
private final LinkedList<Object> stack = new LinkedList<>();
public MimeMessageBuilder() {
}
@ -543,12 +543,8 @@ public class MimeMessage extends Message {
@Override
public void body(BodyDescriptor bd, InputStream in) throws IOException, MimeException {
expect(Part.class);
try {
Body body = MimeUtility.createBody(in, bd.getTransferEncoding(), bd.getMimeType());
((Part)stack.peek()).setBody(body);
} catch (MessagingException me) {
throw new MimeException(me.getMessage(), me);
}
Body body = MimeUtility.createBody(in, bd.getTransferEncoding(), bd.getMimeType());
((Part)stack.peek()).setBody(body);
}
@Override
@ -613,13 +609,9 @@ public class MimeMessage extends Message {
@Override
public void field(Field parsedField) throws MimeException {
expect(Part.class);
try {
String name = parsedField.getName();
String raw = parsedField.getRaw().toString();
((Part) stack.peek()).addRawHeader(name, raw);
} catch (MessagingException me) {
throw new MimeException(me);
}
String name = parsedField.getName();
String raw = parsedField.getRaw().toString();
((Part) stack.peek()).addRawHeader(name, raw);
}
}

View file

@ -954,7 +954,7 @@ public class MimeUtility {
return null;
}
public static Part findFirstPartByMimeType(Part part, String mimeType) throws MessagingException {
public static Part findFirstPartByMimeType(Part part, String mimeType) {
if (part.getBody() instanceof Multipart) {
Multipart multipart = (Multipart)part.getBody();
for (BodyPart bodyPart : multipart.getBodyParts()) {
@ -986,7 +986,7 @@ public class MimeUtility {
}
public static Body createBody(InputStream in, String contentTransferEncoding, String contentType)
throws IOException, MessagingException {
throws IOException {
if (contentTransferEncoding != null) {
contentTransferEncoding = MimeUtility.getHeaderParameter(contentTransferEncoding, null);

View file

@ -48,11 +48,7 @@ public class MessageHeaderParser {
public void field(Field rawField) throws MimeException {
String name = rawField.getName();
String raw = rawField.getRaw().toString();
try {
part.addRawHeader(name, raw);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
part.addRawHeader(name, raw);
}
@Override

View file

@ -18,8 +18,7 @@ class FetchPartCallback implements ImapResponseCallback {
}
@Override
public Object foundLiteral(ImapResponse response,
FixedLengthInputStream literal) throws MessagingException, IOException {
public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws IOException {
if (response.getTag() == null &&
ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) {
//TODO: check for correct UID
@ -29,8 +28,7 @@ class FetchPartCallback implements ImapResponseCallback {
String contentType = mPart
.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
return MimeUtility.createBody(literal, contentTransferEncoding,
contentType);
return MimeUtility.createBody(literal, contentTransferEncoding, contentType);
}
return null;
}

View file

@ -1256,7 +1256,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
attachmentPresenter.processMessageToForward(messageViewInfo);
}
private void processDraftMessage(MessageViewInfo messageViewInfo) throws MessagingException {
private void processDraftMessage(MessageViewInfo messageViewInfo) {
Message message = messageViewInfo.message;
mDraftId = MessagingController.getInstance(getApplication()).getId(message);
mSubjectView.setText(message.getSubject());

View file

@ -310,11 +310,8 @@ public class AttachmentPresenter {
* The recursion depth. Currently unused.
*
* @return {@code true} if all attachments were able to be attached, {@code false} otherwise.
*
* @throws MessagingException
* In case of an error
*/
public boolean loadAttachments(Part part, int depth) throws MessagingException {
public boolean loadAttachments(Part part, int depth) {
if (part.getBody() instanceof Multipart) {
Multipart mp = (Multipart) part.getBody();
boolean ret = true;

View file

@ -3806,47 +3806,43 @@ public class MessagingController implements Runnable {
Log.d(K9.LOG_TAG, "Got message " + account.getDescription() + ":" + message.getFolder()
+ ":" + message.getUid() + " for sendAlternate");
try {
Intent msg = new Intent(Intent.ACTION_SEND);
String quotedText = null;
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
quotedText = MessageExtractor.getTextFromPart(part);
}
if (quotedText != null) {
msg.putExtra(Intent.EXTRA_TEXT, quotedText);
}
msg.putExtra(Intent.EXTRA_SUBJECT, message.getSubject());
Address[] from = message.getFrom();
String[] senders = new String[from.length];
for (int i = 0; i < from.length; i++) {
senders[i] = from[i].toString();
}
msg.putExtra(Intents.Share.EXTRA_FROM, senders);
Address[] to = message.getRecipients(RecipientType.TO);
String[] recipientsTo = new String[to.length];
for (int i = 0; i < to.length; i++) {
recipientsTo[i] = to[i].toString();
}
msg.putExtra(Intent.EXTRA_EMAIL, recipientsTo);
Address[] cc = message.getRecipients(RecipientType.CC);
String[] recipientsCc = new String[cc.length];
for (int i = 0; i < cc.length; i++) {
recipientsCc[i] = cc[i].toString();
}
msg.putExtra(Intent.EXTRA_CC, recipientsCc);
msg.setType("text/plain");
context.startActivity(Intent.createChooser(msg, context.getString(R.string.send_alternate_chooser_title)));
} catch (MessagingException me) {
Log.e(K9.LOG_TAG, "Unable to send email through alternate program", me);
Intent msg = new Intent(Intent.ACTION_SEND);
String quotedText = null;
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
quotedText = MessageExtractor.getTextFromPart(part);
}
if (quotedText != null) {
msg.putExtra(Intent.EXTRA_TEXT, quotedText);
}
msg.putExtra(Intent.EXTRA_SUBJECT, message.getSubject());
Address[] from = message.getFrom();
String[] senders = new String[from.length];
for (int i = 0; i < from.length; i++) {
senders[i] = from[i].toString();
}
msg.putExtra(Intents.Share.EXTRA_FROM, senders);
Address[] to = message.getRecipients(RecipientType.TO);
String[] recipientsTo = new String[to.length];
for (int i = 0; i < to.length; i++) {
recipientsTo[i] = to[i].toString();
}
msg.putExtra(Intent.EXTRA_EMAIL, recipientsTo);
Address[] cc = message.getRecipients(RecipientType.CC);
String[] recipientsCc = new String[cc.length];
for (int i = 0; i < cc.length; i++) {
recipientsCc[i] = cc[i].toString();
}
msg.putExtra(Intent.EXTRA_CC, recipientsCc);
msg.setType("text/plain");
context.startActivity(Intent.createChooser(msg, context.getString(R.string.send_alternate_chooser_title)));
}
/**

View file

@ -33,17 +33,12 @@ public class CryptoHelper {
*/
public boolean isEncrypted(Message message) {
String data = null;
try {
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
data = MessageExtractor.getTextFromPart(part);
}
} catch (MessagingException e) {
// guess not...
// TODO: maybe log this?
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
data = MessageExtractor.getTextFromPart(part);
}
if (data == null) {
@ -56,17 +51,12 @@ public class CryptoHelper {
public boolean isSigned(Message message) {
String data = null;
try {
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
data = MessageExtractor.getTextFromPart(part);
}
} catch (MessagingException e) {
// guess not...
// TODO: maybe log this?
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
if (part == null) {
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
}
if (part != null) {
data = MessageExtractor.getTextFromPart(part);
}
if (data == null) {

View file

@ -333,7 +333,7 @@ public class QuotedMessageHelper {
/** Fetch the body text from a messagePart in the desired messagePart format. This method handles
* conversions between formats (html to text and vice versa) if necessary.
*/
public static String getBodyTextFromMessage(Part messagePart, SimpleMessageFormat format) throws MessagingException {
public static String getBodyTextFromMessage(Part messagePart, SimpleMessageFormat format) {
Part part;
if (format == SimpleMessageFormat.HTML) {
// HTML takes precedence, then text.

View file

@ -20,7 +20,7 @@ abstract class BinaryAttachmentBody implements Body {
protected String mEncoding;
@Override
public abstract InputStream getInputStream() throws MessagingException;
public abstract InputStream getInputStream();
@Override
public void writeTo(OutputStream out) throws IOException, MessagingException {

View file

@ -1507,7 +1507,7 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
return output.toByteArray();
}
private String getTransferEncoding(Part part) throws MessagingException {
private String getTransferEncoding(Part part) {
String[] contentTransferEncoding = part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING);
if (contentTransferEncoding.length > 0) {
return contentTransferEncoding[0].toLowerCase(Locale.US);

View file

@ -2,15 +2,11 @@ package com.fsck.k9.mailstore;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Set;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.util.Log;
import com.fsck.k9.Account;
@ -155,7 +151,7 @@ public class LocalMessage extends MimeMessage {
@Override
public void setSubject(String subject) throws MessagingException {
public void setSubject(String subject) {
mSubject = subject;
}
@ -181,13 +177,13 @@ public class LocalMessage extends MimeMessage {
}
@Override
public void setFrom(Address from) throws MessagingException {
public void setFrom(Address from) {
this.mFrom = new Address[] { from };
}
@Override
public void setReplyTo(Address[] replyTo) throws MessagingException {
public void setReplyTo(Address[] replyTo) {
if (replyTo == null || replyTo.length == 0) {
mReplyTo = null;
} else {
@ -201,7 +197,7 @@ public class LocalMessage extends MimeMessage {
* which removes (expensive) them before adding them
*/
@Override
public void setRecipients(RecipientType type, Address[] addresses) throws MessagingException {
public void setRecipients(RecipientType type, Address[] addresses) {
if (type == RecipientType.TO) {
if (addresses == null || addresses.length == 0) {
this.mTo = null;
@ -221,7 +217,7 @@ public class LocalMessage extends MimeMessage {
this.mBcc = addresses;
}
} else {
throw new MessagingException("Unrecognized recipient type.");
throw new IllegalArgumentException("Unrecognized recipient type.");
}
}

View file

@ -119,15 +119,11 @@ public class MimePartStreamParser {
@Override
public void field(Field parsedField) throws MimeException {
try {
String name = parsedField.getName();
String raw = parsedField.getRaw().toString();
String name = parsedField.getName();
String raw = parsedField.getRaw().toString();
Part part = (Part) stack.peek();
part.addRawHeader(name, raw);
} catch (MessagingException e) {
throw new MimeException(e);
}
Part part = (Part) stack.peek();
part.addRawHeader(name, raw);
}
@Override

View file

@ -21,7 +21,7 @@ public class TempFileBody extends BinaryAttachmentBody implements SizeAware {
}
@Override
public InputStream getInputStream() throws MessagingException {
public InputStream getInputStream() {
try {
return new FileInputStream(mFile);
} catch (FileNotFoundException e) {

View file

@ -176,8 +176,7 @@ public class QuotedMessagePresenter {
populateUIWithQuotedMessage(messageViewInfo, account.isDefaultQuotedTextShown(), action);
}
public void processDraftMessage(MessageViewInfo messageViewInfo, Map<IdentityField, String> k9identity)
throws MessagingException {
public void processDraftMessage(MessageViewInfo messageViewInfo, Map<IdentityField, String> k9identity) {
quoteStyle = k9identity.get(IdentityField.QUOTE_STYLE) != null
? QuoteStyle.valueOf(k9identity.get(IdentityField.QUOTE_STYLE))
: account.getQuoteStyle();
@ -308,8 +307,8 @@ public class QuotedMessagePresenter {
* @param bodyLength Length of reply.
* @param viewMessageContent Update mMessageContentView or not.
*/
private void processSourceMessageText(Part rootMessagePart, int bodyOffset, int bodyLength, boolean viewMessageContent)
throws MessagingException {
private void processSourceMessageText(
Part rootMessagePart, int bodyOffset, int bodyLength, boolean viewMessageContent) {
Part textPart = MimeUtility.findFirstPartByMimeType(rootMessagePart, "text/plain");
if (textPart == null) {
return;