View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 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  import org.apache.oro.text.regex.MalformedPatternException;
23  import org.apache.oro.text.regex.Pattern;
24  import org.apache.oro.text.regex.Perl5Compiler;
25  import org.apache.oro.text.regex.Perl5Matcher;
26  
27  import javax.mail.MessagingException;
28  
29  /***
30   * <P>Matches recipients whose address matches a regular expression.</P>
31   * <P>Is equivalent to the {@link SenderIsRegex} matcher but matching on the recipient.</P>
32   * <P>Configuration string: a regular expression.</P>
33   * <PRE><CODE>
34   * &lt;mailet match=&quot;RecipientIsRegex=&lt;regular-expression&gt;&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
35   * </CODE></PRE>
36   * <P>The example below will match any recipient in the format user@log.anything</P>
37   * <PRE><CODE>
38   * &lt;mailet match=&quot;RecipientIsRegex=(.*)@log\.(.*)&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
39   * &lt;/mailet&gt;
40   * </CODE></PRE>
41   *
42   * @version CVS $Revision: 365582 $ $Date: 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) $
43   */
44  
45  public class RecipientIsRegex extends GenericRecipientMatcher {
46      Pattern pattern   = null;
47  
48      public void init() throws javax.mail.MessagingException {
49          String patternString = getCondition();
50          if (patternString == null) {
51              throw new MessagingException("Pattern is missing");
52          }
53          
54          patternString = patternString.trim();
55          Perl5Compiler compiler = new Perl5Compiler();
56          try {
57              pattern = compiler.compile(patternString, Perl5Compiler.READ_ONLY_MASK);
58          } catch(MalformedPatternException mpe) {
59              throw new MessagingException("Malformed pattern: " + patternString, mpe);
60          }
61      }
62  
63      public boolean matchRecipient(MailAddress recipient) {
64          String myRecipient = recipient.toString();
65          Perl5Matcher matcher  = new Perl5Matcher();
66          if (matcher.matches(myRecipient, pattern)){
67              return true;
68          } else {
69              return false;
70          }
71      }
72  }