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  
21  
22  package org.apache.james.transport.mailets;
23  
24  import org.apache.mailet.base.GenericMailet;
25  import org.apache.mailet.Mail;
26  import org.apache.mailet.MailetException;
27  import java.util.Iterator;
28  import java.util.ArrayList;
29  import java.util.StringTokenizer;
30  import javax.mail.MessagingException;
31  
32  /**
33   * This mailet sets attributes on the Mail.
34   * 
35   * Sample configuration:
36   * <pre><code>
37   * &lt;mailet match="All" class="RemoveMailAttribute"&gt;
38   *   &lt;name&gt;attribute_name1&lt;/name&gt;
39   *   &lt;name&gt;attribute_name2&lt;/name&gt;
40   * &lt;/mailet&gt;
41   * </code></pre>
42   *
43   * @version CVS $Revision: 713949 $ $Date: 2008-11-14 07:40:21 +0000 (Fri, 14 Nov 2008) $
44   * @since 2.2.0
45   */
46  public class RemoveMailAttribute extends GenericMailet {
47      
48      private ArrayList attributesToRemove = new ArrayList();
49      
50      /**
51       * Return a string describing this mailet.
52       *
53       * @return a string describing this mailet
54       */
55      public String getMailetInfo() {
56          return "Remove Mail Attribute Mailet";
57      }
58  
59      /**
60       * Initialize the mailet
61       *
62       * @throws MailetException if the processor parameter is missing
63       */
64      public void init() throws MailetException
65      {
66          String name = getInitParameter("name");
67  
68          if (name != null) {
69              StringTokenizer st = new StringTokenizer(name, ",") ;
70              while (st.hasMoreTokens()) {
71                  String attribute_name = st.nextToken().trim() ;
72                  attributesToRemove.add(attribute_name);
73              }
74          } else {
75              throw new MailetException("Please configure at least one attribute to remove");
76          }
77      }
78  
79      /**
80       * Remove the configured attributes
81       *
82       * @param mail the mail to process
83       *
84       * @throws MessagingException in all cases
85       */
86      public void service(Mail mail) throws MessagingException {
87          Iterator iter = attributesToRemove.iterator();
88          while (iter.hasNext()) {
89              String attribute_name = iter.next().toString();
90              mail.removeAttribute (attribute_name);
91          }
92      }
93      
94  
95  }