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.jsieve.mailet;
20  
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  
26  import javax.mail.Address;
27  import javax.mail.MessagingException;
28  import javax.mail.internet.InternetAddress;
29  import javax.mail.internet.MimeMessage;
30  import javax.mail.internet.MimeMultipart;
31  
32  import org.apache.jsieve.mail.Action;
33  import org.apache.jsieve.mail.ActionReject;
34  import org.apache.mailet.Mail;
35  import org.apache.mailet.MailAddress;
36  import org.apache.jsieve.mailet.mdn.ActionModeAutomatic;
37  import org.apache.jsieve.mailet.mdn.Disposition;
38  import org.apache.jsieve.mailet.mdn.DispositionModifier;
39  import org.apache.jsieve.mailet.mdn.MDNFactory;
40  import org.apache.jsieve.mailet.mdn.ModifierError;
41  import org.apache.jsieve.mailet.mdn.SendingModeAutomatic;
42  import org.apache.jsieve.mailet.mdn.TypeDeleted;
43  
44  public class RejectAction implements MailAction {
45  
46      public void execute(Action action, Mail mail, ActionContext context)
47              throws MessagingException {
48          if (action instanceof ActionReject) {
49              final ActionReject actionReject = (ActionReject) action;
50              execute(actionReject, mail, context);
51          }
52  
53      }
54  
55      /**
56       * <p>
57       * Method execute executes the passed ActionReject. It sends an RFC 2098
58       * compliant reject MDN back to the sender.
59       * </p>
60       * <p>
61       * NOTE: The Mimecontent type should be 'report', but as we do not yet have
62       * a DataHandler for this yet, its currently 'text'!
63       * 
64       * @param anAction not null
65       * @param aMail not null
66       * @param context not null
67       * @throws MessagingException
68       */
69      public void execute(ActionReject anAction, Mail aMail, ActionContext context) throws MessagingException
70      {
71          ActionUtils.detectAndHandleLocalLooping(aMail, context, "reject");
72  
73          // Create the MDN part
74          StringBuffer humanText = new StringBuffer(128);
75          humanText
76                  .append("This message was refused by the recipient's mail filtering program.");
77          humanText.append("\r\n");
78          humanText.append("The reason given was:");
79          humanText.append("\r\n");
80          humanText.append("\r\n");
81          humanText.append(anAction.getMessage());
82  
83          String reporting_UA_name = null;
84          try
85          {
86              reporting_UA_name = InetAddress.getLocalHost()
87                      .getCanonicalHostName();
88          }
89          catch (UnknownHostException ex)
90          {
91              reporting_UA_name = "localhost";
92          }
93  
94          String reporting_UA_product = context.getServerInfo();
95  
96          String[] originalRecipients = aMail.getMessage().getHeader(
97                  "Original-Recipient");
98          String original_recipient = null;
99          if (null != originalRecipients && originalRecipients.length > 0)
100         {
101             original_recipient = originalRecipients[0];
102         }
103 
104         MailAddress soleRecipient = ActionUtils.getSoleRecipient(aMail);
105         String final_recipient = soleRecipient.toString();
106 
107         String original_message_id = aMail.getMessage().getMessageID();
108 
109         DispositionModifier modifiers[] = {new ModifierError()};
110         Disposition disposition = new Disposition(new ActionModeAutomatic(),
111                 new SendingModeAutomatic(), new TypeDeleted(), modifiers);
112 
113         MimeMultipart multiPart = MDNFactory.create(humanText.toString(),
114                 reporting_UA_name, reporting_UA_product, original_recipient,
115                 final_recipient, original_message_id, disposition);
116 
117         // Send the message
118         MimeMessage reply = (MimeMessage) aMail.getMessage().reply(false);
119         reply.setFrom(soleRecipient.toInternetAddress());
120         reply.setContent(multiPart);
121         reply.saveChanges();
122         Address[] recipientAddresses = reply.getAllRecipients();
123         if (null != recipientAddresses)
124         {
125             Collection recipients = new ArrayList(recipientAddresses.length);
126             for (int i = 0; i < recipientAddresses.length; i++)
127             {
128                 recipients.add(new MailAddress(
129                         (InternetAddress) recipientAddresses[i]));
130             }
131             context.post(null, recipients, reply);
132         }
133         else
134         {
135             context.getLog().info("Unable to send reject MDN. Could not determine the recipient.");
136         }
137         // Ghost the original mail
138         aMail.setState(Mail.GHOST);
139     }
140 
141 }