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 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: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
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: Iterator iter = getInitParameterNames();
64: while (iter.hasNext()) {
65: String name = iter.next().toString();
66: String value = getInitParameter(name);
67: attributesToSet.put(name, value);
68: }
69: entries = attributesToSet.entrySet();
70: }
71:
72: /**
73: * Sets the configured attributes
74: *
75: * @param mail the mail to process
76: *
77: * @throws MessagingException in all cases
78: */
79: public void service(Mail mail) throws MessagingException {
80: if (entries != null) {
81: Iterator iter = entries.iterator();
82: while (iter.hasNext()) {
83: Map.Entry entry = (Map.Entry) iter.next();
84: mail.setAttribute((String) entry.getKey(),
85: (Serializable) entry.getValue());
86: }
87: }
88: }
89:
90: }
|