james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject svn commit: r1418609 [2/2] - in /james: mailbox/trunk/api/src/main/java/org/apache/james/mailbox/ mailbox/trunk/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/ mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/ mailbox/trunk/jp...
Date Sat, 08 Dec 2012 06:46:08 GMT
Modified: james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java (original)
+++ james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java Sat Dec  8 06:46:05 2012
@@ -28,8 +28,8 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.SortedMap;
 import java.util.Map.Entry;
+import java.util.SortedMap;
 
 import javax.mail.Flags;
 import javax.mail.Flags.Flag;
@@ -43,8 +43,8 @@ import org.apache.james.mailbox.maildir.
 import org.apache.james.mailbox.maildir.mail.model.MaildirMessage;
 import org.apache.james.mailbox.model.MessageMetaData;
 import org.apache.james.mailbox.model.MessageRange;
-import org.apache.james.mailbox.model.UpdatedFlags;
 import org.apache.james.mailbox.model.MessageRange.Type;
+import org.apache.james.mailbox.model.UpdatedFlags;
 import org.apache.james.mailbox.store.SimpleMessageMetaData;
 import org.apache.james.mailbox.store.mail.AbstractMessageMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
@@ -56,11 +56,10 @@ public class MaildirMessageMapper extend
     private final MaildirStore maildirStore;
     private final static int BUF_SIZE = 2048;
 
-    public MaildirMessageMapper(MailboxSession session, MaildirStore  maildirStore) {
+    public MaildirMessageMapper(MailboxSession session, MaildirStore maildirStore) {
         super(session, maildirStore, maildirStore);
         this.maildirStore = maildirStore;
     }
-    
 
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#countMessagesInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox)
@@ -73,8 +72,8 @@ public class MaildirMessageMapper extend
         File[] newFiles = newFolder.listFiles();
         File[] curFiles = curFolder.listFiles();
         if (newFiles == null || curFiles == null)
-            throw new MailboxException("Unable to count messages in Mailbox " + mailbox,
-                    new IOException("Not a valid Maildir folder: " + maildirStore.getFolderName(mailbox)));
+            throw new MailboxException("Unable to count messages in Mailbox " + mailbox, new IOException(
+                    "Not a valid Maildir folder: " + maildirStore.getFolderName(mailbox)));
         int count = newFiles.length + curFiles.length;
         return count;
     }
@@ -90,14 +89,15 @@ public class MaildirMessageMapper extend
         String[] unseenMessages = curFolder.list(MaildirMessageName.FILTER_UNSEEN_MESSAGES);
         String[] newUnseenMessages = newFolder.list(MaildirMessageName.FILTER_UNSEEN_MESSAGES);
         if (newUnseenMessages == null || unseenMessages == null)
-            throw new MailboxException("Unable to count unseen messages in Mailbox " + mailbox,
-                    new IOException("Not a valid Maildir folder: " + maildirStore.getFolderName(mailbox)));
+            throw new MailboxException("Unable to count unseen messages in Mailbox " + mailbox, new IOException(
+                    "Not a valid Maildir folder: " + maildirStore.getFolderName(mailbox)));
         int count = newUnseenMessages.length + unseenMessages.length;
         return count;
     }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#delete(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.store.mail.model.Message)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#delete(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
      */
     @Override
     public void delete(Mailbox<Integer> mailbox, Message<Integer> message) throws MailboxException {
@@ -110,11 +110,13 @@ public class MaildirMessageMapper extend
     }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.model.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.model.MessageRange,
+     *      org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
     @Override
     public Iterator<Message<Integer>> findInMailbox(Mailbox<Integer> mailbox, MessageRange set, FetchType fType, int max)
-    throws MailboxException {
+            throws MailboxException {
         final List<Message<Integer>> results;
         final long from = set.getUidFrom();
         final long to = set.getUidTo();
@@ -132,108 +134,28 @@ public class MaildirMessageMapper extend
             break;
         case RANGE:
             results = findMessagesInMailboxBetweenUIDs(mailbox, null, from, to, max);
-            break;       
+            break;
         }
         return results.iterator();
-       
-    }
 
-    private List<Message<Integer>> findMessageInMailboxWithUID(Mailbox<Integer> mailbox, long uid)
-    throws MailboxException {
-        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
-        try {
-            MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, uid);
-        
-             ArrayList<Message<Integer>> messages = new ArrayList<Message<Integer>>();
-             if (messageName != null && messageName.getFile().exists()) {
-                 messages.add(new MaildirMessage(mailbox, uid, messageName));
-             }
-             return messages;
-
-        } catch (IOException e) {
-            throw new MailboxException("Failure while search for Message with uid " + uid + " in Mailbox " + mailbox, e );
-        }
-    }
-
-    private List<Message<Integer>> findMessagesInMailboxBetweenUIDs(Mailbox<Integer> mailbox,
-            FilenameFilter filter, long from, long to, int max) throws MailboxException {
-        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
-        int cur = 0;
-        SortedMap<Long, MaildirMessageName> uidMap = null;
-        try {
-            if (filter != null)
-                uidMap = folder.getUidMap(mailboxSession, filter, from, to);
-            else
-                uidMap = folder.getUidMap(mailboxSession, from, to);
-            
-            ArrayList<Message<Integer>> messages = new ArrayList<Message<Integer>>();
-            for (Entry<Long, MaildirMessageName> entry : uidMap.entrySet()) {
-                messages.add(new MaildirMessage(mailbox, entry.getKey(), entry.getValue()));
-                if (max != -1) {
-                	cur++;
-                	if (cur >= max) break;
-                }
-            }
-            return messages;
-        } catch (IOException e) {
-            throw new MailboxException("Failure while search for Messages in Mailbox " + mailbox, e );
-        }
-       
-    }
-
-
-    private List<Message<Integer>> findMessagesInMailbox(Mailbox<Integer> mailbox,
-            FilenameFilter filter, int limit) throws MailboxException {
-        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
-        try {
-            SortedMap<Long, MaildirMessageName> uidMap = folder.getUidMap(mailboxSession, filter, limit);
-            
-            ArrayList<Message<Integer>> filtered = new ArrayList<Message<Integer>>(uidMap.size());
-            for (Entry<Long, MaildirMessageName> entry : uidMap.entrySet())
-                filtered.add(new MaildirMessage(mailbox, entry.getKey(), entry.getValue()));
-            return filtered;
-        } catch (IOException e) {
-            throw new MailboxException("Failure while search for Messages in Mailbox " + mailbox, e );
-        }
-       
-    }
-
-    private List<Message<Integer>> findDeletedMessageInMailboxWithUID(
-            Mailbox<Integer> mailbox, long uid) throws MailboxException {
-        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
-        try {
-            MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, uid);
-             ArrayList<Message<Integer>> messages = new ArrayList<Message<Integer>>();
-             if (MaildirMessageName.FILTER_DELETED_MESSAGES.accept(null, messageName.getFullName())) {
-                 messages.add(new MaildirMessage(mailbox, uid, messageName));
-             }
-             return messages;
-
-        } catch (IOException e) {
-            throw new MailboxException("Failure while search for Messages in Mailbox " + mailbox, e );
-        }
-       
     }
 
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#findRecentMessageUidsInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox)
      */
     @Override
-    public List<Long> findRecentMessageUidsInMailbox(Mailbox<Integer> mailbox)
-    throws MailboxException {
+    public List<Long> findRecentMessageUidsInMailbox(Mailbox<Integer> mailbox) throws MailboxException {
         MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
         SortedMap<Long, MaildirMessageName> recentMessageNames = folder.getRecentMessages(mailboxSession);
         return new ArrayList<Long>(recentMessageNames.keySet());
-       
-        
+
     }
 
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#findFirstUnseenMessageUid(org.apache.james.mailbox.store.mail.model.Mailbox)
      */
     @Override
-    public Long findFirstUnseenMessageUid(Mailbox<Integer> mailbox)
-    throws MailboxException {
+    public Long findFirstUnseenMessageUid(Mailbox<Integer> mailbox) throws MailboxException {
         List<Message<Integer>> result = findMessagesInMailbox(mailbox, MaildirMessageName.FILTER_UNSEEN_MESSAGES, 1);
         if (result.isEmpty()) {
             return null;
@@ -242,33 +164,144 @@ public class MaildirMessageMapper extend
         }
     }
 
-
     /**
-     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#endRequest()
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#updateFlags(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      javax.mail.Flags, boolean, boolean,
+     *      org.apache.james.mailbox.model.MessageRange)
      */
     @Override
-    public void endRequest() {
-        // not used
-        
+    public Iterator<UpdatedFlags> updateFlags(final Mailbox<Integer> mailbox, final Flags flags, final boolean value,
+            final boolean replace, final MessageRange set) throws MailboxException {
+        final List<UpdatedFlags> updatedFlags = new ArrayList<UpdatedFlags>();
+        final MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
+
+        Iterator<Message<Integer>> it = findInMailbox(mailbox, set, FetchType.Metadata, -1);
+        while (it.hasNext()) {
+            final Message<Integer> member = it.next();
+            Flags originalFlags = member.createFlags();
+            if (replace) {
+                member.setFlags(flags);
+            } else {
+                Flags current = member.createFlags();
+                if (value) {
+                    current.add(flags);
+                } else {
+                    current.remove(flags);
+                }
+                member.setFlags(current);
+            }
+            Flags newFlags = member.createFlags();
+
+            try {
+                MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, member.getUid());
+                if (messageName != null) {
+                    File messageFile = messageName.getFile();
+                    // System.out.println("save existing " + message +
+                    // " as " + messageFile.getName());
+                    messageName.setFlags(member.createFlags());
+                    // this automatically moves messages from new to cur if
+                    // needed
+                    String newMessageName = messageName.getFullName();
+
+                    File newMessageFile;
+
+                    // See MAILBOX-57
+                    if (newFlags.contains(Flag.RECENT)) {
+                        // message is recent so save it in the new folder
+                        newMessageFile = new File(folder.getNewFolder(), newMessageName);
+                    } else {
+                        newMessageFile = new File(folder.getCurFolder(), newMessageName);
+                    }
+                    long modSeq;
+                    // if the flags don't have change we should not try to move
+                    // the file
+                    if (newMessageFile.equals(messageFile) == false) {
+                        FileUtils.moveFile(messageFile, newMessageFile);
+                        modSeq = newMessageFile.lastModified();
+
+                    } else {
+                        modSeq = messageFile.lastModified();
+                    }
+                    member.setModSeq(modSeq);
+
+                    updatedFlags.add(new UpdatedFlags(member.getUid(), modSeq, originalFlags, newFlags));
+
+                    long uid = member.getUid();
+                    folder.update(mailboxSession, uid, newMessageName);
+                }
+            } catch (IOException e) {
+                throw new MailboxException("Failure while save Message " + member + " in Mailbox " + mailbox, e);
+            }
+
+        }
+        return updatedFlags.iterator();
+
     }
 
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<Integer> mailbox, MessageRange set)
+            throws MailboxException {
+        List<Message<Integer>> results = new ArrayList<Message<Integer>>();
+        final long from = set.getUidFrom();
+        final long to = set.getUidTo();
+        final Type type = set.getType();
+        switch (type) {
+        default:
+        case ALL:
+            results = findMessagesInMailbox(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, -1);
+            break;
+        case FROM:
+            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, -1,
+                    -1);
+            break;
+        case ONE:
+            results = findDeletedMessageInMailboxWithUID(mailbox, from);
+            break;
+        case RANGE:
+            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, to,
+                    -1);
+            break;
+        }
+        Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
+        for (int i = 0; i < results.size(); i++) {
+            Message<Integer> m = results.get(i);
+            long uid = m.getUid();
+            uids.put(uid, new SimpleMessageMetaData(m));
+            delete(mailbox, m);
+        }
+
+        return uids;
+    }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#copy(org.apache.james.mailbox.store.mail.model.Mailbox, long, long, org.apache.james.mailbox.store.mail.model.Message)
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#move(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
      */
     @Override
-    protected MessageMetaData copy(Mailbox<Integer> mailbox, long uid, long modSeq, Message<Integer> original) throws MailboxException {
+    public MessageMetaData move(Mailbox<Integer> mailbox, Message<Integer> original) throws MailboxException {
+        throw new UnsupportedOperationException("Not implemented - see https://issues.apache.org/jira/browse/IMAP-370");
+    }
+
+    /**
+     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#copy(org.apache
+     *      .james.mailbox.store.mail.model.Mailbox, long, long,
+     *      org.apache.james.mailbox.store.mail.model.Message)
+     */
+    @Override
+    protected MessageMetaData copy(Mailbox<Integer> mailbox, long uid, long modSeq, Message<Integer> original)
+            throws MailboxException {
         SimpleMessage<Integer> theCopy = new SimpleMessage<Integer>(mailbox, original);
         Flags flags = theCopy.createFlags();
         flags.add(Flag.RECENT);
         theCopy.setFlags(flags);
-        return save(mailbox, theCopy);        
+        return save(mailbox, theCopy);
     }
 
     /**
-     * @see
-     * org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(org.apache.james.mailbox.store.mail.model.Mailbox,
-     * org.apache.james.mailbox.store.mail.model.Message)
+     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
      */
     @Override
     protected MessageMetaData save(Mailbox<Integer> mailbox, Message<Integer> message) throws MailboxException {
@@ -348,134 +381,114 @@ public class MaildirMessageMapper extend
 
     }
 
-
     /**
-     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#begin()
+     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#endRequest()
      */
     @Override
-    protected void begin() throws MailboxException {
-        //nothing todo
-    }
-
+    public void endRequest() {
+        // not used
 
-    /**
-     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#commit()
-     */
-    @Override
-    protected void commit() throws MailboxException {
-        //nothing todo
     }
 
+    private List<Message<Integer>> findMessageInMailboxWithUID(Mailbox<Integer> mailbox, long uid)
+            throws MailboxException {
+        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
+        try {
+            MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, uid);
 
-    /**
-     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#rollback()
-     */
-    @Override
-    protected void rollback() throws MailboxException {
-        //nothing todo
+            ArrayList<Message<Integer>> messages = new ArrayList<Message<Integer>>();
+            if (messageName != null && messageName.getFile().exists()) {
+                messages.add(new MaildirMessage(mailbox, uid, messageName));
+            }
+            return messages;
+
+        } catch (IOException e) {
+            throw new MailboxException("Failure while search for Message with uid " + uid + " in Mailbox " + mailbox, e);
+        }
     }
 
-    /**
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#updateFlags(org.apache.james.mailbox.store.mail.model.Mailbox, javax.mail.Flags, boolean, boolean, org.apache.james.mailbox.model.MessageRange)
-     */
-    @Override
-    public Iterator<UpdatedFlags> updateFlags(final Mailbox<Integer> mailbox, final Flags flags, final boolean value, final boolean replace, final MessageRange set) throws MailboxException {
-        final List<UpdatedFlags> updatedFlags = new ArrayList<UpdatedFlags>();
-        final MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
+    private List<Message<Integer>> findMessagesInMailboxBetweenUIDs(Mailbox<Integer> mailbox, FilenameFilter filter,
+            long from, long to, int max) throws MailboxException {
+        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
+        int cur = 0;
+        SortedMap<Long, MaildirMessageName> uidMap = null;
+        try {
+            if (filter != null)
+                uidMap = folder.getUidMap(mailboxSession, filter, from, to);
+            else
+                uidMap = folder.getUidMap(mailboxSession, from, to);
 
-        Iterator<Message<Integer>> it = findInMailbox(mailbox, set, FetchType.Metadata, -1);
-        while(it.hasNext()) {
-        	final Message<Integer> member = it.next();
-            Flags originalFlags = member.createFlags();
-            if (replace) {
-                member.setFlags(flags);
-            } else {
-                Flags current = member.createFlags();
-                if (value) {
-                    current.add(flags);
-                } else {
-                    current.remove(flags);
+            ArrayList<Message<Integer>> messages = new ArrayList<Message<Integer>>();
+            for (Entry<Long, MaildirMessageName> entry : uidMap.entrySet()) {
+                messages.add(new MaildirMessage(mailbox, entry.getKey(), entry.getValue()));
+                if (max != -1) {
+                    cur++;
+                    if (cur >= max)
+                        break;
                 }
-                member.setFlags(current);
             }
-            Flags newFlags = member.createFlags();
+            return messages;
+        } catch (IOException e) {
+            throw new MailboxException("Failure while search for Messages in Mailbox " + mailbox, e);
+        }
 
-            try {
-                MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, member.getUid());
-                if (messageName != null) {
-                    File messageFile = messageName.getFile();
-                    // System.out.println("save existing " + message +
-                    // " as " + messageFile.getName());
-                    messageName.setFlags(member.createFlags());
-                    // this automatically moves messages from new to cur if
-                    // needed
-                    String newMessageName = messageName.getFullName();
+    }
 
-                    File newMessageFile;
-                    
-                    // See MAILBOX-57
-                    if (newFlags.contains(Flag.RECENT)) {
-                        // message is recent so save it in the new folder
-                        newMessageFile = new File(folder.getNewFolder(), newMessageName);
-                    } else {
-                        newMessageFile = new File(folder.getCurFolder(), newMessageName);
-                    }
-                    long modSeq;
-                    // if the flags don't have change we should not try to move the file
-                    if (newMessageFile.equals(messageFile) == false) {
-                        FileUtils.moveFile(messageFile, newMessageFile );
-                        modSeq = newMessageFile.lastModified();
+    private List<Message<Integer>> findMessagesInMailbox(Mailbox<Integer> mailbox, FilenameFilter filter, int limit)
+            throws MailboxException {
+        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
+        try {
+            SortedMap<Long, MaildirMessageName> uidMap = folder.getUidMap(mailboxSession, filter, limit);
 
-                    } else {
-                        modSeq = messageFile.lastModified();
-                    } 
-                    member.setModSeq(modSeq);
-                    
-                    updatedFlags.add(new UpdatedFlags(member.getUid(), modSeq, originalFlags, newFlags));
+            ArrayList<Message<Integer>> filtered = new ArrayList<Message<Integer>>(uidMap.size());
+            for (Entry<Long, MaildirMessageName> entry : uidMap.entrySet())
+                filtered.add(new MaildirMessage(mailbox, entry.getKey(), entry.getValue()));
+            return filtered;
+        } catch (IOException e) {
+            throw new MailboxException("Failure while search for Messages in Mailbox " + mailbox, e);
+        }
 
-                    long uid = member.getUid();
-                    folder.update(mailboxSession, uid, newMessageName);
-                }
-            } catch (IOException e) {
-                throw new MailboxException("Failure while save Message " + member + " in Mailbox " + mailbox, e);
+    }
+
+    private List<Message<Integer>> findDeletedMessageInMailboxWithUID(Mailbox<Integer> mailbox, long uid)
+            throws MailboxException {
+        MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
+        try {
+            MaildirMessageName messageName = folder.getMessageNameByUid(mailboxSession, uid);
+            ArrayList<Message<Integer>> messages = new ArrayList<Message<Integer>>();
+            if (MaildirMessageName.FILTER_DELETED_MESSAGES.accept(null, messageName.getFullName())) {
+                messages.add(new MaildirMessage(mailbox, uid, messageName));
             }
+            return messages;
 
+        } catch (IOException e) {
+            throw new MailboxException("Failure while search for Messages in Mailbox " + mailbox, e);
         }
-        return updatedFlags.iterator();       
-        
+
+    }
+
+    /**
+     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#begin()
+     */
+    @Override
+    protected void begin() throws MailboxException {
+        // nothing todo
     }
 
+    /**
+     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#commit()
+     */
+    @Override
+    protected void commit() throws MailboxException {
+        // nothing todo
+    }
 
+    /**
+     * @see org.apache.james.mailbox.store.transaction.TransactionalMapper#rollback()
+     */
     @Override
-    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<Integer> mailbox, MessageRange set) throws MailboxException {
-        List<Message<Integer>> results = new ArrayList<Message<Integer>>();
-        final long from = set.getUidFrom();
-        final long to = set.getUidTo();
-        final Type type = set.getType();
-        switch (type) {
-        default:
-        case ALL:
-            results = findMessagesInMailbox(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, -1);
-            break;
-        case FROM:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, -1, -1);
-            break;
-        case ONE:
-            results = findDeletedMessageInMailboxWithUID(mailbox, from);
-            break;
-        case RANGE:
-            results = findMessagesInMailboxBetweenUIDs(mailbox, MaildirMessageName.FILTER_DELETED_MESSAGES, from, to, -1);
-            break;       
-        }
-        Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
-        for (int i = 0; i < results.size(); i++) {
-            Message<Integer> m = results.get(i);
-            long uid = m.getUid();
-            uids.put(uid, new SimpleMessageMetaData(m));
-            delete(mailbox, m);
-        }
-        
-        return uids;
+    protected void rollback() throws MailboxException {
+        // nothing todo
     }
 
 }

Modified: james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java (original)
+++ james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java Sat Dec  8 06:46:05 2012
@@ -46,12 +46,13 @@ public class InMemoryMessageMapper exten
 
     private Map<Long, Map<Long, Message<Long>>> mailboxByUid;
     private static final int INITIAL_SIZE = 256;
-    
-    public InMemoryMessageMapper(MailboxSession session, UidProvider<Long> uidProvider, ModSeqProvider<Long> modSeqProvider) {
+
+    public InMemoryMessageMapper(MailboxSession session, UidProvider<Long> uidProvider,
+            ModSeqProvider<Long> modSeqProvider) {
         super(session, uidProvider, modSeqProvider);
         this.mailboxByUid = new ConcurrentHashMap<Long, Map<Long, Message<Long>>>(INITIAL_SIZE);
     }
-    
+
     private Map<Long, Message<Long>> getMembershipByUidForMailbox(Mailbox<Long> mailbox) {
         Map<Long, Message<Long>> membershipByUid = mailboxByUid.get(mailbox.getMailboxId());
         if (membershipByUid == null) {
@@ -60,7 +61,7 @@ public class InMemoryMessageMapper exten
         }
         return membershipByUid;
     }
-    
+
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#countMessagesInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox)
      */
@@ -73,7 +74,7 @@ public class InMemoryMessageMapper exten
      */
     public long countUnseenMessagesInMailbox(Mailbox<Long> mailbox) throws MailboxException {
         long count = 0;
-        for(Message<Long> member:getMembershipByUidForMailbox(mailbox).values()) {
+        for (Message<Long> member : getMembershipByUidForMailbox(mailbox).values()) {
             if (!member.isSeen()) {
                 count++;
             }
@@ -83,72 +84,73 @@ public class InMemoryMessageMapper exten
 
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#delete(org.apache.james.mailbox.store.mail.model.Mailbox,
-     * org.apache.james.mailbox.store.mail.model.Message)
+     *      org.apache.james.mailbox.store.mail.model.Message)
      */
     public void delete(Mailbox<Long> mailbox, Message<Long> message) throws MailboxException {
         getMembershipByUidForMailbox(mailbox).remove(message.getUid());
     }
 
-
     /**
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.model.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.model.MessageRange,
+     *      org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
-    public Iterator<Message<Long>> findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType ftype, int max) throws MailboxException {
+    public Iterator<Message<Long>> findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType ftype, int max)
+            throws MailboxException {
         List<Message<Long>> results;
         final MessageRange.Type type = set.getType();
         switch (type) {
-            case ALL:
-                results = new ArrayList<Message<Long>>(getMembershipByUidForMailbox(mailbox).values());
-                break;
-            case FROM:
-                results = new ArrayList<Message<Long>>(getMembershipByUidForMailbox(mailbox).values());
-                for (final Iterator<Message<Long>> it=results.iterator();it.hasNext();) {
-                   if (it.next().getUid()< set.getUidFrom()) {
-                       it.remove(); 
-                   }
-                }
-                break;
-            case RANGE:
-                results = new ArrayList<Message<Long>>(getMembershipByUidForMailbox(mailbox).values());
-                for (final Iterator<Message<Long>> it=results.iterator();it.hasNext();) {
-                   final long uid = it.next().getUid();
-                if (uid<set.getUidFrom() || uid>set.getUidTo()) {
-                       it.remove(); 
-                   }
+        case ALL:
+            results = new ArrayList<Message<Long>>(getMembershipByUidForMailbox(mailbox).values());
+            break;
+        case FROM:
+            results = new ArrayList<Message<Long>>(getMembershipByUidForMailbox(mailbox).values());
+            for (final Iterator<Message<Long>> it = results.iterator(); it.hasNext();) {
+                if (it.next().getUid() < set.getUidFrom()) {
+                    it.remove();
                 }
-                break;
-            case ONE:
-                results  = new ArrayList<Message<Long>>(1);
-                final Message<Long> member = getMembershipByUidForMailbox(mailbox).get(set.getUidFrom());
-                if (member != null) {
-                    results.add(member);
+            }
+            break;
+        case RANGE:
+            results = new ArrayList<Message<Long>>(getMembershipByUidForMailbox(mailbox).values());
+            for (final Iterator<Message<Long>> it = results.iterator(); it.hasNext();) {
+                final long uid = it.next().getUid();
+                if (uid < set.getUidFrom() || uid > set.getUidTo()) {
+                    it.remove();
                 }
-                break;
-            default:
-                results = new ArrayList<Message<Long>>();
-                break;
+            }
+            break;
+        case ONE:
+            results = new ArrayList<Message<Long>>(1);
+            final Message<Long> member = getMembershipByUidForMailbox(mailbox).get(set.getUidFrom());
+            if (member != null) {
+                results.add(member);
+            }
+            break;
+        default:
+            results = new ArrayList<Message<Long>>();
+            break;
         }
         Collections.sort(results);
-        
+
         if (max > 0 && results.size() > max) {
-            results = results.subList(0, max -1);
+            results = results.subList(0, max - 1);
         }
         return results.iterator();
     }
-    
 
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#findRecentMessageUidsInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox)
      */
     public List<Long> findRecentMessageUidsInMailbox(Mailbox<Long> mailbox) throws MailboxException {
         final List<Long> results = new ArrayList<Long>();
-        for(Message<Long> member:getMembershipByUidForMailbox(mailbox).values()) {
+        for (Message<Long> member : getMembershipByUidForMailbox(mailbox).values()) {
             if (member.isRecent()) {
                 results.add(member.getUid());
             }
         }
         Collections.sort(results);
-        
+
         return results;
     }
 
@@ -158,7 +160,7 @@ public class InMemoryMessageMapper exten
     public Long findFirstUnseenMessageUid(Mailbox<Long> mailbox) throws MailboxException {
         List<Message<Long>> memberships = new ArrayList<Message<Long>>(getMembershipByUidForMailbox(mailbox).values());
         Collections.sort(memberships);
-        for (int i = 0;  i < memberships.size(); i++) {
+        for (int i = 0; i < memberships.size(); i++) {
             Message<Long> m = memberships.get(i);
             if (m.isSeen() == false) {
                 return m.getUid();
@@ -167,11 +169,39 @@ public class InMemoryMessageMapper exten
         return null;
     }
 
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(final Mailbox<Long> mailbox, MessageRange set)
+            throws MailboxException {
+        final Map<Long, MessageMetaData> filteredResult = new HashMap<Long, MessageMetaData>();
+
+        Iterator<Message<Long>> it = findInMailbox(mailbox, set, FetchType.Metadata, -1);
+        while (it.hasNext()) {
+            Message<Long> member = it.next();
+            if (member.isDeleted()) {
+                filteredResult.put(member.getUid(), new SimpleMessageMetaData(member));
+
+                delete(mailbox, member);
+            }
+        }
+        return filteredResult;
+    }
+
     public void deleteAll() {
         mailboxByUid.clear();
     }
 
     /**
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#move(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
+     */
+    @Override
+    public MessageMetaData move(Mailbox<Long> mailbox, Message<Long> original) throws MailboxException {
+        throw new UnsupportedOperationException("Not implemented - see https://issues.apache.org/jira/browse/IMAP-370");
+    }
+
+    /**
      * Do nothing
      */
     public void endRequest() {
@@ -179,14 +209,16 @@ public class InMemoryMessageMapper exten
     }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#copy(org.apache.james.mailbox.store.mail.model.Mailbox, long, long, org.apache.james.mailbox.store.mail.model.Message)
+     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#copy(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      long, long, org.apache.james.mailbox.store.mail.model.Message)
      */
-    protected MessageMetaData copy(Mailbox<Long> mailbox, long uid, long modSeq, Message<Long> original) throws MailboxException {
+    protected MessageMetaData copy(Mailbox<Long> mailbox, long uid, long modSeq, Message<Long> original)
+            throws MailboxException {
         SimpleMessage<Long> message = new SimpleMessage<Long>(mailbox, original);
         message.setUid(uid);
         message.setModSeq(modSeq);
         Flags flags = original.createFlags();
-        
+
         // Mark message as recent as it is a copy
         flags.add(Flag.RECENT);
         message.setFlags(flags);
@@ -194,14 +226,15 @@ public class InMemoryMessageMapper exten
     }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.store.mail.model.Message)
+     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
      */
     protected MessageMetaData save(Mailbox<Long> mailbox, Message<Long> message) throws MailboxException {
         SimpleMessage<Long> copy = new SimpleMessage<Long>(mailbox, message);
         copy.setUid(message.getUid());
         copy.setModSeq(message.getModSeq());
         getMembershipByUidForMailbox(mailbox).put(message.getUid(), copy);
-        
+
         return new SimpleMessageMetaData(message);
     }
 
@@ -209,36 +242,19 @@ public class InMemoryMessageMapper exten
      * Do nothing
      */
     protected void begin() throws MailboxException {
-        
+
     }
 
     /**
      * Do nothing
      */
     protected void commit() throws MailboxException {
-        
-    }
 
+    }
 
     /**
      * Do nothing
      */
-    protected void rollback() throws MailboxException {        
-    }
-
-    @Override
-    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(final Mailbox<Long> mailbox, MessageRange set) throws MailboxException {
-        final Map<Long, MessageMetaData> filteredResult = new HashMap<Long, MessageMetaData>();
-
-        Iterator<Message<Long>> it = findInMailbox(mailbox, set, FetchType.Metadata, -1);
-        while(it.hasNext()) {
-            Message<Long> member = it.next();
-            if (member.isDeleted()) {
-                filteredResult.put(member.getUid(), new SimpleMessageMetaData(member));
-
-                delete(mailbox, member);
-            }
-        }
-        return filteredResult;
+    protected void rollback() throws MailboxException {
     }
 }

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java Sat Dec  8 06:46:05 2012
@@ -85,6 +85,8 @@ public class StoreMailboxManager<Id> imp
     
     private int copyBatchSize = 0;
 
+	private int moveBatchSize = 0;
+
     private MailboxPathLocker locker;
 
     private MessageSearchIndex<Id> index;
@@ -114,6 +116,10 @@ public class StoreMailboxManager<Id> imp
         this.copyBatchSize = copyBatchSize;
     }
     
+    public void setMoveBatchSize(int moveBatchSize) {
+		this.moveBatchSize = moveBatchSize;
+	}
+
     public void setFetchBatchSize(int fetchBatchSize) {
         this.fetchBatchSize = fetchBatchSize;
     }
@@ -486,6 +492,26 @@ public class StoreMailboxManager<Id> imp
     }
 
     /**
+     * @see org.apache.james.mailbox.MailboxManager#moveMessages(MessageRange, MailboxPath, MailboxPath, MailboxSession)
+     */
+    @SuppressWarnings("unchecked")
+	public List<MessageRange> moveMessages(MessageRange set, MailboxPath from, MailboxPath to, MailboxSession session) throws MailboxException {
+        StoreMessageManager<Id> toMailbox = (StoreMessageManager<Id>) getMailbox(to, session);
+        StoreMessageManager<Id> fromMailbox = (StoreMessageManager<Id>) getMailbox(from, session);
+        
+        if (moveBatchSize > 0) {
+            List<MessageRange> movedRanges = new ArrayList<MessageRange>();
+            Iterator<MessageRange> ranges = set.split(moveBatchSize).iterator();
+            while(ranges.hasNext()) {
+                movedRanges.addAll(fromMailbox.copyTo(ranges.next(), toMailbox, session));
+            }
+            return movedRanges;
+        } else {
+            return fromMailbox.moveTo(set, toMailbox, session);
+        }
+    }
+
+    /**
      * @see org.apache.james.mailbox.MailboxManager#search(org.apache.james.mailbox.model.MailboxQuery, org.apache.james.mailbox.MailboxSession)
      */
     public List<MailboxMetaData> search(final MailboxQuery mailboxExpression, MailboxSession session)

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java Sat Dec  8 06:46:05 2012
@@ -574,6 +574,34 @@ public class StoreMessageManager<Id> imp
         }, true);
     }
 
+    /**
+     * Move the {@link MessageRange} to the {@link StoreMessageManager}
+     * 
+     * @param set
+     * @param toMailbox
+     * @param session
+     * @throws MailboxException
+     */
+    public List<MessageRange> moveTo(final MessageRange set, final StoreMessageManager<Id> toMailbox, final MailboxSession session) throws MailboxException {
+        if (!isWriteable(session)) {
+            throw new ReadOnlyException(new StoreMailboxPath<Id>(getMailboxEntity()), session.getPathDelimiter());
+        }
+        if (!toMailbox.isWriteable(session)) {
+            throw new ReadOnlyException(new StoreMailboxPath<Id>(toMailbox.getMailboxEntity()), session.getPathDelimiter());
+        }
+
+        //TODO lock the from mailbox too, in a non-deadlocking manner - how?
+        return locker.executeWithLock(session, new StoreMailboxPath<Id>(toMailbox.getMailboxEntity()), new MailboxPathLocker.LockAwareExecution<List<MessageRange>>() {
+
+            @Override
+            public List<MessageRange> execute() throws MailboxException {
+                SortedMap<Long, MessageMetaData> movedUids = move(set, toMailbox, session);
+                dispatcher.added(session, movedUids, toMailbox.getMailboxEntity());
+                return MessageRange.toRanges(new ArrayList<Long>(movedUids.keySet()));
+            }
+        }, true);
+    }
+
     protected MessageMetaData appendMessageToStore(final Message<Id> message, MailboxSession session) throws MailboxException {
         final MessageMapper<Id> mapper = mapperFactory.getMessageMapper(session);
         return mapperFactory.getMessageMapper(session).execute(new Mapper.Transaction<MessageMetaData>() {
@@ -679,6 +707,26 @@ public class StoreMessageManager<Id> imp
         return copiedRows.iterator();
     }
 
+    private Iterator<MessageMetaData> move(Iterator<Message<Id>> originalRows,
+			MailboxSession session) throws MailboxException {
+        final List<MessageMetaData> movedRows = new ArrayList<MessageMetaData>();
+        final MessageMapper<Id> messageMapper = mapperFactory.getMessageMapper(session);
+
+        while (originalRows.hasNext()) {
+            final Message<Id> originalMessage = originalRows.next();
+            MessageMetaData data = messageMapper.execute(new Mapper.Transaction<MessageMetaData>() {
+                public MessageMetaData run() throws MailboxException {
+                    return messageMapper.move(getMailboxEntity(), originalMessage);
+
+                }
+
+            });
+            movedRows.add(data);
+        }
+        return movedRows.iterator();
+	}
+
+
     /**
      * @see org.apache.james.mailbox.store.AbstractStoreMessageManager#copy(org.apache.james.mailbox.model.MessageRange,
      *      org.apache.james.mailbox.store.AbstractStoreMessageManager,
@@ -698,6 +746,22 @@ public class StoreMessageManager<Id> imp
         return copiedMessages;
     }
 
+    private SortedMap<Long, MessageMetaData> move(MessageRange set,
+			final StoreMessageManager<Id> to, final MailboxSession session) throws MailboxException {
+        MessageMapper<Id> messageMapper = mapperFactory.getMessageMapper(session);
+
+        final SortedMap<Long, MessageMetaData> movedMessages = new TreeMap<Long, MessageMetaData>();
+        Iterator<Message<Id>> originalRows = messageMapper.findInMailbox(mailbox, set, FetchType.Full, -1);
+        Iterator<MessageMetaData> ids = to.move(originalRows, session);
+        while (ids.hasNext()) {
+            MessageMetaData data = ids.next();
+            movedMessages.put(data.getUid(), data);
+        }
+
+        return movedMessages;
+	}
+
+
     /**
      * Return the count of unseen messages
      * 

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java Sat Dec  8 06:46:05 2012
@@ -152,6 +152,16 @@ public interface MessageMapper<Id> exten
      */
     MessageMetaData copy(Mailbox<Id> mailbox,Message<Id> original) throws MailboxException;
     
+    /**
+     * Move the given {@link Message} to a new mailbox and return the uid of the moved. Be aware that the given uid is just a suggestion for the uid of the moved
+     * message. Implementation may choose to use a different one, so only depend on the returned uid!
+     * 
+     * @param mailbox the Mailbox to move to
+     * @param original the original to move
+     * @throws StorageException
+     */
+    MessageMetaData move(Mailbox<Id> mailbox,Message<Id> original) throws MailboxException;
+    
     
     /**
      * Return the last uid which were used for storing a Message in the {@link Mailbox}

Modified: james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java (original)
+++ james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java Sat Dec  8 06:46:05 2012
@@ -34,8 +34,8 @@ import org.apache.james.mailbox.exceptio
 import org.apache.james.mailbox.model.MessageMetaData;
 import org.apache.james.mailbox.model.MessageRange;
 import org.apache.james.mailbox.model.MessageResult;
-import org.apache.james.mailbox.model.UpdatedFlags;
 import org.apache.james.mailbox.model.MessageResult.FetchGroup;
+import org.apache.james.mailbox.model.UpdatedFlags;
 import org.apache.james.mailbox.store.mail.MessageMapper;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.Message;
@@ -44,141 +44,134 @@ import org.apache.james.mailbox.store.ma
 import org.junit.Test;
 
 public class StoreMessageResultIteratorTest {
-	
-	@Test
-	public void testBatching() {
-		MessageRange range = MessageRange.range(1, 10);
-		int batchSize = 3;
-		StoreMessageResultIterator<Long> it = new StoreMessageResultIterator<Long>(new MessageMapper<Long>() {
-
-			@Override
-			public void endRequest() {	
-				throw new UnsupportedOperationException();
-			}
-
-			@Override
-			public <T> T execute(Transaction<T> transaction)
-					throws MailboxException {
-				throw new UnsupportedOperationException();
-			}
-
-			@Override
-			public Iterator<Message<Long>> findInMailbox(
-					Mailbox<Long> mailbox,
-					MessageRange set,
-					org.apache.james.mailbox.store.mail.MessageMapper.FetchType type,
-					int limit) throws MailboxException {
-				long start = set.getUidFrom();
-				long end = set.getUidTo();
-				long calcEnd = start + limit;
-				if (calcEnd > end) {
-					calcEnd = end;
-				}
-				
-				List<Message<Long>> messages = new ArrayList<Message<Long>>();
-				long i = start;
-				while ( i < calcEnd ) {
-					long uid = i;
-					SimpleMessage<Long> m = new SimpleMessage<Long>(null, 0, 0, new SharedByteArrayInputStream("".getBytes()), new Flags(), new PropertyBuilder(), 1L);
-					m.setUid(uid);
-					messages.add(m);
-					i++;
-				}
-				return messages.iterator();
-			}
-
-			@Override
-			public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(
-					Mailbox<Long> mailbox, MessageRange set)
-					throws MailboxException {
-				throw new UnsupportedOperationException();
-
-			}
-
-			@Override
-			public long countMessagesInMailbox(Mailbox<Long> mailbox)
-					throws MailboxException {
-				throw new UnsupportedOperationException();
-
-			}
-
-			@Override
-			public long countUnseenMessagesInMailbox(Mailbox<Long> mailbox)
-					throws MailboxException {
-				throw new UnsupportedOperationException();
-			}
-
-			@Override
-			public void delete(Mailbox<Long> mailbox, Message<Long> message)
-					throws MailboxException {
-				throw new UnsupportedOperationException();				
-			}
-
-			@Override
-			public Long findFirstUnseenMessageUid(Mailbox<Long> mailbox)
-					throws MailboxException {
-				throw new UnsupportedOperationException();
-			}
-
-			@Override
-			public List<Long> findRecentMessageUidsInMailbox(
-					Mailbox<Long> mailbox) throws MailboxException {
-				throw new UnsupportedOperationException();
-
-			}
-
-			@Override
-			public MessageMetaData add(Mailbox<Long> mailbox,
-					Message<Long> message) throws MailboxException {
-				throw new UnsupportedOperationException();
-			}
-
-			@Override
-			public Iterator<UpdatedFlags> updateFlags(Mailbox<Long> mailbox,
-					Flags flags, boolean value, boolean replace,
-					MessageRange set) throws MailboxException {
-				throw new UnsupportedOperationException();
-			}
-
-			@Override
-			public MessageMetaData copy(Mailbox<Long> mailbox,
-					Message<Long> original) throws MailboxException {
-				throw new UnsupportedOperationException();
-
-			}
-
-			@Override
-			public long getLastUid(Mailbox<Long> mailbox)
-					throws MailboxException {
-				throw new UnsupportedOperationException();
-			}
-
-			@Override
-			public long getHighestModSeq(Mailbox<Long> mailbox)
-					throws MailboxException {
-				throw new UnsupportedOperationException();
-			}
-			
-		}, null, range, batchSize, new FetchGroup() {
-			
-			@Override
-			public Set<PartContentDescriptor> getPartContentDescriptors() {
-				return null;
-			}
-			
-			@Override
-			public int content() {
-				return FetchGroup.MINIMAL;
-			}
-		});
-		
-		long i = 1;
-		while(it.hasNext()) {
-			MessageResult r = it.next();
-			Assert.assertEquals(i++, r.getUid());
-		}
-		Assert.assertEquals(10, i);
-		
-	}
+
+    @Test
+    public void testBatching() {
+        MessageRange range = MessageRange.range(1, 10);
+        int batchSize = 3;
+        StoreMessageResultIterator<Long> it = new StoreMessageResultIterator<Long>(new MessageMapper<Long>() {
+
+            @Override
+            public void endRequest() {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public <T> T execute(Transaction<T> transaction) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public Iterator<Message<Long>> findInMailbox(Mailbox<Long> mailbox, MessageRange set,
+                    org.apache.james.mailbox.store.mail.MessageMapper.FetchType type, int limit)
+                    throws MailboxException {
+                long start = set.getUidFrom();
+                long end = set.getUidTo();
+                long calcEnd = start + limit;
+                if (calcEnd > end) {
+                    calcEnd = end;
+                }
+
+                List<Message<Long>> messages = new ArrayList<Message<Long>>();
+                long i = start;
+                while (i < calcEnd) {
+                    long uid = i;
+                    SimpleMessage<Long> m = new SimpleMessage<Long>(null, 0, 0, new SharedByteArrayInputStream(
+                            "".getBytes()), new Flags(), new PropertyBuilder(), 1L);
+                    m.setUid(uid);
+                    messages.add(m);
+                    i++;
+                }
+                return messages.iterator();
+            }
+
+            @Override
+            public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<Long> mailbox, MessageRange set)
+                    throws MailboxException {
+                throw new UnsupportedOperationException();
+
+            }
+
+            @Override
+            public long countMessagesInMailbox(Mailbox<Long> mailbox) throws MailboxException {
+                throw new UnsupportedOperationException();
+
+            }
+
+            @Override
+            public long countUnseenMessagesInMailbox(Mailbox<Long> mailbox) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public void delete(Mailbox<Long> mailbox, Message<Long> message) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public Long findFirstUnseenMessageUid(Mailbox<Long> mailbox) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public List<Long> findRecentMessageUidsInMailbox(Mailbox<Long> mailbox) throws MailboxException {
+                throw new UnsupportedOperationException();
+
+            }
+
+            @Override
+            public MessageMetaData add(Mailbox<Long> mailbox, Message<Long> message) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public Iterator<UpdatedFlags> updateFlags(Mailbox<Long> mailbox, Flags flags, boolean value,
+                    boolean replace, MessageRange set) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public MessageMetaData copy(Mailbox<Long> mailbox, Message<Long> original) throws MailboxException {
+                throw new UnsupportedOperationException();
+
+            }
+
+            @Override
+            public long getLastUid(Mailbox<Long> mailbox) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public long getHighestModSeq(Mailbox<Long> mailbox) throws MailboxException {
+                throw new UnsupportedOperationException();
+            }
+
+            @Override
+            public MessageMetaData move(Mailbox<Long> mailbox, Message<Long> original) throws MailboxException {
+                throw new UnsupportedOperationException();
+
+            }
+
+        }, null, range, batchSize, new FetchGroup() {
+
+            @Override
+            public Set<PartContentDescriptor> getPartContentDescriptors() {
+                return null;
+            }
+
+            @Override
+            public int content() {
+                return FetchGroup.MINIMAL;
+            }
+        });
+
+        long i = 1;
+        while (it.hasNext()) {
+            MessageResult r = it.next();
+            Assert.assertEquals(i++, r.getUid());
+        }
+        Assert.assertEquals(10, i);
+
+    }
 
 }

Modified: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/api/ImapConstants.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/api/ImapConstants.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/api/ImapConstants.java (original)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/api/ImapConstants.java Sat Dec  8 06:46:05 2012
@@ -203,6 +203,8 @@ public interface ImapConstants {
 
     public static final String COPY_COMMAND_NAME = "COPY";
 
+    public static final String MOVE_COMMAND_NAME = "MOVE";
+
     public static final String CLOSE_COMMAND_NAME = "CLOSE";
 
     public static final String CHECK_COMMAND_NAME = "CHECK";

Modified: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/CopyCommandParser.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/CopyCommandParser.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/CopyCommandParser.java (original)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/CopyCommandParser.java Sat Dec  8 06:46:05 2012
@@ -33,9 +33,12 @@ import org.apache.james.protocols.imap.D
 public class CopyCommandParser extends AbstractUidCommandParser {
 
     public CopyCommandParser() {
-        super(ImapCommand.selectedStateCommand(ImapConstants.COPY_COMMAND_NAME));
+        this(ImapCommand.selectedStateCommand(ImapConstants.COPY_COMMAND_NAME));
     }
 
+    protected CopyCommandParser(ImapCommand command) {
+    	super(command);
+    }
     /**
      * @see
      * org.apache.james.imap.decode.parser.AbstractUidCommandParser#decode(org.apache.james.imap.api.ImapCommand,
@@ -46,8 +49,13 @@ public class CopyCommandParser extends A
         IdRange[] idSet = request.parseIdRange(session);
         String mailboxName = request.mailbox();
         request.eol();
-        final ImapMessage result = new CopyRequest(command, idSet, mailboxName, useUids, tag);
+        final ImapMessage result = createRequest(command, tag, useUids, idSet, mailboxName);
         return result;
     }
 
+	protected CopyRequest createRequest(ImapCommand command, String tag,
+			boolean useUids, IdRange[] idSet, String mailboxName) {
+		return new CopyRequest(command, idSet, mailboxName, useUids, tag);
+}
+
 }

Modified: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java (original)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java Sat Dec  8 06:46:05 2012
@@ -85,6 +85,7 @@ public class ImapParserFactory implement
         _imapCommands.put(ImapConstants.CLOSE_COMMAND_NAME, CloseCommandParser.class);
         _imapCommands.put(ImapConstants.EXPUNGE_COMMAND_NAME, ExpungeCommandParser.class);
         _imapCommands.put(ImapConstants.COPY_COMMAND_NAME, CopyCommandParser.class);
+        _imapCommands.put(ImapConstants.MOVE_COMMAND_NAME, MoveCommandParser.class);
         _imapCommands.put(ImapConstants.SEARCH_COMMAND_NAME, SearchCommandParser.class);
         _imapCommands.put(ImapConstants.FETCH_COMMAND_NAME, FetchCommandParser.class);
         _imapCommands.put(ImapConstants.STORE_COMMAND_NAME, StoreCommandParser.class);

Added: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/MoveCommandParser.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/MoveCommandParser.java?rev=1418609&view=auto
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/MoveCommandParser.java (added)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/MoveCommandParser.java Sat Dec  8 06:46:05 2012
@@ -0,0 +1,22 @@
+package org.apache.james.imap.decode.parser;
+
+import org.apache.james.imap.api.ImapCommand;
+import org.apache.james.imap.api.ImapConstants;
+import org.apache.james.imap.api.message.IdRange;
+import org.apache.james.imap.message.request.MoveRequest;
+
+/**
+ * Parse MOVE commands
+ */
+public class MoveCommandParser extends CopyCommandParser {
+	
+	public MoveCommandParser() {
+		super(ImapCommand.selectedStateCommand(ImapConstants.MOVE_COMMAND_NAME));
+	}
+	
+	protected MoveRequest createRequest(ImapCommand command, String tag,
+			boolean useUids, IdRange[] idSet, String mailboxName) {
+		return new MoveRequest(command, idSet, mailboxName, useUids, tag);
+	}
+
+}

Added: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/message/request/MoveRequest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/message/request/MoveRequest.java?rev=1418609&view=auto
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/message/request/MoveRequest.java (added)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/message/request/MoveRequest.java Sat Dec  8 06:46:05 2012
@@ -0,0 +1,17 @@
+package org.apache.james.imap.message.request;
+
+import org.apache.james.imap.api.ImapCommand;
+import org.apache.james.imap.api.message.IdRange;
+import org.apache.james.imap.api.message.request.ImapRequest;
+
+/**
+ * {@link ImapRequest} which request the move of messages
+ */
+public class MoveRequest extends CopyRequest {
+
+	public MoveRequest(ImapCommand command, IdRange[] idSet,
+			String mailboxName, boolean useUids, String tag) {
+		super(command, idSet, mailboxName, useUids, tag);
+	}
+
+}

Modified: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/CopyProcessor.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/CopyProcessor.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/CopyProcessor.java (original)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/CopyProcessor.java Sat Dec  8 06:46:05 2012
@@ -44,6 +44,10 @@ import org.apache.james.mailbox.model.Me
 public class CopyProcessor extends AbstractMailboxProcessor<CopyRequest> {
 
     public CopyProcessor(final ImapProcessor next, final MailboxManager mailboxManager, final StatusResponseFactory factory) {
+        this(CopyRequest.class, next, mailboxManager, factory);
+    }
+
+    protected CopyProcessor(final Class<? extends CopyRequest> acceptableClass, final ImapProcessor next, final MailboxManager mailboxManager, final StatusResponseFactory factory) {
         super(CopyRequest.class, next, mailboxManager, factory);
     }
 
@@ -74,8 +78,10 @@ public class CopyProcessor extends Abstr
                 for (int i = 0; i < idSet.length; i++) {
                     MessageRange messageSet = messageRange(currentMailbox, idSet[i], useUids);
                     if (messageSet != null) {
-                        List<MessageRange> copiedUids = mailboxManager.copyMessages(messageSet, currentMailbox.getPath(), targetMailbox, mailboxSession);
-                        for (MessageRange mr : copiedUids) {
+                        List<MessageRange> processedUids = process(
+								targetMailbox, currentMailbox, mailboxSession,
+								mailboxManager, messageSet);
+                        for (MessageRange mr : processedUids) {
                             // Set recent flag on copied message as this SHOULD be
                             // done.
                             // See RFC 3501 6.4.7. COPY Command
@@ -108,4 +114,13 @@ public class CopyProcessor extends Abstr
             no(command, tag, responder, HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING);
         }
     }
+
+	protected List<MessageRange> process(final MailboxPath targetMailbox,
+			final SelectedMailbox currentMailbox,
+			final MailboxSession mailboxSession,
+			final MailboxManager mailboxManager, MessageRange messageSet)
+			throws MailboxException {
+		List<MessageRange> processedUids = mailboxManager.copyMessages(messageSet, currentMailbox.getPath(), targetMailbox, mailboxSession);
+		return processedUids;
+}
 }

Modified: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/DefaultProcessorChain.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/DefaultProcessorChain.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/DefaultProcessorChain.java (original)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/DefaultProcessorChain.java Sat Dec  8 06:46:05 2012
@@ -52,7 +52,8 @@ public class DefaultProcessorChain {
         final UnsubscribeProcessor unsubscribeProcessor = new UnsubscribeProcessor(closeProcessor, mailboxManager, subscriptionManager, statusResponseFactory);
         final SubscribeProcessor subscribeProcessor = new SubscribeProcessor(unsubscribeProcessor, mailboxManager, subscriptionManager, statusResponseFactory);
         final CopyProcessor copyProcessor = new CopyProcessor(subscribeProcessor, mailboxManager, statusResponseFactory);
-        final AuthenticateProcessor authenticateProcessor = new AuthenticateProcessor(copyProcessor, mailboxManager, statusResponseFactory);
+        final MoveProcessor moveProcessor = new MoveProcessor(copyProcessor, mailboxManager, statusResponseFactory);
+        final AuthenticateProcessor authenticateProcessor = new AuthenticateProcessor(moveProcessor, mailboxManager, statusResponseFactory);
         final ExpungeProcessor expungeProcessor = new ExpungeProcessor(authenticateProcessor, mailboxManager, statusResponseFactory);
         final ExamineProcessor examineProcessor = new ExamineProcessor(expungeProcessor, mailboxManager, statusResponseFactory);
         final AppendProcessor appendProcessor = new AppendProcessor(examineProcessor, mailboxManager, statusResponseFactory);

Added: james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/MoveProcessor.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/MoveProcessor.java?rev=1418609&view=auto
==============================================================================
--- james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/MoveProcessor.java (added)
+++ james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/MoveProcessor.java Sat Dec  8 06:46:05 2012
@@ -0,0 +1,31 @@
+package org.apache.james.imap.processor;
+
+import java.util.List;
+
+import org.apache.james.imap.api.message.response.StatusResponseFactory;
+import org.apache.james.imap.api.process.ImapProcessor;
+import org.apache.james.imap.api.process.SelectedMailbox;
+import org.apache.james.imap.message.request.MoveRequest;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+
+public class MoveProcessor extends CopyProcessor {
+
+	public MoveProcessor(ImapProcessor next, MailboxManager mailboxManager,
+			StatusResponseFactory factory) {
+		super(MoveRequest.class, next, mailboxManager, factory);
+	}
+
+	protected List<MessageRange> process(final MailboxPath targetMailbox,
+			final SelectedMailbox currentMailbox,
+			final MailboxSession mailboxSession,
+			final MailboxManager mailboxManager, MessageRange messageSet)
+			throws MailboxException {
+		List<MessageRange> processedUids = mailboxManager.moveMessages(messageSet, currentMailbox.getPath(), targetMailbox, mailboxSession);
+		return processedUids;
+	}
+
+}

Modified: james/protocols/trunk/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/protocols/trunk/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java (original)
+++ james/protocols/trunk/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java Sat Dec  8 06:46:05 2012
@@ -346,6 +346,10 @@ public class MailboxEventAnalyserTest {
             throw new UnsupportedOperationException("Not implemented");
 
         }
+        public List<MessageRange> moveMessages(MessageRange set, MailboxPath from, MailboxPath to, MailboxSession session) throws MailboxException {
+            throw new UnsupportedOperationException("Not implemented");
+
+        }
     };
     private final class MyMailboxSession implements MailboxSession {
         private long sessionId;



---------------------------------------------------------------------
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