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 java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.StringTokenizer;
27
28 import javax.mail.MessagingException;
29 import javax.mail.internet.MimeMessage;
30
31 import org.apache.mailet.base.GenericMailet;
32 import org.apache.mailet.Mail;
33
34 /**
35 * <p>Convert attributes to headers</p>
36 *
37 * <p>Sample configuration:</p>
38 * <pre><code>
39 * <mailet match="All" class="MailAttributesToMimeHeaders">
40 * <simplemapping>org.apache.james.attribute1;
41 * headerName1</simplemapping>
42 * <simplemapping>org.apache.james.attribute2;
43 * headerName2</simplemapping> </mailet>
44 * </code></pre>
45 */
46 public class MailAttributesToMimeHeaders extends GenericMailet {
47
48 /**
49 * HashMap which holds the attributeName and headerName
50 */
51 private HashMap map = new HashMap();
52
53 /**
54 * @see org.apache.mailet.GenericMailet#init()
55 */
56 public void init() throws MessagingException {
57 String simplemappings = getInitParameter("simplemapping");
58 if (simplemappings != null) {
59
60 StringTokenizer st = new StringTokenizer(simplemappings, ",");
61 while (st.hasMoreTokens()) {
62
63 String parameters[] = st.nextToken().split(";");
64
65 // Check if we have a valid config
66 if (parameters.length > 2 || parameters.length < 2) {
67 throw new MessagingException(
68 "Invalid config. Please use \"attributeName; headerName\"");
69 } else {
70 // Add it to the map
71 map.put(parameters[0].trim(), parameters[1].trim());
72 }
73 }
74 } else {
75 throw new MessagingException(
76 "Invalid config. Please use \"attributeName; headerName\"");
77 }
78 }
79
80 /**
81 * @see org.apache.mailet.GenericMailet#service(Mail)
82 */
83 public void service(Mail mail) {
84 MimeMessage message;
85 try {
86 message = mail.getMessage();
87
88 Iterator keys = map.keySet().iterator();
89
90 while (keys.hasNext()) {
91 String key = keys.next().toString();
92 String value = (String) mail.getAttribute(key);
93 String headerName = map.get(key).toString();
94
95 // Check if we have all needed values
96 if (headerName != null && value != null) {
97 // Add the header
98 message.setHeader(headerName, value);
99 }
100 }
101 message.saveChanges();
102 } catch (MessagingException e) {
103 log(e.getMessage());
104 }
105 }
106
107 }