001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.processor;
017    
018    import java.io.File;
019    import java.io.FileOutputStream;
020    import java.io.IOException;
021    import java.io.OutputStream;
022    
023    import org.apache.commons.io.FileUtils;
024    import org.apache.commons.io.IOUtils;
025    import org.jboss.netty.buffer.ChannelBuffer;
026    import org.jboss.netty.buffer.ChannelBufferInputStream;
027    
028    import com.hs.mail.container.config.Config;
029    import com.hs.mail.imap.ImapSession;
030    import com.hs.mail.imap.mailbox.Mailbox;
031    import com.hs.mail.imap.mailbox.MailboxManager;
032    import com.hs.mail.imap.message.request.AppendRequest;
033    import com.hs.mail.imap.message.request.ImapRequest;
034    import com.hs.mail.imap.message.responder.Responder;
035    import com.hs.mail.imap.message.response.HumanReadableText;
036    
037    /**
038     * 
039     * @author Won Chul Doh
040     * @since Feb 1, 2010
041     *
042     */
043    public class AppendProcessor extends AbstractImapProcessor {
044    
045            @Override
046            protected void doProcess(ImapSession session, ImapRequest message,
047                            Responder responder) throws Exception {
048                    AppendRequest request = (AppendRequest) message;
049                    String mailboxName = request.getMailbox();
050                    MailboxManager manager = getMailboxManager();
051                    Mailbox mailbox = manager.getMailbox(session.getUserID(), mailboxName);
052                    if (mailbox == null) {
053                            // SHOULD NOT automatically create the mailbox.
054                            responder.taggedNo(request, "[TRYCREATE]",
055                                            HumanReadableText.MAILBOX_NOT_FOUND);
056                    } else {
057                            File temp = File.createTempFile("mail", null, Config.getTempDirectory());
058                            ChannelBuffer buffer = request.getMessage();
059                            try {
060                                    writeMessage(buffer, temp);
061                                    manager.appendMessage(mailbox.getMailboxID(),
062                                                    request.getDatetime(), request.getFlags(), temp);
063                            } catch (Exception ex) {
064                                    forceDelete(temp);
065                                    throw ex;
066                            }
067                            responder.okCompleted(request);
068                    }
069            }
070            
071            private void writeMessage(ChannelBuffer buffer, File dst)
072                            throws IOException {
073                    ChannelBufferInputStream is = new ChannelBufferInputStream(buffer);
074                    OutputStream os = null;
075                    try {
076                            os = new FileOutputStream(dst);
077                            IOUtils.copyLarge(is, os);
078                    } finally {
079                            IOUtils.closeQuietly(os);
080                    }
081            }
082    
083            private void forceDelete(File file) {
084                    try {
085                            FileUtils.forceDelete(file);
086                    } catch (IOException e) {
087                            // Don't re-throw this exception
088                    }
089            }       
090    
091    }