001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.fetchmail;
021    
022    import java.util.List;
023    import java.util.Set;
024    
025    import javax.mail.MessagingException;
026    import javax.mail.Session;
027    
028    import org.apache.james.dnsservice.api.DNSService;
029    import org.apache.james.queue.api.MailQueue;
030    import org.apache.james.user.api.UsersRepository;
031    import org.apache.mailet.MailAddress;
032    import org.slf4j.Logger;
033    
034    /**
035     * <p>
036     * Class <code>ProcessorAbstract</code> is an abstract class that provides
037     * support for JavaMail processors. Concrete implementations are required to
038     * implement the abstract method <code>void process()</code> to process a
039     * JavaMail element.
040     * </p>
041     * 
042     * <p>
043     * Typically, processors are chained. A Store processor delegates to a Folder
044     * processor that delegates to a Message processor.
045     * </p>
046     * 
047     * <p>
048     * <code>ProcessorAbstract</code> wraps an Account - see
049     * <code>org.apache.james.fetchmail.Account</code> - providing contextual
050     * information about the environment for the processor.
051     * </p>
052     */
053    abstract public class ProcessorAbstract {
054        /**
055         * The prefix to place in front of any mail attributes used by this
056         * Processor.
057         */
058        private String fieldAttributePrefix;
059    
060        /**
061         * The Account for this task
062         */
063        private Account fieldAccount;
064    
065        /**
066         * Constructor for ProcessorAbstract.
067         */
068        private ProcessorAbstract() {
069            super();
070        }
071    
072        /**
073         * Constructor for ProcessorAbstract.
074         * 
075         * @param account
076         *            The <code>Account</code> to be processed
077         */
078        protected ProcessorAbstract(Account account) {
079            this();
080            setAccount(account);
081        }
082    
083        /**
084         * Returns the defaultDomainName.
085         * 
086         * @return String
087         */
088        protected String getDefaultDomainName() {
089            return getConfiguration().getDefaultDomainName();
090        }
091    
092        /**
093         * Returns the defaultLocalPart.
094         * 
095         * @return String
096         */
097        protected String getDefaultLocalPart() {
098            // TODO Consider making this configurable
099            return "FETCHMAIL-SERVICE";
100        }
101    
102        /**
103         * Returns the message ids. of messages for which processing has been
104         * deferred as the recipient could not be found
105         * 
106         * @return List
107         */
108        protected List<String> getDeferredRecipientNotFoundMessageIDs() {
109            return getAccount().getDeferredRecipientNotFoundMessageIDs();
110        }
111    
112        /**
113         * Returns the fetchTaskName.
114         * 
115         * @return String
116         */
117        protected String getFetchTaskName() {
118            return getConfiguration().getFetchTaskName();
119        }
120    
121        /**
122         * Returns the host.
123         * 
124         * @return String
125         */
126        protected String getHost() {
127            return getConfiguration().getHost();
128        }
129    
130        /**
131         * Returns the javaMailFolderName.
132         * 
133         * @return String
134         */
135        protected String getJavaMailFolderName() {
136            return getConfiguration().getJavaMailFolderName();
137        }
138    
139        /**
140         * Returns the javaMailProviderName.
141         * 
142         * @return String
143         */
144        protected String getJavaMailProviderName() {
145            return getConfiguration().getJavaMailProviderName();
146        }
147    
148        /**
149         * Returns the logger.
150         * 
151         * @return Logger
152         */
153        protected Logger getLogger() {
154            return getConfiguration().getLogger();
155        }
156    
157        /**
158         * Returns the password.
159         * 
160         * @return String
161         */
162        protected String getPassword() {
163            return getAccount().getPassword();
164        }
165    
166        /**
167         * Returns the recipient.
168         * 
169         * @return MailAddress
170         */
171        protected MailAddress getRecipient() {
172            return getAccount().getRecipient();
173        }
174    
175        /**
176         * Method getRemoteReceivedHeaderIndex.
177         * 
178         * @return int
179         */
180        protected int getRemoteReceivedHeaderIndex() {
181            return getConfiguration().getRemoteReceivedHeaderIndex();
182        }
183    
184        /**
185         * Returns the DNSService
186         * 
187         * @return DNSService
188         */
189        protected DNSService getDNSServer() {
190            return getConfiguration().getDNSServer();
191        }
192    
193        /**
194         * Returns the session.
195         * 
196         * @return Session
197         */
198        protected Session getSession() {
199            return getAccount().getSession();
200        }
201    
202        /**
203         * Returns the repository of local users.
204         * 
205         * @return UsersRepository
206         */
207        protected UsersRepository getLocalUsers() {
208            return getConfiguration().getLocalUsers();
209        }
210    
211        /**
212         * Returns the user.
213         * 
214         * @return String
215         */
216        protected String getUser() {
217            return getAccount().getUser();
218        }
219    
220        /**
221         * Returns the fetchAll.
222         * 
223         * @return boolean
224         */
225        protected boolean isFetchAll() {
226            return getConfiguration().isFetchAll();
227        }
228    
229        /**
230         * Returns the isDeferRecipientNotFound.
231         * 
232         * @return boolean
233         */
234        protected boolean isDeferRecipientNotFound() {
235            return getConfiguration().isDeferRecipientNotFound();
236        }
237    
238        /**
239         * Returns the ignoreOriginalRecipient.
240         * 
241         * @return boolean
242         */
243        protected boolean isIgnoreRecipientHeader() {
244            return getAccount().isIgnoreRecipientHeader();
245        }
246    
247        /**
248         * Returns the customRecipientHeader.
249         * 
250         * @return String
251         */
252        protected String getCustomRecipientHeader() {
253            return getAccount().getCustomRecipientHeader();
254        }
255    
256        /**
257         * Returns the leave.
258         * 
259         * @return boolean
260         */
261        protected boolean isLeave() {
262            return getConfiguration().isLeave();
263        }
264    
265        /**
266         * Returns the markSeen.
267         * 
268         * @return boolean
269         */
270        protected boolean isMarkSeen() {
271            return getConfiguration().isMarkSeen();
272        }
273    
274        /**
275         * Returns the leaveBlacklisted.
276         * 
277         * @return boolean
278         */
279        protected boolean isLeaveBlacklisted() {
280            return getConfiguration().isLeaveBlacklisted();
281        }
282    
283        /**
284         * Returns the leaveRemoteRecipient.
285         * 
286         * @return boolean
287         */
288        protected boolean isLeaveRemoteRecipient() {
289            return getConfiguration().isLeaveRemoteRecipient();
290        }
291    
292        /**
293         * Returns the leaveUserUndefinded.
294         * 
295         * @return boolean
296         */
297        protected boolean isLeaveUserUndefined() {
298            return getConfiguration().isLeaveUserUndefined();
299        }
300    
301        /**
302         * Returns the leaveRemoteReceivedHeaderInvalid.
303         * 
304         * @return boolean
305         */
306        protected boolean isLeaveRemoteReceivedHeaderInvalid() {
307            return getConfiguration().isLeaveRemoteReceivedHeaderInvalid();
308        }
309    
310        /**
311         * Returns the LeaveMaxMessageSizeExceeded.
312         * 
313         * @return boolean
314         */
315        protected boolean isLeaveMaxMessageSizeExceeded() {
316            return getConfiguration().isLeaveMaxMessageSizeExceeded();
317        }
318    
319        /**
320         * Returns the leaveUndeliverable.
321         * 
322         * @return boolean
323         */
324        protected boolean isLeaveUndeliverable() {
325            return getConfiguration().isLeaveUndeliverable();
326        }
327    
328        /**
329         * Returns the RejectUserUndefinded.
330         * 
331         * @return boolean
332         */
333        protected boolean isRejectUserUndefined() {
334            return getConfiguration().isRejectUserUndefined();
335        }
336    
337        /**
338         * Returns the RejectRemoteReceivedHeaderInvalid.
339         * 
340         * @return boolean
341         */
342        protected boolean isRejectRemoteReceivedHeaderInvalid() {
343            return getConfiguration().isRejectRemoteReceivedHeaderInvalid();
344        }
345    
346        /**
347         * Returns the RejectMaxMessageSizeExceeded.
348         * 
349         * @return boolean
350         */
351        protected boolean isRejectMaxMessageSizeExceeded() {
352            return getConfiguration().isRejectMaxMessageSizeExceeded();
353        }
354    
355        /**
356         * Returns the RejectUserBlacklisted.
357         * 
358         * @return boolean
359         */
360        protected boolean isRejectBlacklisted() {
361            return getConfiguration().isRejectBlacklisted();
362        }
363    
364        /**
365         * Returns the RejectRemoteRecipient.
366         * 
367         * @return boolean
368         */
369        protected boolean isRejectRemoteRecipient() {
370            return getConfiguration().isRejectRemoteRecipient();
371        }
372    
373        /**
374         * Returns the markBlacklistedSeen.
375         * 
376         * @return boolean
377         */
378        protected boolean isMarkBlacklistedSeen() {
379            return getConfiguration().isMarkBlacklistedSeen();
380        }
381    
382        /**
383         * Returns the markRecipientNotFoundSeen.
384         * 
385         * @return boolean
386         */
387        protected boolean isMarkRecipientNotFoundSeen() {
388            return getConfiguration().isMarkRecipientNotFoundSeen();
389        }
390    
391        /**
392         * Returns the leaveRecipientNotFound.
393         * 
394         * @return boolean
395         */
396        protected boolean isLeaveRecipientNotFound() {
397            return getConfiguration().isLeaveRecipientNotFound();
398        }
399    
400        /**
401         * Returns the rejectRecipientNotFound.
402         * 
403         * @return boolean
404         */
405        protected boolean isRejectRecipientNotFound() {
406            return getConfiguration().isRejectRecipientNotFound();
407        }
408    
409        /**
410         * Returns the markRemoteRecipientSeen.
411         * 
412         * @return boolean
413         */
414        protected boolean isMarkRemoteRecipientSeen() {
415            return getConfiguration().isMarkRemoteRecipientSeen();
416        }
417    
418        /**
419         * Returns the markUserUndefindedSeen.
420         * 
421         * @return boolean
422         */
423        protected boolean isMarkUserUndefinedSeen() {
424            return getConfiguration().isMarkUserUndefinedSeen();
425        }
426    
427        /**
428         * Returns the markRemoteReceivedHeaderInvalidSeen.
429         * 
430         * @return boolean
431         */
432        protected boolean isMarkRemoteReceivedHeaderInvalidSeen() {
433            return getConfiguration().isMarkRemoteReceivedHeaderInvalidSeen();
434        }
435    
436        /**
437         * Returns the MarkMaxMessageSizeExceededSeen.
438         * 
439         * @return boolean
440         */
441        protected boolean isMarkMaxMessageSizeExceededSeen() {
442            return getConfiguration().isMarkMaxMessageSizeExceededSeen();
443        }
444    
445        /**
446         * Returns the markUndeliverableSeen.
447         * 
448         * @return boolean
449         */
450        protected boolean isMarkUndeliverableSeen() {
451            return getConfiguration().isMarkUndeliverableSeen();
452        }
453    
454        /**
455         * Answers true if the folder should be opened read only. For this to be
456         * true... - isKeep() must be true - isMarkSeen() must be false
457         * 
458         * @return boolean
459         */
460        protected boolean isOpenReadOnly() {
461            return getConfiguration().isOpenReadOnly();
462        }
463    
464        /**
465         * Returns the recurse.
466         * 
467         * @return boolean
468         */
469        protected boolean isRecurse() {
470            return getConfiguration().isRecurse();
471        }
472    
473        /**
474         * Process the mail elements of the receiver
475         */
476        abstract public void process() throws MessagingException;
477    
478        /**
479         * Returns the blacklist.
480         * 
481         * @return Set
482         */
483        protected Set<MailAddress> getBlacklist() {
484            return getConfiguration().getBlacklist();
485        }
486    
487        /**
488         * Returns the <code>ParsedConfiguration</code> from the
489         * <code>Account</code>.
490         * 
491         * @return ParsedConfiguration
492         */
493        protected ParsedConfiguration getConfiguration() {
494            return getAccount().getParsedConfiguration();
495        }
496    
497        /**
498         * Returns a lazy initialised attributePrefix.
499         * 
500         * @return String
501         */
502        protected String getAttributePrefix() {
503            String value = null;
504            if (null == (value = getAttributePrefixBasic())) {
505                updateAttributePrefix();
506                return getAttributePrefix();
507            }
508            return value;
509        }
510    
511        /**
512         * Returns the attributePrefix.
513         * 
514         * @return String
515         */
516        private String getAttributePrefixBasic() {
517            return fieldAttributePrefix;
518        }
519    
520        /**
521         * Returns the computed attributePrefix.
522         * 
523         * @return String
524         */
525        protected String computeAttributePrefix() {
526            return getClass().getPackage().getName() + ".";
527        }
528    
529        /**
530         * Sets the attributePrefix.
531         * 
532         * @param attributePrefix
533         *            The attributePrefix to set
534         */
535        protected void setAttributePrefix(String attributePrefix) {
536            fieldAttributePrefix = attributePrefix;
537        }
538    
539        /**
540         * Updates the attributePrefix.
541         */
542        protected void updateAttributePrefix() {
543            setAttributePrefix(computeAttributePrefix());
544        }
545    
546        /**
547         * Returns the account.
548         * 
549         * @return Account
550         */
551        public Account getAccount() {
552            return fieldAccount;
553        }
554    
555        /**
556         * Sets the account.
557         * 
558         * @param account
559         *            The account to set
560         */
561        protected void setAccount(Account account) {
562            fieldAccount = account;
563        }
564    
565        /**
566         * Returns the getMaxMessageSizeLimit.
567         * 
568         * @return int
569         */
570        protected int getMaxMessageSizeLimit() {
571            return getConfiguration().getMaxMessageSizeLimit();
572        }
573    
574        protected MailQueue getMailQueue() {
575            return getConfiguration().getMailQueue();
576        }
577    
578    }