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