Merge branch 'master' of github.com:open-keychain/open-keychain

This commit is contained in:
Dominik Schürmann 2014-05-31 20:54:30 +02:00
commit 39997a802e
7 changed files with 57 additions and 66 deletions

View File

@ -166,12 +166,12 @@ public class HkpKeyserver extends Keyserver {
mPort = port; mPort = port;
} }
private String query(String request) throws QueryException, HttpError { private String query(String request) throws QueryFailedException, HttpError {
InetAddress ips[]; InetAddress ips[];
try { try {
ips = InetAddress.getAllByName(mHost); ips = InetAddress.getAllByName(mHost);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw new QueryException(e.toString()); throw new QueryFailedException(e.toString());
} }
for (int i = 0; i < ips.length; ++i) { for (int i = 0; i < ips.length; ++i) {
try { try {
@ -196,16 +196,16 @@ public class HkpKeyserver extends Keyserver {
} }
} }
throw new QueryException("querying server(s) for '" + mHost + "' failed"); throw new QueryFailedException("querying server(s) for '" + mHost + "' failed");
} }
@Override @Override
public ArrayList<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses, public ArrayList<ImportKeysListEntry> search(String query) throws QueryFailedException,
InsufficientQuery { QueryNeedsRepairException {
ArrayList<ImportKeysListEntry> results = new ArrayList<ImportKeysListEntry>(); ArrayList<ImportKeysListEntry> results = new ArrayList<ImportKeysListEntry>();
if (query.length() < 3) { if (query.length() < 3) {
throw new InsufficientQuery(); throw new QueryTooShortException();
} }
String encodedQuery; String encodedQuery;
@ -226,12 +226,12 @@ public class HkpKeyserver extends Keyserver {
if (e.getData().toLowerCase(Locale.US).contains("no keys found")) { if (e.getData().toLowerCase(Locale.US).contains("no keys found")) {
return results; return results;
} else if (e.getData().toLowerCase(Locale.US).contains("too many")) { } else if (e.getData().toLowerCase(Locale.US).contains("too many")) {
throw new TooManyResponses(); throw new TooManyResponsesException();
} else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) { } else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) {
throw new InsufficientQuery(); throw new QueryTooShortException();
} }
} }
throw new QueryException("querying server(s) for '" + mHost + "' failed"); throw new QueryFailedException("querying server(s) for '" + mHost + "' failed");
} }
final Matcher matcher = PUB_KEY_LINE.matcher(data); final Matcher matcher = PUB_KEY_LINE.matcher(data);
@ -287,7 +287,7 @@ public class HkpKeyserver extends Keyserver {
} }
@Override @Override
public String get(String keyIdHex) throws QueryException { public String get(String keyIdHex) throws QueryFailedException {
HttpClient client = new DefaultHttpClient(); HttpClient client = new DefaultHttpClient();
try { try {
String query = "http://" + mHost + ":" + mPort + String query = "http://" + mHost + ":" + mPort +
@ -296,7 +296,7 @@ public class HkpKeyserver extends Keyserver {
HttpGet get = new HttpGet(query); HttpGet get = new HttpGet(query);
HttpResponse response = client.execute(get); HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new QueryException("not found"); throw new QueryFailedException("not found");
} }
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();

View File

@ -34,8 +34,8 @@ public class KeybaseKeyserver extends Keyserver {
private String mQuery; private String mQuery;
@Override @Override
public ArrayList<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses, public ArrayList<ImportKeysListEntry> search(String query) throws QueryFailedException,
InsufficientQuery { QueryNeedsRepairException {
ArrayList<ImportKeysListEntry> results = new ArrayList<ImportKeysListEntry>(); ArrayList<ImportKeysListEntry> results = new ArrayList<ImportKeysListEntry>();
if (query.startsWith("0x")) { if (query.startsWith("0x")) {
@ -65,13 +65,13 @@ public class KeybaseKeyserver extends Keyserver {
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(Constants.TAG, "keybase result parsing error", e); Log.e(Constants.TAG, "keybase result parsing error", e);
throw new QueryException("Unexpected structure in keybase search result: " + e.getMessage()); throw new QueryFailedException("Unexpected structure in keybase search result: " + e.getMessage());
} }
return results; return results;
} }
private JSONObject getUser(String keybaseId) throws QueryException { private JSONObject getUser(String keybaseId) throws QueryFailedException {
try { try {
return getFromKeybase("_/api/1.0/user/lookup.json?username=", keybaseId); return getFromKeybase("_/api/1.0/user/lookup.json?username=", keybaseId);
} catch (Exception e) { } catch (Exception e) {
@ -79,11 +79,12 @@ public class KeybaseKeyserver extends Keyserver {
if (keybaseId != null) { if (keybaseId != null) {
detail = ". Query was for user '" + keybaseId + "'"; detail = ". Query was for user '" + keybaseId + "'";
} }
throw new QueryException(e.getMessage() + detail); throw new QueryFailedException(e.getMessage() + detail);
} }
} }
private ImportKeysListEntry makeEntry(JSONObject match) throws QueryException, JSONException { private ImportKeysListEntry makeEntry(JSONObject match) throws QueryFailedException, JSONException {
final ImportKeysListEntry entry = new ImportKeysListEntry(); final ImportKeysListEntry entry = new ImportKeysListEntry();
entry.setQuery(mQuery); entry.setQuery(mQuery);
@ -127,7 +128,7 @@ public class KeybaseKeyserver extends Keyserver {
return entry; return entry;
} }
private JSONObject getFromKeybase(String path, String query) throws QueryException { private JSONObject getFromKeybase(String path, String query) throws QueryFailedException {
try { try {
String url = "https://keybase.io/" + path + URLEncoder.encode(query, "utf8"); String url = "https://keybase.io/" + path + URLEncoder.encode(query, "utf8");
Log.d(Constants.TAG, "keybase query: " + url); Log.d(Constants.TAG, "keybase query: " + url);
@ -143,29 +144,29 @@ public class KeybaseKeyserver extends Keyserver {
try { try {
JSONObject json = new JSONObject(text); JSONObject json = new JSONObject(text);
if (JWalk.getInt(json, "status", "code") != 0) { if (JWalk.getInt(json, "status", "code") != 0) {
throw new QueryException("Keybase autocomplete search failed"); throw new QueryFailedException("Keybase autocomplete search failed");
} }
return json; return json;
} catch (JSONException e) { } catch (JSONException e) {
throw new QueryException("Keybase.io query returned broken JSON"); throw new QueryFailedException("Keybase.io query returned broken JSON");
} }
} else { } else {
String message = readAll(conn.getErrorStream(), conn.getContentEncoding()); String message = readAll(conn.getErrorStream(), conn.getContentEncoding());
throw new QueryException("Keybase.io query error (status=" + response + throw new QueryFailedException("Keybase.io query error (status=" + response +
"): " + message); "): " + message);
} }
} catch (Exception e) { } catch (Exception e) {
throw new QueryException("Keybase.io query error"); throw new QueryFailedException("Keybase.io query error");
} }
} }
@Override @Override
public String get(String id) throws QueryException { public String get(String id) throws QueryFailedException {
try { try {
JSONObject user = getUser(id); JSONObject user = getUser(id);
return JWalk.getString(user, "them", "public_keys", "primary", "bundle"); return JWalk.getString(user, "them", "public_keys", "primary", "bundle");
} catch (Exception e) { } catch (Exception e) {
throw new QueryException(e.getMessage()); throw new QueryFailedException(e.getMessage());
} }
} }

View File

@ -24,19 +24,23 @@ import java.io.InputStream;
import java.util.List; import java.util.List;
public abstract class Keyserver { public abstract class Keyserver {
public static class QueryException extends Exception { public static class QueryFailedException extends Exception {
private static final long serialVersionUID = 2703768928624654512L; private static final long serialVersionUID = 2703768928624654512L;
public QueryException(String message) { public QueryFailedException(String message) {
super(message); super(message);
} }
} }
public static class TooManyResponses extends Exception { public static class QueryNeedsRepairException extends Exception {
private static final long serialVersionUID = 2693768928624654512L;
}
public static class TooManyResponsesException extends QueryNeedsRepairException {
private static final long serialVersionUID = 2703768928624654513L; private static final long serialVersionUID = 2703768928624654513L;
} }
public static class InsufficientQuery extends Exception { public static class QueryTooShortException extends QueryNeedsRepairException {
private static final long serialVersionUID = 2703768928624654514L; private static final long serialVersionUID = 2703768928624654514L;
} }
@ -44,10 +48,10 @@ public abstract class Keyserver {
private static final long serialVersionUID = -507574859137295530L; private static final long serialVersionUID = -507574859137295530L;
} }
abstract List<ImportKeysListEntry> search(String query) throws QueryException, TooManyResponses, abstract List<ImportKeysListEntry> search(String query) throws QueryFailedException,
InsufficientQuery; QueryNeedsRepairException;
abstract String get(String keyIdHex) throws QueryException; abstract String get(String keyIdHex) throws QueryFailedException;
abstract void add(String armoredKey) throws AddKeyException; abstract void add(String armoredKey) throws AddKeyException;

View File

@ -273,37 +273,29 @@ public class ImportKeysListFragment extends ListFragment implements
break; break;
case LOADER_ID_SERVER_QUERY: case LOADER_ID_SERVER_QUERY:
if (error == null) {
AppMsg.makeText(
getActivity(), getResources().getQuantityString(R.plurals.keys_found,
mAdapter.getCount(), mAdapter.getCount()),
AppMsg.STYLE_INFO
).show();
} else if (error instanceof Keyserver.InsufficientQuery) {
AppMsg.makeText(getActivity(), R.string.error_keyserver_insufficient_query,
AppMsg.STYLE_ALERT).show();
} else if (error instanceof Keyserver.QueryException) {
AppMsg.makeText(getActivity(), R.string.error_keyserver_query,
AppMsg.STYLE_ALERT).show();
} else if (error instanceof Keyserver.TooManyResponses) {
AppMsg.makeText(getActivity(), R.string.error_keyserver_too_many_responses,
AppMsg.STYLE_ALERT).show();
}
break;
case LOADER_ID_KEYBASE: case LOADER_ID_KEYBASE:
// TODO: possibly fine-tune message building for these two cases
if (error == null) { if (error == null) {
AppMsg.makeText( AppMsg.makeText(
getActivity(), getResources().getQuantityString(R.plurals.keys_found, getActivity(), getResources().getQuantityString(R.plurals.keys_found,
mAdapter.getCount(), mAdapter.getCount()), mAdapter.getCount(), mAdapter.getCount()),
AppMsg.STYLE_INFO AppMsg.STYLE_INFO
).show(); ).show();
} else if (error instanceof Keyserver.QueryException) { } else if (error instanceof Keyserver.QueryTooShortException) {
AppMsg.makeText(getActivity(), R.string.error_keyserver_query, AppMsg.makeText(getActivity(), R.string.error_keyserver_insufficient_query,
AppMsg.STYLE_ALERT).show(); AppMsg.STYLE_ALERT).show();
} else if (error instanceof Keyserver.TooManyResponsesException) {
AppMsg.makeText(getActivity(), R.string.error_keyserver_too_many_responses,
AppMsg.STYLE_ALERT).show();
} else if (error instanceof Keyserver.QueryFailedException) {
Log.d(Constants.TAG,
"Unrecoverable keyserver query error: " + error.getLocalizedMessage());
String alert = getActivity().getString(R.string.error_searching_keys);
alert = alert + " (" + error.getLocalizedMessage() + ")";
AppMsg.makeText(getActivity(), alert, AppMsg.STYLE_ALERT).show();
} }
break;
default: default:
break; break;

View File

@ -94,14 +94,10 @@ public class ImportKeysListKeybaseLoader
mEntryList.addAll(searchResult); mEntryList.addAll(searchResult);
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, null); mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, null);
} catch (Keyserver.InsufficientQuery e) { } catch (Keyserver.QueryFailedException e) {
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e); mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e);
} catch (Keyserver.QueryException e) { } catch (Keyserver.QueryNeedsRepairException e) {
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e);
} catch (Keyserver.TooManyResponses e) {
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e); mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e);
} }
} }
} }

View File

@ -116,11 +116,9 @@ public class ImportKeysListServerLoader
mEntryList.addAll(searchResult); mEntryList.addAll(searchResult);
} }
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, null); mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, null);
} catch (Keyserver.InsufficientQuery e) { } catch (Keyserver.QueryFailedException e) {
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e); mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e);
} catch (Keyserver.QueryException e) { } catch (Keyserver.QueryNeedsRepairException e) {
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e);
} catch (Keyserver.TooManyResponses e) {
mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e); mEntryListWrapper = new AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>(mEntryList, e);
} }
} }

View File

@ -295,9 +295,9 @@
<string name="error_jelly_bean_needed">You need Android 4.1 to use Android\'s NFC Beam feature!</string> <string name="error_jelly_bean_needed">You need Android 4.1 to use Android\'s NFC Beam feature!</string>
<string name="error_nfc_needed">NFC is not available on your device!</string> <string name="error_nfc_needed">NFC is not available on your device!</string>
<string name="error_nothing_import">Nothing to import!</string> <string name="error_nothing_import">Nothing to import!</string>
<string name="error_keyserver_insufficient_query">Insufficient server query</string> <string name="error_keyserver_insufficient_query">Key search query too short</string>
<string name="error_keyserver_query">Querying keyserver failed</string> <string name="error_searching_keys">Unrecoverable error searching for keys at server</string>
<string name="error_keyserver_too_many_responses">Too many possible keys. Please refine your query!</string> <string name="error_keyserver_too_many_responses">Key search query returned too many candidates; Please refine query</string>
<string name="error_import_file_no_content">File has no content</string> <string name="error_import_file_no_content">File has no content</string>
<string name="error_generic_report_bug">A generic error occurred, please create a new bug report for OpenKeychain.</string> <string name="error_generic_report_bug">A generic error occurred, please create a new bug report for OpenKeychain.</string>
<plurals name="error_import_non_pgp_part"> <plurals name="error_import_non_pgp_part">