1 /************************************************************************
2 * Copyright (c) 2000-2006 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 org.apache.mailet.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailetException;
23 import java.io.Serializable;
24 import java.util.Iterator;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import javax.mail.MessagingException;
29
30 /***
31 * This mailet sets attributes on the Mail.
32 *
33 * Sample configuration:
34 * <mailet match="All" class="SetMailAttribute">
35 * <name1>value1</name1>
36 * <name2>value2</name2>
37 * </mailet>
38 *
39 * @version CVS $Revision: 365582 $ $Date: 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) $
40 * @since 2.2.0
41 */
42 public class SetMailAttribute extends GenericMailet {
43
44 private HashMap attributesToSet = new HashMap(2);
45
46 private Set entries;
47
48 /***
49 * Return a string describing this mailet.
50 *
51 * @return a string describing this mailet
52 */
53 public String getMailetInfo() {
54 return "Set Mail Attribute Mailet";
55 }
56
57 /***
58 * Initialize the mailet
59 *
60 * @throws MailetException if the processor parameter is missing
61 */
62 public void init() throws MailetException
63 {
64 Iterator iter = getInitParameterNames();
65 while (iter.hasNext()) {
66 String name = iter.next().toString();
67 String value = getInitParameter (name);
68 attributesToSet.put (name,value);
69 }
70 entries = attributesToSet.entrySet();
71 }
72
73 /***
74 * Sets the configured attributes
75 *
76 * @param mail the mail to process
77 *
78 * @throws MessagingException in all cases
79 */
80 public void service(Mail mail) throws MessagingException {
81 if (entries != null) {
82 Iterator iter = entries.iterator();
83 while (iter.hasNext()) {
84 Map.Entry entry = (Map.Entry)iter.next();
85 mail.setAttribute ((String)entry.getKey(),(Serializable)entry.getValue());
86 }
87 }
88 }
89
90
91 }