01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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 extends GenericMailet {
39:
40: /**
41: * The name of the header to be added.
42: */
43: private String headerName;
44:
45: /**
46: * The value to be set for the header.
47: */
48: private String headerValue;
49:
50: /**
51: * Initialize the mailet.
52: */
53: public void init() throws MessagingException {
54: headerName = getInitParameter("name");
55: headerValue = getInitParameter("value");
56:
57: // Check if needed config values are used
58: if (headerName == null || headerName.equals("")
59: || headerValue == null || headerValue.equals("")) {
60: throw new MessagingException(
61: "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: }
|