This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 26239da838ca51d766b4416840afdee65fef5dd5
Author: Tung Van TRAN <[email protected]>
AuthorDate: Wed Mar 29 16:44:45 2023 +0700

    RightManager should support reactive api
---
 .../org/apache/james/mailbox/RightManager.java     |  6 ++
 .../james/mailbox/store/StoreMailboxManager.java   | 15 +++++
 .../james/mailbox/store/StoreRightManager.java     | 75 +++++++++++++---------
 .../james/mailbox/store/StoreRightManagerTest.java |  4 +-
 4 files changed, 66 insertions(+), 34 deletions(-)

diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java 
b/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java
index 2eb2a2721d..01fa333a44 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/RightManager.java
@@ -45,6 +45,8 @@ public interface RightManager {
      */
     boolean hasRight(MailboxPath mailboxPath, Right right, MailboxSession 
session) throws MailboxException;
 
+    Publisher<Boolean> hasRightReactive(MailboxPath mailboxPath, Right right, 
MailboxSession session);
+
     boolean hasRight(Mailbox mailbox, Right right, MailboxSession session) 
throws MailboxException;
 
     /**
@@ -104,6 +106,8 @@ public interface RightManager {
      */
     Rfc4314Rights myRights(MailboxPath mailboxPath, MailboxSession session) 
throws MailboxException;
 
+    Publisher<Rfc4314Rights> myRightsReactive(MailboxPath mailboxPath, 
MailboxSession session);
+
     /**
      * Returns the rights applicable to the user who has sent the current
      * request on the mailbox designated by this mailboxPath.
@@ -140,6 +144,8 @@ public interface RightManager {
      */
     void applyRightsCommand(MailboxPath mailboxPath, MailboxACL.ACLCommand 
mailboxACLCommand, MailboxSession session) throws MailboxException;
 
+    Publisher<Void> applyRightsCommandReactive(MailboxPath mailboxPath, 
MailboxACL.ACLCommand mailboxACLCommand, MailboxSession session);
+
     /**
      * Update the Mailbox ACL of the designated mailbox. We can either ADD 
REPLACE or REMOVE entries.
      *
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 1a5304e0e1..788c53b47e 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
@@ -937,6 +937,11 @@ public class StoreMailboxManager implements MailboxManager 
{
         return storeRightManager.hasRight(mailboxPath, right, session);
     }
 
+    @Override
+    public Publisher<Boolean> hasRightReactive(MailboxPath mailboxPath, Right 
right, MailboxSession session) {
+        return storeRightManager.hasRightReactive(mailboxPath, right, session);
+    }
+
     @Override
     public boolean hasRight(Mailbox mailbox, Right right, MailboxSession 
session) throws MailboxException {
         return storeRightManager.hasRight(mailbox, right, session);
@@ -952,6 +957,11 @@ public class StoreMailboxManager implements MailboxManager 
{
         return storeRightManager.myRights(mailboxPath, session);
     }
 
+    @Override
+    public Publisher<Rfc4314Rights> myRightsReactive(MailboxPath mailboxPath, 
MailboxSession session) {
+        return storeRightManager.myRightsReactive(mailboxPath, session);
+    }
+
     @Override
     public Mono<Rfc4314Rights> myRights(MailboxId mailboxId, MailboxSession 
session) {
         return storeRightManager.myRights(mailboxId, session);
@@ -987,6 +997,11 @@ public class StoreMailboxManager implements MailboxManager 
{
         storeRightManager.applyRightsCommand(mailboxPath, mailboxACLCommand, 
session);
     }
 
+    @Override
+    public Publisher<Void> applyRightsCommandReactive(MailboxPath mailboxPath, 
MailboxACL.ACLCommand mailboxACLCommand, MailboxSession session) {
+        return storeRightManager.applyRightsCommandReactive(mailboxPath, 
mailboxACLCommand, session);
+    }
+
     @Override
     public void applyRightsCommand(MailboxId mailboxId, MailboxACL.ACLCommand 
mailboxACLCommand, MailboxSession session) throws MailboxException {
         storeRightManager.applyRightsCommand(mailboxId, mailboxACLCommand, 
session);
diff --git 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
index d8034cba36..5c4b20b07f 100644
--- 
a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
+++ 
b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreRightManager.java
@@ -34,6 +34,7 @@ import org.apache.james.core.Username;
 import org.apache.james.events.EventBus;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.RightManager;
+import org.apache.james.mailbox.acl.ACLDiff;
 import org.apache.james.mailbox.acl.MailboxACLResolver;
 import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
 import org.apache.james.mailbox.exception.DifferentDomainException;
@@ -78,6 +79,13 @@ public class StoreRightManager implements RightManager {
         return myRights(mailboxPath, session).contains(right);
     }
 
+    @Override
+    public Mono<Boolean> hasRightReactive(MailboxPath mailboxPath, Right 
right, MailboxSession session) {
+        return myRightsReactive(mailboxPath, session)
+            .filter(rights -> rights.contains(right))
+            .hasElement();
+    }
+
     @Override
     public boolean hasRight(MailboxId mailboxId, Right right, MailboxSession 
session) throws MailboxException {
         return block(Mono.from(myRights(mailboxId, session))).contains(right);
@@ -90,17 +98,20 @@ public class StoreRightManager implements RightManager {
 
     @Override
     public Rfc4314Rights myRights(MailboxPath mailboxPath, MailboxSession 
session) throws MailboxException {
-        if (mailboxPath.belongsTo(session)) {
-            if 
(mailboxSessionMapperFactory.getMailboxMapper(session).pathExists(mailboxPath).block())
 {
-                return MailboxACL.FULL_RIGHTS;
-            } else {
-                throw new MailboxNotFoundException(mailboxPath);
-            }
-        }
-        MailboxMapper mapper = 
mailboxSessionMapperFactory.getMailboxMapper(session);
-        Mailbox mailbox = blockOptional(mapper.findMailboxByPath(mailboxPath))
-            .orElseThrow(() -> new MailboxNotFoundException(mailboxPath));
-        return myRights(mailbox, session);
+        return MailboxReactorUtils.block(myRightsReactive(mailboxPath, 
session));
+    }
+
+    @Override
+    public Mono<Rfc4314Rights> myRightsReactive(MailboxPath mailboxPath, 
MailboxSession session) {
+        return mailboxSessionMapperFactory.getMailboxMapper(session)
+            .findMailboxByPath(mailboxPath)
+            .map(mailbox -> {
+                if (mailboxPath.belongsTo(session)) {
+                    return MailboxACL.FULL_RIGHTS;
+                } else {
+                    return myRights(mailbox, session);
+                }
+            }).switchIfEmpty(Mono.error(new 
MailboxNotFoundException(mailboxPath)));
     }
 
     @Override
@@ -158,21 +169,27 @@ public class StoreRightManager implements RightManager {
 
     @Override
     public void applyRightsCommand(MailboxPath mailboxPath, ACLCommand 
mailboxACLCommand, MailboxSession session) throws MailboxException {
-        assertSharesBelongsToUserDomain(mailboxPath.getUser(), 
mailboxACLCommand);
-        MailboxMapper mapper = 
mailboxSessionMapperFactory.getMailboxMapper(session);
-        block(mapper.findMailboxByPath(mailboxPath)
-            .flatMap(Throwing.<Mailbox, Mono<Void>>function(mailbox -> {
-                assertHaveAccessTo(mailbox, session);
+        MailboxReactorUtils.block(applyRightsCommandReactive(mailboxPath, 
mailboxACLCommand, session));
+    }
 
-                return mapper.updateACL(mailbox, mailboxACLCommand)
-                    .flatMap(aclDiff -> 
eventBus.dispatch(EventFactory.aclUpdated()
-                            .randomEventId()
-                            .mailboxSession(session)
-                            .mailbox(mailbox)
-                            .aclDiff(aclDiff)
-                            .build(),
-                        new MailboxIdRegistrationKey(mailbox.getMailboxId())));
-            }).sneakyThrow()));
+    @Override
+    public Mono<Void> applyRightsCommandReactive(MailboxPath mailboxPath, 
ACLCommand mailboxACLCommand, MailboxSession session) {
+        return Mono.just(mailboxSessionMapperFactory.getMailboxMapper(session))
+            .doOnNext(Throwing.consumer(mapper -> 
assertSharesBelongsToUserDomain(mailboxPath.getUser(), mailboxACLCommand)))
+            .flatMap(mapper -> mapper.findMailboxByPath(mailboxPath)
+                .doOnNext(Throwing.consumer(mailbox -> 
assertHaveAccessTo(mailbox, session)))
+                .flatMap(mailbox -> mapper.updateACL(mailbox, 
mailboxACLCommand)
+                    .flatMap(aclDiff -> dispatchACLUpdateEvent(session, 
mailbox, aclDiff))));
+    }
+
+    private Mono<Void> dispatchACLUpdateEvent(MailboxSession session, Mailbox 
mailbox, ACLDiff aclDiff) {
+        return eventBus.dispatch(EventFactory.aclUpdated()
+                .randomEventId()
+                .mailboxSession(session)
+                .mailbox(mailbox)
+                .aclDiff(aclDiff)
+                .build(),
+            new MailboxIdRegistrationKey(mailbox.getMailboxId()));
     }
 
     @Override
@@ -275,13 +292,7 @@ public class StoreRightManager implements RightManager {
 
     private Mono<Void> setRights(MailboxACL mailboxACL, MailboxMapper mapper, 
Mailbox mailbox, MailboxSession session) {
         return mapper.setACL(mailbox, mailboxACL)
-            .flatMap(aclDiff -> eventBus.dispatch(EventFactory.aclUpdated()
-                    .randomEventId()
-                    .mailboxSession(session)
-                    .mailbox(mailbox)
-                    .aclDiff(aclDiff)
-                    .build(),
-                new MailboxIdRegistrationKey(mailbox.getMailboxId())));
+            .flatMap(aclDiff -> dispatchACLUpdateEvent(session, mailbox, 
aclDiff));
     }
 
     /**
diff --git 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
index 0dad3c0519..f9360efe97 100644
--- 
a/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
+++ 
b/mailbox/store/src/test/java/org/apache/james/mailbox/store/StoreRightManagerTest.java
@@ -82,8 +82,8 @@ class StoreRightManagerTest {
     @Test
     void hasRightShouldThrowMailboxNotFoundExceptionWhenMailboxDoesNotExist() {
         MailboxPath mailboxPath = MailboxPath.forUser(MailboxFixture.ALICE, 
"unexisting mailbox");
-        when(mockedMailboxMapper.pathExists(mailboxPath))
-            .thenReturn(Mono.just(false));
+        when(mockedMailboxMapper.findMailboxByPath(mailboxPath))
+            .thenReturn(Mono.empty());
 
         assertThatThrownBy(() ->
             storeRightManager.hasRight(mailboxPath, Right.Read, aliceSession))


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to