You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/04/19 04:04:25 UTC
[james-project] 34/49: JAMES-3737 IMAP: Reactify DELETE
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit a8e3e5fc859cc71218ebf90a1ee2b18691dd89e7
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon Apr 4 21:28:19 2022 +0700
JAMES-3737 IMAP: Reactify DELETE
---
.../org/apache/james/mailbox/MailboxManager.java | 7 +++
.../james/mailbox/store/StoreMailboxManager.java | 15 ++++++
.../james/imap/api/process/SelectedMailbox.java | 2 +
.../james/imap/processor/DeleteProcessor.java | 59 ++++++++++++++--------
.../imap/processor/base/SelectedMailboxImpl.java | 6 +++
5 files changed, 69 insertions(+), 20 deletions(-)
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
index 58054330d2..79f08eda5e 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
@@ -38,7 +38,10 @@ import org.apache.james.mailbox.model.ThreadId;
import org.apache.james.mailbox.model.search.MailboxQuery;
import org.reactivestreams.Publisher;
+import com.github.fge.lambdas.Throwing;
+
import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
/**
* <p>
@@ -162,6 +165,10 @@ public interface MailboxManager extends RequestAware, RightManager, MailboxAnnot
*/
void deleteMailbox(MailboxPath mailboxPath, MailboxSession session) throws MailboxException;
+ default Mono<Void> deleteMailboxReactive(MailboxPath mailboxPath, MailboxSession session) {
+ return Mono.fromRunnable(Throwing.runnable(() -> deleteMailbox(mailboxPath, session)));
+ }
+
/**
* Delete the mailbox with the given id
*
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
index d27c95088b..44176ac5c7 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
@@ -449,6 +449,21 @@ public class StoreMailboxManager implements MailboxManager {
.flatMap(mailbox -> doDeleteMailbox(mailboxMapper, mailbox, session))));
}
+ @Override
+ public Mono<Void> deleteMailboxReactive(MailboxPath mailboxPath, MailboxSession session) {
+ LOGGER.info("deleteMailbox {}", mailboxPath);
+ if (!mailboxPath.belongsTo(session)) {
+ LOGGER.info("Mailbox {} does not belong to {}", mailboxPath.asString(), session.getUser().asString());
+ return Mono.error(new MailboxNotFoundException(mailboxPath.asString()));
+ }
+ MailboxMapper mailboxMapper = mailboxSessionMapperFactory.getMailboxMapper(session);
+
+ return mailboxMapper.executeReactive(mailboxMapper.findMailboxByPath(mailboxPath)
+ .flatMap(mailbox -> doDeleteMailbox(mailboxMapper, mailbox, session))
+ .switchIfEmpty(Mono.error(() -> new MailboxNotFoundException(mailboxPath))))
+ .then();
+ }
+
private Mono<Mailbox> doDeleteMailbox(MailboxMapper mailboxMapper, Mailbox mailbox, MailboxSession session) {
MessageMapper messageMapper = mailboxSessionMapperFactory.getMessageMapper(session);
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/api/process/SelectedMailbox.java b/protocols/imap/src/main/java/org/apache/james/imap/api/process/SelectedMailbox.java
index 529d6f618d..d6ab546301 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/api/process/SelectedMailbox.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/api/process/SelectedMailbox.java
@@ -95,6 +95,8 @@ public interface SelectedMailbox {
*/
MailboxPath getPath() throws MailboxException;
+ Mono<MailboxPath> getPathReactive();
+
/**
* Return the mailboxId of the selected Mailbox.
*
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java
index 9b13ad3fe5..8a19c7aa2e 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/DeleteProcessor.java
@@ -37,6 +37,8 @@ import org.apache.james.util.MDCBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import reactor.core.publisher.Mono;
+
public class DeleteProcessor extends AbstractMailboxProcessor<DeleteRequest> {
private static final Logger LOGGER = LoggerFactory.getLogger(DeleteProcessor.class);
@@ -45,27 +47,44 @@ public class DeleteProcessor extends AbstractMailboxProcessor<DeleteRequest> {
}
@Override
- protected void processRequest(DeleteRequest request, ImapSession session, Responder responder) {
- final MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(request.getMailboxName());
- try {
- final SelectedMailbox selected = session.getSelected();
- if (selected != null && selected.getPath().equals(mailboxPath)) {
- session.deselect().block();
- }
- final MailboxManager mailboxManager = getMailboxManager();
- mailboxManager.deleteMailbox(mailboxPath, session.getMailboxSession());
- unsolicitedResponses(session, responder, false).block();
- okComplete(request, responder);
- } catch (MailboxNotFoundException e) {
- LOGGER.debug("Delete failed for mailbox {} as it doesn't exist", mailboxPath, e);
- no(request, responder, HumanReadableText.FAILURE_NO_SUCH_MAILBOX);
- } catch (TooLongMailboxNameException e) {
- LOGGER.debug("The mailbox name length is over limit: {}", mailboxPath.getName(), e);
- taggedBad(request, responder, HumanReadableText.FAILURE_MAILBOX_NAME);
- } catch (MailboxException e) {
- LOGGER.error("Delete failed for mailbox {}", mailboxPath, e);
- no(request, responder, HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING);
+ protected Mono<Void> processRequestReactive(DeleteRequest request, ImapSession session, Responder responder) {
+ MailboxManager mailboxManager = getMailboxManager();
+ MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(request.getMailboxName());
+ SelectedMailbox selected = session.getSelected();
+
+ return deselect(session, selected, mailboxPath)
+ .then(mailboxManager.deleteMailboxReactive(mailboxPath, session.getMailboxSession()))
+ .then(unsolicitedResponses(session, responder, false))
+ .then(Mono.fromRunnable(() -> okComplete(request, responder)))
+ .then()
+ .onErrorResume(MailboxNotFoundException.class, e -> {
+ LOGGER.debug("Delete failed for mailbox {} as it doesn't exist", mailboxPath, e);
+ no(request, responder, HumanReadableText.FAILURE_NO_SUCH_MAILBOX);
+ return Mono.empty();
+ })
+ .onErrorResume(TooLongMailboxNameException.class, e -> {
+ LOGGER.debug("The mailbox name length is over limit: {}", mailboxPath.getName(), e);
+ taggedBad(request, responder, HumanReadableText.FAILURE_MAILBOX_NAME);
+ return Mono.empty();
+ })
+ .onErrorResume(MailboxException.class, e -> {
+ LOGGER.error("Delete failed for mailbox {}", mailboxPath, e);
+ no(request, responder, HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING);
+ return Mono.empty();
+ });
+ }
+
+ private Mono<Void> deselect(ImapSession session, SelectedMailbox selected, MailboxPath mailboxPath) {
+ if (selected == null) {
+ return Mono.empty();
}
+ return selected.getPathReactive()
+ .flatMap(selectedPath -> {
+ if (selectedPath.equals(mailboxPath)) {
+ return session.deselect();
+ }
+ return Mono.empty();
+ });
}
@Override
diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java
index e280b50d53..8d23817f1a 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/base/SelectedMailboxImpl.java
@@ -242,6 +242,12 @@ public class SelectedMailboxImpl implements SelectedMailbox, EventListener {
return mailboxManager.getMailbox(mailboxId, mailboxSession).getMailboxPath();
}
+ @Override
+ public Mono<MailboxPath> getPathReactive() {
+ return Mono.from(mailboxManager.getMailboxReactive(mailboxId, mailboxSession))
+ .map(Throwing.function(MessageManager::getMailboxPath));
+ }
+
@Override
public MailboxId getMailboxId() {
return mailboxId;
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org