From 9475285013accafd24d5bc14da9ba01ca218cbe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 17:11:06 +0200 Subject: Better exception handling for import of keys --- .../keychain/pgp/UncachedKeyRing.java | 10 ++--- .../keychain/ui/ImportKeysListFragment.java | 4 +- .../keychain/ui/adapter/ImportKeysListLoader.java | 48 +++++++--------------- 3 files changed, 21 insertions(+), 41 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java index de8c683ff..0e59b7fdb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -134,9 +134,7 @@ public class UncachedKeyRing { } - public static List fromStream(InputStream stream) - throws PgpGeneralException, IOException { - + public static List fromStream(InputStream stream) throws IOException { List result = new Vector(); while(stream.available() > 0) { @@ -147,8 +145,10 @@ public class UncachedKeyRing { while ((obj = objectFactory.nextObject()) != null) { Log.d(Constants.TAG, "Found class: " + obj.getClass()); if (!(obj instanceof PGPKeyRing)) { - throw new PgpGeneralException( - "Bad object of type " + obj.getClass().getName() + " in stream!"); + Log.d(Constants.TAG, + "Bad object of type " + obj.getClass().getName() + " in stream, proceed with next object..."); + // skip object + continue; } result.add(new UncachedKeyRing((PGPKeyRing) obj)); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index 469601a54..1617a84e4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -287,8 +287,8 @@ public class ImportKeysListFragment extends ListFragment implements if (error == null) { // No error mCachedKeyData = ((ImportKeysListLoader) loader).getParcelableRings(); - } else if (error instanceof ImportKeysListLoader.FileHasNoContentException) { - Notify.showNotify(getActivity(), R.string.error_import_file_no_content, Notify.Style.ERROR); + } else if (error instanceof ImportKeysListLoader.NoValidKeysException) { + Notify.showNotify(getActivity(), R.string.error_import_no_valid_keys, Notify.Style.ERROR); } else if (error instanceof ImportKeysListLoader.NonPgpPartException) { Notify.showNotify(getActivity(), ((ImportKeysListLoader.NonPgpPartException) error).getCount() + " " + getResources(). diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java index 4a8eb0cb1..9996e0381 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java @@ -37,7 +37,7 @@ import java.util.List; public class ImportKeysListLoader extends AsyncTaskLoader>> { - public static class FileHasNoContentException extends Exception { + public static class NoValidKeysException extends Exception { } public static class NonPgpPartException extends Exception { @@ -118,8 +118,6 @@ public class ImportKeysListLoader * @return */ private void generateListOfKeyrings(InputData inputData) { - boolean isEmpty = true; - PositionAwareInputStream progressIn = new PositionAwareInputStream( inputData.getInputStream()); @@ -127,42 +125,24 @@ public class ImportKeysListLoader // PGPObject chunks after the first one, e.g. files with several consecutive ASCII // armor blocks BufferedInputStream bufferedInput = new BufferedInputStream(progressIn); - bufferedInput.mark(1024); try { - - // read all available blocks... (asc files can contain many blocks with BEGIN END) - while (bufferedInput.available() > 0) { - // TODO: deal with non-keyring objects? - List rings = UncachedKeyRing.fromStream(bufferedInput); - for (UncachedKeyRing key : rings) { - ImportKeysListEntry item = new ImportKeysListEntry(getContext(), key); - mData.add(item); - mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(key.getEncoded())); - isEmpty = false; - } + // parse all keyrings + List rings = UncachedKeyRing.fromStream(bufferedInput); + for (UncachedKeyRing key : rings) { + ImportKeysListEntry item = new ImportKeysListEntry(getContext(), key); + mData.add(item); + mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(key.getEncoded())); } - } catch (Exception e) { - Log.e(Constants.TAG, "Exception on parsing key file!", e); + } catch (IOException e) { + Log.e(Constants.TAG, "IOException on parsing key file! Return NoValidKeysException!", e); - try { - bufferedInput.reset(); - } catch (IOException e1) { - } - Log.d(Constants.TAG, "Last 1024 byte input data: " + convertStreamToString(bufferedInput)); - mEntryListWrapper = new AsyncTaskResultWrapper>(mData, e); - } - - if (isEmpty) { - FileHasNoContentException e = new FileHasNoContentException(); - Log.e(Constants.TAG, "File has no content!", e); + NoValidKeysException e1 = new NoValidKeysException(); mEntryListWrapper = new AsyncTaskResultWrapper> - (mData, e); + (mData, e1); + } catch (Exception e) { + Log.e(Constants.TAG, "Other Exception on parsing key file!", e); + mEntryListWrapper = new AsyncTaskResultWrapper>(mData, e); } } - static String convertStreamToString(java.io.InputStream is) { - java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; - } - } -- cgit v1.2.3