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  package org.apache.james.postage.mail;
20  
21  import javax.mail.MessagingException;
22  import javax.mail.internet.MimeMessage;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.james.postage.result.MailProcessingRecord;
26  import org.apache.james.postage.result.PostageRunnerResult;
27  
28  /***
29   * handles the process of retrieving, analyzing, matching and finally (optionally) dismissing one
30   * received message.
31   */
32  public abstract class MailAnalyzeStrategy {
33  
34      protected static Log log = LogFactory.getLog(MailAnalyzeStrategy.class);
35  
36      private String queue = null;
37      private PostageRunnerResult results = null;
38  
39      public MailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results) {
40          this.queue = receivingQueueName;
41          this.results = results;
42      }
43      
44      public void handle() throws Exception { 
45          MailProcessingRecord mailProcessingRecord = prepareRecord();
46  
47          MimeMessage message = loadMessage();
48          
49          // do we _really_ have to handle this?
50          if (!MailMatchingUtils.isMatchCandidate(message)) return;
51  
52          String id = MailMatchingUtils.getMailIdHeader(message);
53          try {
54              mailProcessingRecord.setByteReceivedTotal(message.getSize());
55      
56              mailProcessingRecord.setMailId(id);
57              mailProcessingRecord.setSubject(message.getSubject());
58      
59              mailProcessingRecord.setTimeFetchEnd(System.currentTimeMillis());
60      
61          } catch (MessagingException e) {
62              log.info(queue + ": failed to process mail. remains on server");
63              return;
64          } finally {
65              MailProcessingRecord matchedAndMergedRecord = results.matchMailRecord(mailProcessingRecord);
66              if (matchedAndMergedRecord != null) {
67                  MailMatchingUtils.validateMail(message, matchedAndMergedRecord);
68                  results.recordValidatedMatch(matchedAndMergedRecord);
69              }
70          }
71      
72          dismissMessage();
73      }
74  
75      /*** 
76       * mandatory override to make the message available
77       */
78      protected MimeMessage loadMessage() throws Exception {
79          return null;
80      }
81  
82      /***
83       * optional override to delete the message.
84       */
85      protected void dismissMessage() throws Exception {
86          ; // empty body
87      }
88      
89      private MailProcessingRecord prepareRecord() {
90          MailProcessingRecord mailProcessingRecord = new MailProcessingRecord();
91          mailProcessingRecord.setReceivingQueue(queue);
92          mailProcessingRecord.setTimeFetchStart(System.currentTimeMillis());
93          return mailProcessingRecord;
94      }
95  }