1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-17 21:32:26 -05:00

remove some more catches

This commit is contained in:
Art O Cathain 2014-09-28 11:59:11 +01:00
parent afb65d5ad7
commit c438bc1222
9 changed files with 151 additions and 204 deletions

View File

@ -3,10 +3,8 @@ package com.fsck.k9.activity.setup;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Locale;
import android.app.AlertDialog;
@ -281,8 +279,8 @@ public class AccountSetupBasics extends K9Activity
URI incomingUri = null;
URI outgoingUri = null;
try {
String userEnc = URLEncoder.encode(user, "UTF-8");
String passwordEnc = URLEncoder.encode(password, "UTF-8");
String userEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(user);
String passwordEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(password);
String incomingUsername = mProvider.incomingUsernameTemplate;
incomingUsername = incomingUsername.replaceAll("\\$email", email);
@ -338,9 +336,6 @@ public class AccountSetupBasics extends K9Activity
}
// Check incoming here. Then check outgoing in onActivityResult()
AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.INCOMING);
} catch (UnsupportedEncodingException enc) {
// This really shouldn't happen since the encoding is hardcoded to UTF-8
Log.e(K9.LOG_TAG, "Couldn't urlencode username or password.", enc);
} catch (URISyntaxException use) {
/*
* If there is some problem with the URI we give up and go on to

View File

@ -0,0 +1,33 @@
package com.fsck.k9.helper;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* Wraps the java.net.URLDecoder to avoid unhelpful checked exceptions.
*/
public class UrlEncodingHelper {
public static String decodeUtf8(String s) {
try {
return URLDecoder.decode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
/*
* This is impossible, UTF-8 is always supported
*/
throw new RuntimeException("UTF-8 not found");
}
}
public static String encodeUtf8(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
/*
* This is impossible, UTF-8 is always supported
*/
throw new RuntimeException("UTF-8 not found");
}
}
}

View File

@ -7,7 +7,6 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@ -16,8 +15,6 @@ import java.net.SocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
@ -199,32 +196,27 @@ public class ImapStore extends Store {
}
if (imapUri.getUserInfo() != null) {
try {
String userinfo = imapUri.getUserInfo();
String[] userInfoParts = userinfo.split(":");
if (userinfo.endsWith(":")) {
// Password is empty. This can only happen after an account was imported.
authenticationType = AuthType.valueOf(userInfoParts[0]);
username = URLDecoder.decode(userInfoParts[1], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[1]);
} else if (userInfoParts.length == 2) {
authenticationType = AuthType.PLAIN;
username = URLDecoder.decode(userInfoParts[0], "UTF-8");
password = URLDecoder.decode(userInfoParts[1], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[0]);
password = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[1]);
} else if (userInfoParts.length == 3) {
authenticationType = AuthType.valueOf(userInfoParts[0]);
username = URLDecoder.decode(userInfoParts[1], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[1]);
if (AuthType.EXTERNAL == authenticationType) {
clientCertificateAlias = URLDecoder.decode(userInfoParts[2], "UTF-8");
clientCertificateAlias = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[2]);
} else {
password = URLDecoder.decode(userInfoParts[2], "UTF-8");
password = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[2]);
}
}
} catch (UnsupportedEncodingException enc) {
// This shouldn't happen since the encoding is hardcoded to UTF-8
throw new IllegalArgumentException("Couldn't urldecode username or password.", enc);
}
}
String path = imapUri.getPath();
@ -261,19 +253,11 @@ public class ImapStore extends Store {
* @see ImapStore#decodeUri(String)
*/
public static String createUri(ServerSettings server) {
String userEnc;
String passwordEnc;
String clientCertificateAliasEnc;
try {
userEnc = URLEncoder.encode(server.username, "UTF-8");
passwordEnc = (server.password != null) ?
URLEncoder.encode(server.password, "UTF-8") : "";
clientCertificateAliasEnc = (server.clientCertificateAlias != null) ?
URLEncoder.encode(server.clientCertificateAlias, "UTF-8") : "";
}
catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Could not encode username or password", e);
}
String userEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.username);
String passwordEnc = (server.password != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.password) : "";
String clientCertificateAliasEnc = (server.clientCertificateAlias != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.clientCertificateAlias) : "";
String scheme;
switch (server.connectionSecurity) {

View File

@ -115,7 +115,6 @@ public class Pop3Store extends Store {
AuthType authType = AuthType.PLAIN;
if (pop3Uri.getUserInfo() != null) {
try {
int userIndex = 0, passwordIndex = 1;
String userinfo = pop3Uri.getUserInfo();
String[] userInfoParts = userinfo.split(":");
@ -126,18 +125,14 @@ public class Pop3Store extends Store {
passwordIndex++;
authType = AuthType.valueOf(userInfoParts[0]);
}
username = URLDecoder.decode(userInfoParts[userIndex], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[userIndex]);
if (userInfoParts.length > passwordIndex) {
if (authType == AuthType.EXTERNAL) {
clientCertificateAlias = URLDecoder.decode(userInfoParts[passwordIndex], "UTF-8");
clientCertificateAlias = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[passwordIndex]);
} else {
password = URLDecoder.decode(userInfoParts[passwordIndex], "UTF-8");
password = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[passwordIndex]);
}
}
} catch (UnsupportedEncodingException enc) {
// This shouldn't happen since the encoding is hardcoded to UTF-8
throw new IllegalArgumentException("Couldn't urldecode username or password.", enc);
}
}
return new ServerSettings(STORE_TYPE, host, port, connectionSecurity, authType, username,
@ -156,19 +151,11 @@ public class Pop3Store extends Store {
* @see Pop3Store#decodeUri(String)
*/
public static String createUri(ServerSettings server) {
String userEnc;
String passwordEnc;
String clientCertificateAliasEnc;
try {
userEnc = URLEncoder.encode(server.username, "UTF-8");
passwordEnc = (server.password != null) ?
URLEncoder.encode(server.password, "UTF-8") : "";
clientCertificateAliasEnc = (server.clientCertificateAlias != null) ?
URLEncoder.encode(server.clientCertificateAlias, "UTF-8") : "";
}
catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Could not encode username or password", e);
}
String userEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.username);
String passwordEnc = (server.password != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.password) : "";
String clientCertificateAliasEnc = (server.clientCertificateAlias != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.clientCertificateAlias) : "";
String scheme;
switch (server.connectionSecurity) {

View File

@ -5,6 +5,7 @@ import android.util.Log;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.controller.MessageRetrievalListener;
import com.fsck.k9.helper.UrlEncodingHelper;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.*;
import com.fsck.k9.mail.filter.EOLConvertingOutputStream;
@ -139,9 +140,8 @@ public class WebDavStore extends Store {
String userInfo = webDavUri.getUserInfo();
if (userInfo != null) {
try {
String[] userInfoParts = userInfo.split(":");
username = URLDecoder.decode(userInfoParts[0], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[0]);
String userParts[] = username.split("\\\\", 2);
if (userParts.length > 1) {
@ -150,11 +150,7 @@ public class WebDavStore extends Store {
alias = username;
}
if (userInfoParts.length > 1) {
password = URLDecoder.decode(userInfoParts[1], "UTF-8");
}
} catch (UnsupportedEncodingException enc) {
// This shouldn't happen since the encoding is hardcoded to UTF-8
throw new IllegalArgumentException("Couldn't urldecode username or password.", enc);
password = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[1]);
}
}
@ -194,16 +190,9 @@ public class WebDavStore extends Store {
* @see WebDavStore#decodeUri(String)
*/
public static String createUri(ServerSettings server) {
String userEnc;
String passwordEnc;
try {
userEnc = URLEncoder.encode(server.username, "UTF-8");
passwordEnc = (server.password != null) ?
URLEncoder.encode(server.password, "UTF-8") : "";
}
catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Could not encode username or password", e);
}
String userEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.username);
String passwordEnc = (server.password != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.password) : "";
String scheme;
switch (server.connectionSecurity) {
@ -479,7 +468,6 @@ public class WebDavStore extends Store {
}
if (folderSlash > 0) {
String folderName;
String fullPathName;
// Removes the final slash if present
@ -489,17 +477,8 @@ public class WebDavStore extends Store {
fullPathName = folderUrl.substring(folderSlash + 1);
// Decodes the url-encoded folder name (i.e. "My%20folder" => "My Folder"
try {
folderName = java.net.URLDecoder.decode(fullPathName, "UTF-8");
} catch (UnsupportedEncodingException uee) {
/**
* If we don't support UTF-8 there's a problem, don't decode
* it then
*/
folderName = fullPathName;
}
return folderName;
return UrlEncodingHelper.decodeUtf8(fullPathName);
}
return null;
@ -1258,21 +1237,16 @@ public class WebDavStore extends Store {
this.mName = name;
String encodedName = "";
try {
String[] urlParts = name.split("/");
String url = "";
for (int i = 0, count = urlParts.length; i < count; i++) {
if (i != 0) {
url = url + "/" + java.net.URLEncoder.encode(urlParts[i], "UTF-8");
url = url + "/" + com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(urlParts[i]);
} else {
url = java.net.URLEncoder.encode(urlParts[i], "UTF-8");
url = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(urlParts[i]);
}
}
encodedName = url;
} catch (UnsupportedEncodingException uee) {
Log.e(K9.LOG_TAG, "UnsupportedEncodingException URLEncoding folder name, skipping encoded");
encodedName = name;
}
encodedName = encodedName.replaceAll("\\+", "%20");
@ -1910,7 +1884,7 @@ public class WebDavStore extends Store {
if (!messageURL.endsWith("/")) {
messageURL += "/";
}
messageURL += URLEncoder.encode(message.getUid() + ":" + System.currentTimeMillis() + ".eml", "UTF-8");
messageURL += com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(message.getUid() + ":" + System.currentTimeMillis() + ".eml");
Log.i(K9.LOG_TAG, "Uploading message as " + messageURL);
@ -1997,12 +1971,9 @@ public class WebDavStore extends Store {
* We have to decode, then encode the URL because Exchange likes to not properly encode all characters
*/
try {
end = java.net.URLDecoder.decode(end, "UTF-8");
end = java.net.URLEncoder.encode(end, "UTF-8");
end = UrlEncodingHelper.decodeUtf8(end);
end = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(end);
end = end.replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException uee) {
Log.e(K9.LOG_TAG, "UnsupportedEncodingException caught in setUrl: " + uee + "\nTrace: "
+ processException(uee));
} catch (IllegalArgumentException iae) {
Log.e(K9.LOG_TAG, "IllegalArgumentException caught in setUrl: " + iae + "\nTrace: "
+ processException(iae));
@ -2405,13 +2376,10 @@ public class WebDavStore extends Store {
*/
try {
if (length > 3) {
end = java.net.URLDecoder.decode(end, "UTF-8");
end = java.net.URLEncoder.encode(end, "UTF-8");
end = UrlEncodingHelper.decodeUtf8(end);
end = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(end);
end = end.replaceAll("\\+", "%20");
}
} catch (UnsupportedEncodingException uee) {
Log.e(K9.LOG_TAG, "UnsupportedEncodingException caught in HttpGeneric(String uri): " + uee
+ "\nTrace: " + processException(uee));
} catch (IllegalArgumentException iae) {
Log.e(K9.LOG_TAG, "IllegalArgumentException caught in HttpGeneric(String uri): " + iae + "\nTrace: "
+ processException(iae));

View File

@ -3,8 +3,6 @@ package com.fsck.k9.mail.store.local;
import java.io.File;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
@ -468,9 +466,8 @@ public class LocalStore extends Store implements Serializable {
}
public void addPendingCommand(PendingCommand command) throws UnavailableStorageException {
try {
for (int i = 0; i < command.arguments.length; i++) {
command.arguments[i] = URLEncoder.encode(command.arguments[i], "UTF-8");
command.arguments[i] = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(command.arguments[i]);
}
final ContentValues cv = new ContentValues();
cv.put("command", command.command);
@ -482,9 +479,6 @@ public class LocalStore extends Store implements Serializable {
return null;
}
});
} catch (UnsupportedEncodingException uee) {
throw new Error("Aparently UTF-8 has been lost to the annals of history.");
}
}
public void removePendingCommand(final PendingCommand command) throws UnavailableStorageException {

View File

@ -23,7 +23,6 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.security.GeneralSecurityException;
import java.util.*;
@ -92,29 +91,24 @@ public class SmtpTransport extends Transport {
}
if (smtpUri.getUserInfo() != null) {
try {
String[] userInfoParts = smtpUri.getUserInfo().split(":");
if (userInfoParts.length == 1) {
authType = AuthType.PLAIN;
username = URLDecoder.decode(userInfoParts[0], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[0]);
} else if (userInfoParts.length == 2) {
authType = AuthType.PLAIN;
username = URLDecoder.decode(userInfoParts[0], "UTF-8");
password = URLDecoder.decode(userInfoParts[1], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[0]);
password = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[1]);
} else if (userInfoParts.length == 3) {
// NOTE: In SmptTransport URIs, the authType comes last!
authType = AuthType.valueOf(userInfoParts[2]);
username = URLDecoder.decode(userInfoParts[0], "UTF-8");
username = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[0]);
if (authType == AuthType.EXTERNAL) {
clientCertificateAlias = URLDecoder.decode(userInfoParts[1], "UTF-8");
clientCertificateAlias = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[1]);
} else {
password = URLDecoder.decode(userInfoParts[1], "UTF-8");
password = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(userInfoParts[1]);
}
}
} catch (UnsupportedEncodingException enc) {
// This shouldn't happen since the encoding is hardcoded to UTF-8
throw new IllegalArgumentException("Couldn't urldecode username or password.", enc);
}
}
return new ServerSettings(TRANSPORT_TYPE, host, port, connectionSecurity,
@ -133,20 +127,12 @@ public class SmtpTransport extends Transport {
* @see SmtpTransport#decodeUri(String)
*/
public static String createUri(ServerSettings server) {
String userEnc;
String passwordEnc;
String clientCertificateAliasEnc;
try {
userEnc = (server.username != null) ?
URLEncoder.encode(server.username, "UTF-8") : "";
passwordEnc = (server.password != null) ?
URLEncoder.encode(server.password, "UTF-8") : "";
clientCertificateAliasEnc = (server.clientCertificateAlias != null) ?
URLEncoder.encode(server.clientCertificateAlias, "UTF-8") : "";
}
catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Could not encode username or password", e);
}
String userEnc = (server.username != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.username) : "";
String passwordEnc = (server.password != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.password) : "";
String clientCertificateAliasEnc = (server.clientCertificateAlias != null) ?
com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(server.clientCertificateAlias) : "";
String scheme;
switch (server.connectionSecurity) {

View File

@ -59,11 +59,11 @@ public class Storage implements SharedPreferences {
if (transportUriStr != null) {
String[] userInfoParts = uri.getUserInfo().split(":");
String usernameEnc = URLEncoder.encode(userInfoParts[0], "UTF-8");
String usernameEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[0]);
String passwordEnc = "";
String authType = "";
if (userInfoParts.length > 1) {
passwordEnc = ":" + URLEncoder.encode(userInfoParts[1], "UTF-8");
passwordEnc = ":" + com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[1]);
}
if (userInfoParts.length > 2) {
authType = ":" + userInfoParts[2];
@ -83,34 +83,34 @@ public class Storage implements SharedPreferences {
if (storeUriStr.startsWith("imap")) {
String[] userInfoParts = uri.getUserInfo().split(":");
if (userInfoParts.length == 2) {
String usernameEnc = URLEncoder.encode(userInfoParts[0], "UTF-8");
String passwordEnc = URLEncoder.encode(userInfoParts[1], "UTF-8");
String usernameEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[0]);
String passwordEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[1]);
newUserInfo = usernameEnc + ":" + passwordEnc;
} else {
String authType = userInfoParts[0];
String usernameEnc = URLEncoder.encode(userInfoParts[1], "UTF-8");
String passwordEnc = URLEncoder.encode(userInfoParts[2], "UTF-8");
String usernameEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[1]);
String passwordEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[2]);
newUserInfo = authType + ":" + usernameEnc + ":" + passwordEnc;
}
} else if (storeUriStr.startsWith("pop3")) {
String[] userInfoParts = uri.getUserInfo().split(":", 2);
String usernameEnc = URLEncoder.encode(userInfoParts[0], "UTF-8");
String usernameEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[0]);
String passwordEnc = "";
if (userInfoParts.length > 1) {
passwordEnc = ":" + URLEncoder.encode(userInfoParts[1], "UTF-8");
passwordEnc = ":" + com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[1]);
}
newUserInfo = usernameEnc + passwordEnc;
} else if (storeUriStr.startsWith("webdav")) {
String[] userInfoParts = uri.getUserInfo().split(":", 2);
String usernameEnc = URLEncoder.encode(userInfoParts[0], "UTF-8");
String usernameEnc = com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[0]);
String passwordEnc = "";
if (userInfoParts.length > 1) {
passwordEnc = ":" + URLEncoder.encode(userInfoParts[1], "UTF-8");
passwordEnc = ":" + com.fsck.k9.helper.UrlEncodingHelper.encodeUtf8(userInfoParts[1]);
}
newUserInfo = usernameEnc + passwordEnc;

View File

@ -790,7 +790,7 @@ public class SingleMessageView extends LinearLayout implements OnClickListener,
// Try to get the filename from the URL
int start = path.lastIndexOf("/");
if (start != -1 && start + 1 < path.length()) {
filename = URLDecoder.decode(path.substring(start + 1), "UTF-8");
filename = com.fsck.k9.helper.UrlEncodingHelper.decodeUtf8(path.substring(start + 1));
} else {
// Use a dummy filename if necessary
filename = "saved_image";