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.GenericMatcher;
25  import org.apache.mailet.Mail;
26  import org.apache.mailet.MailAddress;
27  import org.apache.oro.text.regex.MalformedPatternException;
28  import org.apache.oro.text.regex.Pattern;
29  import org.apache.oro.text.regex.Perl5Compiler;
30  import org.apache.oro.text.regex.Perl5Matcher;
31  
32  import java.util.Collection;
33  
34  import javax.mail.MessagingException;
35  
36  /**
37   * <P>Matches mails that are sent by a sender whose address matches a regular expression.</P>
38   * <P>Is equivalent to the {@link RecipientIsRegex} matcher but matching on the sender.</P>
39   * <P>Configuration string: a regular expression.</P>
40   * <PRE><CODE>
41   * &lt;mailet match=&quot;SenderIsRegex=&lt;regular-expression&gt;&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
42   * </CODE></PRE>
43   * <P>The example below will match any sender in the format user@log.anything</P>
44   * <PRE><CODE>
45   * &lt;mailet match=&quot;SenderIsRegex=(.*)@log\.(.*)&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
46   * &lt;/mailet&gt;
47   * </CODE></PRE>
48   * <P>Another example below will match any sender having some variations of the string
49   * <I>mp3</I> inside the username part.</P>
50   * <PRE><CODE>
51   * &lt;mailet match=&quot;SenderIsRegex=(.*)(mp3|emmepitre)(.*)@&quot; class=&quot;&lt;any-class&gt;&quot;&gt;
52   * &lt;/mailet&gt;
53   * </CODE></PRE>
54   *
55   * @version CVS $Revision: 717869 $ $Date: 2008-11-15 15:56:18 +0000 (Sat, 15 Nov 2008) $
56   * @since 2.2.0
57   */
58  public class SenderIsRegex extends GenericMatcher {
59      Pattern pattern   = null;
60  
61      public void init() throws MessagingException {
62          String patternString = getCondition();
63          if ((patternString == null) || (patternString.equals(""))) {
64              throw new MessagingException("Pattern is missing");
65          }
66          
67          patternString = patternString.trim();
68          Perl5Compiler compiler = new Perl5Compiler();
69          try {
70              pattern = compiler.compile(patternString, Perl5Compiler.READ_ONLY_MASK);
71          } catch(MalformedPatternException mpe) {
72              throw new MessagingException("Malformed pattern: " + patternString, mpe);
73          }
74      }
75  
76      public Collection match(Mail mail) {
77          MailAddress mailAddress = mail.getSender();
78          if (mailAddress == null) {
79              return null;
80          }
81          String senderString = mailAddress.toString();
82          Perl5Matcher matcher  = new Perl5Matcher();
83          if (matcher.matches(senderString, pattern)) {
84              return mail.getRecipients();
85          }
86          return null;
87      }
88  }