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 java.util.ArrayList;
25  import java.util.StringTokenizer;
26  
27  import javax.mail.MessagingException;
28  import javax.mail.internet.MimeMessage;
29  
30  import org.apache.mailet.base.GenericMailet;
31  import org.apache.mailet.Mail;
32  import org.apache.mailet.MailetException;
33  
34  /**
35   * Remove mime headers
36   * 
37   * Sample configuration:
38   * 
39   * <pre><code>
40   * &lt;mailet match="All" class="RemoveMimeHeader"&gt;
41   * &lt;name&gt;header1&lt;/name&gt;
42   * &lt;name&gt;header2&lt;/name&gt;
43   * &lt;/mailet&gt;
44   * </code></pre>
45   * 
46   */
47  public class RemoveMimeHeader extends GenericMailet {
48      
49      /**
50       * Arraylist which holds the headers which should be removed
51       */
52      ArrayList headers = new ArrayList();
53  
54      /**
55       * @see org.apache.mailet.GenericMailet#init()
56       */
57      public void init() throws MailetException {
58          String header = getInitParameter("name");
59          if (header != null) {
60              StringTokenizer st = new StringTokenizer(header, ",");
61              while (st.hasMoreTokens()) {
62                  headers.add(st.nextToken());
63              }
64          } else {
65              throw new MailetException(
66                      "Invalid config. Please specify atleast one name");
67          }
68      }
69      
70      /**
71       * @see org.apache.mailet.GenericMailet#service(Mail)
72       */
73      public void service(Mail mail) {
74          try {
75              MimeMessage  message = mail.getMessage();
76          
77              // loop through the headers
78              for (int i = 0; i < headers.size(); i++) {
79                  message.removeHeader((String) headers.get(i));
80              }
81              
82              message.saveChanges();
83  
84          } catch (MessagingException e) {
85              // just log the exception
86              log("Unable to remove headers: " + e.getMessage());
87          }
88      }
89      
90      /**
91       * @see org.apache.mailet.GenericMailet#getMailetInfo()
92       */
93      public String getMailetInfo() {
94          return "RemoveMimeHeader Mailet";
95      }
96  }