View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
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   ****************************************************************/
19  
20  
21  
22  package org.apache.james.transport.matchers;
23  
24  import org.apache.mailet.base.GenericRecipientMatcher;
25  import org.apache.mailet.MailAddress;
26  import org.apache.oro.text.regex.MalformedPatternException;
27  import org.apache.oro.text.regex.Pattern;
28  import org.apache.oro.text.regex.Perl5Compiler;
29  import org.apache.oro.text.regex.Perl5Matcher;
30  
31  import javax.mail.MessagingException;
32  
33  /**
34   * <P>Matches recipients whose address matches a regular expression.</P>
35   * <P>Is equivalent to the {@link SenderIsRegex} matcher but matching on the recipient.</P>
36   * <P>Configuration string: a regular expression.</P>
37   * <PRE><CODE>
38   * &lt;mailet match=&quot;RecipientIsRegex=&lt;regular-expression&gt;&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
39   * </CODE></PRE>
40   * <P>The example below will match any recipient in the format user@log.anything</P>
41   * <PRE><CODE>
42   * &lt;mailet match=&quot;RecipientIsRegex=(.*)@log\.(.*)&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
43   * &lt;/mailet&gt;
44   * </CODE></PRE>
45   *
46   * @version CVS $Revision: 717869 $ $Date: 2008-11-15 15:56:18 +0000 (Sat, 15 Nov 2008) $
47   */
48  
49  public class RecipientIsRegex extends GenericRecipientMatcher {
50      Pattern pattern   = null;
51  
52      public void init() throws javax.mail.MessagingException {
53          String patternString = getCondition();
54          if ((patternString == null) || (patternString.equals("")) ) {
55              throw new MessagingException("Pattern is missing");
56          }
57          
58          patternString = patternString.trim();
59          Perl5Compiler compiler = new Perl5Compiler();
60          try {
61              pattern = compiler.compile(patternString, Perl5Compiler.READ_ONLY_MASK);
62          } catch(MalformedPatternException mpe) {
63              throw new MessagingException("Malformed pattern: " + patternString, mpe);
64          }
65      }
66  
67      public boolean matchRecipient(MailAddress recipient) {
68          String myRecipient = recipient.toString();
69          Perl5Matcher matcher  = new Perl5Matcher();
70          if (matcher.matches(myRecipient, pattern)){
71              return true;
72          } else {
73              return false;
74          }
75      }
76  }