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