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.matchers;
21  
22  import org.apache.mailet.GenericMatcher;
23  import org.apache.mailet.Mail;
24  
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Locale;
28  import java.util.StringTokenizer;
29  
30  /***
31   * Checkes the sender's displayed domain name against a supplied list.
32   *
33   * Sample configuration:
34   *
35   * <mailet match="SenderHostIs=domain.com" class="ToProcessor">
36   *   <processor> spam </processor>
37   * </mailet>
38   *
39   * @version 1.0.0, 2002-09-10
40   */
41  public class SenderHostIs extends GenericMatcher {
42      /***
43       * The collection of host names to match against.
44       */
45      private Collection senderHosts;
46  
47      /***
48       * Initialize the mailet.
49       */
50      public void init()  {
51          //Parse the condition...
52          StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
53  
54          //..into a vector of domain names.
55          senderHosts = new java.util.HashSet();
56          while (st.hasMoreTokens()) {
57              senderHosts.add(st.nextToken().toLowerCase(Locale.US));
58          }
59          senderHosts = Collections.unmodifiableCollection(senderHosts);
60      }
61  
62      /***
63       * Takes the message and checks the sender (if there is one) against
64       * the vector of host names.
65       *
66       * Returns the collection of recipients if there's a match.
67       *
68       * @param mail the mail being processed
69       */
70      public Collection match(Mail mail) {
71          try {
72              if (mail.getSender() != null && senderHosts.contains(mail.getSender().getHost().toLowerCase(Locale.US))) {
73                  return mail.getRecipients();
74              }
75          } catch (Exception e) {
76              log(e.getMessage());
77          }
78  
79          return null;    //No match.
80      }
81  }