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