JAMES-1977 Add a matcher for rejecting emails with too much lines
With many thanks to:
Nguyen Thi Mai and Le Thi Huong Lai for their work on Quotas and In memory
subscriptions
Tran Thi & My Linh for their work on TooMuchLines
txc1996 & Van Thanh For improving readability of SMTPAuthIsSuccessfulTest
thienan090196 & Tran Thi & My Linh for their tests on RecipientIsLocalTest
>From Passerelle Numeriques VietNam
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/bde101f7
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/bde101f7
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/bde101f7
Branch: refs/heads/master
Commit: bde101f7a5862ca105941fe4007d138fe92d18c3
Parents: d52c598
Author: benwa <[email protected]>
Authored: Tue Mar 21 10:23:58 2017 +0700
Committer: benwa <[email protected]>
Committed: Wed Mar 29 08:00:16 2017 +0700
----------------------------------------------------------------------
.../james/transport/matchers/TooManyLines.java | 72 +++++++++
.../transport/matchers/TooManyLinesTest.java | 147 +++++++++++++++++++
2 files changed, 219 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/bde101f7/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
----------------------------------------------------------------------
diff --git
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
new file mode 100644
index 0000000..ef2743f
--- /dev/null
+++
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
@@ -0,0 +1,72 @@
+/****************************************************************
+ * 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 javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMatcher;
+
+import com.google.common.collect.ImmutableList;
+
+public class TooManyLines extends GenericMatcher {
+
+ private int maximumLineCount;
+
+ @Override
+ public void init() throws MessagingException {
+ String condition = getCondition();
+
+ maximumLineCount = parseCondition(condition);
+
+ if (maximumLineCount < 1) {
+ throw new MessagingException("Condition should be strictly
positive");
+ }
+ }
+
+ private int parseCondition(String condition) throws MessagingException {
+ if (condition == null) {
+ throw new MessagingException("Missing condition");
+ }
+
+ try {
+ return Integer.valueOf(condition);
+ } catch (NumberFormatException e) {
+ throw new MessagingException("Invalid formating. Condition is
expected to be an integer");
+ }
+ }
+
+ @Override
+ public Collection<MailAddress> match(Mail mail) throws MessagingException {
+ if (mail.getMessage() == null) {
+ return ImmutableList.of();
+ }
+
+ if (mail.getMessage().getLineCount() > maximumLineCount) {
+ return ImmutableList.copyOf(mail.getRecipients());
+ }
+
+ return ImmutableList.of();
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/james-project/blob/bde101f7/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyLinesTest.java
----------------------------------------------------------------------
diff --git
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyLinesTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyLinesTest.java
new file mode 100644
index 0000000..575d396
--- /dev/null
+++
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/TooManyLinesTest.java
@@ -0,0 +1,147 @@
+/****************************************************************
+ * 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 static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class TooManyLinesTest {
+
+ private TooManyLines testee;
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Before
+ public void setUp() {
+ testee = new TooManyLines();
+ }
+
+ @Test
+ public void initShouldThrowOnAbsentCondition() throws Exception {
+ expectedException.expect(MessagingException.class);
+
+ testee.init(FakeMatcherConfig.builder().matcherName("name").build());
+ }
+
+ @Test
+ public void initShouldThrowOnInvalidCondition() throws Exception {
+ expectedException.expect(MessagingException.class);
+
+ testee.init(
+ FakeMatcherConfig.builder()
+ .condition("a")
+ .matcherName("name")
+ .build());
+ }
+
+ @Test
+ public void initShouldThrowOnEmptyCondition() throws Exception {
+ expectedException.expect(MessagingException.class);
+
+ testee.init(FakeMatcherConfig.builder()
+ .condition("")
+ .matcherName("name")
+ .build());
+ }
+
+ @Test
+ public void initShouldThrowOnZeroCondition() throws Exception {
+ expectedException.expect(MessagingException.class);
+
+ testee.init(FakeMatcherConfig.builder()
+ .condition("0")
+ .matcherName("name")
+ .build());
+ }
+
+ @Test
+ public void initShouldThrowOnNegativeCondition() throws MessagingException
{
+ expectedException.expect(MessagingException.class);
+
+ testee.init(FakeMatcherConfig.builder()
+ .condition("-10")
+ .matcherName("name")
+ .build());
+ }
+
+ @Test
+ public void
matchShouldReturnNoRecipientWhenMailHaveNoMimeMessageAndConditionIs100() throws
Exception {
+ testee.init(FakeMatcherConfig.builder()
+ .condition("100")
+ .matcherName("name")
+ .build());
+
+ Collection<MailAddress> result =
testee.match(FakeMail.builder().build());
+
+ assertThat(result).isEmpty();
+
+ }
+
+ @Test
+ public void matchShouldAcceptMailsUnderLimit() throws Exception {
+ testee.init(FakeMatcherConfig.builder()
+ .condition("100")
+ .matcherName("name")
+ .build());
+
+ FakeMail fakeMail = FakeMail.builder()
+ .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+ .setMultipartWithBodyParts(MimeMessageBuilder.bodyPartBuilder()
+ .data("content")
+ .build())
+ .build())
+ .build();
+
+ Collection<MailAddress> result = testee.match(fakeMail);
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ public void matchShouldRejectMailsOverLimit() throws Exception {
+
testee.init(FakeMatcherConfig.builder().condition("10").matcherName("name").build());
+
+ FakeMail fakeMail = FakeMail.builder()
+ .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+ .setMultipartWithBodyParts(MimeMessageBuilder.bodyPartBuilder()
+ .data("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11")
+ .build())
+ .build())
+ .build();
+
+ Collection<MailAddress> result = testee.match(fakeMail);
+
+ assertThat(result).containsAll(fakeMail.getRecipients());
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]