KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.mailet.MailAddress;
23
24 import org.apache.oro.text.regex.*;
25
26 import java.util.Collection JavaDoc;
27
28 import javax.mail.MessagingException JavaDoc;
29
30 /**
31  * <P>Matches mails that are sent by a sender whose address matches a regular expression.</P>
32  * <P>Is equivalent to the {@link RecipientIsRegex} matcher but matching on the sender.</P>
33  * <P>Configuration string: a regular expression.</P>
34  * <PRE><CODE>
35  * &lt;mailet match=&quot;SenderIsRegex=&lt;regular-expression&gt;&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
36  * </CODE></PRE>
37  * <P>The example below will match any sender in the format user@log.anything</P>
38  * <PRE><CODE>
39  * &lt;mailet match=&quot;SenderIsRegex=(.*)@log\.(.*)&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
40  * &lt;/mailet&gt;
41  * </CODE></PRE>
42  * <P>Another example below will match any sender having some variations of the string
43  * <I>mp3</I> inside the username part.</P>
44  * <PRE><CODE>
45  * &lt;mailet match=&quot;SenderIsRegex=(.*)(mp3|emmepitre)(.*)@&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
46  * &lt;/mailet&gt;
47  * </CODE></PRE>
48  *
49  * @version CVS $Revision: 1.1.2.3 $ $Date: 2004/03/15 03:54:21 $
50  * @since 2.2.0
51  */

52 public class SenderIsRegex extends GenericMatcher {
53     Pattern pattern = null;
54
55     public void init() throws MessagingException JavaDoc {
56         String JavaDoc patternString = getCondition();
57         if (patternString == null) {
58             throw new MessagingException JavaDoc("Pattern is missing");
59         }
60         
61         patternString = patternString.trim();
62         Perl5Compiler compiler = new Perl5Compiler();
63         try {
64             pattern = compiler.compile(patternString, Perl5Compiler.READ_ONLY_MASK);
65         } catch(MalformedPatternException mpe) {
66             throw new MessagingException JavaDoc("Malformed pattern: " + patternString, mpe);
67         }
68     }
69
70     public Collection JavaDoc match(Mail mail) {
71         MailAddress mailAddress = mail.getSender();
72         if (mailAddress == null) {
73             return null;
74         }
75         String JavaDoc senderString = mailAddress.toString();
76         Perl5Matcher matcher = new Perl5Matcher();
77         if (matcher.matches(senderString, pattern)) {
78             return mail.getRecipients();
79         }
80         return null;
81     }
82 }
83
Popular Tags