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.GenericRecipientMatcher;
21: import org.apache.mailet.MailAddress;
22: import org.apache.oro.text.regex.MalformedPatternException;
23: import org.apache.oro.text.regex.Pattern;
24: import org.apache.oro.text.regex.Perl5Compiler;
25: import org.apache.oro.text.regex.Perl5Matcher;
26:
27: import javax.mail.MessagingException;
28:
29: /**
30: * <P>Matches recipients whose address matches a regular expression.</P>
31: * <P>Is equivalent to the {@link SenderIsRegex} matcher but matching on the recipient.</P>
32: * <P>Configuration string: a regular expression.</P>
33: * <PRE><CODE>
34: * <mailet match="RecipientIsRegex=<regular-expression>" class="<any-class>">
35: * </CODE></PRE>
36: * <P>The example below will match any recipient in the format user@log.anything</P>
37: * <PRE><CODE>
38: * <mailet match="RecipientIsRegex=(.*)@log\.(.*)" class="<any-class>">
39: * </mailet>
40: * </CODE></PRE>
41: *
42: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
43: */
44:
45: public class RecipientIsRegex extends GenericRecipientMatcher {
46: Pattern pattern = null;
47:
48: public void init() throws javax.mail.MessagingException {
49: String patternString = getCondition();
50: if (patternString == null) {
51: throw new MessagingException("Pattern is missing");
52: }
53:
54: patternString = patternString.trim();
55: Perl5Compiler compiler = new Perl5Compiler();
56: try {
57: pattern = compiler.compile(patternString,
58: Perl5Compiler.READ_ONLY_MASK);
59: } catch (MalformedPatternException mpe) {
60: throw new MessagingException("Malformed pattern: "
61: + patternString, mpe);
62: }
63: }
64:
65: public boolean matchRecipient(MailAddress recipient) {
66: String myRecipient = recipient.toString();
67: Perl5Matcher matcher = new Perl5Matcher();
68: if (matcher.matches(myRecipient, pattern)) {
69: return true;
70: } else {
71: return false;
72: }
73: }
74: }
|