Author: matthieu
Date: Fri Dec 11 12:33:11 2015
New Revision: 1719383

URL: http://svn.apache.org/viewvc?rev=1719383&view=rev
Log:
JAMES-1644 Implement inMailboxes filtering in GetMessageListMethod

Modified:
    
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java

Modified: 
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java?rev=1719383&r1=1719382&r2=1719383&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap-integration-testing/src/test/java/org/apache/james/jmap/methods/GetMessageListMethodTest.java
 Fri Dec 11 12:33:11 2015
@@ -149,4 +149,77 @@ public abstract class GetMessageListMeth
                     +   
"\"canCalculateUpdates\":false,\"position\":0,\"total\":0,\"threadIds\":[],\"messageIds\":[\"1\",\"2\"]},"
                     + "\"#0\"]]"));
     }
+
+    @Test
+    public void 
getMessageListShouldFilterMessagesWhenInMailboxesFilterMatches() throws 
Exception {
+        String user = "user";
+        
jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, user, 
"mailbox");
+
+        jmapServer.serverProbe().appendMessage(user, new 
MailboxPath(MailboxConstants.USER_NAMESPACE, user, "mailbox"), 
+                new ByteArrayInputStream("Subject: 
test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags());
+        embeddedElasticSearch.awaitForElasticSearch();
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMessageList\", 
{\"filter\":{\"inMailboxes\":[\"mailbox\"]}}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .content(startsWith("[[\"getMessageList\","
+                    + 
"{\"accountId\":null,\"filter\":null,\"sort\":[],\"collapseThreads\":false,\"state\":null,"
+                    +   
"\"canCalculateUpdates\":false,\"position\":0,\"total\":0,\"threadIds\":[],\"messageIds\":[\"1\"]},"
+                    + "\"#0\"]]"));
+    }
+
+    @Test
+    public void 
getMessageListShouldFilterMessagesWhenMultipleInMailboxesFilterMatches() throws 
Exception {
+        String user = "user";
+        
jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, user, 
"mailbox");
+        jmapServer.serverProbe().appendMessage(user, new 
MailboxPath(MailboxConstants.USER_NAMESPACE, user, "mailbox"), 
+                new ByteArrayInputStream("Subject: 
test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags());
+
+        
jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, user, 
"mailbox2");
+        embeddedElasticSearch.awaitForElasticSearch();
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMessageList\", 
{\"filter\":{\"inMailboxes\":[\"mailbox\",\"mailbox2\"]}}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .content(startsWith("[[\"getMessageList\","
+                    + 
"{\"accountId\":null,\"filter\":null,\"sort\":[],\"collapseThreads\":false,\"state\":null,"
+                    +   
"\"canCalculateUpdates\":false,\"position\":0,\"total\":0,\"threadIds\":[],\"messageIds\":[\"1\"]},"
+                    + "\"#0\"]]"));
+    }
+
+    @Test
+    public void 
getMessageListShouldFilterMessagesWhenInMailboxesFilterDoesntMatches() throws 
Exception {
+        String user = "user";
+        
jmapServer.serverProbe().createMailbox(MailboxConstants.USER_NAMESPACE, user, 
"mailbox");
+
+        jmapServer.serverProbe().appendMessage(user, new 
MailboxPath(MailboxConstants.USER_NAMESPACE, user, "mailbox"), 
+                new ByteArrayInputStream("Subject: 
test\r\n\r\ntestmail".getBytes()), new Date(), false, new Flags());
+        embeddedElasticSearch.awaitForElasticSearch();
+
+        given()
+            .accept(ContentType.JSON)
+            .contentType(ContentType.JSON)
+            .header("Authorization", accessToken.serialize())
+            .body("[[\"getMessageList\", 
{\"filter\":{\"inMailboxes\":[\"mailbox2\"]}}, \"#0\"]]")
+        .when()
+            .post("/jmap")
+        .then()
+            .statusCode(200)
+            .content(startsWith("[[\"getMessageList\","
+                    + 
"{\"accountId\":null,\"filter\":null,\"sort\":[],\"collapseThreads\":false,\"state\":null,"
+                    +   
"\"canCalculateUpdates\":false,\"position\":0,\"total\":0,\"threadIds\":[],\"messageIds\":[]},"
+                    + "\"#0\"]]"));
+    }
 }

Modified: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java?rev=1719383&r1=1719382&r2=1719383&view=diff
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
 (original)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessageListMethod.java
 Fri Dec 11 12:33:11 2015
@@ -24,6 +24,7 @@ import java.util.Optional;
 
 import javax.inject.Inject;
 
+import org.apache.james.jmap.model.FilterCondition;
 import org.apache.james.jmap.model.GetMessageListRequest;
 import org.apache.james.jmap.model.GetMessageListResponse;
 import org.apache.james.mailbox.MailboxManager;
@@ -67,17 +68,18 @@ public class GetMessageListMethod<Id ext
     public GetMessageListResponse process(JmapRequest request, MailboxSession 
mailboxSession) {
         Preconditions.checkArgument(request instanceof GetMessageListRequest);
         try {
-            return getMessageListResponse(mailboxSession);
+            return getMessageListResponse((GetMessageListRequest) request, 
mailboxSession);
         } catch (MailboxException e) {
             throw Throwables.propagate(e);
         }
     }
 
-    private GetMessageListResponse getMessageListResponse(MailboxSession 
mailboxSession) throws MailboxException {
+    private GetMessageListResponse 
getMessageListResponse(GetMessageListRequest jmapRequest, MailboxSession 
mailboxSession) throws MailboxException {
         GetMessageListResponse.Builder builder = 
GetMessageListResponse.builder();
 
         mailboxManager.list(mailboxSession)
             .stream()
+            .filter(mailboxPath -> isMailboxRequested(jmapRequest, 
mailboxPath))
             .map(mailboxPath -> getMailbox(mailboxPath, mailboxSession))
             .map(messageManager -> getMessageIds(messageManager.get(), 
mailboxSession))
             .flatMap(List::stream)
@@ -87,6 +89,22 @@ public class GetMessageListMethod<Id ext
         return builder.build();
     }
 
+    private boolean isMailboxRequested(GetMessageListRequest jmapRequest, 
MailboxPath mailboxPath) {
+        if (jmapRequest.getFilter().isPresent()) {
+            return jmapRequest.getFilter()
+                .filter(FilterCondition.class::isInstance)
+                .map(FilterCondition.class::cast)
+                .map(FilterCondition::getInMailboxes)
+                .filter(list -> isMailboxInList(mailboxPath, list))
+                .isPresent();
+        }
+        return true;
+    }
+
+    private boolean isMailboxInList(MailboxPath mailboxPath, List<String> 
inMailboxes) {
+        return inMailboxes.contains(mailboxPath.getName());
+    }
+
     private Optional<MessageManager> getMailbox(MailboxPath mailboxPath, 
MailboxSession mailboxSession) {
         try {
             return Optional.of(mailboxManager.getMailbox(mailboxPath, 
mailboxSession));



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

Reply via email to