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