Split different exception scopes

Before this change the initinalization of the trusted key store was
combined in one try-catch-block. If anything went wrong an new in memory
trusted key store was created. Programming against an exception is an
bad pattern.

So the initialization is now splitted into multiple try-catch-blocks
with it's own scopes and the decision if the trusted key store is newly
created in memory or loaded from a exisitng file is done by an if
condition check.

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2023-01-09 13:04:59 +01:00 committed by Marcel Hibbe (Rebase PR Action)
parent 1bbbe20834
commit eabd0b2d2c

View File

@ -54,13 +54,22 @@ public class MagicTrustManager implements X509TrustManager {
private KeyStore trustedKeyStore = null; private KeyStore trustedKeyStore = null;
public MagicTrustManager() { public MagicTrustManager() {
keystoreFile = new File(NextcloudTalkApplication.Companion.getSharedApplication().getDir("CertsKeystore", keystoreFile = new File(NextcloudTalkApplication.Companion.getSharedApplication()
Context.MODE_PRIVATE), "keystore.bks"); .getDir("CertsKeystore", Context.MODE_PRIVATE),
"keystore.bks");
try (FileInputStream fileInputStream = new FileInputStream(keystoreFile)) { try {
trustedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
} catch (KeyStoreException e) {
Log.e(TAG, "Trusted key store can't be created.", e);
}
if (keystoreFile.exists()) {
try (FileInputStream fileInputStream = new FileInputStream(keystoreFile)) {
trustedKeyStore.load(fileInputStream, null); trustedKeyStore.load(fileInputStream, null);
} catch (Exception exception) { } catch (Exception exception) {
Log.e(TAG, "Error during opening the trusted key store.", exception);
}
} else {
try { try {
trustedKeyStore.load(null, null); trustedKeyStore.load(null, null);
} catch (Exception e) { } catch (Exception e) {