Repository: james-project Updated Branches: refs/heads/master 45bde53be -> 0d701b075
MAILET-142 Add mimeType matching filter in StripAttachment Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0d701b07 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0d701b07 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0d701b07 Branch: refs/heads/master Commit: 0d701b07580933660f6d4bcd8443a27d562226f7 Parents: 45bde53 Author: Antoine Duprat <[email protected]> Authored: Tue Dec 20 14:17:12 2016 +0100 Committer: Benoit Tellier <[email protected]> Committed: Thu Dec 22 08:06:09 2016 +0700 ---------------------------------------------------------------------- .../transport/mailets/StripAttachment.java | 17 ++- .../transport/mailets/StripAttachmentTest.java | 125 ++++++++++++++++--- 2 files changed, 120 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/0d701b07/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java index e9c8592..20ff946 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/StripAttachment.java @@ -66,12 +66,13 @@ import com.google.common.collect.ImmutableList; * <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> @@ -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" @@ -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; @@ -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); @@ -308,7 +313,7 @@ public class StripAttachment extends GenericMailet { } 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)) { @@ -319,6 +324,10 @@ public class StripAttachment extends GenericMailet { 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)); http://git-wip-us.apache.org/repos/asf/james-project/blob/0d701b07/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java index add3c55..63a211e 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java @@ -61,6 +61,11 @@ import com.google.common.base.Optional; 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 @@ -119,8 +124,8 @@ public class StripAttachmentTest { 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); @@ -142,22 +147,56 @@ public class StripAttachmentTest { 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 @@ -179,8 +218,8 @@ public class StripAttachmentTest { 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); @@ -213,8 +252,8 @@ public class StripAttachmentTest { 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); @@ -259,8 +298,8 @@ public class StripAttachmentTest { 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); @@ -282,15 +321,65 @@ public class StripAttachmentTest { } @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); } @@ -671,7 +760,7 @@ public class StripAttachmentTest { 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(); @@ -689,7 +778,7 @@ public class StripAttachmentTest { 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(); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
