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