View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.imap.cmd;
14  
15  import org.abstracthorizon.mercury.common.command.CommandException;
16  import org.abstracthorizon.mercury.imap.IMAPSession;
17  import org.abstracthorizon.mercury.imap.util.ComposedSequence;
18  import org.abstracthorizon.mercury.imap.util.MessageUtilities;
19  import org.abstracthorizon.mercury.imap.util.ParserException;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.mail.Folder;
26  import javax.mail.Message;
27  import javax.mail.MessagingException;
28  import javax.mail.internet.MimeMessage;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Copy IMAP Command
35   *
36   * @author Daniel Sendula
37   */
38  public class Copy extends UIDCommand {
39  
40      /** Logger */
41      public static Logger logger = LoggerFactory.getLogger(Copy.class);
42  
43      /** List of message to be processed */
44      protected List<MimeMessage> toProcess = new ArrayList<MimeMessage>();
45  
46      /**
47       * Constructor
48       * @param mnemonic mnemonic
49       */
50      public Copy(String mnemonic) {
51          super(mnemonic);
52      }
53  
54      /**
55       * Executes the command
56       * @param session
57       * @throws ParserException
58       * @throws MessagingException
59       * @throws CommandException
60       * @throws IOException
61       */
62      protected void execute(IMAPSession session) throws ParserException, MessagingException, CommandException, IOException {
63          // "COPY" SP sequence-set SP mailbox
64          ComposedSequence sequenceSet = new ComposedSequence();
65          if (!session.getScanner().sequence_set(sequenceSet)) {
66              throw new ParserException("<sequence-set>");
67          }
68          if (!session.getScanner().is_char(' ')) {
69              throw new ParserException("<SP>");
70          }
71          StringBuffer mailboxName = new StringBuffer();
72          if (!session.getScanner().mailbox(mailboxName)) {
73              throw new ParserException("<mailbox_name>");
74          }
75          checkEOL(session);
76  
77          Folder fromFolder = session.getSelectedFolder();
78  
79          Folder toFolder = session.getStore().getFolder(mailboxName.toString());
80          if (toFolder == null) {
81              throw new MissingMailbox(mailboxName.toString());
82          }
83          toFolder.open(Folder.READ_WRITE);
84          try {
85              MessageUtilities.sequenceIterator(session, this, fromFolder, sequenceSet, asuid);
86              Message[] messages = new Message[toProcess.size()];
87              messages = (Message[])toProcess.toArray(messages);
88              toFolder.appendMessages(messages);
89              sendOK(session);
90          } finally {
91              try {
92                  toFolder.close(false);
93              } catch (MessagingException ee) {
94                  session.setKeepLog(true);
95                  logger.error("Copy", ee);
96                  // We don't want possible exception from close to mess with real exception
97              }
98          }
99      }
100 
101     /**
102      * Processes messages
103      * @param session session
104      * @param m mime message
105      * @throws MessagingException
106      */
107     public void process(IMAPSession session, MimeMessage m) throws MessagingException {
108         toProcess.add(m);
109     }
110 }