View Javadoc

1   /************************************************************************
2    * Copyright (c) 2003-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17   
18  package org.apache.james.fetchmail;
19  
20  import java.util.List;
21  import java.util.Set;
22  
23  import javax.mail.MessagingException;
24  import javax.mail.Session;
25  
26  import org.apache.avalon.framework.logger.Logger;
27  import org.apache.james.services.MailServer;
28  import org.apache.mailet.MailAddress;
29  import org.apache.james.services.UsersRepository;
30  
31  /***
32   * <p>Class <code>ProcessorAbstract</code> is an abstract class that
33   * provides support for JavaMail processors. Concrete implementations are
34   * required to implement the abstract method <code>void process()</code> to
35   * process a JavaMail element.</p>
36   * 
37   * <p>Typically, processors are chained. A Store processor delegates to a Folder
38   * processor that delegates to a Message processor.</p>
39   * 
40   * <p><code>ProcessorAbstract</code> wraps an Account - see 
41   * <code>org.apache.james.fetchmail.Account</code>
42   * - providing contextual information about the environment for the processor.</p>
43   * 
44   * <p>Creation Date: 27-May-03</p>
45   * 
46   */
47  abstract public class ProcessorAbstract
48  {
49      /***
50       * The prefix to place in front of any mail attributes used by this Processor.
51       */ 
52      private String fieldAttributePrefix;
53      
54      /***
55       * The Account for this task
56       */
57      private Account fieldAccount;       
58  
59      /***
60       * Constructor for ProcessorAbstract.
61       */
62      private ProcessorAbstract()
63      {
64          super();
65      }
66      
67      /***
68       * Constructor for ProcessorAbstract.
69       * @param account The <code>Account</code> to be processed 
70       */
71      protected ProcessorAbstract(Account account)
72      {
73          this();
74          setAccount(account);        
75      }   
76      
77      /***
78       * Returns the defaultDomainName.
79       * @return String
80       */
81      protected String getDefaultDomainName()
82      {
83          return getConfiguration().getDefaultDomainName();
84      }
85      
86      /***
87       * Returns the defaultLocalPart.
88       * 
89       * @return String
90       */
91      protected String getDefaultLocalPart()
92      {
93          // TODO Consider making this configurable
94          return "FETCHMAIL-SERVICE";
95      }    
96      
97      /***
98       * Returns the message ids. of messages for which processing has been
99       * deferred as the recipient could not be found
100      * @return List
101      */
102     protected List getDeferredRecipientNotFoundMessageIDs()
103     {
104         return getAccount().getDeferredRecipientNotFoundMessageIDs();
105     }    
106     
107     /***
108      * Returns the fetchTaskName.
109      * @return String
110      */
111     protected String getFetchTaskName()
112     {
113         return getConfiguration().getFetchTaskName();
114     }   
115 
116 
117     /***
118      * Returns the host.
119      * @return String
120      */
121     protected String getHost()
122     {
123         return getConfiguration().getHost();
124     }
125 
126 
127     /***
128      * Returns the javaMailFolderName.
129      * @return String
130      */
131     protected String getJavaMailFolderName()
132     {
133         return getConfiguration().getJavaMailFolderName();
134     }
135 
136 
137 
138     /***
139      * Returns the javaMailProviderName.
140      * @return String
141      */
142     protected String getJavaMailProviderName()
143     {
144         return getConfiguration().getJavaMailProviderName();
145     }
146 
147 
148 
149     /***
150      * Returns the logger.
151      * @return Logger
152      */
153     protected Logger getLogger()
154     {
155         return getConfiguration().getLogger();
156     }
157 
158 
159     /***
160      * Returns the password.
161      * @return String
162      */
163     protected String getPassword()
164     {
165         return getAccount().getPassword();
166     }
167 
168 
169     /***
170      * Returns the recipient.
171      * @return MailAddress
172      */
173     protected MailAddress getRecipient()
174     {
175         return getAccount().getRecipient();
176     }
177     
178     /***
179      * Method getRemoteReceivedHeaderIndex.
180      * @return int
181      */
182     protected int getRemoteReceivedHeaderIndex()
183     {
184         return getConfiguration().getRemoteReceivedHeaderIndex();    
185     }
186 
187 
188     /***
189      * Returns the server.
190      * @return MailServer
191      */
192     protected MailServer getServer()
193     {
194         return getConfiguration().getServer();
195     }
196     
197     /***
198      * Returns the session.
199      * @return Session
200      */
201     protected Session getSession()
202     {
203         return getAccount().getSession();
204     }    
205     
206     /***
207      * Returns the repository of local users.
208      * @return UsersRepository
209      */
210     protected UsersRepository getLocalUsers()
211     {
212         return getConfiguration().getLocalUsers();
213     }   
214 
215 
216     /***
217      * Returns the user.
218      * @return String
219      */
220     protected String getUser()
221     {
222         return getAccount().getUser();
223     }
224 
225 
226     /***
227      * Returns the fetchAll.
228      * @return boolean
229      */
230     protected boolean isFetchAll()
231     {
232         return getConfiguration().isFetchAll();
233     }
234 
235 
236     /***
237      * Returns the isDeferRecipientNotFound.
238      * @return boolean
239      */
240     protected boolean isDeferRecipientNotFound()
241     {
242         return getConfiguration().isDeferRecipientNotFound();
243     }
244     
245     /***
246      * Returns the ignoreOriginalRecipient.
247      * @return boolean
248      */
249     protected boolean isIgnoreRecipientHeader()
250     {
251         return getAccount().isIgnoreRecipientHeader();
252     }
253 
254     /***
255      * Returns the customRecipientHeader.
256      * @return String
257      */
258     protected String getCustomRecipientHeader()
259     {
260         return getAccount().getCustomRecipientHeader();
261     }
262 
263     /***
264      * Returns the leave.
265      * @return boolean
266      */
267     protected boolean isLeave()
268     {
269         return getConfiguration().isLeave();
270     }
271 
272 
273     /***
274      * Returns the markSeen.
275      * @return boolean
276      */
277     protected boolean isMarkSeen()
278     {
279         return getConfiguration().isMarkSeen();
280     }
281     
282     /***
283      * Returns the leaveBlacklisted.
284      * @return boolean
285      */
286     protected boolean isLeaveBlacklisted()
287     {
288         return getConfiguration().isLeaveBlacklisted();
289     }
290     
291     /***
292      * Returns the leaveRemoteRecipient.
293      * @return boolean
294      */
295     protected boolean isLeaveRemoteRecipient()
296     {
297         return getConfiguration().isLeaveRemoteRecipient();
298     }   
299     
300     /***
301      * Returns the leaveUserUndefinded.
302      * @return boolean
303      */
304     protected boolean isLeaveUserUndefined()
305     {
306         return getConfiguration().isLeaveUserUndefined();
307     }
308     
309     /***
310      * Returns the leaveRemoteReceivedHeaderInvalid.
311      * @return boolean
312      */
313     protected boolean isLeaveRemoteReceivedHeaderInvalid()
314     {
315         return getConfiguration().isLeaveRemoteReceivedHeaderInvalid();
316     }    
317     
318     /***
319      * Returns the LeaveMaxMessageSizeExceeded.
320      * @return boolean
321      */
322     protected boolean isLeaveMaxMessageSizeExceeded()
323     {
324         return getConfiguration().isLeaveMaxMessageSizeExceeded();
325     }    
326     
327     /***
328      * Returns the leaveUndeliverable.
329      * @return boolean
330      */
331     protected boolean isLeaveUndeliverable()
332     {
333         return getConfiguration().isLeaveUndeliverable();
334     }       
335 
336     /***
337      * Returns the RejectUserUndefinded.
338      * @return boolean
339      */
340     protected boolean isRejectUserUndefined()
341     {
342         return getConfiguration().isRejectUserUndefined();
343     }
344     
345     /***
346      * Returns the RejectRemoteReceivedHeaderInvalid.
347      * @return boolean
348      */
349     protected boolean isRejectRemoteReceivedHeaderInvalid()
350     {
351         return getConfiguration().isRejectRemoteReceivedHeaderInvalid();
352     }    
353     
354     /***
355      * Returns the RejectMaxMessageSizeExceeded.
356      * @return boolean
357      */
358     protected boolean isRejectMaxMessageSizeExceeded()
359     {
360         return getConfiguration().isRejectMaxMessageSizeExceeded();
361     }    
362     
363     /***
364      * Returns the RejectUserBlacklisted.
365      * @return boolean
366      */
367     protected boolean isRejectBlacklisted()
368     {
369         return getConfiguration().isRejectBlacklisted();
370     }   
371     
372     /***
373      * Returns the RejectRemoteRecipient.
374      * @return boolean
375      */
376     protected boolean isRejectRemoteRecipient()
377     {
378         return getConfiguration().isRejectRemoteRecipient();
379     }
380     
381     /***
382      * Returns the markBlacklistedSeen.
383      * @return boolean
384      */
385     protected boolean isMarkBlacklistedSeen()
386     {
387         return getConfiguration().isMarkBlacklistedSeen();
388     }
389     
390     /***
391      * Returns the markRecipientNotFoundSeen.
392      * @return boolean
393      */
394     protected boolean isMarkRecipientNotFoundSeen()
395     {
396         return getConfiguration().isMarkRecipientNotFoundSeen();
397     }
398     
399     /***
400      * Returns the leaveRecipientNotFound.
401      * @return boolean
402      */
403     protected boolean isLeaveRecipientNotFound()
404     {
405         return getConfiguration().isLeaveRecipientNotFound();
406     }
407     
408     /***
409      * Returns the rejectRecipientNotFound.
410      * @return boolean
411      */
412     protected boolean isRejectRecipientNotFound()
413     {
414         return getConfiguration().isRejectRecipientNotFound();
415     }       
416 
417     /***
418      * Returns the markRemoteRecipientSeen.
419      * @return boolean
420      */
421     protected boolean isMarkRemoteRecipientSeen()
422     {
423         return getConfiguration().isMarkRemoteRecipientSeen();
424     }   
425     
426     /***
427      * Returns the markUserUndefindedSeen.
428      * @return boolean
429      */
430     protected boolean isMarkUserUndefinedSeen()
431     {
432         return getConfiguration().isMarkUserUndefinedSeen();
433     }
434     
435     /***
436      * Returns the markRemoteReceivedHeaderInvalidSeen.
437      * @return boolean
438      */
439     protected boolean isMarkRemoteReceivedHeaderInvalidSeen()
440     {
441         return getConfiguration().isMarkRemoteReceivedHeaderInvalidSeen();
442     }    
443     
444     /***
445      * Returns the MarkMaxMessageSizeExceededSeen.
446      * @return boolean
447      */
448     protected boolean isMarkMaxMessageSizeExceededSeen()
449     {
450         return getConfiguration().isMarkMaxMessageSizeExceededSeen();
451     }    
452     
453     /***
454      * Returns the markUndeliverableSeen.
455      * @return boolean
456      */
457     protected boolean isMarkUndeliverableSeen()
458     {
459         return getConfiguration().isMarkUndeliverableSeen();
460     }
461     
462     /***
463      * Answers true if the folder should be opened read only.
464      * For this to be true... 
465      * - isKeep() must be true
466      * - isMarkSeen() must be false
467      * @return boolean
468      */
469     protected boolean isOpenReadOnly()
470     {
471         return getConfiguration().isOpenReadOnly();
472     }   
473 
474 
475     /***
476      * Returns the recurse.
477      * @return boolean
478      */
479     protected boolean isRecurse()
480     {
481         return getConfiguration().isRecurse();
482     }
483 
484 
485     
486     
487     /***
488      * Process the mail elements of the receiver
489      */
490     abstract public void process() throws MessagingException;
491     
492     /***
493      * Returns the blacklist.
494      * @return Set
495      */
496     protected Set getBlacklist()
497     {
498         return getConfiguration().getBlacklist();
499     }
500     
501     /***
502      * Returns the <code>ParsedConfiguration</code> from the <code>Account</code>.
503      * @return ParsedConfiguration
504      */
505     protected ParsedConfiguration getConfiguration()
506     {
507         return getAccount().getParsedConfiguration();
508     }    
509 
510     /***
511      * Returns a lazy initialised attributePrefix.
512      * @return String
513      */
514     protected String getAttributePrefix()
515     {
516         String value = null;
517         if (null == (value = getAttributePrefixBasic()))
518         {
519             updateAttributePrefix();
520             return getAttributePrefix();
521         }    
522         return value;
523     }
524     
525     /***
526      * Returns the attributePrefix.
527      * @return String
528      */
529     private String getAttributePrefixBasic()
530     {
531         return fieldAttributePrefix;
532     }
533     
534     /***
535      * Returns the computed attributePrefix.
536      * @return String
537      */
538     protected String computeAttributePrefix()
539     {
540         return getClass().getPackage().getName() + ".";
541     }       
542 
543     /***
544      * Sets the attributePrefix.
545      * @param attributePrefix The attributePrefix to set
546      */
547     protected void setAttributePrefix(String attributePrefix)
548     {
549         fieldAttributePrefix = attributePrefix;
550     }
551     
552     /***
553      * Updates the attributePrefix.
554      */
555     protected void updateAttributePrefix()
556     {
557         setAttributePrefix(computeAttributePrefix());
558     }    
559 
560     /***
561      * Returns the account.
562      * @return Account
563      */
564     public Account getAccount()
565     {
566         return fieldAccount;
567     }
568 
569     /***
570      * Sets the account.
571      * @param account The account to set
572      */
573     protected void setAccount(Account account)
574     {
575         fieldAccount = account;
576     }
577     
578     /***
579      * Returns the getMaxMessageSizeLimit.
580      * @return int
581      */
582     protected int getMaxMessageSizeLimit()
583     {
584         return getConfiguration().getMaxMessageSizeLimit();
585     }    
586 
587 }