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