View Javadoc

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