KickJava   Java API By Example, From Geeks To Geeks.

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


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.GenericRecipientMatcher;
21 import org.apache.mailet.MailAddress;
22
23 import org.apache.oro.text.regex.*;
24
25 import javax.mail.MessagingException JavaDoc;
26
27 /**
28  * <P>Matches recipients whose address matches a regular expression.</P>
29  * <P>Is equivalent to the {@link SenderIsRegex} matcher but matching on the recipient.</P>
30  * <P>Configuration string: a regular expression.</P>
31  * <PRE><CODE>
32  * &lt;mailet match=&quot;RecipientIsRegex=&lt;regular-expression&gt;&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
33  * </CODE></PRE>
34  * <P>The example below will match any recipient in the format user@log.anything</P>
35  * <PRE><CODE>
36  * &lt;mailet match=&quot;RecipientIsRegex=(.*)@log\.(.*)&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
37  * &lt;/mailet&gt;
38  * </CODE></PRE>
39  *
40  * @version CVS $Revision: 1.1.2.4 $ $Date: 2004/03/15 03:54:21 $
41  */

42
43 public class RecipientIsRegex extends GenericRecipientMatcher {
44     Pattern pattern = null;
45
46     public void init() throws javax.mail.MessagingException JavaDoc {
47         String JavaDoc patternString = getCondition();
48         if (patternString == null) {
49             throw new MessagingException JavaDoc("Pattern is missing");
50         }
51         
52         patternString = patternString.trim();
53         Perl5Compiler compiler = new Perl5Compiler();
54         try {
55             pattern = compiler.compile(patternString, Perl5Compiler.READ_ONLY_MASK);
56         } catch(MalformedPatternException mpe) {
57             throw new MessagingException JavaDoc("Malformed pattern: " + patternString, mpe);
58         }
59     }
60
61     public boolean matchRecipient(MailAddress recipient) {
62         String JavaDoc myRecipient = recipient.toString();
63         Perl5Matcher matcher = new Perl5Matcher();
64         if (matcher.matches(myRecipient, pattern)){
65             return true;
66         } else {
67             return false;
68         }
69     }
70 }
71
Popular Tags