View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.mailets;
19  
20  import javax.mail.MessagingException;
21  import javax.mail.internet.MimeMessage ;
22  
23  import org.apache.mailet.GenericMailet ;
24  import org.apache.mailet.Mail ;
25  
26  /***
27   * Adds a specified header and value to the message.
28   *
29   * Sample configuration:
30   *
31   * <mailet match="All" class="AddHeader">
32   *   <name>X-MailetHeader</name>
33   *   <value>TheHeaderValue</value>
34   * </mailet>
35   *
36   * @version 1.0.0, 2002-09-11
37   */
38  public class SetMimeHeader
39         extends GenericMailet {
40  
41      /***
42       * The name of the header to be added.
43       */
44      private String headerName;
45  
46      /***
47       * The value to be set for the header.
48       */
49      private String headerValue;
50  
51      /***
52       * Initialize the mailet.
53       */
54      public void init() throws MessagingException {
55          headerName = getInitParameter("name");
56          headerValue = getInitParameter("value");
57          
58          // Check if needed config values are used
59          if (headerName == null || headerName.equals("") || headerValue == null
60                  || headerValue.equals("")) {
61              throw new MessagingException("Please configure a name and a value");
62          }
63      }
64  
65      /***
66       * Takes the message and adds a header to it.
67       *
68       * @param mail the mail being processed
69       *
70       * @throws MessagingException if an error arises during message processing
71       */
72      public void service(Mail mail) {
73          try {
74              MimeMessage message = mail.getMessage () ;
75  
76              //Set the header name and value (supplied at init time).
77              message.setHeader(headerName, headerValue);
78              message.saveChanges();
79          } catch (javax.mail.MessagingException me) {
80              log (me.getMessage());
81          }
82      }
83  
84      /***
85       * Return a string describing this mailet.
86       *
87       * @return a string describing this mailet
88       */
89      public String getMailetInfo() {
90          return "SetMimeHeader Mailet" ;
91      }
92  
93  }
94