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.RFC2822Headers;
21: import org.apache.mailet.GenericMatcher;
22: import org.apache.mailet.Mail;
23: import org.apache.oro.text.regex.MalformedPatternException;
24: import org.apache.oro.text.regex.Pattern;
25: import org.apache.oro.text.regex.Perl5Compiler;
26: import org.apache.oro.text.regex.Perl5Matcher;
27:
28: import javax.mail.MessagingException;
29: import javax.mail.internet.MimeMessage;
30: import java.util.Collection;
31:
32: /**
33: * This is a generic matcher that uses regular expressions. If any of
34: * the regular expressions match, the matcher is considered to have
35: * matched. This is an abstract class that must be subclassed to feed
36: * patterns. Patterns are provided by calling the compile method. A
37: * subclass will generally call compile() once during init(), but it
38: * could subclass match(), and call it as necessary during message
39: * processing (e.g., if a file of expressions changed).
40: *
41: * @
42: */
43:
44: abstract public class GenericRegexMatcher extends GenericMatcher {
45: protected Object[][] patterns;
46:
47: public void compile(Object[][] patterns)
48: throws MalformedPatternException {
49: // compile a bunch of regular expressions
50: this .patterns = patterns;
51: for (int i = 0; i < patterns.length; i++) {
52: String pattern = (String) patterns[i][1];
53: patterns[i][1] = new Perl5Compiler().compile(pattern,
54: Perl5Compiler.READ_ONLY_MASK
55: | Perl5Compiler.SINGLELINE_MASK);
56: }
57: }
58:
59: abstract public void init() throws MessagingException;
60:
61: public Collection match(Mail mail) throws MessagingException {
62: MimeMessage message = mail.getMessage();
63: Perl5Matcher matcher = new Perl5Matcher();
64:
65: //Loop through all the patterns
66: if (patterns != null)
67: for (int i = 0; i < patterns.length; i++) {
68: //Get the header name
69: String headerName = (String) patterns[i][0];
70: //Get the patterns for that header
71: Pattern pattern = (Pattern) patterns[i][1];
72: //Get the array of header values that match that
73: String headers[] = message.getHeader(headerName);
74: //Loop through the header values
75: if (headers != null)
76: for (int j = 0; j < headers.length; j++) {
77: if (matcher.matches(headers[j], pattern)) {
78: // log("Match: " + headerName + "[" + j + "]: " + headers[j]);
79: return mail.getRecipients();
80: }
81: //log(" " + headerName + "[" + j + "]: " + headers[j]);
82: }
83: }
84: return null;
85: }
86: }
|