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