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  package org.apache.james.transport.matchers;
21  
22  
23  import org.apache.mailet.GenericMatcher;
24  import org.apache.mailet.Mail;
25  import org.apache.mailet.MatcherConfig;
26  import java.util.Collection;
27  import javax.mail.MessagingException;
28  import java.io.Serializable;
29  
30  import org.apache.oro.text.regex.MalformedPatternException;
31  import org.apache.oro.text.regex.Pattern;
32  import org.apache.oro.text.regex.Perl5Compiler;
33  import org.apache.oro.text.regex.Perl5Matcher;
34  
35  /***
36   * <P>This Matcher determines if the mail contains the attribute specified in the
37   * condition and that attribute matches the supplied regular expression,
38   * it returns all recipients if that is the case.</P>
39   * <P>Sample configuration:</P>
40   * <PRE><CODE>
41   * &lt;mailet match="HasMailAttributeWithValueRegex=whatever,<regex>" class=&quot;&lt;any-class&gt;&quot;&gt;
42   * </CODE></PRE>
43   * Note: as it is not possible to put arbitrary objects in the configuration,
44   * toString() is called on the attribute value, and that is the value matched against.
45   *
46   * @version CVS $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (Mon, 08 Jan 2007) $
47   * @since 2.2.0
48   **/
49  public class HasMailAttributeWithValueRegex extends GenericMatcher 
50  {
51      
52      private String attributeName;
53      private Perl5Matcher matcher  = new Perl5Matcher();
54      private Pattern pattern   = null;
55  
56      /***
57       * Return a string describing this matcher.
58       *
59       * @return a string describing this matcher
60       */
61      public String getMatcherInfo() {
62          return "Has Mail Attribute Value Matcher";
63      }
64  
65      public void init (MatcherConfig conf) throws MessagingException
66      {
67          String condition = conf.getCondition();
68          int idx = condition.indexOf(',');
69          if (idx != -1) {
70              attributeName = condition.substring(0,idx).trim();
71              String pattern_string = condition.substring (idx+1, condition.length()).trim();
72              try {
73                  Perl5Compiler compiler = new Perl5Compiler();
74                  pattern = compiler.compile(pattern_string);
75              } catch(MalformedPatternException mpe) {
76                  throw new MessagingException("Malformed pattern: " + pattern_string, mpe);
77              }
78          } else {
79              throw new MessagingException ("malformed condition for HasMailAttributeWithValueRegex. must be of the form: attr,regex");
80          }
81      }
82  
83      /***
84       * @param mail the mail to check.
85       * @return all recipients if the part of the condition prior to the first equalsign
86       * is the name of an attribute set on the mail and the part of the condition after
87       * interpreted as a regular expression matches the toString value of the
88       * corresponding attributes value.
89       **/
90      public Collection match (Mail mail) throws MessagingException
91      {
92          Serializable obj = mail.getAttribute (attributeName);
93          //to be a little more generic the toString of the value is what is matched against
94          if ( obj != null && matcher.matches(obj.toString(), pattern)) {
95              return mail.getRecipients();
96          } 
97          return null;
98      }
99      
100 }