01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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(), ", ",
51: false);
52:
53: //..into a vector of domain names.
54: senderHosts = new java.util.HashSet();
55: while (st.hasMoreTokens()) {
56: senderHosts.add(st.nextToken().toLowerCase(Locale.US));
57: }
58: senderHosts = Collections.unmodifiableCollection(senderHosts);
59: }
60:
61: /**
62: * Takes the message and checks the sender (if there is one) against
63: * the vector of host names.
64: *
65: * Returns the collection of recipients if there's a match.
66: *
67: * @param mail the mail being processed
68: */
69: public Collection match(Mail mail) {
70: try {
71: if (mail.getSender() != null
72: && senderHosts.contains(mail.getSender().getHost()
73: .toLowerCase(Locale.US))) {
74: return mail.getRecipients();
75: }
76: } catch (Exception e) {
77: log(e.getMessage());
78: }
79:
80: return null; //No match.
81: }
82: }
|