/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/
package org.apache.jsieve.mailet;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.jsieve.mail.Action;
import org.apache.jsieve.mail.ActionFileInto;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;

public class FileIntoAction implements MailAction {
    
    private static final char HIERARCHY_DELIMITER = '.';

    public void execute(Action action, Mail mail, ActionContext context) throws MessagingException {
        if (action instanceof ActionFileInto) {
            final ActionFileInto fileIntoAction = (ActionFileInto) action;
            execute(fileIntoAction, mail, context);
        }
    }

    /**
     * <p>
     * Executes the passed ActionFileInto.
     * </p>
     * 
     * <p>
     * This implementation accepts any destination with the root of <code>INBOX</code>.
     * </p>
     * 
     * <p>
     * As the current POP3 server does not support sub-folders, the mail is
     * stored in the INBOX for the recipient of the mail and the full intended
     * destination added as a prefix to the message's subject.
     * </p>
     * 
     * <p>
     * When IMAP support is added to James, it will be possible to support
     * sub-folders of <code>INBOX</code> fully.
     * </p>
     * 
     * @param anAction
     * @param aMail
     * @param context not null
     * @throws MessagingException
     */
    public void execute(ActionFileInto anAction, Mail aMail, final ActionContext context) throws MessagingException
    {
        String destinationMailbox = anAction.getDestination();
        MailAddress recipient;
        boolean delivered = false;
        try
        {
            recipient = ActionUtils.getSoleRecipient(aMail);
            MimeMessage localMessage = createMimeMessage(aMail, recipient);
            
            if (!(destinationMailbox.length() > 0 
                    && destinationMailbox.charAt(0) == HIERARCHY_DELIMITER)) {
                destinationMailbox =  HIERARCHY_DELIMITER + destinationMailbox;
            }
            
            final String url = "mailbox://" + recipient.getUser() + "@localhost/" + 
                destinationMailbox.replace(HIERARCHY_DELIMITER, '/');
            //TODO: copying this message so many times seems a waste
            context.post(url, localMessage);
            delivered = true;
        }
        catch (MessagingException ex)
        {
            final Log log = context.getLog();
            if (log.isDebugEnabled()) {
                log.debug("Error while storing mail into. "+destinationMailbox, ex);
            }
            throw ex;
        }
        finally
        {
            // Ensure the mail is always ghosted
            aMail.setState(Mail.GHOST);
        }
        if (delivered)
        {
            final Log log = context.getLog();
            if (log.isDebugEnabled()) {
                log.debug("Filed Message ID: "
                    + aMail.getMessage().getMessageID()
                    + " into destination: \""
                    + destinationMailbox + "\"");
            }
        }
    }
    
    private static MimeMessage createMimeMessage(Mail aMail, MailAddress recipient) throws MessagingException {
        // Adapted from LocalDelivery Mailet
        // Add qmail's de facto standard Delivered-To header
        MimeMessage localMessage = new MimeMessage(aMail.getMessage())
        {
            protected void updateHeaders() throws MessagingException
            {
                if (getMessageID() == null)
                    super.updateHeaders();
                else
                    modified = false;
            }
        };
        localMessage.addHeader("Delivered-To", recipient.toString());

        localMessage.saveChanges();
        return localMessage;
    }
}

@@ -3306,136 +3306,339 @@
 ing 
-url = %22mailbox://%22 + recipient.getUser() + %22@localhost/%22 + %0A                destinationMailbox.replace(HIERARCHY_DELIMITER, '/')
+mailbox = destinationMailbox.replace(HIERARCHY_DELIMITER, '/');%0A            final String host;%0A            if (mailbox.charAt(0) == '/') %7B%0A                host = %22@localhost%22;%0A            %7D else %7B%0A                host = %22@localhost/%22;%0A            %7D%0A            final String url = %22mailbox://%22 + recipient.getUser() + host + mailbox
 ;%0A  

Check for case when mailbox starts with delimiter