Convert 'ServerSettings' to Kotlin

This commit is contained in:
cketti 2021-01-30 14:32:33 +01:00
parent 2338381de1
commit 69ed32ff8a
8 changed files with 49 additions and 225 deletions

View file

@ -36,6 +36,9 @@ import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import timber.log.Timber;
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableMap;
public class SettingsImporter {
@ -1058,7 +1061,7 @@ public class SettingsImporter {
int port = convertPort(importedServer.port);
ConnectionSecurity connectionSecurity = convertConnectionSecurity(importedServer.connectionSecurity);
Map<String, String> extra = importedServer.extras != null ?
Collections.unmodifiableMap(importedServer.extras.settings) : null;
unmodifiableMap(importedServer.extras.settings) : emptyMap();
return new ServerSettings(type, importedServer.host, port, connectionSecurity,
importedServer.authenticationType, importedServer.username, importedServer.password,

View file

@ -24,7 +24,7 @@ class JmapBackendFactory(
val serverSettings = decodeStoreUri(account.storeUri)
val jmapConfig = JmapConfig(
username = serverSettings.username,
password = serverSettings.password,
password = serverSettings.password!!,
baseUrl = serverSettings.host,
accountId = serverSettings.extra["accountId"]!!
)
@ -34,7 +34,7 @@ class JmapBackendFactory(
override fun decodeStoreUri(storeUri: String): ServerSettings {
val uri = Uri.parse(storeUri)
val username = uri.getQueryParameter("username")
val username = uri.getQueryParameter("username")!!
val password = uri.getQueryParameter("password")
val baseUrl = uri.getQueryParameter("baseUrl")
val accountId = uri.getQueryParameter("accountId")
@ -43,7 +43,17 @@ class JmapBackendFactory(
"accountId" to accountId
)
return ServerSettings("jmap", baseUrl, 433, ConnectionSecurity.SSL_TLS_REQUIRED, AuthType.PLAIN, username, password, null, extra)
return ServerSettings(
type = "jmap",
host = baseUrl,
port = 433,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = username,
password = password,
clientCertificateAlias = null,
extra = extra
)
}
override fun createStoreUri(serverSettings: ServerSettings): String {

View file

@ -53,6 +53,9 @@ import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import timber.log.Timber;
import static java.util.Collections.emptyMap;
public class AccountSetupIncoming extends K9Activity implements OnClickListener {
private static final String EXTRA_ACCOUNT = "account";
private static final String EXTRA_MAKE_DEFAULT = "makeDefault";
@ -569,7 +572,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
String host = mServerView.getText().toString();
int port = Integer.parseInt(mPortView.getText().toString());
Map<String, String> extra = null;
Map<String, String> extra = emptyMap();
if (mStoreType.equals(Protocols.IMAP)) {
boolean autoDetectNamespace = mImapAutoDetectNamespaceView.isChecked();
String pathPrefix = mImapPathPrefixView.getText().toString();

View file

@ -489,7 +489,8 @@ public class AccountSetupOutgoing extends K9Activity implements OnClickListener,
String newHost = mServerView.getText().toString();
int newPort = Integer.parseInt(mPortView.getText().toString());
ServerSettings server = new ServerSettings(Protocols.SMTP, newHost, newPort, securityType, authType, username, password, clientCertificateAlias);
ServerSettings server = new ServerSettings(Protocols.SMTP, newHost, newPort, securityType, authType, username,
password, clientCertificateAlias);
uri = backendManager.createTransportUri(server);
DI.get(LocalKeyStoreManager.class).deleteCertificate(mAccount, newHost, newPort, MailServerDirection.OUTGOING);
mAccount.setTransportUri(uri);

View file

@ -7,6 +7,7 @@ import java.util.Map;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.store.imap.ImapStoreSettings;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -227,7 +228,7 @@ public class ImapStoreUriTest {
@Test
public void testCreateDecodeStoreUriWithSpecialCharactersInUsernameAndPassword() {
ServerSettings settings = new ServerSettings("imap", "server", 143,
ConnectionSecurity.NONE, AuthType.PLAIN, "user@doma:n", "p@ssw:rd%", null, null);
ConnectionSecurity.NONE, AuthType.PLAIN, "user@doma:n", "p@ssw:rd%", null);
String uri = ImapStoreUriCreator.create(settings);

View file

@ -1,212 +1,26 @@
package com.fsck.k9.mail;
package com.fsck.k9.mail
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Locale
/**
* Generic container to hold server settings.
* Container for incoming or outgoing server settings
*/
public final class ServerSettings {
/**
* Name of the protocol these server settings belong to. Must be all lower case.
*/
public final String type;
/**
* The host name of the server.
*
* {@code null} if not applicable for the store or transport.
*/
public final String host;
/**
* The port number of the server.
*
* {@code -1} if not applicable for the store or transport.
*/
public final int port;
/**
* The type of connection security to be used when connecting to the server.
*
* {@link ConnectionSecurity#NONE} if not applicable for the store or transport.
*/
public final ConnectionSecurity connectionSecurity;
/**
* The authentication method to use when connecting to the server.
*
* {@code null} if not applicable for the store or transport.
*/
public final AuthType authenticationType;
/**
* The username part of the credentials needed to authenticate to the server.
*
* {@code null} if not applicable for the store or transport.
*/
public final String username;
/**
* The password part of the credentials needed to authenticate to the server.
*
* {@code null} if not applicable for the store or transport.
*/
public final String password;
/**
* The alias to retrieve a client certificate using Android 4.0 KeyChain API
* for TLS client certificate authentication with the server.
*
* {@code null} if not applicable for the store or transport.
*/
public final String clientCertificateAlias;
/**
* Store- or transport-specific settings as key/value pair.
*
* {@code null} if not applicable for the store or transport.
*/
private final Map<String, String> extra;
/**
* Creates a new {@code ServerSettings} object.
*
* @param type
* see {@link ServerSettings#type}
* @param host
* see {@link ServerSettings#host}
* @param port
* see {@link ServerSettings#port}
* @param connectionSecurity
* see {@link ServerSettings#connectionSecurity}
* @param authenticationType
* see {@link ServerSettings#authenticationType}
* @param username
* see {@link ServerSettings#username}
* @param password
* see {@link ServerSettings#password}
* @param clientCertificateAlias
* see {@link ServerSettings#clientCertificateAlias}
*/
public ServerSettings(String type, String host, int port,
ConnectionSecurity connectionSecurity, AuthType authenticationType, String username,
String password, String clientCertificateAlias) {
this.type = checkType(type);
this.host = host;
this.port = port;
this.connectionSecurity = connectionSecurity;
this.authenticationType = authenticationType;
this.username = username;
this.password = password;
this.clientCertificateAlias = clientCertificateAlias;
this.extra = null;
data class ServerSettings @JvmOverloads constructor(
@JvmField val type: String,
@JvmField val host: String?,
@JvmField val port: Int,
@JvmField val connectionSecurity: ConnectionSecurity,
@JvmField val authenticationType: AuthType,
@JvmField val username: String,
@JvmField val password: String?,
@JvmField val clientCertificateAlias: String?,
val extra: Map<String, String?> = emptyMap()
) {
init {
require(type == type.toLowerCase(Locale.ROOT)) { "type must be all lower case" }
}
/**
* Creates a new {@code ServerSettings} object.
*
* @param type
* see {@link ServerSettings#type}
* @param host
* see {@link ServerSettings#host}
* @param port
* see {@link ServerSettings#port}
* @param connectionSecurity
* see {@link ServerSettings#connectionSecurity}
* @param authenticationType
* see {@link ServerSettings#authenticationType}
* @param username
* see {@link ServerSettings#username}
* @param password
* see {@link ServerSettings#password}
* @param clientCertificateAlias
* see {@link ServerSettings#clientCertificateAlias}
* @param extra
* see {@link ServerSettings#extra}
*/
public ServerSettings(String type, String host, int port,
ConnectionSecurity connectionSecurity, AuthType authenticationType, String username,
String password, String clientCertificateAlias, Map<String, String> extra) {
this.type = checkType(type);
this.host = host;
this.port = port;
this.connectionSecurity = connectionSecurity;
this.authenticationType = authenticationType;
this.username = username;
this.password = password;
this.clientCertificateAlias = clientCertificateAlias;
this.extra = (extra != null) ? Collections.unmodifiableMap(new HashMap<>(extra)) : null;
}
/**
* Creates an "empty" {@code ServerSettings} object.
*
* Everything but {@link ServerSettings#type} is unused.
*
* @param type
* see {@link ServerSettings#type}
*/
public ServerSettings(String type) {
this.type = checkType(type);
host = null;
port = -1;
connectionSecurity = ConnectionSecurity.NONE;
authenticationType = null;
username = null;
password = null;
clientCertificateAlias = null;
extra = null;
}
/**
* Returns store- or transport-specific settings as key/value pair.
*
* @return additional set of settings as key/value pair.
*/
public Map<String, String> getExtra() {
return extra;
}
public ServerSettings newPassword(String newPassword) {
return new ServerSettings(type, host, port, connectionSecurity, authenticationType,
username, newPassword, clientCertificateAlias);
}
public ServerSettings newClientCertificateAlias(String newAlias) {
return new ServerSettings(type, host, port, connectionSecurity, AuthType.EXTERNAL,
username, password, newAlias);
}
private String checkType(String type) {
if (type == null) {
throw new IllegalArgumentException("type == null");
}
if (!type.equals(type.toLowerCase(Locale.ROOT))) {
throw new IllegalArgumentException("type must be all lower case");
}
return type;
}
@Override
public boolean equals(Object obj) {
if (! (obj instanceof ServerSettings)) {
return false;
}
ServerSettings that = (ServerSettings) obj;
return type.equals(that.type) &&
port == that.port &&
connectionSecurity == that.connectionSecurity &&
authenticationType == that.authenticationType &&
(host == null ? that.host == null : host.equals(that.host)) &&
(username == null ? that.username == null : username.equals(that.username)) &&
(password == null ? that.password == null : password.equals(that.password)) &&
(clientCertificateAlias == null ? that.clientCertificateAlias == null :
clientCertificateAlias.equals(that.clientCertificateAlias));
fun newPassword(newPassword: String?): ServerSettings {
return this.copy(password = newPassword)
}
}

View file

@ -11,15 +11,11 @@ object ImapStoreSettings {
@JvmStatic
val ServerSettings.autoDetectNamespace: Boolean
get() {
return extra?.get(AUTODETECT_NAMESPACE_KEY)?.toBoolean() ?: true
}
get() = extra[AUTODETECT_NAMESPACE_KEY]?.toBoolean() ?: true
@JvmStatic
val ServerSettings.pathPrefix: String?
get() {
return extra?.get(PATH_PREFIX_KEY)
}
get() = extra[PATH_PREFIX_KEY]
@JvmStatic
fun createExtra(autoDetectNamespace: Boolean, pathPrefix: String?): Map<String, String?> {

View file

@ -17,23 +17,19 @@ public class WebDavStoreSettings {
private static final String MAILBOX_PATH_KEY = "mailboxPath";
public static String getAlias(ServerSettings serverSettings) {
Map<String, String> extra = serverSettings.getExtra();
return extra == null ? null : extra.get(ALIAS_KEY);
return serverSettings.getExtra().get(ALIAS_KEY);
}
public static String getPath(ServerSettings serverSettings) {
Map<String, String> extra = serverSettings.getExtra();
return extra == null ? null : extra.get(PATH_KEY);
return serverSettings.getExtra().get(PATH_KEY);
}
public static String getAuthPath(ServerSettings serverSettings) {
Map<String, String> extra = serverSettings.getExtra();
return extra == null ? null : extra.get(AUTH_PATH_KEY);
return serverSettings.getExtra().get(AUTH_PATH_KEY);
}
public static String getMailboxPath(ServerSettings serverSettings) {
Map<String, String> extra = serverSettings.getExtra();
return extra == null ? null : extra.get(MAILBOX_PATH_KEY);
return serverSettings.getExtra().get(MAILBOX_PATH_KEY);
}
public static Map<String, String> createExtra(String alias, String path, String authPath, String mailboxPath) {