Skip to content

Commit

Permalink
MAILET-156 AutomaticallySentMailDetectorImpl should not rely on Mime4…
Browse files Browse the repository at this point in the history
…J default configuration
  • Loading branch information
chibenwa committed Jun 1, 2017
1 parent 49fbba7 commit f19648a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Expand Up @@ -36,6 +36,13 @@

public class AutomaticallySentMailDetectorImpl implements AutomaticallySentMailDetector {

private static final MimeConfig MIME_ENTITY_CONFIG = MimeConfig.custom()
.setMaxContentLen(-1)
.setMaxHeaderCount(-1)
.setMaxHeaderLen(-1)
.setMaxLineLen(-1)
.build();

private static final String[] MAILING_LIST_HEADERS = new String[] {
"List-Help",
"List-Subscribe",
Expand Down Expand Up @@ -91,8 +98,7 @@ public boolean isAutoSubmitted(Mail mail) throws MessagingException {

public boolean isMdnSentAutomatically(Mail mail) throws MessagingException {
ResultCollector resultCollector = new ResultCollector(false);
MimeConfig config = MimeConfig.custom().setMaxLineLen(-1).setMaxHeaderLen(-1).build();
MimeStreamParser parser = new MimeStreamParser(config);
MimeStreamParser parser = new MimeStreamParser(MIME_ENTITY_CONFIG);
parser.setContentHandler(createMdnContentHandler(resultCollector));
try {
parser.parse(mail.getMessage().getInputStream());
Expand Down
Expand Up @@ -22,6 +22,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.Properties;

import javax.activation.DataHandler;
Expand All @@ -33,6 +34,7 @@

import org.apache.mailet.MailAddress;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.MimeMessageBuilder;
import org.junit.Test;

public class AutomaticallySentMailDetectorImplTest {
Expand Down Expand Up @@ -286,4 +288,24 @@ public void isMdnSentAutomaticallyShouldManageItsMimeType() throws Exception {
assertThat(new AutomaticallySentMailDetectorImpl().isMdnSentAutomatically(fakeMail)).isFalse();
}

@Test
public void isMdnSentAutomaticallyShouldNotThrowOnBodyPartsWithManyLines() throws Exception {
int mime4jDefaultMaxHeaderCount = 1000;
int headerCount = mime4jDefaultMaxHeaderCount + 10;
MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
.addHeaders()
.setMultipartWithBodyParts(MimeMessageBuilder.bodyPartBuilder()
.addHeaders(Collections.nCopies(headerCount, new MimeMessageBuilder.Header("name", "value")))
.data("The body part have 1010 headers, which overpass MIME4J default limits")
.build())
.build();

FakeMail fakeMail = FakeMail.builder()
.sender(MailAddressFixture.ANY_AT_JAMES)
.mimeMessage(message)
.build();

assertThat(new AutomaticallySentMailDetectorImpl().isMdnSentAutomatically(fakeMail)).isFalse();
}

}
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

Expand Down Expand Up @@ -132,7 +133,11 @@ public BodyPartBuilder addHeader(String name, String value) {
}

public BodyPartBuilder addHeaders(Header... headers) {
this.headers.addAll(Arrays.asList(headers));
return addHeaders(Arrays.asList(headers));
}

public BodyPartBuilder addHeaders(Collection<Header> headers) {
this.headers.addAll(headers);
return this;
}

Expand Down Expand Up @@ -317,7 +322,11 @@ public MimeMessageBuilder addHeader(String name, String value) {
}

public MimeMessageBuilder addHeaders(Header... headers) {
this.headers.addAll(Arrays.asList(headers));
return addHeaders(Arrays.asList(headers));
}

public MimeMessageBuilder addHeaders(Collection<Header> headers) {
this.headers.addAll(headers);
return this;
}

Expand Down

0 comments on commit f19648a

Please sign in to comment.