01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/package org.apache.james.transport.matchers;
19:
20: import org.apache.mailet.GenericMatcher;
21: import org.apache.mailet.Mail;
22: import org.apache.mailet.MatcherConfig;
23: import java.util.Collection;
24: import javax.mail.MessagingException;
25: import java.io.Serializable;
26:
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: /**
33: * <P>This Matcher determines if the mail contains the attribute specified in the
34: * condition and that attribute matches the supplied regular expression,
35: * it returns all recipients if that is the case.</P>
36: * <P>Sample configuration:</P>
37: * <PRE><CODE>
38: * <mailet match="HasMailAttributeWithValueRegex=whatever,<regex>" class="<any-class>">
39: * </CODE></PRE>
40: * Note: as it is not possible to put arbitrary objects in the configuration,
41: * toString() is called on the attribute value, and that is the value matched against.
42: *
43: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
44: * @since 2.2.0
45: **/
46: public class HasMailAttributeWithValueRegex extends GenericMatcher {
47:
48: private String attributeName;
49: private Perl5Matcher matcher = new Perl5Matcher();
50: private Pattern pattern = null;
51:
52: /**
53: * Return a string describing this matcher.
54: *
55: * @return a string describing this matcher
56: */
57: public String getMatcherInfo() {
58: return "Has Mail Attribute Value Matcher";
59: }
60:
61: public void init(MatcherConfig conf) throws MessagingException {
62: String condition = conf.getCondition();
63: int idx = condition.indexOf(',');
64: if (idx != -1) {
65: attributeName = condition.substring(0, idx).trim();
66: String pattern_string = condition.substring(idx + 1,
67: condition.length()).trim();
68: try {
69: Perl5Compiler compiler = new Perl5Compiler();
70: pattern = compiler.compile(pattern_string);
71: } catch (MalformedPatternException mpe) {
72: throw new MessagingException("Malformed pattern: "
73: + pattern_string, mpe);
74: }
75: } else {
76: throw new MessagingException(
77: "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: Serializable obj = mail.getAttribute(attributeName);
90: //to be a little more generic the toString of the value is what is matched against
91: if (obj != null && matcher.matches(obj.toString(), pattern)) {
92: return mail.getRecipients();
93: }
94: return null;
95: }
96:
97: }
|