Better exception handling for import of keys

This commit is contained in:
Dominik Schürmann
2014-07-31 17:11:06 +02:00
parent 1d2c93ca8a
commit 9475285013
9 changed files with 27 additions and 47 deletions

View File

@@ -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));
}

View File

@@ -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().

View File

@@ -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 (IOException e) {
Log.e(Constants.TAG, "IOException on parsing key file! Return NoValidKeysException!", e);
NoValidKeysException e1 = new NoValidKeysException();
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>
(mData, e1);
} catch (Exception e) {
Log.e(Constants.TAG, "Exception on parsing key file!", e);
try {
bufferedInput.reset();
} catch (IOException e1) {
}
Log.d(Constants.TAG, "Last 1024 byte input data: " + convertStreamToString(bufferedInput));
Log.e(Constants.TAG, "Other Exception on parsing key file!", e);
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mData, e);
}
if (isEmpty) {
FileHasNoContentException e = new FileHasNoContentException();
Log.e(Constants.TAG, "File has no content!", 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() : "";
}
}