james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1719394 - in /james/project/trunk: mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/ server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/ server/protocols/jmap/src/main/java/org/apache/james/jmap/model/ ser...
Date Fri, 11 Dec 2015 12:35:22 GMT
Author: matthieu
Date: Fri Dec 11 12:35:21 2015
New Revision: 1719394

URL: http://svn.apache.org/viewvc?rev=1719394&view=rev
Log:
JAMES-1644 (feature disabled) retrieve Message properties in GetMessagesMethod

Modified:
    james/project/trunk/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
    james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
    james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java

Modified: james/project/trunk/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
URL: http://svn.apache.org/viewvc/james/project/trunk/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java?rev=1719394&r1=1719393&r2=1719394&view=diff
==============================================================================
--- james/project/trunk/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
(original)
+++ james/project/trunk/mailbox/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMailboxMapper.java
Fri Dec 11 12:35:21 2015
@@ -37,7 +37,7 @@ public class InMemoryMailboxMapper imple
     
     private static final int INITIAL_SIZE = 128;
     private final Map<InMemoryId, Mailbox<InMemoryId>> mailboxesById;
-    private final static AtomicLong IDS = new AtomicLong();
+    private final AtomicLong mailboxIdGenerator = new AtomicLong();
 
     public InMemoryMailboxMapper() {
         mailboxesById = new ConcurrentHashMap<InMemoryId, Mailbox<InMemoryId>>(INITIAL_SIZE);
@@ -99,7 +99,7 @@ public class InMemoryMailboxMapper imple
     public void save(Mailbox<InMemoryId> mailbox) throws MailboxException {
         InMemoryId id = mailbox.getMailboxId();
         if (id == null) {
-            id = InMemoryId.of(IDS.incrementAndGet());
+            id = InMemoryId.of(mailboxIdGenerator.incrementAndGet());
             ((SimpleMailbox<InMemoryId>) mailbox).setMailboxId(id);
         }
         mailboxesById.put(id, mailbox);

Modified: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java?rev=1719394&r1=1719393&r2=1719394&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/GetMessagesMethod.java
Fri Dec 11 12:35:21 2015
@@ -31,7 +31,9 @@ import javax.inject.Inject;
 import org.apache.james.jmap.model.GetMessagesRequest;
 import org.apache.james.jmap.model.GetMessagesResponse;
 import org.apache.james.jmap.model.Message;
+import org.apache.james.jmap.model.Message.Builder;
 import org.apache.james.jmap.model.MessageId;
+import org.apache.james.jmap.model.Property;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.model.MessageRange;
@@ -43,8 +45,11 @@ import org.apache.james.mailbox.store.ma
 import org.javatuples.Pair;
 
 import com.github.fge.lambdas.Throwing;
+import com.github.fge.lambdas.functions.ThrowingBiFunction;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 
 public class GetMessagesMethod<Id extends MailboxId> implements Method {
 
@@ -79,11 +84,13 @@ public class GetMessagesMethod<Id extend
         GetMessagesRequest getMessagesRequest = (GetMessagesRequest) request;
         
         Function<MessageId, Stream<Pair<org.apache.james.mailbox.store.mail.model.Message<Id>,
MailboxPath>>> loadMessages = loadMessage(mailboxSession);
-        Function<Pair<org.apache.james.mailbox.store.mail.model.Message<Id>,
MailboxPath>, Message> toJmapMessage = toJmapMessage(mailboxSession);
+        Function<Pair<org.apache.james.mailbox.store.mail.model.Message<Id>,
MailboxPath>, Message> convertToJmapMessage = toJmapMessage(mailboxSession);
+        Function<Message, Message> filterFields = new JmapMessageFactory(getMessagesRequest);
         
         List<Message> result = getMessagesRequest.getIds().stream()
             .flatMap(loadMessages)
-            .map(toJmapMessage)
+            .map(convertToJmapMessage)
+//            .map(filterFields)
             .collect(Collectors.toList());
 
         return new GetMessagesResponse(result);
@@ -101,7 +108,6 @@ public class GetMessagesMethod<Id extend
                                     Pair<org.apache.james.mailbox.store.mail.model.Message<Id>,

                                          MailboxPath>>> 
                 loadMessage(MailboxSession mailboxSession) {
-        
         return Throwing
                 .function((MessageId messageId) -> {
                      MailboxPath mailboxPath = messageId.getMailboxPath(mailboxSession);
@@ -123,4 +129,33 @@ public class GetMessagesMethod<Id extend
         return targetStream.map(x -> Pair.with(x, mailboxPath));
     }
 
+    private static class JmapMessageFactory implements Function<Message, Message> {
+        
+        public ImmutableMap<Property, ThrowingBiFunction<Message, Message.Builder,
Message.Builder>> fieldCopiers = 
+                ImmutableMap.of(
+                        Property.id, (message, builder) -> builder.id(message.getId()),
+                        Property.subject, (message, builder) -> builder.subject(message.getSubject())
+                        );
+        
+        private final ImmutableList<Property> selectedProperties;
+        
+        public JmapMessageFactory(GetMessagesRequest messagesRequest) {
+            this.selectedProperties = messagesRequest.getProperties().orElse(Property.all());
+        }
+
+        @Override
+        public Message apply(Message input) {
+            Message.Builder builder = Message.builder();
+            
+            selectCopiers().forEach(f -> f.apply(input, builder));
+            
+            return builder.build();
+        }
+
+        private Stream<ThrowingBiFunction<Message, Builder, Builder>> selectCopiers()
{
+            return Stream.concat(selectedProperties.stream(), Stream.of(Property.id))
+                .filter(fieldCopiers::containsKey)
+                .map(fieldCopiers::get);
+        }   
+    }
 }

Modified: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java?rev=1719394&r1=1719393&r2=1719394&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Mailbox.java
Fri Dec 11 12:35:21 2015
@@ -285,4 +285,5 @@ public class Mailbox {
         return Objects.hash(id, name, parentId, role, sortOrder, mustBeOnlyMailbox, mayReadItems,
mayAddItems, 
                 mayRemoveItems, mayCreateChild, mayRename, mayDelete, totalMessages, unreadMessages,
totalThreads, unreadThreads);
     }
+    
 }

Modified: james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java?rev=1719394&r1=1719393&r2=1719394&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Property.java
Fri Dec 11 12:35:21 2015
@@ -18,6 +18,47 @@
  ****************************************************************/
 package org.apache.james.jmap.model;
 
-public class Property {
+import com.google.common.collect.ImmutableList;
 
+public enum Property {
+    id("id"),
+    blobId("blobId"),
+    threadId("threadId"),
+    mailboxIds("mailboxIds"),
+    inReplyToMessageId("inReplyToMessageId"),
+    isUnread("isUnread"),
+    isFlagged("isFlagged"),
+    isAnswered("isAnswered"),
+    isDraft("isDraft"),
+    hasAttachment("hasAttachment"),
+    headers("headers"),
+    from("from"),
+    to("to"),
+    cc("cc"),
+    bcc("bcc"),
+    replyTo("replyTo"),
+    subject("subject"),
+    date("date"),
+    size("size"),
+    preview("preview"),
+    textBody("textBody"),
+    htmlBody("htmlBody"),
+    attachments("attachments"),
+    attachedMessages("attachedMessages"),
+    body("body"),
+    headers_property("headers.property");
+    
+    private String property;
+
+    private Property(String property) {
+        this.property = property;
+    }
+    
+    public String getProperty() {
+        return property;
+    }
+    
+    public static ImmutableList<Property> all() {
+        return ImmutableList.copyOf(values());
+    }
 }

Modified: james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java?rev=1719394&r1=1719393&r2=1719394&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/GetMessagesMethodTest.java
Fri Dec 11 12:35:21 2015
@@ -29,7 +29,9 @@ import java.util.Locale;
 
 import org.apache.james.jmap.model.GetMessagesRequest;
 import org.apache.james.jmap.model.GetMessagesResponse;
+import org.apache.james.jmap.model.Message;
 import org.apache.james.jmap.model.MessageId;
+import org.apache.james.jmap.model.Property;
 import org.apache.james.mailbox.MailboxSession;
 import org.apache.james.mailbox.MessageManager;
 import org.apache.james.mailbox.acl.SimpleGroupMembershipResolver;
@@ -40,6 +42,7 @@ import org.apache.james.mailbox.inmemory
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.MockAuthenticator;
 import org.apache.james.mailbox.store.StoreMailboxManager;
+import org.assertj.core.groups.Tuple;
 import org.junit.Before;
 import org.junit.Test;
 import org.slf4j.Logger;
@@ -120,11 +123,13 @@ public class GetMessagesMethodTest {
     @Test
     public void processShouldFetchMessages() throws MailboxException {
         MessageManager inbox = mailboxManager.getMailbox(inboxPath, session);
-        ByteArrayInputStream messageContent = new ByteArrayInputStream("my message".getBytes(Charsets.UTF_8));
         Date now = new Date();
-        long message1Uid = inbox.appendMessage(messageContent, now, session, false, null);
-        long message2Uid = inbox.appendMessage(messageContent, now, session, false, null);
-        long message3Uid = inbox.appendMessage(messageContent, now, session, false, null);
+        ByteArrayInputStream message1Content = new ByteArrayInputStream("Subject: message
1 subject\r\n\r\nmy message".getBytes(Charsets.UTF_8));
+        long message1Uid = inbox.appendMessage(message1Content, now, session, false, null);
+        ByteArrayInputStream message2Content = new ByteArrayInputStream("Subject: message
2 subject\r\n\r\nmy message".getBytes(Charsets.UTF_8));
+        long message2Uid = inbox.appendMessage(message2Content, now, session, false, null);
+        ByteArrayInputStream message3Content = new ByteArrayInputStream("Great-Header: message
3 subject\r\n\r\nmy message".getBytes(Charsets.UTF_8));
+        long message3Uid = inbox.appendMessage(message3Content, now, session, false, null);
         
         GetMessagesRequest request = GetMessagesRequest.builder()
                 .ids(new MessageId(ROBERT, inboxPath, message1Uid),
@@ -135,7 +140,71 @@ public class GetMessagesMethodTest {
         GetMessagesMethod<InMemoryId> testee = new GetMessagesMethod<>(mailboxSessionMapperFactory,
mailboxSessionMapperFactory);
         GetMessagesResponse result = testee.process(request, session);
         
-        assertThat(result.list()).extracting(message -> message.getId().getUid()).containsOnly(message1Uid,
message2Uid, message3Uid);
+        assertThat(result.list())
+            .extracting(message -> message.getId().getUid(), Message::getSubject)
+            .containsOnly(
+                    Tuple.tuple(message1Uid, "message 1 subject"), 
+                    Tuple.tuple(message2Uid, "message 2 subject"),
+                    Tuple.tuple(message3Uid, "(No subject)"));
+    }
+
+    @Test
+    public void processShouldReturnOnlyMessageIdsOnEmptyPropertyList() throws MailboxException
{
+        MessageManager inbox = mailboxManager.getMailbox(inboxPath, session);
+        Date now = new Date();
+        ByteArrayInputStream message1Content = new ByteArrayInputStream("Subject: message
1 subject\r\n\r\nmy message".getBytes(Charsets.UTF_8));
+        long message1Uid = inbox.appendMessage(message1Content, now, session, false, null);
+        
+        GetMessagesRequest request = GetMessagesRequest.builder()
+                .ids(new MessageId(ROBERT, inboxPath, message1Uid))
+                .properties(new Property[0])
+                .build();
+
+        GetMessagesMethod<InMemoryId> testee = new GetMessagesMethod<>(mailboxSessionMapperFactory,
mailboxSessionMapperFactory);
+        GetMessagesResponse result = testee.process(request, session);
+        
+        assertThat(result.list())
+            .extracting(message -> message.getId().getUid(), Message::getSubject)
+            .containsOnly(Tuple.tuple(message1Uid, "message 1 subject")); 
+    }
+
+    @Test
+    public void processShouldReturnIdWhenNotInPropertyList() throws MailboxException {
+        MessageManager inbox = mailboxManager.getMailbox(inboxPath, session);
+        Date now = new Date();
+        ByteArrayInputStream message1Content = new ByteArrayInputStream("Subject: message
1 subject\r\n\r\nmy message".getBytes(Charsets.UTF_8));
+        long message1Uid = inbox.appendMessage(message1Content, now, session, false, null);
+        
+        GetMessagesRequest request = GetMessagesRequest.builder()
+                .ids(new MessageId(ROBERT, inboxPath, message1Uid))
+                .properties(Property.subject)
+                .build();
+
+        GetMessagesMethod<InMemoryId> testee = new GetMessagesMethod<>(mailboxSessionMapperFactory,
mailboxSessionMapperFactory);
+        GetMessagesResponse result = testee.process(request, session);
+        
+        assertThat(result.list())
+            .extracting(message -> message.getId().getUid(), Message::getSubject)
+            .containsOnly(Tuple.tuple(message1Uid, "message 1 subject")); 
     }
     
+    @Test
+    public void processShouldReturnAllFieldsWhenUndefinedPropertyList() throws MailboxException
{
+        MessageManager inbox = mailboxManager.getMailbox(inboxPath, session);
+        Date now = new Date();
+        ByteArrayInputStream message1Content = new ByteArrayInputStream("Subject: message
1 subject\r\n\r\nmy message".getBytes(Charsets.UTF_8));
+        long message1Uid = inbox.appendMessage(message1Content, now, session, false, null);
+        
+        GetMessagesRequest request = GetMessagesRequest.builder()
+                .ids(new MessageId(ROBERT, inboxPath, message1Uid))
+                .build();
+
+        GetMessagesMethod<InMemoryId> testee = new GetMessagesMethod<>(mailboxSessionMapperFactory,
mailboxSessionMapperFactory);
+        GetMessagesResponse result = testee.process(request, session);
+        
+        assertThat(result.list())
+            .extracting(message -> message.getId().getUid(), Message::getSubject)
+            .containsOnly(Tuple.tuple(message1Uid, "message 1 subject")); 
+    }
+
 }

Modified: james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java?rev=1719394&r1=1719393&r2=1719394&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java
(original)
+++ james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/GetMessagesRequestTest.java
Fri Dec 11 12:35:21 2015
@@ -29,7 +29,7 @@ public class GetMessagesRequestTest {
 
     @Test
     public void shouldAllowOptionalAccountId() {
-        GetMessagesRequest result = GetMessagesRequest.builder().ids(MessageId.of("user-inbox-1")).properties(new
Property()).build();
+        GetMessagesRequest result = GetMessagesRequest.builder().ids(MessageId.of("user-inbox-1")).properties(Property.id).build();
         assertThat(result).isNotNull();
         assertThat(result.getAccountId()).isEmpty();
     }
@@ -41,7 +41,7 @@ public class GetMessagesRequestTest {
 
     @Test
     public void shouldAllowEmptyMessagesList() {
-        GetMessagesRequest result = GetMessagesRequest.builder().accountId("accountId").ids().properties(new
Property()).build();
+        GetMessagesRequest result = GetMessagesRequest.builder().accountId("accountId").ids().properties(Property.id).build();
         assertThat(result).isNotNull();
         assertThat(result.getIds()).isEmpty();
     }



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


Mime
View raw message