JAMES-2082 Solve some other unbound futures when exposed on top level APIs

Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/2209b4f1
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/2209b4f1
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/2209b4f1

Branch: refs/heads/master
Commit: 2209b4f1b89fff440785f249eb41a1b3ac97a107
Parents: ff5a1dd
Author: benwa <btell...@linagora.com>
Authored: Sun Jul 9 15:42:47 2017 +0700
Committer: Antoine Duprat <adup...@linagora.com>
Committed: Mon Jul 10 14:23:59 2017 +0200

----------------------------------------------------------------------
 .../james/jmap/mailet/VacationMailet.java       | 34 +++++++++++++-------
 .../jmap/methods/SetVacationResponseMethod.java |  2 +-
 .../james/jmap/mailet/VacationMailetTest.java   |  2 ++
 .../methods/SetVacationResponseMethodTest.java  |  2 ++
 4 files changed, 27 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/2209b4f1/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationMailet.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationMailet.java
 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationMailet.java
index 8d0ef5c..2aba067 100644
--- 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationMailet.java
+++ 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/VacationMailet.java
@@ -31,6 +31,7 @@ import org.apache.james.jmap.api.vacation.RecipientId;
 import org.apache.james.jmap.api.vacation.Vacation;
 import org.apache.james.jmap.api.vacation.VacationRepository;
 import org.apache.james.jmap.utils.MimeMessageBodyGenerator;
+import org.apache.james.util.CompletableFutureUtil;
 import org.apache.james.util.date.ZonedDateTimeProvider;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
@@ -39,6 +40,8 @@ import org.apache.mailet.base.GenericMailet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Functions;
+
 public class VacationMailet extends GenericMailet {
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(VacationMailet.class);
@@ -77,16 +80,22 @@ public class VacationMailet extends GenericMailet {
 
     public CompletableFuture<Void> manageVacation(MailAddress recipient, Mail 
processedMail, ZonedDateTime processingDate) {
         AccountId accountId = AccountId.fromString(recipient.toString());
-        CompletableFuture<Vacation> vacationFuture = 
vacationRepository.retrieveVacation(accountId);
-        CompletableFuture<Boolean> hasAlreadyBeenSent = 
notificationRegistry.isRegistered(
-            AccountId.fromString(recipient.toString()),
-            RecipientId.fromMailAddress(processedMail.getSender()));
-
-        return vacationFuture.thenAcceptBoth(hasAlreadyBeenSent, (vacation, 
alreadySent) -> {
-            if (shouldSendNotification(vacation, processingDate, alreadySent)) 
{
-                sendNotification(recipient, processedMail, vacation);
-            }
-        });
+
+        return CompletableFutureUtil.combine(
+                vacationRepository.retrieveVacation(accountId),
+                notificationRegistry.isRegistered(
+                    AccountId.fromString(recipient.toString()),
+                    RecipientId.fromMailAddress(processedMail.getSender())),
+                (vacation, alreadySent) ->
+                    sendNotificationIfRequired(recipient, processedMail, 
processingDate, vacation, alreadySent))
+            .thenCompose(f -> f);
+    }
+
+    private CompletableFuture<Void> sendNotificationIfRequired(MailAddress 
recipient, Mail processedMail, ZonedDateTime processingDate, Vacation vacation, 
Boolean alreadySent) {
+        if (shouldSendNotification(vacation, processingDate, alreadySent)) {
+            return sendNotification(recipient, processedMail, vacation);
+        }
+        return CompletableFuture.completedFuture(null);
     }
 
     private boolean shouldSendNotification(Vacation vacation, ZonedDateTime 
processingDate, boolean alreadySent) {
@@ -94,18 +103,19 @@ public class VacationMailet extends GenericMailet {
             && ! alreadySent;
     }
 
-    private void sendNotification(MailAddress recipient, Mail processedMail, 
Vacation vacation) {
+    private CompletableFuture<Void> sendNotification(MailAddress recipient, 
Mail processedMail, Vacation vacation) {
         try {
             VacationReply vacationReply = VacationReply.builder(processedMail)
                 .receivedMailRecipient(recipient)
                 .vacation(vacation)
                 .build(mimeMessageBodyGenerator);
             sendNotification(vacationReply);
-            
notificationRegistry.register(AccountId.fromString(recipient.toString()),
+            return 
notificationRegistry.register(AccountId.fromString(recipient.toString()),
                 RecipientId.fromMailAddress(processedMail.getSender()),
                 vacation.getToDate());
         } catch (MessagingException e) {
             LOGGER.warn("Failed to send JMAP vacation notification from {} to 
{}", recipient, processedMail.getSender(), e);
+            return CompletableFuture.completedFuture(null);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/2209b4f1/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
index ce9e397..208cb2d 100644
--- 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
+++ 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetVacationResponseMethod.java
@@ -102,7 +102,7 @@ public class SetVacationResponseMethod implements Method {
     private Stream<JmapResponse> process(ClientId clientId, AccountId 
accountId, VacationResponse vacationResponse) {
         if (vacationResponse.isValid()) {
             vacationRepository.modifyVacation(accountId, 
vacationResponse.getPatch()).join();
-            notificationRegistry.flush(accountId);
+            notificationRegistry.flush(accountId).join();
             return Stream.of(JmapResponse.builder()
                 .clientId(clientId)
                 .responseName(RESPONSE_NAME)

http://git-wip-us.apache.org/repos/asf/james-project/blob/2209b4f1/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationMailetTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationMailetTest.java
 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationMailetTest.java
index 1568dd0..5fc3297 100644
--- 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationMailetTest.java
+++ 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/VacationMailetTest.java
@@ -203,6 +203,8 @@ public class VacationMailetTest {
             .thenReturn(CompletableFuture.completedFuture(false));
         when(notificationRegistry.isRegistered(secondAccountId, 
RecipientId.fromMailAddress(originalSender)))
             .thenReturn(CompletableFuture.completedFuture(false));
+        when(notificationRegistry.register(any(), any(), any()))
+            .thenReturn(CompletableFuture.completedFuture(null));
 
         testee.service(mail);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/2209b4f1/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
index c01501c..e8c0007 100644
--- 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
+++ 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetVacationResponseMethodTest.java
@@ -198,6 +198,8 @@ public class SetVacationResponseMethodTest {
 
         when(mailboxSession.getUser()).thenReturn(USER);
         when(vacationRepository.modifyVacation(eq(accountId), 
any())).thenReturn(CompletableFuture.completedFuture(null));
+        when(notificationRegistry.flush(any()))
+            .thenReturn(CompletableFuture.completedFuture(null));
 
         Stream<JmapResponse> result = testee.process(setVacationRequest, 
clientId, mailboxSession);
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to