KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > SenderHostIs


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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 JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 /**
28  * Checkes the sender's displayed domain name against a supplied list.
29  *
30  * Sample configuration:
31  *
32  * <mailet match="SenderHostIs=domain.com" class="ToProcessor">
33  * <processor> spam </processor>
34  * </mailet>
35  *
36  * @version 1.0.0, 2002-09-10
37  */

38 public class SenderHostIs extends GenericMatcher {
39     /**
40      * The collection of host names to match against.
41      */

42     private Collection JavaDoc senderHosts;
43
44     /**
45      * Initialize the mailet.
46      */

47     public void init() {
48         //Parse the condition...
49
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getCondition(), ", ", false);
50
51         //..into a vector of domain names.
52
senderHosts = new java.util.HashSet JavaDoc();
53         while (st.hasMoreTokens()) {
54             senderHosts.add(((String JavaDoc)st.nextToken()).toLowerCase());
55         }
56         senderHosts = Collections.unmodifiableCollection(senderHosts);
57     }
58
59     /**
60      * Takes the message and checks the sender (if there is one) against
61      * the vector of host names.
62      *
63      * Returns the collection of recipients if there's a match.
64      *
65      * @param mail the mail being processed
66      */

67     public Collection JavaDoc match(Mail mail) {
68         try {
69             if (mail.getSender() != null && senderHosts.contains(mail.getSender().getHost().toLowerCase())) {
70                 return mail.getRecipients();
71             }
72         } catch (Exception JavaDoc e) {
73             log(e.getMessage());
74         }
75
76         return null; //No match.
77
}
78 }
79
Popular Tags