Index: server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Or.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Or.java (revision ) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Or.java (revision ) @@ -0,0 +1,106 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.matchers; + +import org.apache.james.mailetcontainer.impl.matchers.GenericCompositeMatcher; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Mail; +import org.apache.mailet.Matcher; + +import java.util.Collection; +import java.util.Iterator; +import java.util.ArrayList; +import javax.mail.MessagingException; + +public class Or extends GenericCompositeMatcher { + + /** + * This is the Or CompositeMatcher - consider it to be a union of the + * results. If any match results in a full set of recipients the matching is + * short-circuited. + * + * @return Collection of Recipient from the Or composition results of the + * child matchers. + */ + public Collection match(Mail mail) throws MessagingException { + Collection finalResult = null; + Matcher matcher = null; + boolean first = true; + + // the size of the complete set of recipients + int size = mail.getRecipients().size(); + + // Loop through until the finalResult is full or all the child matchers + // have been executed + for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { + matcher = (Matcher) matcherIter.next(); + // log("Matching with " + // + matcher + // .getMatcherConfig() + // .getMatcherName() + // ); + Collection result = matcher.match(mail); + if (first) { + if (result == null) { + result = new ArrayList(0); + } + finalResult = result; + first = false; + } else { + // Check if we need to Or ... + // if the finalResult and the subsequent result are the same + // collection, then it contains the same recipients + // so we can short-circuit building the OR of the two + if (finalResult != result) { + if (result != null) { + if (finalResult == null) { + finalResult = result; + } else { + // the two results are different collections, so we + // must OR them + // Ensure that the finalResult only contains one + // copy of the recipients in the result collection + MailAddress recipient = null; + for (Iterator i = result.iterator(); i.hasNext();) { + recipient = (MailAddress) i.next(); + if (!finalResult.contains(recipient)) { + System.out.println(recipient); + finalResult.add(recipient); + } + } + recipient = null; + } + } + } + } + if (finalResult.size() == size) { + // we have a complete set of recipients, no need to OR in + // anymore + // i.e. short-circuit the Or + break; + } + result = null; + matcher = null; + } + // log("OrMatch: end."); + return finalResult; + } + +} Index: server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Xor.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Xor.java (revision ) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Xor.java (revision ) @@ -0,0 +1,94 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.matchers; + +import java.util.Collection; +import java.util.Iterator; +import java.util.ArrayList; + +import org.apache.james.mailetcontainer.impl.matchers.GenericCompositeMatcher; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Mail; +import javax.mail.MessagingException; +import org.apache.mailet.Matcher; + +public class Xor extends GenericCompositeMatcher { + + /** + * This is the Xor CompositeMatcher - consider it to be the inequality + * operator for recipients. If any recipients match other matcher results + * then the result does not include that recipient. + * + * @return Collection of Recipients from the Xor composition of the child + * matchers. + */ + public Collection match(Mail mail) throws MessagingException { + Collection finalResult = null; + Matcher matcher = null; + boolean first = true; + for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { + matcher = (Matcher) (matcherIter.next()); + Collection result = matcher.match(mail); + if (result == null) { + result = new ArrayList(0); + } + // log("Matching with " + + // matcher.getMatcherConfig().getMatcherName() + + // " result="+result.toString() ); + + if (first) { + finalResult = result; + first = false; + } else { + // Check if we need to Xor ... + // if the finalResult and the subsequent result are the same + // collection, then it contains the same recipients + // so we can short-circuit building the XOR and return an empty + // set + if (finalResult == result) { + // the XOR of the same collection is empty + finalResult.clear(); + // log("same collection - so clear"); + } else { + // the two results are different collections, so we XOR them + // Ensure that the finalResult does not contain recipients + // in the result collection + MailAddress recipient = null; + for (Iterator i = result.iterator(); i.hasNext();) { + recipient = (MailAddress) (i.next()); + if (!finalResult.contains(recipient)) { + finalResult.add(recipient); + } else { + finalResult.remove(recipient); + } + } + recipient = null; + // log("xor recipients into new finalResult="+finalResult); + } + // basically the finalResult gets replaced with a smaller result + // otherwise finalResult would have been equal to result (in all + // cases) + } + result = null; + } + return finalResult; + } + +} Index: server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/And.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/And.java (revision ) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/And.java (revision ) @@ -0,0 +1,106 @@ +package org.apache.james.transport.matchers; + +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +import org.apache.james.mailetcontainer.impl.matchers.GenericCompositeMatcher; +import org.apache.mailet.Mail; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import javax.mail.MessagingException; + +/** + * This matcher performs And conjunction between the two recipients + */ +public class And extends GenericCompositeMatcher { + + /** + * This is the And CompositeMatcher - consider it to be an intersection of + * the results. If any match returns an empty recipient result the matching + * is short-circuited. + * + * @return Collection of Recipient from the And composition results of the + * child Matchers. + */ + public Collection match(Mail mail) throws MessagingException { + Collection finalResult = null; + Matcher matcher = null; + boolean first = true; + for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { + matcher = (Matcher) (matcherIter.next()); + Collection result = matcher.match(mail); + + if (result == null) { + // short-circuit + // log("Matching with " + + // matcher.getMatcherConfig().getMatcherName() + + // " result.size()=0"); + return new ArrayList(0); + } + if (result.size() == 0) { + return result; + } + + // log("Matching with " + + // matcher.getMatcherConfig().getMatcherName() + + // " result.size()="+result.size()); + + if (first) { + finalResult = result; + first = false; + } else { + // Check if we need to And ... + // if the finalResult and the subsequent result are the same + // collection, then it contains the same recipients + // so we can short-circuit building the AND of the two + if (finalResult != result) { + // the two results are different collections, so we AND + // them + // Ensure that the finalResult only contains recipients + // in the result collection + Collection newResult = new ArrayList(); + MailAddress recipient = null; + for (Iterator i = finalResult.iterator(); i.hasNext();) { + recipient = (MailAddress) i.next(); + // log("recipient="+recipient.toString()); + if (result.contains(recipient)) { + newResult.add(recipient); + } + } + recipient = null; + // basically the finalResult gets replaced with a + // smaller result + // otherwise finalResult would have been equal to result + // (in all cases) + finalResult = newResult; + } + } + result = null; + } + matcher = null; + // log("answer is "+finalResult.toString()); + return finalResult; + } + +} Index: server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java (revision 1306725) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Xor.java (revision 1306725) @@ -1,93 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.mailetcontainer.impl.matchers; - -import java.util.Collection; -import java.util.Iterator; -import java.util.ArrayList; - -import org.apache.mailet.MailAddress; -import org.apache.mailet.Mail; -import javax.mail.MessagingException; -import org.apache.mailet.Matcher; - -public class Xor extends GenericCompositeMatcher { - - /** - * This is the Xor CompositeMatcher - consider it to be the inequality - * operator for recipients. If any recipients match other matcher results - * then the result does not include that recipient. - * - * @return Collection of Recipients from the Xor composition of the child - * matchers. - */ - public Collection match(Mail mail) throws MessagingException { - Collection finalResult = null; - Matcher matcher = null; - boolean first = true; - for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { - matcher = (Matcher) (matcherIter.next()); - Collection result = matcher.match(mail); - if (result == null) { - result = new ArrayList(0); - } - // log("Matching with " + - // matcher.getMatcherConfig().getMatcherName() + - // " result="+result.toString() ); - - if (first) { - finalResult = result; - first = false; - } else { - // Check if we need to Xor ... - // if the finalResult and the subsequent result are the same - // collection, then it contains the same recipients - // so we can short-circuit building the XOR and return an empty - // set - if (finalResult == result) { - // the XOR of the same collection is empty - finalResult.clear(); - // log("same collection - so clear"); - } else { - // the two results are different collections, so we XOR them - // Ensure that the finalResult does not contain recipients - // in the result collection - MailAddress recipient = null; - for (Iterator i = result.iterator(); i.hasNext();) { - recipient = (MailAddress) (i.next()); - if (!finalResult.contains(recipient)) { - finalResult.add(recipient); - } else { - finalResult.remove(recipient); - } - } - recipient = null; - // log("xor recipients into new finalResult="+finalResult); - } - // basically the finalResult gets replaced with a smaller result - // otherwise finalResult would have been equal to result (in all - // cases) - } - result = null; - } - return finalResult; - } - -} Index: server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/XorTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/XorTest.java (revision ) +++ server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/XorTest.java (revision ) @@ -0,0 +1,109 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.transport.matchers; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import javax.mail.MessagingException; +import javax.mail.internet.ParseException; + +import org.apache.james.mailetcontainer.impl.matchers.CompositeMatcher; +import org.apache.james.transport.matchers.All; +import org.apache.james.transport.matchers.RecipientIs; +import org.apache.james.transport.matchers.Xor; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailContext; +import org.apache.mailet.base.test.FakeMatcherConfig; +import static org.junit.Assert.*; +import org.junit.Test; + +public class XorTest { + + private FakeMailContext context; + private FakeMail mockedMail; + private CompositeMatcher matcher; + + private void setupMockedMail() throws ParseException { + mockedMail = new FakeMail(); + mockedMail.setRecipients(Arrays.asList(new MailAddress[]{ + new MailAddress("test@james.apache.org"), + new MailAddress("test2@james.apache.org")})); + + } + + /** + * Setup a composite Or matcher and test it + * @throws MessagingException + */ + private void setupMatcher() throws MessagingException { + context = new FakeMailContext(); + matcher = new Xor(); + FakeMatcherConfig mci = new FakeMatcherConfig("Xor", context); + matcher.init(mci); + } + + private void setupChild(String match) throws MessagingException { + Matcher child = null; + if (match.equals("All")) { + child = new All(); + } else { + child = new RecipientIs(); + } + FakeMatcherConfig sub = new FakeMatcherConfig(match, context); + child.init(sub); + matcher.add(child); + + } + + // test if all recipients was returned + @Test + public void testIntersectSame() throws MessagingException { + setupMockedMail(); + setupMatcher(); + setupChild("RecipientIsRegex=test@james.apache.org"); + setupChild("RecipientIsRegex=test@james.apache.org"); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(0, matchedRecipients.size()); + } + + @Test + public void testNoIntersect() throws MessagingException { + setupMockedMail(); + setupMatcher(); + setupChild("RecipientIsRegex=test@james.apache.org"); + setupChild("RecipientIsRegex=test2@james.apache.org"); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(2, matchedRecipients.size()); + + Iterator iterator = matchedRecipients.iterator(); + MailAddress address = (MailAddress) iterator.next(); + assertEquals(address, "test@james.apache.org"); + address = (MailAddress) iterator.next(); + assertEquals(address, "test2@james.apache.org"); + } +} Index: server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/AndTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/AndTest.java (revision ) +++ server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/AndTest.java (revision ) @@ -0,0 +1,105 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.transport.matchers; + +import org.apache.james.mailetcontainer.impl.matchers.CompositeMatcher; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailContext; +import org.apache.mailet.base.test.FakeMatcherConfig; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collection; + +import javax.mail.MessagingException; +import javax.mail.internet.ParseException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class AndTest { + + private FakeMailContext context; + private FakeMail mockedMail; + private CompositeMatcher matcher; + + private void setupMockedMail() throws ParseException { + mockedMail = new FakeMail(); + mockedMail.setRecipients(Arrays.asList(new MailAddress[]{ + new MailAddress("test@james.apache.org"), + new MailAddress("test2@james.apache.org")})); + + } + + /** + * Setup a composite Or matcher and test it + * @throws MessagingException + */ + private void setupMatcher() throws MessagingException { + context = new FakeMailContext(); + matcher = new And(); + FakeMatcherConfig mci = new FakeMatcherConfig("And", context); + matcher.init(mci); + } + + private void setupChild(String match) throws MessagingException { + Matcher child = null; + if (match.equals("All")) { + child = new All(); + } else { + child = new RecipientIs(); + } + FakeMatcherConfig sub = new FakeMatcherConfig(match, context); + child.init(sub); + matcher.add(child); + + } + + // test if all recipients was returned + @Test + public void testAndIntersectSameTwice() throws MessagingException { + setupMockedMail(); + setupMatcher(); + setupChild("RecipientIs=test@james.apache.org"); + setupChild("RecipientIs=test@james.apache.org"); + setupChild("All"); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(1, matchedRecipients.size()); + MailAddress address = (MailAddress) matchedRecipients.iterator().next(); + assertEquals(address, "test@james.apache.org"); + } + + @Test + public void testAndNoIntersect() throws MessagingException { + setupMockedMail(); + setupMatcher(); + setupChild("RecipientIs=test@james.apache.org"); + setupChild("RecipientIs=test2@james.apache.org"); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(0, matchedRecipients.size()); + } +} Index: server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/XorTest.java =================================================================== --- server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/XorTest.java (revision 1306725) +++ server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/XorTest.java (revision 1306725) @@ -1,106 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailetcontainer.impl.matchers; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import javax.mail.MessagingException; -import javax.mail.internet.ParseException; -import org.apache.james.transport.matchers.All; -import org.apache.james.transport.matchers.RecipientIs; -import org.apache.mailet.MailAddress; -import org.apache.mailet.Matcher; -import org.apache.mailet.base.test.FakeMail; -import org.apache.mailet.base.test.FakeMailContext; -import org.apache.mailet.base.test.FakeMatcherConfig; -import static org.junit.Assert.*; -import org.junit.Test; - -public class XorTest { - - private FakeMailContext context; - private FakeMail mockedMail; - private CompositeMatcher matcher; - - private void setupMockedMail() throws ParseException { - mockedMail = new FakeMail(); - mockedMail.setRecipients(Arrays.asList(new MailAddress[]{ - new MailAddress("test@james.apache.org"), - new MailAddress("test2@james.apache.org")})); - - } - - /** - * Setup a composite Or matcher and test it - * @throws MessagingException - */ - private void setupMatcher() throws MessagingException { - context = new FakeMailContext(); - matcher = new Xor(); - FakeMatcherConfig mci = new FakeMatcherConfig("Xor", context); - matcher.init(mci); - } - - private void setupChild(String match) throws MessagingException { - Matcher child = null; - if (match.equals("All")) { - child = new All(); - } else { - child = new RecipientIs(); - } - FakeMatcherConfig sub = new FakeMatcherConfig(match, context); - child.init(sub); - matcher.add(child); - - } - - // test if all recipients was returned - @Test - public void testIntersectSame() throws MessagingException { - setupMockedMail(); - setupMatcher(); - setupChild("RecipientIsRegex=test@james.apache.org"); - setupChild("RecipientIsRegex=test@james.apache.org"); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNotNull(matchedRecipients); - assertEquals(0, matchedRecipients.size()); - } - - @Test - public void testNoIntersect() throws MessagingException { - setupMockedMail(); - setupMatcher(); - setupChild("RecipientIsRegex=test@james.apache.org"); - setupChild("RecipientIsRegex=test2@james.apache.org"); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNotNull(matchedRecipients); - assertEquals(2, matchedRecipients.size()); - - Iterator iterator = matchedRecipients.iterator(); - MailAddress address = (MailAddress) iterator.next(); - assertEquals(address, "test@james.apache.org"); - address = (MailAddress) iterator.next(); - assertEquals(address, "test2@james.apache.org"); - } -} Index: server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/AndTest.java =================================================================== --- server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/AndTest.java (revision 1306725) +++ server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/AndTest.java (revision 1306725) @@ -1,102 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailetcontainer.impl.matchers; - -import java.util.Arrays; -import java.util.Collection; -import javax.mail.MessagingException; -import javax.mail.internet.ParseException; -import org.apache.james.transport.matchers.All; -import org.apache.james.transport.matchers.RecipientIs; -import org.apache.mailet.MailAddress; -import org.apache.mailet.Matcher; -import org.apache.mailet.base.test.FakeMail; -import org.apache.mailet.base.test.FakeMailContext; -import org.apache.mailet.base.test.FakeMatcherConfig; -import static org.junit.Assert.*; -import org.junit.Test; - -public class AndTest { - - private FakeMailContext context; - private FakeMail mockedMail; - private CompositeMatcher matcher; - - private void setupMockedMail() throws ParseException { - mockedMail = new FakeMail(); - mockedMail.setRecipients(Arrays.asList(new MailAddress[]{ - new MailAddress("test@james.apache.org"), - new MailAddress("test2@james.apache.org")})); - - } - - /** - * Setup a composite Or matcher and test it - * @throws MessagingException - */ - private void setupMatcher() throws MessagingException { - context = new FakeMailContext(); - matcher = new And(); - FakeMatcherConfig mci = new FakeMatcherConfig("And", context); - matcher.init(mci); - } - - private void setupChild(String match) throws MessagingException { - Matcher child = null; - if (match.equals("All")) { - child = new All(); - } else { - child = new RecipientIs(); - } - FakeMatcherConfig sub = new FakeMatcherConfig(match, context); - child.init(sub); - matcher.add(child); - - } - - // test if all recipients was returned - @Test - public void testAndIntersectSameTwice() throws MessagingException { - setupMockedMail(); - setupMatcher(); - setupChild("RecipientIs=test@james.apache.org"); - setupChild("RecipientIs=test@james.apache.org"); - setupChild("All"); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNotNull(matchedRecipients); - assertEquals(1, matchedRecipients.size()); - MailAddress address = (MailAddress) matchedRecipients.iterator().next(); - assertEquals(address, "test@james.apache.org"); - } - - @Test - public void testAndNoIntersect() throws MessagingException { - setupMockedMail(); - setupMatcher(); - setupChild("RecipientIs=test@james.apache.org"); - setupChild("RecipientIs=test2@james.apache.org"); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNotNull(matchedRecipients); - assertEquals(0, matchedRecipients.size()); - } -} Index: server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/OrTest.java =================================================================== --- server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/OrTest.java (revision 1306725) +++ server/mailetcontainer-camel/src/test/java/org/apache/james/mailetcontainer/impl/matchers/OrTest.java (revision 1306725) @@ -1,90 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.mailetcontainer.impl.matchers; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import javax.mail.MessagingException; -import javax.mail.internet.ParseException; -import org.apache.james.transport.matchers.RecipientIs; -import org.apache.mailet.MailAddress; -import org.apache.mailet.Matcher; -import org.apache.mailet.base.test.FakeMail; -import org.apache.mailet.base.test.FakeMailContext; -import org.apache.mailet.base.test.FakeMatcherConfig; -import static org.junit.Assert.*; -import org.junit.Test; - -public class OrTest { - - private FakeMail mockedMail; - - private CompositeMatcher matcher; - - private void setupMockedMail() throws ParseException { - mockedMail = new FakeMail(); - mockedMail.setRecipients(Arrays.asList(new MailAddress[] { - new MailAddress("test@james.apache.org"), - new MailAddress("test2@james.apache.org") })); - - } - - /** - * Setup a composite Or matcher and test it - * @throws MessagingException - */ - private void setupMatcher() throws MessagingException { - FakeMailContext context = new FakeMailContext(); - matcher = new Or(); - FakeMatcherConfig mci = new FakeMatcherConfig("Or",context); - matcher.init(mci); - Matcher child = null; - FakeMatcherConfig sub = null; - child = new RecipientIs(); - sub = new FakeMatcherConfig("RecipientIsRegex=test@james.apache.org",context); - child.init(sub); - matcher.add(child); - child = new RecipientIs(); - sub = new FakeMatcherConfig("RecipientIsRegex=test2@james.apache.org",context); - child.init(sub); - matcher.add(child); - } - - // test if all recipients was returned - @Test - public void testAllRecipientsReturned() throws MessagingException { - setupMockedMail(); - setupMatcher(); - - Collection matchedRecipients = matcher.match(mockedMail); - - assertNotNull(matchedRecipients); - assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size()); - - // Now ensure they match the actual recipients - Iterator iterator = matchedRecipients.iterator(); - MailAddress address = (MailAddress)iterator.next(); - assertEquals(address,"test@james.apache.org"); - address = (MailAddress)iterator.next(); - assertEquals(address,"test2@james.apache.org"); - } - -} Index: server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java (revision 1306725) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/And.java (revision 1306725) @@ -1,104 +0,0 @@ -package org.apache.james.mailetcontainer.impl.matchers; - -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -import java.util.Collection; -import java.util.Iterator; -import java.util.ArrayList; - -import org.apache.mailet.MailAddress; -import org.apache.mailet.Mail; -import javax.mail.MessagingException; -import org.apache.mailet.Matcher; - -/** - * This matcher performs And conjunction between the two recipients - */ -public class And extends GenericCompositeMatcher { - - /** - * This is the And CompositeMatcher - consider it to be an intersection of - * the results. If any match returns an empty recipient result the matching - * is short-circuited. - * - * @return Collection of Recipient from the And composition results of the - * child Matchers. - */ - public Collection match(Mail mail) throws MessagingException { - Collection finalResult = null; - Matcher matcher = null; - boolean first = true; - for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { - matcher = (Matcher) (matcherIter.next()); - Collection result = matcher.match(mail); - - if (result == null) { - // short-circuit - // log("Matching with " + - // matcher.getMatcherConfig().getMatcherName() + - // " result.size()=0"); - return new ArrayList(0); - } - if (result.size() == 0) { - return result; - } - - // log("Matching with " + - // matcher.getMatcherConfig().getMatcherName() + - // " result.size()="+result.size()); - - if (first) { - finalResult = result; - first = false; - } else { - // Check if we need to And ... - // if the finalResult and the subsequent result are the same - // collection, then it contains the same recipients - // so we can short-circuit building the AND of the two - if (finalResult != result) { - // the two results are different collections, so we AND - // them - // Ensure that the finalResult only contains recipients - // in the result collection - Collection newResult = new ArrayList(); - MailAddress recipient = null; - for (Iterator i = finalResult.iterator(); i.hasNext();) { - recipient = (MailAddress) i.next(); - // log("recipient="+recipient.toString()); - if (result.contains(recipient)) { - newResult.add(recipient); - } - } - recipient = null; - // basically the finalResult gets replaced with a - // smaller result - // otherwise finalResult would have been equal to result - // (in all cases) - finalResult = newResult; - } - } - result = null; - } - matcher = null; - // log("answer is "+finalResult.toString()); - return finalResult; - } - -} Index: server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/OrTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/OrTest.java (revision ) +++ server/mailetcontainer-camel/src/test/java/org/apache/james/transport/matchers/OrTest.java (revision ) @@ -0,0 +1,93 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.matchers; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import javax.mail.MessagingException; +import javax.mail.internet.ParseException; + +import org.apache.james.mailetcontainer.impl.matchers.CompositeMatcher; +import org.apache.james.transport.matchers.Or; +import org.apache.james.transport.matchers.RecipientIs; +import org.apache.mailet.MailAddress; +import org.apache.mailet.Matcher; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailContext; +import org.apache.mailet.base.test.FakeMatcherConfig; +import static org.junit.Assert.*; +import org.junit.Test; + +public class OrTest { + + private FakeMail mockedMail; + + private CompositeMatcher matcher; + + private void setupMockedMail() throws ParseException { + mockedMail = new FakeMail(); + mockedMail.setRecipients(Arrays.asList(new MailAddress[] { + new MailAddress("test@james.apache.org"), + new MailAddress("test2@james.apache.org") })); + + } + + /** + * Setup a composite Or matcher and test it + * @throws MessagingException + */ + private void setupMatcher() throws MessagingException { + FakeMailContext context = new FakeMailContext(); + matcher = new Or(); + FakeMatcherConfig mci = new FakeMatcherConfig("Or",context); + matcher.init(mci); + Matcher child = null; + FakeMatcherConfig sub = null; + child = new RecipientIs(); + sub = new FakeMatcherConfig("RecipientIsRegex=test@james.apache.org",context); + child.init(sub); + matcher.add(child); + child = new RecipientIs(); + sub = new FakeMatcherConfig("RecipientIsRegex=test2@james.apache.org",context); + child.init(sub); + matcher.add(child); + } + + // test if all recipients was returned + @Test + public void testAllRecipientsReturned() throws MessagingException { + setupMockedMail(); + setupMatcher(); + + Collection matchedRecipients = matcher.match(mockedMail); + + assertNotNull(matchedRecipients); + assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size()); + + // Now ensure they match the actual recipients + Iterator iterator = matchedRecipients.iterator(); + MailAddress address = (MailAddress)iterator.next(); + assertEquals(address,"test@james.apache.org"); + address = (MailAddress)iterator.next(); + assertEquals(address,"test2@james.apache.org"); + } + +} Index: server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Or.java =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Or.java (revision 1306725) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Or.java (revision 1306725) @@ -1,105 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.mailetcontainer.impl.matchers; - -import org.apache.mailet.MailAddress; -import org.apache.mailet.Mail; -import org.apache.mailet.Matcher; - -import java.util.Collection; -import java.util.Iterator; -import java.util.ArrayList; -import javax.mail.MessagingException; - -public class Or extends GenericCompositeMatcher { - - /** - * This is the Or CompositeMatcher - consider it to be a union of the - * results. If any match results in a full set of recipients the matching is - * short-circuited. - * - * @return Collection of Recipient from the Or composition results of the - * child matchers. - */ - public Collection match(Mail mail) throws MessagingException { - Collection finalResult = null; - Matcher matcher = null; - boolean first = true; - - // the size of the complete set of recipients - int size = mail.getRecipients().size(); - - // Loop through until the finalResult is full or all the child matchers - // have been executed - for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { - matcher = (Matcher) matcherIter.next(); - // log("Matching with " - // + matcher - // .getMatcherConfig() - // .getMatcherName() - // ); - Collection result = matcher.match(mail); - if (first) { - if (result == null) { - result = new ArrayList(0); - } - finalResult = result; - first = false; - } else { - // Check if we need to Or ... - // if the finalResult and the subsequent result are the same - // collection, then it contains the same recipients - // so we can short-circuit building the OR of the two - if (finalResult != result) { - if (result != null) { - if (finalResult == null) { - finalResult = result; - } else { - // the two results are different collections, so we - // must OR them - // Ensure that the finalResult only contains one - // copy of the recipients in the result collection - MailAddress recipient = null; - for (Iterator i = result.iterator(); i.hasNext();) { - recipient = (MailAddress) i.next(); - if (!finalResult.contains(recipient)) { - System.out.println(recipient); - finalResult.add(recipient); - } - } - recipient = null; - } - } - } - } - if (finalResult.size() == size) { - // we have a complete set of recipients, no need to OR in - // anymore - // i.e. short-circuit the Or - break; - } - result = null; - matcher = null; - } - // log("OrMatch: end."); - return finalResult; - } - -} Index: server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Not.java =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Not.java (revision 1306725) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/mailetcontainer/impl/matchers/Not.java (revision 1306725) @@ -1,60 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ - -package org.apache.james.mailetcontainer.impl.matchers; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; - -import org.apache.mailet.Matcher; -import org.apache.mailet.Mail; - -import javax.mail.MessagingException; - -public class Not extends GenericCompositeMatcher { - - /** - * This is the Not CompositeMatcher - consider what wasn't in the result set - * of each child matcher. Of course it is easier to understand if it only - * includes one matcher in the composition, the normal recommended use. @See - * CompositeMatcher interface. - * - * @return Collectiom of Recipient from the Negated composition of the child - * Matcher(s). - */ - public Collection match(Mail mail) throws MessagingException { - Collection finalResult = mail.getRecipients(); - Matcher matcher = null; - for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { - matcher = (Matcher) (matcherIter.next()); - // log("Matching with " + - // matcher.getMatcherConfig().getMatcherName()); - Collection result = matcher.match(mail); - if (result == finalResult) { - // Not is an empty list - finalResult = null; - } else if (result != null) { - finalResult = new ArrayList(finalResult); - finalResult.removeAll(result); - } - } - return finalResult; - } -} Index: server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Not.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Not.java (revision ) +++ server/mailetcontainer-camel/src/main/java/org/apache/james/transport/matchers/Not.java (revision ) @@ -0,0 +1,61 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.matchers; + +import org.apache.james.mailetcontainer.impl.matchers.GenericCompositeMatcher; +import org.apache.mailet.Mail; +import org.apache.mailet.Matcher; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +import javax.mail.MessagingException; + +public class Not extends GenericCompositeMatcher { + + /** + * This is the Not CompositeMatcher - consider what wasn't in the result set + * of each child matcher. Of course it is easier to understand if it only + * includes one matcher in the composition, the normal recommended use. @See + * CompositeMatcher interface. + * + * @return Collectiom of Recipient from the Negated composition of the child + * Matcher(s). + */ + public Collection match(Mail mail) throws MessagingException { + Collection finalResult = mail.getRecipients(); + Matcher matcher = null; + for (Iterator matcherIter = iterator(); matcherIter.hasNext();) { + matcher = (Matcher) (matcherIter.next()); + // log("Matching with " + + // matcher.getMatcherConfig().getMatcherName()); + Collection result = matcher.match(mail); + if (result == finalResult) { + // Not is an empty list + finalResult = null; + } else if (result != null) { + finalResult = new ArrayList(finalResult); + finalResult.removeAll(result); + } + } + return finalResult; + } +}