james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject james-project git commit: JAMES-2440 Don't fail when the filename is unparsable
Date Wed, 27 Jun 2018 02:30:09 GMT
Repository: james-project
Updated Branches:
  refs/heads/master b10fa18aa -> c9f8c9c72


JAMES-2440 Don't fail when the filename is unparsable


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c9f8c9c7
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c9f8c9c7
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c9f8c9c7

Branch: refs/heads/master
Commit: c9f8c9c72c212dd9332e5cf4f80974c805de6d7d
Parents: b10fa18
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Tue Jun 26 15:30:54 2018 +0200
Committer: benwa <btellier@linagora.com>
Committed: Wed Jun 27 09:28:24 2018 +0700

----------------------------------------------------------------------
 .../james/transport/mailets/StripAttachment.java    | 16 ++++++++++++----
 .../transport/mailets/StripAttachmentTest.java      | 13 +++++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/c9f8c9c7/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 2a82413..c77f935 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
@@ -365,11 +365,19 @@ public class StripAttachment extends GenericMailet {
         }
     }
 
-    private String getFilename(BodyPart bodyPart) throws UnsupportedEncodingException, MessagingException
{
-        String fileName = bodyPart.getFileName();
-        if (fileName != null) {
-            return renameWithConfigurationPattern(decodeFilename(fileName));
+    @VisibleForTesting String getFilename(BodyPart bodyPart) {
+        try {
+            String fileName = bodyPart.getFileName();
+            if (fileName != null) {
+                return renameWithConfigurationPattern(decodeFilename(fileName));
+            }
+        } catch (Exception e) {
+            LOGGER.warn("Unparsable filename, using a random filename instead.", e);
         }
+        return randomFilename();
+    }
+
+    private String randomFilename() {
         return UUID.randomUUID().toString();
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/c9f8c9c7/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 202b15a..aefc067 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
@@ -33,6 +33,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 
+import javax.mail.BodyPart;
 import javax.mail.MessagingException;
 import javax.mail.Part;
 import javax.mail.internet.MimeBodyPart;
@@ -920,4 +921,16 @@ class StripAttachmentTest {
         String prefix = OutputFileName.prependedPrefix(expectedPrefix);
         assertThat(prefix).isEqualTo(expectedPrefix);
     }
+
+    @Test
+    void getFilenameShouldReturnRandomFilenameWhenExceptionOccured() throws Exception {
+        BodyPart bodyPart = mock(BodyPart.class);
+        when(bodyPart.getFileName())
+            .thenThrow(new MessagingException());
+
+        StripAttachment mailet = new StripAttachment();
+        String filename = mailet.getFilename(bodyPart);
+
+        assertThat(filename).isNotNull();
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message