Move exception error strings out of mail package

This commit is contained in:
Jan Berkel 2014-12-17 15:28:48 +01:00
parent ac33de6310
commit 27e0c75021
8 changed files with 54 additions and 21 deletions

View file

@ -186,10 +186,11 @@ public class AccountSetupCheckSettings extends K9Activity implements OnClickList
} else { } else {
showErrorDialog( showErrorDialog(
R.string.account_setup_failed_dlg_server_message_fmt, R.string.account_setup_failed_dlg_server_message_fmt,
(cve.getMessage() == null ? "" : cve.getMessage())); errorMessageForCertificateException(cve));
} }
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -461,4 +462,15 @@ public class AccountSetupCheckSettings extends K9Activity implements OnClickList
public void dialogCancelled(int dialogId) { public void dialogCancelled(int dialogId) {
// nothing to do here... // nothing to do here...
} }
private String errorMessageForCertificateException(CertificateValidationException e) {
switch (e.getReason()) {
case Expired: return getString(R.string.client_certificate_expired, e.getAlias(), e.getMessage());
case MissingCapability: return getString(R.string.auth_external_error);
case RetrievalFailure: return getString(R.string.client_certificate_retrieval_failure, e.getAlias());
case UseMessage: return e.getMessage();
case Unknown:
default: return "";
}
}
} }

View file

@ -24,5 +24,4 @@ public class CertificateChainException extends CertificateException {
public X509Certificate[] getCertChain() { public X509Certificate[] getCertChain() {
return mCertChain; return mCertChain;
} }
} }

View file

@ -11,23 +11,48 @@ import android.security.KeyChainException;
public class CertificateValidationException extends MessagingException { public class CertificateValidationException extends MessagingException {
public static final long serialVersionUID = -1; public static final long serialVersionUID = -1;
private final Reason mReason;
private X509Certificate[] mCertChain; private X509Certificate[] mCertChain;
private boolean mNeedsUserAttention = false; private boolean mNeedsUserAttention = false;
private String mAlias;
public enum Reason {
Unknown, UseMessage, Expired, MissingCapability, RetrievalFailure
}
public CertificateValidationException(String message) { public CertificateValidationException(String message) {
this(message, Reason.UseMessage, null);
}
public CertificateValidationException(Reason reason) {
this(null, reason, null);
}
public CertificateValidationException(String message, Reason reason, String alias) {
super(message); super(message);
/* /*
* Instances created without a Throwable parameter as a cause are * Instances created without a Throwable parameter as a cause are
* presumed to need user attention. * presumed to need user attention.
*/ */
mNeedsUserAttention = true; mNeedsUserAttention = true;
mReason = reason;
mAlias = alias;
} }
public CertificateValidationException(final String message, Throwable throwable) { public CertificateValidationException(final String message, Throwable throwable) {
super(message, throwable); super(message, throwable);
mReason = Reason.Unknown;
scanForCause(); scanForCause();
} }
public String getAlias() {
return mAlias;
}
public Reason getReason() {
return mReason;
}
private void scanForCause() { private void scanForCause() {
Throwable throwable = getCause(); Throwable throwable = getCause();

View file

@ -20,11 +20,12 @@ import android.security.KeyChain;
import android.security.KeyChainException; import android.security.KeyChainException;
import android.util.Log; import android.util.Log;
import com.fsck.k9.R;
import com.fsck.k9.mail.CertificateValidationException; import com.fsck.k9.mail.CertificateValidationException;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import static com.fsck.k9.mail.K9MailLib.LOG_TAG; import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
import static com.fsck.k9.mail.CertificateValidationException.Reason;
import static com.fsck.k9.mail.CertificateValidationException.Reason.RetrievalFailure;
/** /**
* For client certificate authentication! Provide private keys and certificates * For client certificate authentication! Provide private keys and certificates
@ -61,13 +62,9 @@ class KeyChainKeyManager extends X509ExtendedKeyManager {
mPrivateKey = fetchPrivateKey(context, alias); mPrivateKey = fetchPrivateKey(context, alias);
} catch (KeyChainException e) { } catch (KeyChainException e) {
// The certificate was possibly deleted. Notify user of error. // The certificate was possibly deleted. Notify user of error.
final String message = context.getString( throw new CertificateValidationException(e.getMessage(), RetrievalFailure, alias);
R.string.client_certificate_retrieval_failure, alias);
throw new CertificateValidationException(message, e);
} catch (InterruptedException e) { } catch (InterruptedException e) {
final String message = context.getString( throw new CertificateValidationException(e.getMessage(), RetrievalFailure, alias);
R.string.client_certificate_retrieval_failure, alias);
throw new MessagingException(message, e);
} }
} }
@ -83,8 +80,7 @@ class KeyChainKeyManager extends X509ExtendedKeyManager {
certificate.checkValidity(); certificate.checkValidity();
} }
} catch (CertificateException e) { } catch (CertificateException e) {
// Client certificate has expired or is not yet valid throw new CertificateValidationException(e.getMessage(), Reason.Expired, alias);
throw new CertificateValidationException(context.getString(R.string.client_certificate_expired, alias, e.toString()));
} }
return chain; return chain;

View file

@ -56,7 +56,6 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.R;
import com.fsck.k9.helper.power.TracingPowerManager; import com.fsck.k9.helper.power.TracingPowerManager;
import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock; import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock;
import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.AuthType;
@ -98,6 +97,7 @@ import org.apache.commons.io.IOUtils;
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_IMAP; import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_IMAP;
import static com.fsck.k9.mail.K9MailLib.LOG_TAG; import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
import static com.fsck.k9.mail.K9MailLib.PUSH_WAKE_LOCK_TIMEOUT; import static com.fsck.k9.mail.K9MailLib.PUSH_WAKE_LOCK_TIMEOUT;
import static com.fsck.k9.mail.CertificateValidationException.Reason;
/** /**
* <pre> * <pre>
@ -687,7 +687,7 @@ public class ImapStore extends RemoteStore {
autoconfigureFolders(connection); autoconfigureFolders(connection);
connection.close(); connection.close();
} catch (IOException ioe) { } catch (IOException ioe) {
throw new MessagingException(K9.app.getString(R.string.error_unable_to_connect), ioe); throw new MessagingException("Unable to connect", ioe);
} }
} }
@ -2510,7 +2510,7 @@ public class ImapStore extends RemoteStore {
saslAuthExternal(); saslAuthExternal();
} else { } else {
// Provide notification to user of a problem authenticating using client certificates // Provide notification to user of a problem authenticating using client certificates
throw new CertificateValidationException(K9.app.getString(R.string.auth_external_error)); throw new CertificateValidationException(Reason.MissingCapability);
} }
break; break;

View file

@ -3,12 +3,11 @@ package com.fsck.k9.mail.store;
import android.util.Log; import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.R;
import com.fsck.k9.mail.*; import com.fsck.k9.mail.*;
import com.fsck.k9.mail.filter.Base64; import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.filter.Hex; import com.fsck.k9.mail.filter.Hex;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.CertificateValidationException;
import com.fsck.k9.mail.ssl.TrustedSocketFactory; import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.MessageRetrievalListener; import com.fsck.k9.mail.MessageRetrievalListener;
@ -33,6 +32,7 @@ import java.util.Set;
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_POP3; import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_POP3;
import static com.fsck.k9.mail.K9MailLib.LOG_TAG; import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
import static com.fsck.k9.mail.CertificateValidationException.Reason.MissingCapability;
public class Pop3Store extends RemoteStore { public class Pop3Store extends RemoteStore {
public static final String STORE_TYPE = "POP3"; public static final String STORE_TYPE = "POP3";
@ -369,7 +369,7 @@ public class Pop3Store extends RemoteStore {
authExternal(); authExternal();
} else { } else {
// Provide notification to user of a problem authenticating using client certificates // Provide notification to user of a problem authenticating using client certificates
throw new CertificateValidationException(K9.app.getString(R.string.auth_external_error)); throw new CertificateValidationException(MissingCapability);
} }
break; break;

View file

@ -7,6 +7,7 @@ import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.filter.EOLConvertingOutputStream; import com.fsck.k9.mail.filter.EOLConvertingOutputStream;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.MessageRetrievalListener; import com.fsck.k9.mail.MessageRetrievalListener;
import com.fsck.k9.mail.CertificateValidationException;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.http.*; import org.apache.http.*;

View file

@ -3,8 +3,6 @@ package com.fsck.k9.mail.transport;
import android.util.Log; import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.R;
import com.fsck.k9.mail.*; import com.fsck.k9.mail.*;
import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.Message.RecipientType;
import com.fsck.k9.mail.filter.Base64; import com.fsck.k9.mail.filter.Base64;
@ -13,6 +11,7 @@ import com.fsck.k9.mail.filter.LineWrapOutputStream;
import com.fsck.k9.mail.filter.PeekableInputStream; import com.fsck.k9.mail.filter.PeekableInputStream;
import com.fsck.k9.mail.filter.SmtpDataStuffing; import com.fsck.k9.mail.filter.SmtpDataStuffing;
import com.fsck.k9.mail.internet.CharsetSupport; import com.fsck.k9.mail.internet.CharsetSupport;
import com.fsck.k9.mail.CertificateValidationException;
import com.fsck.k9.mail.store.StoreConfig; import com.fsck.k9.mail.store.StoreConfig;
import com.fsck.k9.mail.ssl.TrustedSocketFactory; import com.fsck.k9.mail.ssl.TrustedSocketFactory;
@ -28,6 +27,7 @@ import java.util.*;
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_SMTP; import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_SMTP;
import static com.fsck.k9.mail.K9MailLib.LOG_TAG; import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
import static com.fsck.k9.mail.CertificateValidationException.Reason.MissingCapability;
public class SmtpTransport extends Transport { public class SmtpTransport extends Transport {
public static final String TRANSPORT_TYPE = "SMTP"; public static final String TRANSPORT_TYPE = "SMTP";
@ -357,7 +357,7 @@ public class SmtpTransport extends Transport {
* EXTERNAL when using client certificates. That way, the * EXTERNAL when using client certificates. That way, the
* user can be notified of a problem during account setup. * user can be notified of a problem during account setup.
*/ */
throw new MessagingException(K9.app.getString(R.string.auth_external_error)); throw new CertificateValidationException(MissingCapability);
} }
break; break;