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  package org.apache.james.transport.mailets;
21  
22  import javax.mail.MessagingException;
23  import javax.mail.internet.MimeMessage ;
24  
25  import org.apache.mailet.GenericMailet ;
26  import org.apache.mailet.Mail ;
27  
28  /***
29   * Adds a specified header and value to the message.
30   *
31   * Sample configuration:
32   *
33   * <mailet match="All" class="AddHeader">
34   *   <name>X-MailetHeader</name>
35   *   <value>TheHeaderValue</value>
36   * </mailet>
37   *
38   * @version 1.0.0, 2002-09-11
39   */
40  public class SetMimeHeader
41         extends GenericMailet {
42  
43      /***
44       * The name of the header to be added.
45       */
46      private String headerName;
47  
48      /***
49       * The value to be set for the header.
50       */
51      private String headerValue;
52  
53      /***
54       * Initialize the mailet.
55       */
56      public void init() throws MessagingException {
57          headerName = getInitParameter("name");
58          headerValue = getInitParameter("value");
59          
60          // Check if needed config values are used
61          if (headerName == null || headerName.equals("") || headerValue == null
62                  || headerValue.equals("")) {
63              throw new MessagingException("Please configure a name and a value");
64          }
65      }
66  
67      /***
68       * Takes the message and adds a header to it.
69       *
70       * @param mail the mail being processed
71       *
72       * @throws MessagingException if an error arises during message processing
73       */
74      public void service(Mail mail) {
75          try {
76              MimeMessage message = mail.getMessage () ;
77  
78              //Set the header name and value (supplied at init time).
79              message.setHeader(headerName, headerValue);
80              message.saveChanges();
81          } catch (javax.mail.MessagingException me) {
82              log (me.getMessage());
83          }
84      }
85  
86      /***
87       * Return a string describing this mailet.
88       *
89       * @return a string describing this mailet
90       */
91      public String getMailetInfo() {
92          return "SetMimeHeader Mailet" ;
93      }
94  
95  }
96