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.util.mail.mdn;
20  
21  import javax.mail.MessagingException;
22  import javax.mail.internet.MimeBodyPart;
23  
24  import org.apache.james.util.mail.MimeMultipartReport;
25  
26  /***
27   * Class <code>MDNFactory</code> creates MimeMultipartReports containing
28   * Message Delivery Notifications as specified by RFC 2298.
29   */
30  public class MDNFactory
31  {
32  
33      /***
34       * Default Constructor
35       */
36      private MDNFactory()
37      {
38          super();
39      }
40      
41      /***
42       * Answers a MimeMultipartReport containing a
43       * Message Delivery Notification as specified by RFC 2298.
44       * 
45       * @param humanText
46       * @param reporting_UA_name
47       * @param reporting_UA_product
48       * @param original_recipient
49       * @param final_recipient
50       * @param original_message_id
51       * @param disposition
52       * @return MimeMultipartReport
53       * @throws MessagingException
54       */
55      static public MimeMultipartReport create(String humanText,
56              String reporting_UA_name,
57              String reporting_UA_product,
58              String original_recipient,
59              String final_recipient,
60              String original_message_id,
61              Disposition disposition) throws MessagingException
62      {
63          // Create the message parts. According to RFC 2298, there are two
64          // compulsory parts and one optional part...
65          MimeMultipartReport multiPart = new MimeMultipartReport();
66          multiPart.setReportType("disposition-notification");
67          
68          // Part 1: The 'human-readable' part
69          MimeBodyPart humanPart = new MimeBodyPart();
70          humanPart.setText(humanText);
71          multiPart.addBodyPart(humanPart);
72  
73          // Part 2: MDN Report Part
74          // 1) reporting-ua-field
75          StringBuffer mdnReport = new StringBuffer(128);
76          mdnReport.append("Reporting-UA: ");
77          mdnReport.append((reporting_UA_name == null ? "" : reporting_UA_name));
78          mdnReport.append("; ");
79          mdnReport.append((reporting_UA_product == null ? "" : reporting_UA_product));
80          mdnReport.append("\r\n");
81          // 2) original-recipient-field
82          if (null != original_recipient)
83          {
84              mdnReport.append("Original-Recipient: ");
85              mdnReport.append("rfc822; ");
86              mdnReport.append(original_recipient);
87              mdnReport.append("\r\n");
88          }
89          // 3) final-recipient-field
90          mdnReport.append("Final-Recepient: ");
91          mdnReport.append("rfc822; ");
92          mdnReport.append((final_recipient == null ? "" : final_recipient));
93          mdnReport.append("\r\n");
94          // 4) original-message-id-field
95          mdnReport.append("Original-Message-ID: ");
96          mdnReport.append((original_message_id == null ? "" : original_message_id));
97          mdnReport.append("\r\n");
98          // 5) disposition-field
99          mdnReport.append(disposition.toString());
100         mdnReport.append("\r\n");
101         MimeBodyPart mdnPart = new MimeBodyPart();
102         mdnPart.setContent(mdnReport.toString(), "message/disposition-notification");
103         multiPart.addBodyPart(mdnPart);
104 
105         // Part 3: The optional third part, the original message is omitted.
106         // We don't want to propogate over-sized, virus infected or
107         // other undesirable mail!
108         // There is the option of adding a Text/RFC822-Headers part, which
109         // includes only the RFC 822 headers of the failed message. This is
110         // described in RFC 1892. It would be a useful addition!        
111         return multiPart;
112     }
113 
114 }