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