aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java
diff options
context:
space:
mode:
authorDominik Schürmann <dominik@dominikschuermann.de>2014-07-31 17:11:06 +0200
committerDominik Schürmann <dominik@dominikschuermann.de>2014-07-31 17:11:06 +0200
commit9475285013accafd24d5bc14da9ba01ca218cbe8 (patch)
tree2a55a2750e67986306be86261db174eb6142d003 /OpenKeychain/src/main/java
parent1d2c93ca8ae4c75e92b5da74e262b9476f22f172 (diff)
downloadopen-keychain-9475285013accafd24d5bc14da9ba01ca218cbe8.tar.gz
open-keychain-9475285013accafd24d5bc14da9ba01ca218cbe8.tar.bz2
open-keychain-9475285013accafd24d5bc14da9ba01ca218cbe8.zip
Better exception handling for import of keys
Diffstat (limited to 'OpenKeychain/src/main/java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java10
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java4
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java48
3 files changed, 21 insertions, 41 deletions
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<UncachedKeyRing> fromStream(InputStream stream)
- throws PgpGeneralException, IOException {
-
+ public static List<UncachedKeyRing> fromStream(InputStream stream) throws IOException {
List<UncachedKeyRing> result = new Vector<UncachedKeyRing>();
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<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> {
- 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<UncachedKeyRing> 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<UncachedKeyRing> 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<ArrayList<ImportKeysListEntry>>(mData, e);
- }
-
- if (isEmpty) {
- FileHasNoContentException e = new FileHasNoContentException();
- Log.e(Constants.TAG, "File has no content!", e);
+ NoValidKeysException e1 = new NoValidKeysException();
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>
- (mData, e);
+ (mData, e1);
+ } catch (Exception e) {
+ Log.e(Constants.TAG, "Other Exception on parsing key file!", e);
+ mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(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() : "";
- }
-
}