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.schedule;
020
021import java.io.IOException;
022import java.net.MalformedURLException;
023import java.net.URL;
024
025import lucee.commons.io.res.Resource;
026import lucee.commons.lang.Md5;
027import lucee.commons.net.HTTPUtil;
028import lucee.commons.security.Credentials;
029import lucee.runtime.net.proxy.ProxyData;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.dt.Date;
032import lucee.runtime.type.dt.Time;
033
034/**
035 * Define a single schedule Task
036 */
037public final class ScheduleTaskImpl implements ScheduleTask {
038    
039        public static int INTERVAL_EVEREY=-1;
040    private String task;
041    private short operation = OPERATION_HTTP_REQUEST;
042    private Resource file;
043    private Date startDate;
044    private Time startTime;
045    private URL url;
046    private Date endDate;
047    private Time endTime;
048    private int interval;
049    private long timeout;
050    private Credentials credentials;
051    private ProxyData proxy;
052    private boolean resolveURL;
053
054    private long nextExecution;
055
056    private String strInterval;
057
058    private boolean publish;
059    private boolean valid=true;
060        private boolean hidden;
061        private boolean readonly;
062        private boolean paused;
063        private boolean autoDelete;
064        private String md5;
065
066    
067    
068    
069    /**
070     * constructor of the class
071     * @param task Task name
072     * @param file Output File
073     * @param startDate Start Date
074     * @param startTime Start  Time
075     * @param endDate
076     * @param endTime
077     * @param url URL to invoke
078     * @param port Port of the URL to invoke
079     * @param interval interval of the job
080     * @param timeout request timeout in miilisconds
081     * @param credentials username and password for the request
082     * @param proxyHost
083     * @param proxyPort
084     * @param proxyCredentials proxy username and password
085     * @param resolveURL resolve links in the output page to absolute references or not
086     * @param publish
087     * @throws IOException
088     * @throws ScheduleException
089     */
090    public ScheduleTaskImpl(String task, Resource file, Date startDate, Time startTime, 
091            Date endDate, Time endTime, String url, int port, String interval,
092            long timeout, Credentials credentials, ProxyData proxy, boolean resolveURL, boolean publish,boolean hidden, 
093            boolean readonly,boolean paused, boolean autoDelete) throws IOException, ScheduleException {
094        
095        
096        String md5=task.toLowerCase()+file+startDate+startTime+endDate+endTime+url+port+interval+timeout+
097        credentials+proxy+resolveURL+publish+hidden+readonly+paused;
098        md5=Md5.getDigestAsString(md5);
099        this.md5=md5;
100        
101        if(file!=null && file.toString().trim().length()>0) {
102                Resource parent = file.getParentResource();
103                if(parent==null || !parent.exists())
104                    throw new IOException("Directory for output file ["+file+"] doesn't exist");
105                if(file.exists() && !file.isFile())
106                    throw new IOException("output file ["+file+"] is not a file");
107        }
108        if(timeout<1) {
109            throw new ScheduleException("value timeout must be greater than 0");
110        }
111        if(startDate==null) throw new ScheduleException("start date is required");
112        if(startTime==null)throw new ScheduleException("start time is required");
113        //if(endTime==null)endTime=new Time(23,59,59,999);
114
115        this.task=task.trim();
116        this.file=file;
117        this.startDate=startDate;
118        this.startTime=startTime;
119        this.endDate=endDate;
120        this.endTime=endTime;
121        this.url=toURL(url,port);
122        this.interval=toInterval(interval);
123        this.strInterval=interval;
124        this.timeout=timeout;
125        this.credentials=credentials;
126        this.proxy=proxy;
127        this.resolveURL=resolveURL;
128        this.publish=publish;
129        this.hidden=hidden;
130        this.readonly=readonly;
131        this.paused=paused;
132        this.autoDelete=autoDelete;
133    }
134
135
136
137    /**
138     * translate a String interval definition to a int definition
139     * @param interval
140     * @return interval
141     * @throws ScheduleException
142     */
143    private static int toInterval(String interval) throws ScheduleException {
144        interval=interval.trim().toLowerCase();
145        int i=Caster.toIntValue(interval,0);
146        if(i==0) {
147            interval=interval.trim();
148            if(interval.equals("once")) return INTERVAL_ONCE;
149            else if(interval.equals("daily")) return INTERVAL_DAY;
150            else if(interval.equals("day")) return INTERVAL_DAY;
151            else if(interval.equals("monthly")) return INTERVAL_MONTH;
152            else if(interval.equals("month")) return INTERVAL_MONTH;
153            else if(interval.equals("weekly")) return INTERVAL_WEEK;
154            else if(interval.equals("week")) return INTERVAL_WEEK;
155            throw new ScheduleException("invalid interval definition ["+interval+"], valid values are [once,daily,monthly,weekly or number]");
156        }
157        if(i<10) {
158            throw new ScheduleException("interval must be at least 10");
159        }
160        return i;
161    }
162
163    /**
164     * translate a urlString and a port definition to a URL Object
165     * @param url URL String 
166     * @param port URL Port Definition
167     * @return returns a URL Object
168     * @throws MalformedURLException
169     */
170    private static URL toURL(String url, int port) throws MalformedURLException {
171        URL u = HTTPUtil.toURL(url,true);
172        if(port==-1) return u;
173        return new URL(u.getProtocol(), u.getHost(), port, u.getFile());
174    }    
175
176    @Override
177    public Credentials getCredentials() {       return credentials;     }
178
179    @Override
180    public boolean hasCredentials() {   return credentials!=null;       }
181    
182    @Override
183    public Resource getResource() {     
184        return file;
185    }
186    
187    @Override
188    public int getInterval() {  return interval;        }
189    
190    @Override
191    public short getOperation() {       return operation;       }
192    
193    @Override
194    public ProxyData getProxyData() {   return proxy;   }
195    
196    @Override
197    public boolean isResolveURL() {     return resolveURL;      }
198    
199    @Override
200    public String getTask() {   return task;    }
201    
202    @Override
203    public long getTimeout() {  return timeout; }
204    
205    @Override
206    public URL getUrl() {       
207        return url;     
208    }
209
210    @Override
211    public void setNextExecution(long nextExecution) {  this.nextExecution=nextExecution;       }
212    
213    @Override
214    public long getNextExecution() {    return nextExecution;   }
215    
216    @Override
217    public Date getEndDate() {  return endDate; }
218    
219    @Override
220    public Date getStartDate() {        return startDate;       }
221    
222    @Override
223    public Time getEndTime() {  return endTime; }
224    
225    @Override
226    public Time getStartTime() {        return startTime;       }
227
228    @Override
229    public String getIntervalAsString() {       return strInterval;     }
230    
231    @Override
232    public String getStringInterval() { return strInterval;     }
233    @Override
234    public boolean isPublish() {
235        return publish;
236    }
237    @Override
238    public boolean isValid() {
239        return valid;
240    }
241    @Override
242    public void setValid(boolean valid) {
243        this.valid = valid;
244    }
245
246
247
248        /**
249         * @return the hidden
250         */
251        public boolean isHidden() {
252                return hidden;
253        }
254
255
256
257        /** 
258         * @param hidden the hidden to set
259         */
260        public void setHidden(boolean hidden) {
261                this.hidden = hidden;
262        }
263
264
265
266        /**
267         * @return the readonly
268         */
269        public boolean isReadonly() {
270                return readonly;
271        }
272
273
274
275        /**
276         * @param readonly the readonly to set
277         */
278        public void setReadonly(boolean readonly) {
279                this.readonly = readonly;
280        }
281
282
283
284        @Override
285        public boolean isPaused() {
286                return paused;
287        }
288
289
290
291        public void setPaused(boolean paused) {
292                this.paused=paused;
293        }
294        
295
296        public boolean isAutoDelete() {
297                return autoDelete;
298        }
299
300
301
302        public void setAutoDelete(boolean autoDelete) {
303                this.autoDelete=autoDelete;
304        }
305
306
307
308        public String md5() {
309                return md5;
310        }
311}