View Javadoc

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