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.Message;
22  import javax.mail.MessagingException;
23  import javax.mail.Session;
24  import javax.mail.internet.MimeMessage;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.james.postage.configuration.MailSender;
29  import org.apache.james.postage.result.MailProcessingRecord;
30  
31  /***
32   * prototype of a mail factory, only missing the use case specific data. <br/>
33   * it prepares the message with all Postage specific headers.<br/>
34   * @see DefaultMailFactory as a template implementation  
35   */
36  public abstract class AbstractMailFactory {
37  
38      private static Log log = LogFactory.getLog(DefaultMailFactory.class);
39  
40      private static final char[] CHARSET = new char[]
41                                      {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
42                                       'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
43                                       'u', 'v', 'w', 'x', 'y', 'z',
44                                       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
45                                       'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
46                                       'U', 'V', 'W', 'X', 'Y', 'Z'};
47  
48      public static char getRandomChar() {
49          return CHARSET[getRandomInt()];
50      }
51  
52      private static int getRandomInt() {
53          return (int)(Math.random() * (double)(CHARSET.length - 1));
54      }
55  
56      public static byte getRandomByte() {
57          return (byte)(Math.random() * 255);
58      }
59  
60      public AbstractMailFactory() {
61          super();
62      }
63  
64      /***
65       * generates a mail containing data common to all test mails: postage headers, 
66       */
67      public Message createMail(Session mailSession, MailSender mailSender, MailProcessingRecord mailProcessingRecord) {
68          
69          MimeMessage message = new MimeMessage(mailSession);
70      
71          try {
72              message.addHeader(HeaderConstants.JAMES_POSTAGE_HEADER, "This is a test mail sent by James Postage");
73              message.addHeader(HeaderConstants.JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER, getValidatorClass().getName());
74              message.setSubject(mailSender.getSubject());
75              message.addHeader("Message-ID", "Postage-" + System.currentTimeMillis());
76              mailProcessingRecord.setSubject(mailSender.getSubject());
77      
78              if (mailProcessingRecord.getMailId() != null) {
79                  message.addHeader(HeaderConstants.MAIL_ID_HEADER, mailProcessingRecord.getMailId());
80              } else {
81                  log.warn("ID header is NULL!");
82                  throw new RuntimeException("could not create mail with ID = NULL");
83              }
84      
85              populateMessage(message, mailSender, mailProcessingRecord);
86      
87          } catch (MessagingException e) {
88              mailProcessingRecord.setErrorTextSending(e.toString());
89              log.error("mail could not be created", e);
90              return null;
91          }
92          return message;
93      }
94  
95      /***
96       * here, the test case specific data must be added to the message.
97       * @param message
98       * @param mailSender
99       * @param mailProcessingRecord
100      * @throws MessagingException
101      */
102     abstract protected void populateMessage(MimeMessage message, MailSender mailSender, MailProcessingRecord mailProcessingRecord) throws MessagingException;
103 
104     /***
105      * the class representing the validator 
106      * 
107      * @return validator class
108      */
109     abstract protected Class getValidatorClass();
110     
111     protected int generateRandomPartSize(int sizeMin, int sizeMax) {
112         return (int)(Math.random() * (double)(sizeMax - sizeMin)) + sizeMin;
113     }
114 
115 }