KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > AddHeader


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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.internet.MimeMessage JavaDoc ;
21
22 import org.apache.james.core.MailImpl ;
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 AddHeader
39        extends GenericMailet {
40
41     /**
42      * The name of the header to be added.
43      */

44     private String JavaDoc headerName;
45
46     /**
47      * The value to be set for the header.
48      */

49     private String JavaDoc headerValue;
50
51     /**
52      * Initialize the mailet.
53      */

54     public void init() {
55         headerName = getInitParameter("name");
56         headerValue = getInitParameter("value");
57     }
58
59     /**
60      * Takes the message and adds a header to it.
61      *
62      * @param mail the mail being processed
63      *
64      * @throws MessagingException if an error arises during message processing
65      */

66     public void service(Mail mail) {
67         try {
68             MimeMessage JavaDoc message = mail.getMessage () ;
69
70             //Set the header name and value (supplied at init time).
71
message.setHeader(headerName, headerValue);
72             message.saveChanges();
73         } catch (javax.mail.MessagingException JavaDoc me) {
74             log (me.getMessage());
75         }
76     }
77
78     /**
79      * Return a string describing this mailet.
80      *
81      * @return a string describing this mailet
82      */

83     public String JavaDoc getMailetInfo() {
84         return "AddHeader Mailet" ;
85     }
86
87 }
88
89
Popular Tags