Skip to content

Commit

Permalink
MAILET-142 Add mimeType matching filter in StripAttachment
Browse files Browse the repository at this point in the history
  • Loading branch information
aduprat authored and chibenwa committed Dec 22, 2016
1 parent 45bde53 commit 0d701b0
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 22 deletions.
Expand Up @@ -66,12 +66,13 @@
* <mailet match="All" class="StripAttachment" >
* <pattern >.*\.xls </pattern> <!-- The regular expression that must be matched -- >
* <!-- notpattern >.*\.xls </notpattern--> <!-- The regular expression that must be matched -- >
* <mimeType>text/calendar</mimeType> <!-- The matching mimeType -- >
* <directory >c:\temp\james_attach </directory> <!-- The directory to save to -- >
* <remove >all </remove> <!-- either "no", "matched", "all" -- >
* <!-- attribute>my.attribute.name</attribute -->
* </mailet >
*
* At least one of pattern and notpattern is required.
* At least one of pattern, notpattern and mimeType is required.
* </pre>
*
* </p>
Expand All @@ -81,6 +82,7 @@ public class StripAttachment extends GenericMailet {
private static final String MULTIPART_MIME_TYPE = "multipart/*";
public static final String PATTERN_PARAMETER_NAME = "pattern";
public static final String NOTPATTERN_PARAMETER_NAME = "notpattern";
public static final String MIMETYPE_PARAMETER_NAME = "mimeType";
public static final String ATTRIBUTE_PARAMETER_NAME = "attribute";
public static final String DIRECTORY_PARAMETER_NAME = "directory";
// Either "no", "matched", "all"
Expand All @@ -101,6 +103,7 @@ public class StripAttachment extends GenericMailet {
private String attributeName;
private Pattern regExPattern;
private Pattern notRegExPattern;
private String mimeType;
private boolean decodeFilename;

private List<ReplacingPattern> filenameReplacingPatterns;
Expand All @@ -115,8 +118,10 @@ public class StripAttachment extends GenericMailet {
public void init() throws MailetException {
regExPattern = regExFromParameter(PATTERN_PARAMETER_NAME);
notRegExPattern = regExFromParameter(NOTPATTERN_PARAMETER_NAME);
if (regExPattern == null && notRegExPattern == null) {
throw new MailetException("At least one of '" + PATTERN_PARAMETER_NAME + "' or '" + NOTPATTERN_PARAMETER_NAME + "' parameter should be provided.");
mimeType = getInitParameter(MIMETYPE_PARAMETER_NAME);
if (regExPattern == null && notRegExPattern == null && Strings.isNullOrEmpty(mimeType)) {
throw new MailetException("At least one of '" + PATTERN_PARAMETER_NAME + "', '" + NOTPATTERN_PARAMETER_NAME + "' or '" + MIMETYPE_PARAMETER_NAME +
"' parameter should be provided.");
}

directoryName = getInitParameter(DIRECTORY_PARAMETER_NAME);
Expand Down Expand Up @@ -308,7 +313,7 @@ private boolean shouldBeRemoved(BodyPart bodyPart, Mail mail) throws MessagingEx
}

boolean shouldRemove = removeAttachments.equals(REMOVE_ALL);
if (fileNameMatches(fileName)) {
if (isMatching(bodyPart, fileName)) {
storeBodyPartAsFile(bodyPart, mail, fileName);
storeBodyPartAsMailAttribute(bodyPart, mail, fileName);
if (removeAttachments.equals(REMOVE_MATCHED)) {
Expand All @@ -319,6 +324,10 @@ private boolean shouldBeRemoved(BodyPart bodyPart, Mail mail) throws MessagingEx
return shouldRemove;
}

private boolean isMatching(BodyPart bodyPart, String fileName) throws MessagingException {
return fileNameMatches(fileName) || bodyPart.isMimeType(mimeType);
}

private void storeBodyPartAsFile(BodyPart bodyPart, Mail mail, String fileName) throws Exception {
if (directoryName != null) {
Optional<String> filename = saveAttachmentToFile(bodyPart, Optional.of(fileName));
Expand Down
Expand Up @@ -61,6 +61,11 @@
public class StripAttachmentTest {

private static final String EXPECTED_ATTACHMENT_CONTENT = "\u0023\u00A4\u00E3\u00E0\u00E9";
private static final Optional<String> ABSENT_MIME_TYPE = Optional.<String> absent();
private static final String MIME_HEADER_DEFAULT = "Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n";
private static final String MIME_HEADER_TEXT_CALENDAR = "Content-Transfer-Encoding: 8bit\r\nContent-Type: text/calendar; charset=utf-8\r\n\r\n";
private static final String MIME_HEADER_TEXT_HTML = "Content-Transfer-Encoding: 8bit\r\nContent-Type: text/html; charset=utf-8\r\n\r\n";

@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
Expand Down Expand Up @@ -119,8 +124,8 @@ public void serviceShouldSaveAttachmentInAFolderWhenPatternMatch() throws Messag
textPart.setText("simple text");
multiPart.addBodyPart(textPart);
String expectedAttachmentContent = EXPECTED_ATTACHMENT_CONTENT;
multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "10.tmp"));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip"));
multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "10.tmp", MIME_HEADER_DEFAULT));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", MIME_HEADER_DEFAULT));

message.setSubject("test");
message.setContent(multiPart);
Expand All @@ -142,22 +147,56 @@ public void serviceShouldSaveAttachmentInAFolderWhenPatternMatch() throws Messag
assertThat(new File(folderPath + attachmentFilename)).hasContent(expectedAttachmentContent);
}

@Test
public void serviceShouldRemoveWhenMimeTypeMatches() throws MessagingException, IOException {
FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
.setProperty("mimeType", "text/calendar")
.setProperty("remove", "matched")
.build();
Mailet mailet = new StripAttachment();
mailet.init(mci);

MimeMultipart multiPart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("simple text");
multiPart.addBodyPart(textPart);
String expectedFileName = "10.ical";
multiPart.addBodyPart(createAttachmentBodyPart("content", expectedFileName, MIME_HEADER_TEXT_CALENDAR));
multiPart.addBodyPart(createAttachmentBodyPart("other content", "11.ical", MIME_HEADER_DEFAULT));
multiPart.addBodyPart(createAttachmentBodyPart("<p>html</p>", "index.html", MIME_HEADER_TEXT_HTML));

MimeMessage message = mimeMessage();
message.setSubject("test");
message.setContent(multiPart);
message.saveChanges();

Mail mail = FakeMail.builder()
.mimeMessage(message)
.build();

mailet.service(mail);

@SuppressWarnings("unchecked")
List<String> removedAttachments = (List<String>) mail.getAttribute(StripAttachment.REMOVED_ATTACHMENTS_ATTRIBUTE_KEY);
assertThat(removedAttachments).containsOnly(expectedFileName);
}

private MimeMessage mimeMessage() {
return new MimeMessage(Session
.getDefaultInstance(new Properties()));
}

private MimeBodyPart createAttachmentBodyPart(String body, String fileName) throws MessagingException, UnsupportedEncodingException {
MimeBodyPart part = createBodyPart(body);
private MimeBodyPart createAttachmentBodyPart(String body, String fileName, String mimeHeaders) throws MessagingException, UnsupportedEncodingException {
MimeBodyPart part = createBodyPart(body, mimeHeaders);
part.setDisposition("attachment");
part.setFileName(fileName);
return part;
}

private MimeBodyPart createBodyPart(String body) throws MessagingException, UnsupportedEncodingException {
return new MimeBodyPart(new ByteArrayInputStream(
("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n"
+ body).getBytes("UTF-8")));
private MimeBodyPart createBodyPart(String body, String mimeHeaders) throws MessagingException, UnsupportedEncodingException {
byte[] content = (mimeHeaders + body).getBytes("UTF-8");
return new MimeBodyPart(new ByteArrayInputStream(content));
}

@Test
Expand All @@ -179,8 +218,8 @@ public void serviceShouldSaveAttachmentInAFolderWhenNotPatternDoesntMatch() thro
part.setText("simple text");
multiPart.addBodyPart(part);
String expectedAttachmentContent = EXPECTED_ATTACHMENT_CONTENT;
multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "temp.tmp"));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "winmail.dat"));
multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "temp.tmp", MIME_HEADER_DEFAULT));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "winmail.dat", MIME_HEADER_DEFAULT));

message.setSubject("test");
message.setContent(multiPart);
Expand Down Expand Up @@ -213,8 +252,8 @@ public void serviceShouldDecodeFilenameAndSaveAttachmentInAFolderWhenPatternMatc
part.setText("simple text");
multiPart.addBodyPart(part);
String expectedAttachmentContent = EXPECTED_ATTACHMENT_CONTENT;
multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?="));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip"));
multiPart.addBodyPart(createAttachmentBodyPart(expectedAttachmentContent, "=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?=", MIME_HEADER_DEFAULT));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", MIME_HEADER_DEFAULT));

message.setSubject("test");
message.setContent(multiPart);
Expand Down Expand Up @@ -259,8 +298,8 @@ public void serviceShouldSaveFilenameAttachmentAndFileContentInCustomAttribute()
part.setText("simple text");
multiPart.addBodyPart(part);
String expectedKey = "10.tmp";
multiPart.addBodyPart(createAttachmentBodyPart(EXPECTED_ATTACHMENT_CONTENT, expectedKey));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip"));
multiPart.addBodyPart(createAttachmentBodyPart(EXPECTED_ATTACHMENT_CONTENT, expectedKey, MIME_HEADER_DEFAULT));
multiPart.addBodyPart(createAttachmentBodyPart("\u0014\u00A3\u00E1\u00E2\u00E4", "temp.zip", MIME_HEADER_DEFAULT));

message.setSubject("test");
message.setContent(multiPart);
Expand All @@ -282,15 +321,65 @@ public void serviceShouldSaveFilenameAttachmentAndFileContentInCustomAttribute()
}

@Test
public void initShouldThrowWhenPatternAndNotPatternAreNull() throws MessagingException {
public void initShouldThrowWhenPatternAndNotPatternAndMimeTypeAreNull() throws MessagingException {
Mailet mailet = new StripAttachment();

FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
.build();

expectedException.expect(MailetException.class);
expectedException.expectMessage("At least one of 'pattern', 'notpattern' or 'mimeType' parameter should be provided.");
mailet.init(mci);
}

@Test
public void initShouldThrowWhenMimeTypeIsEmpty() throws MessagingException {
Mailet mailet = new StripAttachment();

FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
.setProperty("mimeType", "")
.build();

expectedException.expect(MailetException.class);
expectedException.expectMessage("At least one of 'pattern' or 'notpattern' parameter should be provided.");
expectedException.expectMessage("At least one of 'pattern', 'notpattern' or 'mimeType' parameter should be provided.");
mailet.init(mci);
}

@Test
public void initShouldWorkWhenPatternIsDefinedAndValid() throws MessagingException {
Mailet mailet = new StripAttachment();

FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
.setProperty("pattern", ".*\\.tmp")
.build();

mailet.init(mci);
}

@Test
public void initShouldWorkWhenNotPatternIsDefinedAndValid() throws MessagingException {
Mailet mailet = new StripAttachment();

FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
.setProperty("notpattern", ".*\\.tmp")
.build();

mailet.init(mci);
}

@Test
public void initShouldWorkWhenMimeTypeIsDefined() throws MessagingException {
Mailet mailet = new StripAttachment();

FakeMailetConfig mci = FakeMailetConfig.builder()
.mailetName("Test")
.setProperty("mimeType", "text/calendar")
.build();

mailet.init(mci);
}

Expand Down Expand Up @@ -671,7 +760,7 @@ public void saveAttachmentShouldUsePartNameIfNoFilename() throws Exception {
Part part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));
part.setFileName("example.tmp");
//When
Optional<String> maybeFilename = mailet.saveAttachmentToFile(part, Optional.<String> absent());
Optional<String> maybeFilename = mailet.saveAttachmentToFile(part, ABSENT_MIME_TYPE);
//Then
assertThat(maybeFilename).isPresent();
String filename = maybeFilename.get();
Expand All @@ -689,7 +778,7 @@ public void saveAttachmentShouldReturnAbsentWhenNoFilenameAtAll() throws Excepti
mailet.init(mci);
Part part = new MimeBodyPart(new ByteArrayInputStream(new byte[0]));

Optional<String> maybeFilename = mailet.saveAttachmentToFile(part, Optional.<String> absent());
Optional<String> maybeFilename = mailet.saveAttachmentToFile(part, ABSENT_MIME_TYPE);
assertThat(maybeFilename).isAbsent();
}

Expand Down

0 comments on commit 0d701b0

Please sign in to comment.