1 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 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 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 88 public Collection match (Mail mail) throws MessagingException 89 { 90 Serializable obj = mail.getAttribute (attributeName); 91 if ( obj != null && matcher.matches(obj.toString(), pattern)) { 93 return mail.getRecipients(); 94 } 95 return null; 96 } 97 98 } 99 | Popular Tags |