Get rid of common base class RemoteStore

This also allows removing some unnecessary methods in former subclasses.
This commit is contained in:
cketti 2020-01-26 05:13:16 +01:00
parent 2afacbc498
commit 9335080545
9 changed files with 38 additions and 150 deletions

View file

@ -0,0 +1,6 @@
package com.fsck.k9.mail
object NetworkTimeouts {
const val SOCKET_CONNECT_TIMEOUT = 30000
const val SOCKET_READ_TIMEOUT = 60000
}

View file

@ -1,52 +0,0 @@
package com.fsck.k9.mail.store;
import java.util.List;
import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
public abstract class RemoteStore {
public static final int SOCKET_CONNECT_TIMEOUT = 30000;
public static final int SOCKET_READ_TIMEOUT = 60000;
protected StoreConfig mStoreConfig;
protected TrustedSocketFactory mTrustedSocketFactory;
public RemoteStore(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory) {
mStoreConfig = storeConfig;
mTrustedSocketFactory = trustedSocketFactory;
}
public abstract Folder<? extends Message> getFolder(String name);
public abstract List<? extends Folder > getPersonalNamespaces() throws MessagingException;
public abstract void checkSettings() throws MessagingException;
public boolean isCopyCapable() {
return false;
}
public boolean isMoveCapable() {
return false;
}
public boolean isPushCapable() {
return false;
}
public boolean isExpungeCapable() {
return false;
}
public boolean isSeenFlagSupported() {
return true;
}
public void sendMessages(List<? extends Message> messages) throws MessagingException { }
}

View file

@ -51,8 +51,8 @@ import timber.log.Timber;
import static com.fsck.k9.mail.ConnectionSecurity.STARTTLS_REQUIRED;
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_IMAP;
import static com.fsck.k9.mail.store.RemoteStore.SOCKET_CONNECT_TIMEOUT;
import static com.fsck.k9.mail.store.RemoteStore.SOCKET_READ_TIMEOUT;
import static com.fsck.k9.mail.NetworkTimeouts.SOCKET_CONNECT_TIMEOUT;
import static com.fsck.k9.mail.NetworkTimeouts.SOCKET_READ_TIMEOUT;
import static com.fsck.k9.mail.store.imap.ImapResponseParser.equalsIgnoreCase;

View file

@ -20,10 +20,10 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.PushReceiver;
import com.fsck.k9.mail.power.PowerManager;
import com.fsck.k9.mail.power.WakeLock;
import com.fsck.k9.mail.store.RemoteStore;
import timber.log.Timber;
import static com.fsck.k9.mail.K9MailLib.PUSH_WAKE_LOCK_TIMEOUT;
import static com.fsck.k9.mail.NetworkTimeouts.SOCKET_READ_TIMEOUT;
import static com.fsck.k9.mail.store.imap.ImapResponseParser.equalsIgnoreCase;
@ -718,7 +718,7 @@ class ImapFolderPusher extends ImapFolder {
private void sendDone() {
try {
imapConnection.setReadTimeout(RemoteStore.SOCKET_READ_TIMEOUT);
imapConnection.setReadTimeout(SOCKET_READ_TIMEOUT);
imapConnection.sendContinuation("DONE");
} catch (IOException e) {
imapConnection.close();

View file

@ -24,7 +24,6 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.NetworkType;
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.RemoteStore;
import com.fsck.k9.mail.store.StoreConfig;
import timber.log.Timber;
@ -35,7 +34,9 @@ import timber.log.Timber;
* TODO Need a default response handler for things like folder updates
* </pre>
*/
public class ImapStore extends RemoteStore {
public class ImapStore {
private final StoreConfig storeConfig;
private final TrustedSocketFactory trustedSocketFactory;
private Set<Flag> permanentFlagsIndex = EnumSet.noneOf(Flag.class);
private ConnectivityManager connectivityManager;
private OAuth2TokenProvider oauthTokenProvider;
@ -65,7 +66,8 @@ public class ImapStore extends RemoteStore {
public ImapStore(ImapStoreSettings serverSettings, StoreConfig storeConfig,
TrustedSocketFactory trustedSocketFactory, ConnectivityManager connectivityManager,
OAuth2TokenProvider oauthTokenProvider) {
super(storeConfig, trustedSocketFactory);
this.storeConfig = storeConfig;
this.trustedSocketFactory = trustedSocketFactory;
host = serverSettings.host;
port = serverSettings.port;
@ -85,7 +87,6 @@ public class ImapStore extends RemoteStore {
folderNameCodec = FolderNameCodec.newInstance();
}
@Override
public ImapFolder getFolder(String name) {
ImapFolder folder;
synchronized (folderCache) {
@ -119,14 +120,13 @@ public class ImapStore extends RemoteStore {
return combinedPrefix;
}
@Override
public List<ImapFolder> getPersonalNamespaces() throws MessagingException {
ImapConnection connection = getConnection();
try {
List<FolderListItem> folders = listFolders(connection, false);
if (!mStoreConfig.isSubscribedFoldersOnly()) {
if (!storeConfig.isSubscribedFoldersOnly()) {
return getFolders(folders);
}
@ -204,7 +204,7 @@ public class ImapStore extends RemoteStore {
if (ImapFolder.INBOX.equalsIgnoreCase(folder)) {
continue;
} else if (folder.equals(mStoreConfig.getOutboxFolder())) {
} else if (folder.equals(storeConfig.getOutboxFolder())) {
/*
* There is a folder on the server with the same name as our local
* outbox. Until we have a good plan to deal with this situation
@ -265,7 +265,6 @@ public class ImapStore extends RemoteStore {
return folderName.substring(prefixLength);
}
@Override
public void checkSettings() throws MessagingException {
try {
ImapConnection connection = createImapConnection();
@ -312,7 +311,7 @@ public class ImapStore extends RemoteStore {
ImapConnection createImapConnection() {
return new ImapConnection(
new StoreImapSettings(),
mTrustedSocketFactory,
trustedSocketFactory,
connectivityManager,
oauthTokenProvider);
}
@ -333,28 +332,8 @@ public class ImapStore extends RemoteStore {
return imapFolders;
}
@Override
public boolean isMoveCapable() {
return true;
}
@Override
public boolean isCopyCapable() {
return true;
}
@Override
public boolean isPushCapable() {
return true;
}
@Override
public boolean isExpungeCapable() {
return true;
}
StoreConfig getStoreConfig() {
return mStoreConfig;
return storeConfig;
}
Set<Flag> getPermanentFlagsIndex() {
@ -400,7 +379,7 @@ public class ImapStore extends RemoteStore {
@Override
public boolean useCompression(final NetworkType type) {
return mStoreConfig.useCompression(type);
return storeConfig.useCompression(type);
}
@Override

View file

@ -27,12 +27,13 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.filter.Hex;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.RemoteStore;
import javax.net.ssl.SSLException;
import timber.log.Timber;
import static com.fsck.k9.mail.CertificateValidationException.Reason.MissingCapability;
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_POP3;
import static com.fsck.k9.mail.NetworkTimeouts.SOCKET_CONNECT_TIMEOUT;
import static com.fsck.k9.mail.NetworkTimeouts.SOCKET_READ_TIMEOUT;
import static com.fsck.k9.mail.store.pop3.Pop3Commands.*;
@ -68,11 +69,11 @@ class Pop3Connection {
socket = new Socket();
}
socket.connect(socketAddress, RemoteStore.SOCKET_CONNECT_TIMEOUT);
socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
in = new BufferedInputStream(socket.getInputStream(), 1024);
out = new BufferedOutputStream(socket.getOutputStream(), 512);
socket.setSoTimeout(RemoteStore.SOCKET_READ_TIMEOUT);
socket.setSoTimeout(SOCKET_READ_TIMEOUT);
if (!isOpen()) {
throw new MessagingException("Unable to connect socket");
@ -118,7 +119,7 @@ class Pop3Connection {
host,
port,
clientCertificateAlias);
socket.setSoTimeout(RemoteStore.SOCKET_READ_TIMEOUT);
socket.setSoTimeout(SOCKET_READ_TIMEOUT);
in = new BufferedInputStream(socket.getInputStream(), 1024);
out = new BufferedOutputStream(socket.getOutputStream(), 512);
if (!isOpen()) {

View file

@ -2,8 +2,6 @@ package com.fsck.k9.mail.store.pop3;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import androidx.annotation.NonNull;
@ -14,11 +12,12 @@ import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.RemoteStore;
import com.fsck.k9.mail.store.StoreConfig;
public class Pop3Store extends RemoteStore {
public class Pop3Store {
private final StoreConfig storeConfig;
private final TrustedSocketFactory trustedSocketFactory;
private final String host;
private final int port;
private final String username;
@ -30,8 +29,8 @@ public class Pop3Store extends RemoteStore {
private Map<String, Pop3Folder> mFolders = new HashMap<>();
public Pop3Store(ServerSettings serverSettings, StoreConfig storeConfig, TrustedSocketFactory socketFactory) {
super(storeConfig, socketFactory);
this.storeConfig = storeConfig;
trustedSocketFactory = socketFactory;
host = serverSettings.host;
port = serverSettings.port;
connectionSecurity = serverSettings.connectionSecurity;
@ -41,7 +40,6 @@ public class Pop3Store extends RemoteStore {
authType = serverSettings.authenticationType;
}
@Override
@NonNull
public Pop3Folder getFolder(String name) {
Pop3Folder folder = mFolders.get(name);
@ -52,14 +50,6 @@ public class Pop3Store extends RemoteStore {
return folder;
}
@Override
public List<Pop3Folder> getPersonalNamespaces() {
List<Pop3Folder> folders = new LinkedList<>();
folders.add(getFolder(Pop3Folder.INBOX));
return folders;
}
@Override
public void checkSettings() throws MessagingException {
Pop3Folder folder = new Pop3Folder(this, Pop3Folder.INBOX);
try {
@ -71,18 +61,12 @@ public class Pop3Store extends RemoteStore {
}
}
@Override
public boolean isSeenFlagSupported() {
return false;
}
StoreConfig getConfig() {
return mStoreConfig;
return storeConfig;
}
public Pop3Connection createConnection() throws MessagingException {
return new Pop3Connection(new StorePop3Settings(), mTrustedSocketFactory);
return new Pop3Connection(new StorePop3Settings(), trustedSocketFactory);
}
private class StorePop3Settings implements Pop3Settings {

View file

@ -6,7 +6,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import com.fsck.k9.mail.AuthType;
import com.fsck.k9.mail.AuthenticationFailedException;
@ -21,7 +20,6 @@ import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
@ -88,21 +86,6 @@ public class Pop3StoreTest {
assertEquals("TestFolder", folder.getServerId());
}
@Test
public void getPersonalNamespace_shouldReturnListConsistingOfInbox() throws Exception {
List<Pop3Folder> folders = store.getPersonalNamespaces();
assertEquals(1, folders.size());
assertEquals("INBOX", folders.get(0).getServerId());
}
@Test
public void isSeenFlagSupported_shouldReturnFalse() throws Exception {
boolean result = store.isSeenFlagSupported();
assertFalse(result);
}
@Test(expected = MessagingException.class)
public void checkSetting_whenConnectionThrowsException_shouldThrowMessagingException()
throws Exception {

View file

@ -25,7 +25,7 @@ import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.filter.Base64;
import com.fsck.k9.mail.ssl.TrustManagerFactory;
import com.fsck.k9.mail.store.RemoteStore;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.StoreConfig;
import com.fsck.k9.mail.store.webdav.WebDavHttpClient.WebDavHttpClientFactory;
import javax.net.ssl.SSLException;
@ -61,7 +61,8 @@ import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8;
* </pre>
*/
@SuppressWarnings("deprecation")
public class WebDavStore extends RemoteStore {
public class WebDavStore {
private final StoreConfig storeConfig;
private ConnectionSecurity mConnectionSecurity;
private String username;
private String alias;
@ -91,7 +92,7 @@ public class WebDavStore extends RemoteStore {
public WebDavStore(TrustManagerFactory trustManagerFactory, WebDavStoreSettings serverSettings, StoreConfig storeConfig,
WebDavHttpClientFactory clientFactory) {
super(storeConfig, null);
this.storeConfig = storeConfig;
httpClientFactory = clientFactory;
this.trustManagerFactory = trustManagerFactory;
@ -154,15 +155,13 @@ public class WebDavStore extends RemoteStore {
}
StoreConfig getStoreConfig() {
return mStoreConfig;
return storeConfig;
}
@Override
public void checkSettings() throws MessagingException {
authenticate();
}
@Override
public List<? extends Folder> getPersonalNamespaces() throws MessagingException {
List<Folder> folderList = new LinkedList<>();
/*
@ -272,7 +271,6 @@ public class WebDavStore extends RemoteStore {
return null;
}
@Override
public WebDavFolder getFolder(String name) {
WebDavFolder folder = this.folderList.get(name);
@ -292,16 +290,6 @@ public class WebDavStore extends RemoteStore {
return sendFolder;
}
@Override
public boolean isMoveCapable() {
return true;
}
@Override
public boolean isCopyCapable() {
return true;
}
private String getSpecialFoldersList() {
return "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>" +
"<propfind xmlns=\"DAV:\">" +
@ -948,9 +936,8 @@ public class WebDavStore extends RemoteStore {
return dataset;
}
@Override
public void sendMessages(List<? extends Message> messages) throws MessagingException {
WebDavFolder tmpFolder = getFolder(mStoreConfig.getDraftsFolder());
WebDavFolder tmpFolder = getFolder(storeConfig.getDraftsFolder());
try {
tmpFolder.open(Folder.OPEN_MODE_RW);
List<? extends Message> retMessages = tmpFolder.appendWebDavMessages(messages);