View Javadoc

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