This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 28263ef49a46eabd77016ee23cf70657437e63f0
Author: Benoit TELLIER <[email protected]>
AuthorDate: Thu Nov 28 15:36:16 2024 +0100

    [ENHANCEMENT] Extract MimeWalkConfiguration from AttachmentFileNameIs
---
 .../transport/matchers/AttachmentFileNameIs.java   | 103 +++++++++++----------
 .../matchers/AttachmentFileNameIsTest.java         |  68 +++++---------
 2 files changed, 74 insertions(+), 97 deletions(-)

diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/AttachmentFileNameIs.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/AttachmentFileNameIs.java
index 72256f1a2d..8abbc62233 100755
--- 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/AttachmentFileNameIs.java
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/AttachmentFileNameIs.java
@@ -61,6 +61,51 @@ import com.google.common.annotations.VisibleForTesting;
  */
 public class AttachmentFileNameIs extends GenericMatcher {
     private static final Logger LOGGER = 
LoggerFactory.getLogger(AttachmentFileNameIs.class);
+
+    record MimeWalkConfiguration(Mask[] masks, boolean isDebug, boolean 
unzipIsRequested) {
+        public static MimeWalkConfiguration DEFAULT = new 
MimeWalkConfiguration(null, false, false);
+
+        public static MimeWalkConfiguration parse(String condition) {
+            StringTokenizer st = new StringTokenizer(condition, ", ", false);
+            ArrayList<Mask> theMasks = new ArrayList<>(20);
+            boolean unzipIsRequested = false;
+            boolean isDebug = false;
+            while (st.hasMoreTokens()) {
+                String fileName = st.nextToken();
+
+                // check possible parameters at the beginning of the condition
+                if (theMasks.isEmpty() && 
fileName.equalsIgnoreCase(UNZIP_REQUEST_PARAMETER)) {
+                    unzipIsRequested = true;
+                    LOGGER.info("zip file analysis requested");
+                    continue;
+                }
+                if (theMasks.isEmpty() && 
fileName.equalsIgnoreCase(DEBUG_REQUEST_PARAMETER)) {
+                    isDebug = true;
+                    LOGGER.info("debug requested");
+                    continue;
+                }
+                Mask mask = new Mask();
+                if (fileName.startsWith("*")) {
+                    mask.suffixMatch = true;
+                    mask.matchString = fileName.substring(1);
+                } else {
+                    mask.suffixMatch = false;
+                    mask.matchString = fileName;
+                }
+                mask.matchString = cleanFileName(mask.matchString);
+                theMasks.add(mask);
+            }
+            return new MimeWalkConfiguration(theMasks.toArray(Mask[]::new), 
isDebug, unzipIsRequested);
+        }
+    }
+
+    /**
+     * Transforms <I>fileName<I> in a trimmed lowercase string usable for 
matching agains the masks.
+     * Also decode encoded words.
+     */
+    public static String cleanFileName(String fileName) {
+        return 
DecoderUtil.decodeEncodedWords(fileName.toLowerCase(Locale.US).trim(), 
DecodeMonitor.SILENT);
+    }
     
     /** Unzip request parameter. */
     protected static final String UNZIP_REQUEST_PARAMETER = "-z";
@@ -86,48 +131,12 @@ public class AttachmentFileNameIs extends GenericMatcher {
      * Controls certain log messages.
      */
     @VisibleForTesting
-    boolean isDebug = false;
-
-    /** contains ParsedMask instances, setup by init */
-    private Mask[] masks = null;
-    
-    /** True if unzip is requested. */
-    @VisibleForTesting
-    boolean unzipIsRequested;
+    MimeWalkConfiguration mimeWalkConfiguration = 
MimeWalkConfiguration.DEFAULT;
     
 
     @Override
     public void init() throws MessagingException {
-        /* sets up fileNameMasks variable by parsing the condition */
-        
-        StringTokenizer st = new StringTokenizer(getCondition(), ", ", false);
-        ArrayList<Mask> theMasks = new ArrayList<>(20);
-        while (st.hasMoreTokens()) {
-            String fileName = st.nextToken();
-            
-            // check possible parameters at the beginning of the condition
-            if (theMasks.isEmpty() && 
fileName.equalsIgnoreCase(UNZIP_REQUEST_PARAMETER)) {
-                unzipIsRequested = true;
-                LOGGER.info("zip file analysis requested");
-                continue;
-            }
-            if (theMasks.isEmpty() && 
fileName.equalsIgnoreCase(DEBUG_REQUEST_PARAMETER)) {
-                isDebug = true;
-                LOGGER.info("debug requested");
-                continue;
-            }
-            Mask mask = new Mask(); 
-            if (fileName.startsWith("*")) {
-                mask.suffixMatch = true;
-                mask.matchString = fileName.substring(1);
-            } else {
-                mask.suffixMatch = false;
-                mask.matchString = fileName;
-            }
-            mask.matchString = cleanFileName(mask.matchString);
-            theMasks.add(mask);
-        }
-        masks = theMasks.toArray(Mask[]::new);
+        mimeWalkConfiguration = MimeWalkConfiguration.parse(getCondition());
     }
 
     /** 
@@ -147,7 +156,7 @@ public class AttachmentFileNameIs extends GenericMatcher {
             }
             
         } catch (Exception e) {
-            if (isDebug) {
+            if (mimeWalkConfiguration.isDebug) {
                 LOGGER.debug("Malformed message", e);
             }
             throw new MessagingException("Malformed message", e);
@@ -200,12 +209,12 @@ public class AttachmentFileNameIs extends GenericMatcher {
                 fileName = cleanFileName(fileName);
                 // check the file name
                 if (matchFound(fileName)) {
-                    if (isDebug) {
+                    if (mimeWalkConfiguration.isDebug()) {
                         LOGGER.debug("matched {}", fileName);
                     }
                     return true;
                 }
-                if (unzipIsRequested && fileName.endsWith(ZIP_SUFFIX) && 
matchFoundInZip(part)) {
+                if (mimeWalkConfiguration.unzipIsRequested() && 
fileName.endsWith(ZIP_SUFFIX) && matchFoundInZip(part)) {
                     return true;
                 }
             }
@@ -225,7 +234,7 @@ public class AttachmentFileNameIs extends GenericMatcher {
      * @param fileName
      */
     protected boolean matchFound(String fileName) {
-        for (Mask mask1 : masks) {
+        for (Mask mask1 : mimeWalkConfiguration.masks) {
             boolean fMatch;
 
             //XXX: file names in mail may contain directory - theoretically
@@ -255,7 +264,7 @@ public class AttachmentFileNameIs extends GenericMatcher {
                 }
                 String fileName = zipEntry.getName();
                 if (matchFound(fileName)) {
-                    if (isDebug) {
+                    if (mimeWalkConfiguration.unzipIsRequested) {
                         LOGGER.debug("matched {}({})", part.getFileName(), 
fileName);
                     }
                     return true;
@@ -264,13 +273,5 @@ public class AttachmentFileNameIs extends GenericMatcher {
             return false;
         }
     }
-
-    /**
-     * Transforms <I>fileName<I> in a trimmed lowercase string usable for 
matching agains the masks.
-     * Also decode encoded words.
-     */
-    protected String cleanFileName(String fileName) {
-        return 
DecoderUtil.decodeEncodedWords(fileName.toLowerCase(Locale.US).trim(), 
DecodeMonitor.SILENT);
-    }
 }
 
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/AttachmentFileNameIsTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/AttachmentFileNameIsTest.java
index 249a82db72..5642dbadcd 100644
--- 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/AttachmentFileNameIsTest.java
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/AttachmentFileNameIsTest.java
@@ -27,6 +27,7 @@ import org.apache.james.util.ClassLoaderUtils;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 class AttachmentFileNameIsTest {
@@ -535,51 +536,26 @@ class AttachmentFileNameIsTest {
             .isNull();
     }
 
-    @Test
-    void shouldSupportDebugMode() throws Exception {
-        AttachmentFileNameIs testee = new AttachmentFileNameIs();
-
-        testee.init(FakeMatcherConfig.builder()
-            .matcherName("AttachmentFileNameIs")
-            .condition("-d file.txt")
-            .build());
-
-        assertThat(testee.isDebug).isTrue();
-    }
-
-    @Test
-    void debugModeShouldBeFalseByDefault() throws Exception {
-        AttachmentFileNameIs testee = new AttachmentFileNameIs();
-
-        testee.init(FakeMatcherConfig.builder()
-            .matcherName("AttachmentFileNameIs")
-            .condition("file.txt")
-            .build());
-
-        assertThat(testee.isDebug).isFalse();
-    }
-
-    @Test
-    void shouldSupportUnzipMode() throws Exception {
-        AttachmentFileNameIs testee = new AttachmentFileNameIs();
-
-        testee.init(FakeMatcherConfig.builder()
-            .matcherName("AttachmentFileNameIs")
-            .condition("-z file.txt")
-            .build());
-
-        assertThat(testee.unzipIsRequested).isTrue();
-    }
-
-    @Test
-    void unzipModeShouldBeFalseByDefault() throws Exception {
-        AttachmentFileNameIs testee = new AttachmentFileNameIs();
-
-        testee.init(FakeMatcherConfig.builder()
-            .matcherName("AttachmentFileNameIs")
-            .condition("file.txt")
-            .build());
-
-        assertThat(testee.unzipIsRequested).isFalse();
+    @Nested
+    class MimeWalkConfigurationTest {
+        @Test
+        void shouldSupportDebugMode() {
+            assertThat(AttachmentFileNameIs.MimeWalkConfiguration.parse("-d 
test.txt").isDebug()).isTrue();
+        }
+
+        @Test
+        void debugModeShouldBeFalseByDefault() {
+            
assertThat(AttachmentFileNameIs.MimeWalkConfiguration.parse("test.txt").isDebug()).isFalse();
+        }
+
+        @Test
+        void shouldSupportUnzipMode() {
+            assertThat(AttachmentFileNameIs.MimeWalkConfiguration.parse("-z 
test.txt").unzipIsRequested()).isTrue();
+        }
+
+        @Test
+        void unzipModeShouldBeFalseByDefault() {
+            
assertThat(AttachmentFileNameIs.MimeWalkConfiguration.parse("test.txt").unzipIsRequested()).isFalse();
+        }
     }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to