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