Make WebDavStore use ServerSettings directly

Instead of using WebDavStoreSettings as a ServerSettings subclass make it a helper to extract WebDav-specific settings.
This commit is contained in:
cketti 2021-01-30 10:24:57 +01:00
parent 42e180d6fa
commit 97051f3f7c
8 changed files with 69 additions and 73 deletions

View file

@ -47,7 +47,6 @@ import com.fsck.k9.ui.R;
import com.fsck.k9.view.ClientCertificateSpinner; import com.fsck.k9.view.ClientCertificateSpinner;
import com.fsck.k9.view.ClientCertificateSpinner.OnClientCertificateChangedListener; import com.fsck.k9.view.ClientCertificateSpinner.OnClientCertificateChangedListener;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputEditText;
@ -247,18 +246,19 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
findViewById(R.id.compression_label).setVisibility(View.GONE); findViewById(R.id.compression_label).setVisibility(View.GONE);
mSubscribedFoldersOnly.setVisibility(View.GONE); mSubscribedFoldersOnly.setVisibility(View.GONE);
WebDavStoreSettings webDavSettings = (WebDavStoreSettings) settings; String path = WebDavStoreSettings.getPath(settings);
if (path != null) {
if (webDavSettings.path != null) { mWebdavPathPrefixView.setText(path);
mWebdavPathPrefixView.setText(webDavSettings.path);
} }
if (webDavSettings.authPath != null) { String authPath = WebDavStoreSettings.getAuthPath(settings);
mWebdavAuthPathView.setText(webDavSettings.authPath); if (authPath != null) {
mWebdavAuthPathView.setText(authPath);
} }
if (webDavSettings.mailboxPath != null) { String mailboxPath = WebDavStoreSettings.getMailboxPath(settings);
mWebdavMailboxPathView.setText(webDavSettings.mailboxPath); if (mailboxPath != null) {
mWebdavMailboxPathView.setText(mailboxPath);
} }
} else { } else {
throw new Exception("Unknown account type: " + mAccount.getStoreUri()); throw new Exception("Unknown account type: " + mAccount.getStoreUri());
@ -575,13 +575,10 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
String pathPrefix = mImapPathPrefixView.getText().toString(); String pathPrefix = mImapPathPrefixView.getText().toString();
extra = ImapStoreSettings.createExtra(autoDetectNamespace, pathPrefix); extra = ImapStoreSettings.createExtra(autoDetectNamespace, pathPrefix);
} else if (mStoreType.equals(Protocols.WEBDAV)) { } else if (mStoreType.equals(Protocols.WEBDAV)) {
extra = new HashMap<>(); String path = mWebdavPathPrefixView.getText().toString();
extra.put(WebDavStoreSettings.PATH_KEY, String authPath = mWebdavAuthPathView.getText().toString();
mWebdavPathPrefixView.getText().toString()); String mailboxPath = mWebdavMailboxPathView.getText().toString();
extra.put(WebDavStoreSettings.AUTH_PATH_KEY, extra = WebDavStoreSettings.createExtra(null, path, authPath, mailboxPath);
mWebdavAuthPathView.getText().toString());
extra.put(WebDavStoreSettings.MAILBOX_PATH_KEY,
mWebdavMailboxPathView.getText().toString());
} }
DI.get(LocalKeyStoreManager.class).deleteCertificate(mAccount, host, port, MailServerDirection.INCOMING); DI.get(LocalKeyStoreManager.class).deleteCertificate(mAccount, host, port, MailServerDirection.INCOMING);

View file

@ -38,11 +38,11 @@ public class WebDavStoreUriCreator {
String uriPath; String uriPath;
Map<String, String> extra = server.getExtra(); Map<String, String> extra = server.getExtra();
if (extra != null) { if (extra != null) {
String path = extra.get(WebDavStoreSettings.PATH_KEY); String path = WebDavStoreSettings.getPath(server);
path = (path != null) ? path : ""; path = (path != null) ? path : "";
String authPath = extra.get(WebDavStoreSettings.AUTH_PATH_KEY); String authPath = WebDavStoreSettings.getAuthPath(server);
authPath = (authPath != null) ? authPath : ""; authPath = (authPath != null) ? authPath : "";
String mailboxPath = extra.get(WebDavStoreSettings.MAILBOX_PATH_KEY); String mailboxPath = WebDavStoreSettings.getMailboxPath(server);
mailboxPath = (mailboxPath != null) ? mailboxPath : ""; mailboxPath = (mailboxPath != null) ? mailboxPath : "";
uriPath = "/" + path + "|" + authPath + "|" + mailboxPath; uriPath = "/" + path + "|" + authPath + "|" + mailboxPath;
} else { } else {

View file

@ -2,10 +2,12 @@ package com.fsck.k9.backend.webdav;
import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.store.webdav.WebDavStoreSettings; import com.fsck.k9.mail.store.webdav.WebDavStoreSettings;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Map;
import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8; import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;
@ -20,7 +22,7 @@ public class WebDavStoreUriDecoder {
* webdav+ssl+://user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED * webdav+ssl+://user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED
* </pre> * </pre>
*/ */
public static WebDavStoreSettings decode(String uri) { public static ServerSettings decode(String uri) {
String host; String host;
int port; int port;
ConnectionSecurity connectionSecurity; ConnectionSecurity connectionSecurity;
@ -106,7 +108,8 @@ public class WebDavStoreUriDecoder {
} }
} }
return new WebDavStoreSettings(host, port, connectionSecurity, null, username, password, Map<String, String> extra = WebDavStoreSettings.createExtra(alias, path, authPath, mailboxPath);
null, alias, path, authPath, mailboxPath);
return new ServerSettings("webdav", host, port, connectionSecurity, null, username, password, null, extra);
} }
} }

View file

@ -171,12 +171,6 @@ public class ServerSettings {
return extra; return extra;
} }
protected void putIfNotNull(Map<String, String> map, String key, String value) {
if (value != null) {
map.put(key, value);
}
}
public ServerSettings newPassword(String newPassword) { public ServerSettings newPassword(String newPassword) {
return new ServerSettings(type, host, port, connectionSecurity, authenticationType, return new ServerSettings(type, host, port, connectionSecurity, authenticationType,
username, newPassword, clientCertificateAlias); username, newPassword, clientCertificateAlias);

View file

@ -22,6 +22,7 @@ import com.fsck.k9.mail.FolderType;
import com.fsck.k9.mail.K9MailLib; import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.filter.Base64; import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.ssl.TrustManagerFactory; import com.fsck.k9.mail.ssl.TrustManagerFactory;
import com.fsck.k9.mail.store.webdav.WebDavHttpClient.WebDavHttpClientFactory; import com.fsck.k9.mail.store.webdav.WebDavHttpClient.WebDavHttpClientFactory;
@ -83,12 +84,12 @@ public class WebDavStore {
private WebDavFolder sendFolder = null; private WebDavFolder sendFolder = null;
private Map<String, WebDavFolder> folderList = new HashMap<>(); private Map<String, WebDavFolder> folderList = new HashMap<>();
public WebDavStore(TrustManagerFactory trustManagerFactory, WebDavStoreSettings serverSettings, public WebDavStore(TrustManagerFactory trustManagerFactory, ServerSettings serverSettings,
DraftsFolderProvider draftsFolderProvider) { DraftsFolderProvider draftsFolderProvider) {
this(trustManagerFactory, serverSettings, draftsFolderProvider, new WebDavHttpClient.WebDavHttpClientFactory()); this(trustManagerFactory, serverSettings, draftsFolderProvider, new WebDavHttpClient.WebDavHttpClientFactory());
} }
public WebDavStore(TrustManagerFactory trustManagerFactory, WebDavStoreSettings serverSettings, public WebDavStore(TrustManagerFactory trustManagerFactory, ServerSettings serverSettings,
DraftsFolderProvider draftsFolderProvider, WebDavHttpClientFactory clientFactory) { DraftsFolderProvider draftsFolderProvider, WebDavHttpClientFactory clientFactory) {
this.draftsFolderProvider = draftsFolderProvider; this.draftsFolderProvider = draftsFolderProvider;
httpClientFactory = clientFactory; httpClientFactory = clientFactory;
@ -101,11 +102,11 @@ public class WebDavStore {
username = serverSettings.username; username = serverSettings.username;
password = serverSettings.password; password = serverSettings.password;
alias = serverSettings.alias; alias = WebDavStoreSettings.getAlias(serverSettings);
path = serverSettings.path; path = WebDavStoreSettings.getPath(serverSettings);
formBasedAuthPath = serverSettings.authPath; formBasedAuthPath = WebDavStoreSettings.getAuthPath(serverSettings);
mailboxPath = serverSettings.mailboxPath; mailboxPath = WebDavStoreSettings.getMailboxPath(serverSettings);
if (path == null || path.equals("")) { if (path == null || path.equals("")) {

View file

@ -4,38 +4,39 @@ package com.fsck.k9.mail.store.webdav;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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.ServerSettings;
/** /**
* This class is used to store the decoded contents of an WebDavStore URI. * Extract WebDav-specific server settings from {@link ServerSettings}.
*/ */
public class WebDavStoreSettings extends ServerSettings { public class WebDavStoreSettings {
public static final String ALIAS_KEY = "alias"; private static final String ALIAS_KEY = "alias";
public static final String PATH_KEY = "path"; private static final String PATH_KEY = "path";
public static final String AUTH_PATH_KEY = "authPath"; private static final String AUTH_PATH_KEY = "authPath";
public static final String MAILBOX_PATH_KEY = "mailboxPath"; private static final String MAILBOX_PATH_KEY = "mailboxPath";
public final String alias; public static String getAlias(ServerSettings serverSettings) {
public final String path; Map<String, String> extra = serverSettings.getExtra();
public final String authPath; return extra == null ? null : extra.get(ALIAS_KEY);
public final String mailboxPath;
public WebDavStoreSettings(String host, int port, ConnectionSecurity connectionSecurity,
AuthType authenticationType, String username, String password, String clientCertificateAlias, String alias,
String path, String authPath, String mailboxPath) {
super("webdav", host, port, connectionSecurity, authenticationType, username,
password, clientCertificateAlias);
this.alias = alias;
this.path = path;
this.authPath = authPath;
this.mailboxPath = mailboxPath;
} }
@Override public static String getPath(ServerSettings serverSettings) {
public Map<String, String> getExtra() { Map<String, String> extra = serverSettings.getExtra();
return extra == null ? null : extra.get(PATH_KEY);
}
public static String getAuthPath(ServerSettings serverSettings) {
Map<String, String> extra = serverSettings.getExtra();
return extra == null ? null : extra.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);
}
public static Map<String, String> createExtra(String alias, String path, String authPath, String mailboxPath) {
Map<String, String> extra = new HashMap<>(); Map<String, String> extra = new HashMap<>();
putIfNotNull(extra, ALIAS_KEY, alias); putIfNotNull(extra, ALIAS_KEY, alias);
putIfNotNull(extra, PATH_KEY, path); putIfNotNull(extra, PATH_KEY, path);
@ -44,9 +45,9 @@ public class WebDavStoreSettings extends ServerSettings {
return extra; return extra;
} }
@Override private static void putIfNotNull(Map<String, String> map, String key, String value) {
public ServerSettings newPassword(String newPassword) { if (value != null) {
return new WebDavStoreSettings(host, port, connectionSecurity, authenticationType, map.put(key, value);
username, newPassword, clientCertificateAlias, alias, path, authPath, mailboxPath); }
} }
} }

View file

@ -6,17 +6,17 @@ import java.util.Collections;
import com.fsck.k9.mail.K9MailLib; import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.Transport; import com.fsck.k9.mail.Transport;
import com.fsck.k9.mail.ssl.TrustManagerFactory; import com.fsck.k9.mail.ssl.TrustManagerFactory;
import com.fsck.k9.mail.store.webdav.DraftsFolderProvider; import com.fsck.k9.mail.store.webdav.DraftsFolderProvider;
import com.fsck.k9.mail.store.webdav.WebDavStore; import com.fsck.k9.mail.store.webdav.WebDavStore;
import com.fsck.k9.mail.store.webdav.WebDavStoreSettings;
import timber.log.Timber; import timber.log.Timber;
public class WebDavTransport extends Transport { public class WebDavTransport extends Transport {
private WebDavStore store; private WebDavStore store;
public WebDavTransport(TrustManagerFactory trustManagerFactory, WebDavStoreSettings serverSettings, public WebDavTransport(TrustManagerFactory trustManagerFactory, ServerSettings serverSettings,
DraftsFolderProvider draftsFolderProvider) { DraftsFolderProvider draftsFolderProvider) {
store = new WebDavStore(trustManagerFactory, serverSettings, draftsFolderProvider); store = new WebDavStore(trustManagerFactory, serverSettings, draftsFolderProvider);

View file

@ -14,6 +14,7 @@ import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.FolderType; import com.fsck.k9.mail.FolderType;
import com.fsck.k9.mail.K9LibRobolectricTestRunner; import com.fsck.k9.mail.K9LibRobolectricTestRunner;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.filter.Base64; import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.ssl.TrustManagerFactory; import com.fsck.k9.mail.ssl.TrustManagerFactory;
@ -73,7 +74,7 @@ public class WebDavStoreTest {
private ArgumentCaptor<HttpGeneric> requestCaptor; private ArgumentCaptor<HttpGeneric> requestCaptor;
private WebDavStoreSettings serverSettings; private ServerSettings serverSettings;
private WebDavStore webDavStore; private WebDavStore webDavStore;
@ -87,7 +88,7 @@ public class WebDavStoreTest {
when(mockHttpClient.getConnectionManager()).thenReturn(mockClientConnectionManager); when(mockHttpClient.getConnectionManager()).thenReturn(mockClientConnectionManager);
when(mockClientConnectionManager.getSchemeRegistry()).thenReturn(mockSchemeRegistry); when(mockClientConnectionManager.getSchemeRegistry()).thenReturn(mockSchemeRegistry);
serverSettings = createWebDavStoreSettings(ConnectionSecurity.SSL_TLS_REQUIRED); serverSettings = createServerSettings(ConnectionSecurity.SSL_TLS_REQUIRED);
webDavStore = createWebDavStore(); webDavStore = createWebDavStore();
} }
@ -336,8 +337,10 @@ public class WebDavStoreTest {
}; };
} }
private WebDavStoreSettings createWebDavStoreSettings(ConnectionSecurity connectionSecurity) { private ServerSettings createServerSettings(ConnectionSecurity connectionSecurity) {
return new WebDavStoreSettings( Map<String, String> extra = WebDavStoreSettings.createExtra(null, null, null, null);
return new ServerSettings(
"webdav",
"webdav.example.org", "webdav.example.org",
443, 443,
connectionSecurity, connectionSecurity,
@ -345,10 +348,7 @@ public class WebDavStoreTest {
"user", "user",
"password", "password",
null, null,
null, extra);
null,
null,
null);
} }
private WebDavStore createWebDavStore() { private WebDavStore createWebDavStore() {
@ -356,7 +356,7 @@ public class WebDavStoreTest {
} }
private WebDavStore createWebDavStore(ConnectionSecurity connectionSecurity) { private WebDavStore createWebDavStore(ConnectionSecurity connectionSecurity) {
WebDavStoreSettings serverSettings = createWebDavStoreSettings(connectionSecurity); ServerSettings serverSettings = createServerSettings(connectionSecurity);
return new WebDavStore(trustManagerFactory, serverSettings, draftsFolderProvider, mockHttpClientFactory); return new WebDavStore(trustManagerFactory, serverSettings, draftsFolderProvider, mockHttpClientFactory);
} }