View Javadoc

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