1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.avalon.framework.component.ComponentException; 21 import org.apache.avalon.framework.component.ComponentManager; 22 import org.apache.avalon.framework.configuration.DefaultConfiguration; 23 import org.apache.james.Constants; 24 import org.apache.james.core.MailImpl; 25 import org.apache.james.services.MailRepository; 26 import org.apache.james.services.MailStore; 27 import org.apache.james.util.RFC2822Headers; 28 import org.apache.mailet.GenericMailet; 29 import org.apache.mailet.MailAddress; 30 import org.apache.mailet.Mail; 31 import javax.mail.internet.InternetAddress ; 32 import javax.mail.MessagingException ; 33 import java.util.Iterator ; 34 35 46 public class FromRepository extends GenericMailet { 47 48 51 private MailRepository repository; 52 53 56 private boolean delete = false; 57 58 61 private String repositoryPath; 62 63 66 private String processor; 67 68 71 public void init() { 72 repositoryPath = getInitParameter("repositoryPath"); 73 processor = (getInitParameter("processor") == null) ? Mail.DEFAULT : getInitParameter("processor"); 74 75 try { 76 delete = (getInitParameter("delete") == null) ? false : new Boolean (getInitParameter("delete")).booleanValue(); 77 } catch (Exception e) { 78 } 80 81 ComponentManager compMgr = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); 82 try { 83 MailStore mailstore = (MailStore) compMgr.lookup("org.apache.james.services.MailStore"); 84 DefaultConfiguration mailConf 85 = new DefaultConfiguration("repository", "generated:ToRepository"); 86 mailConf.setAttribute("destinationURL", repositoryPath); 87 mailConf.setAttribute("type", "MAIL"); 88 repository = (MailRepository) mailstore.select(mailConf); 89 } catch (ComponentException cnfe) { 90 log("Failed to retrieve Store component:" + cnfe.getMessage()); 91 } catch (Exception e) { 92 log("Failed to retrieve Store component:" + e.getMessage()); 93 } 94 } 95 96 102 public void service(Mail trigger) throws MessagingException { 103 trigger.setState(Mail.GHOST); 104 java.util.Collection processed = new java.util.ArrayList (); 105 Iterator list = repository.list(); 106 while (list.hasNext()) { 107 String key = (String ) list.next(); 108 try { 109 MailImpl mail = repository.retrieve(key); 110 if (mail != null && mail.getRecipients() != null) { 111 log((new StringBuffer (160).append("Spooling mail ").append(mail.getName()).append(" from ").append(repositoryPath)).toString()); 112 113 122 123 mail.setAttribute("FromRepository", Boolean.TRUE); 124 mail.setState(processor); 125 getMailetContext().sendMail(mail); 126 if (delete) processed.add(key); 127 } 128 } catch (MessagingException e) { 129 log((new StringBuffer (160).append("Unable to re-spool mail ").append(key).append(" from ").append(repositoryPath)).toString(), e); 130 } 131 } 132 if (delete) repository.remove(processed); 133 } 134 135 140 public String getMailetInfo() { 141 return "FromRepository Mailet"; 142 } 143 } 144 | Popular Tags |