FileDocCategorySizeDatePackage
FromRepository.javaAPI DocApache James 2.3.15999Fri Jan 12 12:56:28 GMT 2007org.apache.james.transport.mailets

FromRepository

public class FromRepository extends org.apache.mailet.GenericMailet
Re-spools Mail found in the specified Repository. <mailet match="RecipientIs=respool@localhost" class="FromRepository"> <repositoryPath> repository path </repositoryPath> <processor> target processor </repositoryPath> <delete&t; [true|false] </delete> </mailet>
version
This is $Revision: 494012 $

Fields Summary
private org.apache.james.services.MailRepository
repository
The repository from where this mailet spools mail.
private boolean
delete
Whether this mailet should delete messages after being spooled
private String
repositoryPath
The path to the repository
private String
processor
The processor that will handle the re-spooled message(s)
Constructors Summary
Methods Summary
public java.lang.StringgetMailetInfo()
Return a string describing this mailet.

return
a string describing this mailet

        return "FromRepository Mailet";
    
public voidinit()
Initialize the mailet, loading configuration information.


               
       
        repositoryPath = getInitParameter("repositoryPath");
        processor = (getInitParameter("processor") == null) ? Mail.DEFAULT : getInitParameter("processor");

        try {
            delete = (getInitParameter("delete") == null) ? false : new Boolean(getInitParameter("delete")).booleanValue();
        } catch (Exception e) {
            // Ignore exception, default to false
        }

        ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
        try {
            Store mailstore = (Store) compMgr.lookup(Store.ROLE);
            DefaultConfiguration mailConf
                = new DefaultConfiguration("repository", "generated:ToRepository");
            mailConf.setAttribute("destinationURL", repositoryPath);
            mailConf.setAttribute("type", "MAIL");
            repository = (MailRepository) mailstore.select(mailConf);
        } catch (ServiceException cnfe) {
            log("Failed to retrieve Store component:" + cnfe.getMessage());
        } catch (Exception e) {
            log("Failed to retrieve Store component:" + e.getMessage());
        }
    
public voidservice(org.apache.mailet.Mail trigger)
Spool mail from a particular repository.

param
triggering e-mail (eventually parameterize via the trigger message)

        trigger.setState(Mail.GHOST);
        java.util.Collection processed = new java.util.ArrayList();
        Iterator list = repository.list();
        while (list.hasNext()) {
            String key = (String) list.next();
            try {
                Mail mail =  repository.retrieve(key);
                if (mail != null && mail.getRecipients() != null) {
                    log((new StringBuffer(160).append("Spooling mail ").append(mail.getName()).append(" from ").append(repositoryPath)).toString());

                    /*
                    log("Return-Path: " + mail.getMessage().getHeader(RFC2822Headers.RETURN_PATH, ", "));
                    log("Sender: " + mail.getSender());
                    log("To: " + mail.getMessage().getHeader(RFC2822Headers.TO, ", "));
                    log("Recipients: ");
                    for (Iterator i = mail.getRecipients().iterator(); i.hasNext(); ) {
                        log("    " + ((MailAddress)i.next()).toString());
                    };
                    */

                    mail.setAttribute("FromRepository", Boolean.TRUE);
                    mail.setState(processor);
                    getMailetContext().sendMail(mail);
                    if (delete) processed.add(key);
                }
            } catch (MessagingException e) {
                log((new StringBuffer(160).append("Unable to re-spool mail ").append(key).append(" from ").append(repositoryPath)).toString(), e);
            }
        }

        if (delete) {
            Iterator delList = processed.iterator();
            while (delList.hasNext()) {
                repository.remove((String)delList.next());
            }
        }