Browse code

[mail-api] BM-14835 Fix: ensure the folder we delete or rename is not selected by the IMAP pool

Thomas Cataldo authored on 05/06/2019 07:19:53
Showing 1 changed files
... ...
@@ -25,7 +25,9 @@ import java.util.List;
25 25
 import java.util.Map;
26 26
 import java.util.Optional;
27 27
 import java.util.concurrent.CompletableFuture;
28
+import java.util.concurrent.ExecutionException;
28 29
 import java.util.concurrent.TimeUnit;
30
+import java.util.concurrent.TimeoutException;
29 31
 import java.util.stream.Collectors;
30 32
 
31 33
 import org.slf4j.Logger;
... ...
@@ -42,8 +44,8 @@ import net.bluemind.backend.mail.api.ImportMailboxItemsStatus.ImportStatus;
42 44
 import net.bluemind.backend.mail.api.ImportMailboxItemsStatus.ImportedMailboxItem;
43 45
 import net.bluemind.backend.mail.api.MailboxFolder;
44 46
 import net.bluemind.backend.mail.replica.api.IDbMailboxRecords;
45
-import net.bluemind.backend.mail.replica.api.ImapBinding;
46 47
 import net.bluemind.backend.mail.replica.api.IMailReplicaUids;
48
+import net.bluemind.backend.mail.replica.api.ImapBinding;
47 49
 import net.bluemind.backend.mail.replica.api.MailboxReplica;
48 50
 import net.bluemind.backend.mail.replica.api.MailboxReplicaRootDescriptor;
49 51
 import net.bluemind.backend.mail.replica.api.MailboxReplicaRootDescriptor.Namespace;
... ...
@@ -68,6 +70,7 @@ import net.bluemind.imap.FlagsList;
68 70
 import net.bluemind.imap.IMAPException;
69 71
 import net.bluemind.imap.ListResult;
70 72
 import net.bluemind.imap.StoreClient;
73
+import net.bluemind.imap.vertx.VXStoreClient;
71 74
 
72 75
 public class ImapReplicatedMailboxesService extends BaseReplicatedMailboxesService
73 76
 		implements IMailboxFolders, IMailboxFoldersByContainer {
... ...
@@ -122,6 +125,7 @@ public class ImapReplicatedMailboxesService extends BaseReplicatedMailboxesServi
122 125
 		CompletableFuture<ItemIdentifier> future = ReplicationEvents.onSubtreeUpdate(toWatch);
123 126
 		return imapContext.withImapClient((sc, fast) -> {
124 127
 			logger.info("Rename attempt of {} to {}", fnOld, fnNew);
128
+			selectInbox(sc, fast);
125 129
 			sc.rename(fnOld, fnNew);
126 130
 			long version = future.get(10, TimeUnit.SECONDS).version;
127 131
 			return Ack.create(version);
... ...
@@ -194,16 +198,24 @@ public class ImapReplicatedMailboxesService extends BaseReplicatedMailboxesServi
194 198
 		final String fnName = newName;
195 199
 		imapContext.withImapClient((sc, fast) -> {
196 200
 			logger.info("Deleting {}", fnName);
201
+			selectInbox(sc, fast);
197 202
 			sc.deleteMailbox(fnName);
198 203
 			return future.get(10, TimeUnit.SECONDS);
199 204
 		});
200 205
 	}
201 206
 
207
+	private void selectInbox(StoreClient sc, VXStoreClient fast)
208
+			throws IMAPException, InterruptedException, ExecutionException, TimeoutException {
209
+		sc.select("INBOX");
210
+		fast.select("INBOX").get(5, TimeUnit.SECONDS);
211
+	}
212
+
202 213
 	@Override
203 214
 	public void deepDelete(long id) {
204 215
 		ItemValue<MailboxFolder> toDelete = getCompleteById(id);
205 216
 		logger.info("Start deepDelete of {}...", toDelete);
206 217
 		imapContext.withImapClient((sc, fast) -> {
218
+			selectInbox(sc, fast);
207 219
 			CompletableFuture<?> rootPromise = deleteChildFolders(toDelete, sc);
208 220
 
209 221
 			rootPromise = rootPromise.thenCompose(v -> {
... ...
@@ -224,6 +236,7 @@ public class ImapReplicatedMailboxesService extends BaseReplicatedMailboxesServi
224 236
 		ItemValue<MailboxFolder> toDelete = getCompleteById(id);
225 237
 		logger.info("Start emptying {}...", toDelete);
226 238
 		imapContext.withImapClient((sc, fast) -> {
239
+			selectInbox(sc, fast);
227 240
 			CompletableFuture<?> rootPromise = deleteChildFolders(toDelete, sc);
228 241
 			rootPromise = rootPromise.thenCompose(v -> {
229 242
 				logger.info("On purge of '{}'", toDelete.value.fullName);