001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.spooler.mail;
020
021import javax.mail.internet.InternetAddress;
022
023import lucee.commons.lang.StringUtil;
024import lucee.runtime.config.Config;
025import lucee.runtime.config.ConfigWeb;
026import lucee.runtime.exp.PageException;
027import lucee.runtime.net.mail.MailException;
028import lucee.runtime.net.smtp.SMTPClient;
029import lucee.runtime.op.Caster;
030import lucee.runtime.spooler.ExecutionPlan;
031import lucee.runtime.spooler.ExecutionPlanImpl;
032import lucee.runtime.spooler.SpoolerTaskSupport;
033import lucee.runtime.type.Struct;
034import lucee.runtime.type.StructImpl;
035import lucee.runtime.type.util.ArrayUtil;
036
037public class MailSpoolerTask extends SpoolerTaskSupport {
038        private static final ExecutionPlan[] EXECUTION_PLANS = new ExecutionPlan[]{
039                new ExecutionPlanImpl(1,60),
040                new ExecutionPlanImpl(1,5*60),
041                new ExecutionPlanImpl(1,3600),
042                new ExecutionPlanImpl(2,24*3600),
043        };
044        
045        
046        private SMTPClient client;
047
048        public MailSpoolerTask(ExecutionPlan[] plans,SMTPClient client, long sendTime) {
049                super(plans, sendTime);
050                this.client=client;
051        }
052
053        public MailSpoolerTask(SMTPClient client, long sendTime) {
054                this(EXECUTION_PLANS,client, sendTime);
055        }
056        
057
058        @Override
059        public String getType() {
060                return "mail";
061        }
062
063        public String subject() {
064                return client.getSubject();
065        }
066        
067        public Struct detail() {
068                StructImpl sct = new StructImpl();
069                sct.setEL("subject", client.getSubject());
070                
071                if(client.hasHTMLText())sct.setEL("body", StringUtil.max(client.getHTMLTextAsString(),1024,"..."));
072                else if(client.hasPlainText())sct.setEL("body", StringUtil.max(client.getPlainTextAsString(),1024,"..."));
073                
074                sct.setEL("from", toString(client.getFrom()));
075                
076                InternetAddress[] adresses = client.getTos();
077                sct.setEL("to", toString(adresses));
078
079                adresses = client.getCcs();
080                if(!ArrayUtil.isEmpty(adresses))sct.setEL("cc", toString(adresses));
081
082                adresses = client.getBccs();
083                if(!ArrayUtil.isEmpty(adresses))sct.setEL("bcc", toString(adresses));
084                
085                return sct;
086        }
087
088        private static String toString(InternetAddress[] adresses) {
089                if(adresses==null) return "";
090                
091                StringBuffer sb=new StringBuffer();
092                for(int i=0;i<adresses.length;i++) {
093                        if(i>0)sb.append(", ");
094                        sb.append(toString(adresses[i]));
095                }
096                return sb.toString();
097        }
098        private static String toString(InternetAddress address) {
099                if(address==null) return "";
100                String addr = address.getAddress();
101                String per = address.getPersonal();
102                if(StringUtil.isEmpty(per)) return addr;
103                if(StringUtil.isEmpty(addr)) return per;
104                
105                
106                return per+" ("+addr+")";
107        }
108        public Object execute(Config config) throws PageException {
109                try {
110                        client._send((ConfigWeb)config);
111                } 
112                catch (MailException e) {
113                        throw Caster.toPageException(e);
114                }
115                return null;
116        }
117
118}