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