diff --git a/src/org/thoughtcrime/securesms/util/DirectoryHelper.java b/src/org/thoughtcrime/securesms/util/DirectoryHelper.java index 9d7b6c1263..558a8ec65d 100644 --- a/src/org/thoughtcrime/securesms/util/DirectoryHelper.java +++ b/src/org/thoughtcrime/securesms/util/DirectoryHelper.java @@ -42,6 +42,7 @@ import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.signalservice.api.SignalServiceAccountManager; import org.whispersystems.signalservice.api.push.ContactTokenDetails; import org.whispersystems.signalservice.api.push.TrustStore; +import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException; import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException; import org.whispersystems.signalservice.internal.contacts.crypto.Quote; import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedQuoteException; @@ -364,10 +365,11 @@ public class DirectoryHelper { Set sanitizedNumbers = sanitizeNumbers(eligibleContactNumbers); List> batches = splitIntoBatches(sanitizedNumbers, CONTACT_DISCOVERY_BATCH_SIZE); List>> futures = new ArrayList<>(batches.size()); + KeyStore iasKeyStore = getIasKeyStore(context); for (Set batch : batches) { Future> future = SignalExecutors.IO.submit(() -> { - return new HashSet<>(accountManager.getRegisteredUsers(getIasKeyStore(context), batch, BuildConfig.MRENCLAVE)); + return new HashSet<>(accountManager.getRegisteredUsers(iasKeyStore, batch, BuildConfig.MRENCLAVE)); }); futures.add(future); } @@ -414,6 +416,9 @@ public class DirectoryHelper { } else if (e.getCause() instanceof PushNetworkException) { Log.w(TAG, "Failed due to poor network.", e); return Optional.absent(); + } else if (e.getCause() instanceof NonSuccessfulResponseCodeException) { + Log.w(TAG, "Failed due to non successful response code.", e); + return Optional.absent(); } else { Log.w(TAG, "Failed for an unknown reason.", e); accountManager.reportContactDiscoveryServiceUnexpectedError(buildErrorReason(e.getCause())); @@ -432,15 +437,17 @@ public class DirectoryHelper { e instanceof Quote.InvalidQuoteFormatException; } - private static KeyStore getIasKeyStore(@NonNull Context context) - throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException - { - TrustStore contactTrustStore = new IasTrustStore(context); + private static KeyStore getIasKeyStore(@NonNull Context context) { + try { + TrustStore contactTrustStore = new IasTrustStore(context); - KeyStore keyStore = KeyStore.getInstance("BKS"); - keyStore.load(contactTrustStore.getKeyStoreInputStream(), contactTrustStore.getKeyStorePassword().toCharArray()); + KeyStore keyStore = KeyStore.getInstance("BKS"); + keyStore.load(contactTrustStore.getKeyStoreInputStream(), contactTrustStore.getKeyStorePassword().toCharArray()); - return keyStore; + return keyStore; + } catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException e) { + throw new AssertionError(e); + } } private static String buildErrorReason(@Nullable Throwable t) {