001    package railo.runtime.spooler.mail;
002    
003    import javax.mail.internet.InternetAddress;
004    
005    import railo.commons.lang.StringUtil;
006    import railo.runtime.config.Config;
007    import railo.runtime.exp.PageException;
008    import railo.runtime.net.mail.MailException;
009    import railo.runtime.net.smtp.SMTPClient;
010    import railo.runtime.op.Caster;
011    import railo.runtime.spooler.ExecutionPlan;
012    import railo.runtime.spooler.ExecutionPlanImpl;
013    import railo.runtime.spooler.SpoolerTaskSupport;
014    import railo.runtime.type.Struct;
015    import railo.runtime.type.StructImpl;
016    import railo.runtime.type.util.ArrayUtil;
017    
018    public class MailSpoolerTask extends SpoolerTaskSupport {
019            private static final ExecutionPlan[] EXECUTION_PLANS = new ExecutionPlan[]{
020                    new ExecutionPlanImpl(2,10),
021                    new ExecutionPlanImpl(5,60),
022                    new ExecutionPlanImpl(24,3600),
023                    new ExecutionPlanImpl(2,24*3600),
024            };
025            
026            
027            private SMTPClient client;
028    
029            public MailSpoolerTask(ExecutionPlan[] plans,SMTPClient client) {
030                    super(plans);
031                    this.client=client;
032            }
033            public MailSpoolerTask(SMTPClient client) {
034                    this(EXECUTION_PLANS,client);
035            }
036            
037    
038            /**
039             * @see railo.runtime.spooler.SpoolerTask#getType()
040             */
041            public String getType() {
042                    return "mail";
043            }
044    
045            public String subject() {
046                    return client.getSubject();
047            }
048            
049            public Struct detail() {
050                    StructImpl sct = new StructImpl();
051                    sct.setEL("subject", client.getSubject());
052                    
053                    if(client.hasHTMLText())sct.setEL("body", StringUtil.max(client.getHTMLTextAsString(),1024,"..."));
054                    else if(client.hasPlainText())sct.setEL("body", StringUtil.max(client.getPlainTextAsString(),1024,"..."));
055                    
056                    sct.setEL("from", toString(client.getFrom()));
057                    
058                    InternetAddress[] adresses = client.getTos();
059                    sct.setEL("to", toString(adresses));
060    
061                    adresses = client.getCcs();
062                    if(!ArrayUtil.isEmpty(adresses))sct.setEL("cc", toString(adresses));
063    
064                    adresses = client.getBccs();
065                    if(!ArrayUtil.isEmpty(adresses))sct.setEL("bcc", toString(adresses));
066                    
067                    return sct;
068            }
069    
070            private static String toString(InternetAddress[] adresses) {
071                    if(adresses==null) return "";
072                    
073                    StringBuffer sb=new StringBuffer();
074                    for(int i=0;i<adresses.length;i++) {
075                            if(i>0)sb.append(", ");
076                            sb.append(toString(adresses[i]));
077                    }
078                    return sb.toString();
079            }
080            private static String toString(InternetAddress address) {
081                    if(address==null) return "";
082                    String addr = address.getAddress();
083                    String per = address.getPersonal();
084                    if(StringUtil.isEmpty(per)) return addr;
085                    if(StringUtil.isEmpty(addr)) return per;
086                    
087                    
088                    return per+" ("+addr+")";
089            }
090            public Object execute(Config config) throws PageException {
091                    try {
092                            client._send(config);
093                    } 
094                    catch (MailException e) {
095                            throw Caster.toPageException(e);
096                    }
097                    return null;
098            }
099    
100    }