Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 81f85c69 authored by Hari's avatar Hari
Browse files

Move command execution code to ImapConnection

parent af59c529
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
@@ -41,6 +42,8 @@ import com.fsck.k9.mail.filter.PeekableInputStream;
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;
import com.fsck.k9.mail.oauth.XOAuth2ChallengeParser;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.imap.selectedstate.command.FolderSelectedStateCommand;
import com.fsck.k9.mail.store.imap.selectedstate.response.SelectedStateResponse;
import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZOutputStream;
import javax.net.ssl.SSLException;
@@ -738,6 +741,16 @@ public class ImapConnection {
        }
    }

    public <R extends SelectedStateResponse> R executeSelectedStateCommand(
            FolderSelectedStateCommand<R> command) throws IOException, MessagingException {
        List<String> splitCommands = command.optimizeAndSplit(isCondstoreCapable());
        List<List<ImapResponse>> responses = new ArrayList<>(splitCommands.size());
        for (String splitCommand : splitCommands) {
            responses.add(executeSimpleCommand(splitCommand));
        }
        return command.parseResponses(responses);
    }

    public List<ImapResponse> readStatusResponse(String tag, String commandToLog, UntaggedHandler untaggedHandler)
            throws IOException, NegativeImapResponseException {
        return responseParser.readStatusResponse(tag, commandToLog, getLogId(), untaggedHandler);
+60 −16
Original line number Diff line number Diff line
@@ -367,9 +367,12 @@ public class ImapFolder extends Folder<ImapMessage> {
        }

        UidCopyCommand command = UidCopyCommand.createWithUids(uids, escapedDestinationFolderName);
        UidCopyResponse response = command.execute(connection, this);

        try {
            UidCopyResponse response = connection.executeSelectedStateCommand(command);
            return response == null ? null : response.getUidMapping();
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

    @Override
@@ -434,9 +437,12 @@ public class ImapFolder extends Folder<ImapMessage> {
                .forbiddenFlags(forbiddenFlags)
                .build();

        UidSearchResponse searchResponse = searchCommand.execute(connection, this);
        try {
            UidSearchResponse searchResponse = connection.executeSelectedStateCommand(searchCommand);
            return searchResponse.getNumbers().size();

        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

    @Override
@@ -460,11 +466,13 @@ public class ImapFolder extends Folder<ImapMessage> {
                    .onlyHighestId(true)
                    .build();

            UidSearchResponse searchResponse = searchCommand.execute(connection, this);
            UidSearchResponse searchResponse = connection.executeSelectedStateCommand(searchCommand);

            return extractHighestUid(searchResponse);
        } catch (NegativeImapResponseException e) {
            return -1L;
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

@@ -514,7 +522,12 @@ public class ImapFolder extends Folder<ImapMessage> {
                .since(earliestDate)
                .forbiddenFlags(includeDeleted ? null : Collections.singleton(Flag.DELETED))
                .build();
        return getMessages(searchCommand.execute(connection, this), listener);
        try {
            UidSearchResponse searchResponse = connection.executeSelectedStateCommand(searchCommand);
            return getMessages(searchResponse, null);
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

    @Override
@@ -551,7 +564,8 @@ public class ImapFolder extends Folder<ImapMessage> {
                .since(earliestDate)
                .forbiddenFlags(Collections.singleton(Flag.DELETED))
                .build();
        UidSearchResponse response = searchCommand.execute(connection, this);

        UidSearchResponse response = connection.executeSelectedStateCommand(searchCommand);
        return response.getNumbers().size() > 0;
    }

@@ -564,7 +578,12 @@ public class ImapFolder extends Folder<ImapMessage> {
                .idSet(mesgSeqs)
                .forbiddenFlags(includeDeleted ? null : Collections.singleton(Flag.DELETED))
                .build();
        return getMessages(searchCommand.execute(connection, this), listener);
        try {
            UidSearchResponse searchResponse = connection.executeSelectedStateCommand(searchCommand);
            return getMessages(searchResponse, null);
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

    protected List<ImapMessage> getMessagesFromUids(final List<String> mesgUids) throws MessagingException {
@@ -579,7 +598,12 @@ public class ImapFolder extends Folder<ImapMessage> {
                .useUids(true)
                .idSet(uidSet)
                .build();
        return getMessages(searchCommand.execute(connection, this), null);
        try {
            UidSearchResponse searchResponse = connection.executeSelectedStateCommand(searchCommand);
            return getMessages(searchResponse, null);
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

    private List<ImapMessage> getMessages(UidSearchResponse searchResponse, MessageRetrievalListener<ImapMessage> listener)
@@ -1176,8 +1200,14 @@ public class ImapFolder extends Folder<ImapMessage> {
        UidSearchCommand searchCommand = new UidSearchCommand.Builder()
                .messageId(messageId)
                .build();
        UidSearchResponse searchResponse;
        try {
            searchResponse = connection.executeSelectedStateCommand(searchCommand);
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }

        List<Long> uids = searchCommand.execute(connection, this).getNumbers();
        List<Long> uids = searchResponse.getNumbers();
        if (uids.size() > 0) {
            return Long.toString(uids.get(0));
        }
@@ -1208,7 +1238,11 @@ public class ImapFolder extends Folder<ImapMessage> {

        UidStoreCommand command = UidStoreCommand.createWithAllUids(value, flags,
                canCreateForwardedFlag);
        command.execute(connection, this);
        try {
            connection.executeSelectedStateCommand(command);
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

    @Override
@@ -1249,7 +1283,11 @@ public class ImapFolder extends Folder<ImapMessage> {

        UidStoreCommand command = UidStoreCommand.createWithUids(uids, value, flags,
                canCreateForwardedFlag);
        command.execute(connection, this);
        try {
            connection.executeSelectedStateCommand(command);
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }

    private void checkOpen() throws MessagingException {
@@ -1258,7 +1296,7 @@ public class ImapFolder extends Folder<ImapMessage> {
        }
    }

    public MessagingException ioExceptionHandler(ImapConnection connection, IOException ioe) {
    private MessagingException ioExceptionHandler(ImapConnection connection, IOException ioe) {
        Timber.e(ioe, "IOException for %s", getLogId());

        if (connection != null) {
@@ -1326,7 +1364,13 @@ public class ImapFolder extends Folder<ImapMessage> {
                    .requiredFlags(requiredFlags)
                    .forbiddenFlags(forbiddenFlags)
                    .build();
            return getMessages(searchCommand.execute(connection, this), null);

            try {
                UidSearchResponse searchResponse = connection.executeSelectedStateCommand(searchCommand);
                return getMessages(searchResponse, null);
            } catch (IOException ioe) {
                throw ioExceptionHandler(connection, ioe);
            }
        } finally {
            inSearch = false;
        }
+11 −22
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.selectedstate.command;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.store.imap.ImapConnection;
import com.fsck.k9.mail.store.imap.ImapFolder;
import com.fsck.k9.mail.store.imap.ImapResponse;
import com.fsck.k9.mail.store.imap.ImapUtility;
import com.fsck.k9.mail.store.imap.selectedstate.response.SelectedStateResponse;


abstract class FolderSelectedStateCommand {
public abstract class FolderSelectedStateCommand<R extends SelectedStateResponse> {

    /* The below limits are 20 octets less than the recommended limits, in order to compensate for
    the length of the command tag, the space after the tag and the CRLF at the end of the command
@@ -57,29 +53,23 @@ abstract class FolderSelectedStateCommand {
        return builder.toString();
    }

    public SelectedStateResponse execute(ImapConnection connection, ImapFolder folder) throws MessagingException {
        return null;
    }

    List<List<ImapResponse>> executeInternal(ImapConnection connection, ImapFolder folder)
            throws IOException, MessagingException {
    public List<String> optimizeAndSplit(boolean condstoreCapable) {
        ImapCommandSplitter.optimizeGroupings(this);
        List<String> commands;
        String commandString = createCommandString();

        if (commandString.length() > getCommandLengthLimit(connection)) {
            commands = ImapCommandSplitter.splitCommand(this, getCommandLengthLimit(connection));
        List<String> commands;
        if (commandString.length() > getCommandLengthLimit(condstoreCapable)) {
            commands = ImapCommandSplitter.splitCommand(this,
                    getCommandLengthLimit(condstoreCapable));
        } else {
            commands = Collections.singletonList(commandString);
        }

        List<List<ImapResponse>> responses = new ArrayList<>();
        for (String command : commands) {
            responses.add(folder.executeSimpleCommand(command));
        }
        return responses;
        return commands;
    }

    public abstract R parseResponses(List<List<ImapResponse>> unparsedResponses);

    Set<Long> getIdSet() {
        return idSet;
    }
@@ -92,9 +82,8 @@ abstract class FolderSelectedStateCommand {
        return idGroups;
    }

    private int getCommandLengthLimit(ImapConnection connection) throws IOException, MessagingException  {
        boolean condstoreSupported = connection.isCondstoreCapable();
        if (condstoreSupported) {
    private int getCommandLengthLimit(boolean condstoreCapable) {
        if (condstoreCapable) {
            return LENGTH_LIMIT_WITH_CONDSTORE;
        } else {
            return LENGTH_LIMIT_WITHOUT_CONDSTORE;
+3 −12
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap.selectedstate.command;


import java.io.IOException;
import java.util.List;
import java.util.Set;

import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.store.imap.Commands;
import com.fsck.k9.mail.store.imap.ImapConnection;
import com.fsck.k9.mail.store.imap.ImapFolder;
import com.fsck.k9.mail.store.imap.ImapResponse;
import com.fsck.k9.mail.store.imap.selectedstate.response.UidCopyResponse;


public class UidCopyCommand extends FolderSelectedStateCommand {
public class UidCopyCommand extends FolderSelectedStateCommand<UidCopyResponse> {
    private String destinationFolderName;

    private UidCopyCommand(Set<Long> uids, String destinationFolderName) {
@@ -27,13 +23,8 @@ public class UidCopyCommand extends FolderSelectedStateCommand {
    }

    @Override
    public UidCopyResponse execute(ImapConnection connection, ImapFolder folder) throws MessagingException {
        try {
            List<List<ImapResponse>> responses = executeInternal(connection, folder);
            return UidCopyResponse.parse(responses);
        } catch (IOException ioe) {
            throw folder.ioExceptionHandler(connection, ioe);
        }
    public UidCopyResponse parseResponses(List<List<ImapResponse>> unparsedResponses) {
        return UidCopyResponse.parse(unparsedResponses);
    }

    public static UidCopyCommand createWithUids(Set<Long> uids, String destinationFolderName) {
+7 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package com.fsck.k9.mail.store.imap.selectedstate.command;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@@ -20,6 +21,7 @@ import com.fsck.k9.mail.store.imap.ImapConnection;
import com.fsck.k9.mail.store.imap.ImapResponse;
import com.fsck.k9.mail.store.imap.ImapResponseCallback;
import com.fsck.k9.mail.store.imap.ImapUtility;
import com.fsck.k9.mail.store.imap.selectedstate.response.SelectedStateResponse;


public class UidFetchCommand extends FolderSelectedStateCommand {
@@ -42,6 +44,11 @@ public class UidFetchCommand extends FolderSelectedStateCommand {
        return builder.toString().trim();
    }

    @Override
    public SelectedStateResponse parseResponses(List unparsedResponses) {
        return null;
    }

    public void send(ImapConnection connection) throws IOException, MessagingException {
        ImapCommandSplitter.optimizeGroupings(this);
        connection.sendCommand(createCommandString(), false);
Loading