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

davsclaus pushed a commit to branch camel-4.4.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.4.x by this push:
     new fb6b25bdf7e Zipsplitter null body (#15638)
fb6b25bdf7e is described below

commit fb6b25bdf7ecb8da1aa8ac36b0c19a3b7a4ca378
Author: thomas-gantenbein-tga 
<[email protected]>
AuthorDate: Sun Sep 22 08:30:03 2024 +0200

    Zipsplitter null body (#15638)
    
    * Return oldExchange when incoming exchange body is null
    
    * Remove deprecated `doPresetup` method
    
    * Reduce cognitive complexity
    
    ---------
    
    Co-authored-by: Thomas Gantenbein <[email protected]>
---
 .../zipfile/ZipFileDataFormatConfigurer.java       |  34 -----
 .../org/apache/camel/configurer/zipFile-dataformat |   2 -
 .../org/apache/camel/dataformat.properties         |   7 -
 .../services/org/apache/camel/dataformat/zipFile   |   2 -
 .../apache/camel/dataformat/zipfile/zipFile.json   |  25 ----
 .../aggregate/zipfile/ZipAggregationStrategy.java  |  70 ++++++----
 .../ZipAggregationStrategyNullBodyTest.java        | 147 +++++++++++++++++++++
 7 files changed, 189 insertions(+), 98 deletions(-)

diff --git 
a/components/camel-zipfile/src/generated/java/org/apache/camel/dataformat/zipfile/ZipFileDataFormatConfigurer.java
 
b/components/camel-zipfile/src/generated/java/org/apache/camel/dataformat/zipfile/ZipFileDataFormatConfigurer.java
deleted file mode 100644
index 8f68fc15ed2..00000000000
--- 
a/components/camel-zipfile/src/generated/java/org/apache/camel/dataformat/zipfile/ZipFileDataFormatConfigurer.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/* Generated by camel build tools - do NOT edit this file! */
-package org.apache.camel.dataformat.zipfile;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.spi.GeneratedPropertyConfigurer;
-import org.apache.camel.support.component.PropertyConfigurerSupport;
-
-/**
- * Generated by camel build tools - do NOT edit this file!
- */
-@SuppressWarnings("unchecked")
-public class ZipFileDataFormatConfigurer extends PropertyConfigurerSupport 
implements GeneratedPropertyConfigurer {
-
-    @Override
-    public boolean configure(CamelContext camelContext, Object target, String 
name, Object value, boolean ignoreCase) {
-        ZipFileDataFormat dataformat = (ZipFileDataFormat) target;
-        switch (ignoreCase ? name.toLowerCase() : name) {
-        case "usingiterator":
-        case "usingIterator": 
dataformat.setUsingIterator(property(camelContext, boolean.class, value)); 
return true;
-        case "allowemptydirectory":
-        case "allowEmptyDirectory": 
dataformat.setAllowEmptyDirectory(property(camelContext, boolean.class, 
value)); return true;
-        case "preservepathelements":
-        case "preservePathElements": 
dataformat.setPreservePathElements(property(camelContext, boolean.class, 
value)); return true;
-        case "maxdecompressedsize":
-        case "maxDecompressedSize": 
dataformat.setMaxDecompressedSize(property(camelContext, long.class, value)); 
return true;
-        default: return false;
-        }
-    }
-
-}
-
diff --git 
a/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/configurer/zipFile-dataformat
 
b/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/configurer/zipFile-dataformat
deleted file mode 100644
index f391b96f073..00000000000
--- 
a/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/configurer/zipFile-dataformat
+++ /dev/null
@@ -1,2 +0,0 @@
-# Generated by camel build tools - do NOT edit this file!
-class=org.apache.camel.dataformat.zipfile.ZipFileDataFormatConfigurer
diff --git 
a/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/dataformat.properties
 
b/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/dataformat.properties
deleted file mode 100644
index 236f525e4cb..00000000000
--- 
a/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/dataformat.properties
+++ /dev/null
@@ -1,7 +0,0 @@
-# Generated by camel build tools - do NOT edit this file!
-dataFormats=zipFile
-groupId=org.apache.camel
-artifactId=camel-zipfile
-version=4.4.4-SNAPSHOT
-projectName=Camel :: Zip File
-projectDescription=Camel Zip file support
diff --git 
a/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/dataformat/zipFile
 
b/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/dataformat/zipFile
deleted file mode 100644
index cb0c8df1753..00000000000
--- 
a/components/camel-zipfile/src/generated/resources/META-INF/services/org/apache/camel/dataformat/zipFile
+++ /dev/null
@@ -1,2 +0,0 @@
-# Generated by camel build tools - do NOT edit this file!
-class=org.apache.camel.dataformat.zipfile.ZipFileDataFormat
diff --git 
a/components/camel-zipfile/src/generated/resources/org/apache/camel/dataformat/zipfile/zipFile.json
 
b/components/camel-zipfile/src/generated/resources/org/apache/camel/dataformat/zipfile/zipFile.json
deleted file mode 100644
index 2fea6f9281e..00000000000
--- 
a/components/camel-zipfile/src/generated/resources/org/apache/camel/dataformat/zipfile/zipFile.json
+++ /dev/null
@@ -1,25 +0,0 @@
-{
-  "dataformat": {
-    "kind": "dataformat",
-    "name": "zipFile",
-    "title": "Zip File",
-    "description": "Compression and decompress streams using 
java.util.zip.ZipStream.",
-    "deprecated": false,
-    "firstVersion": "2.11.0",
-    "label": "dataformat,transformation,file",
-    "javaType": "org.apache.camel.dataformat.zipfile.ZipFileDataFormat",
-    "supportLevel": "Stable",
-    "groupId": "org.apache.camel",
-    "artifactId": "camel-zipfile",
-    "version": "4.4.4-SNAPSHOT",
-    "modelName": "zipFile",
-    "modelJavaType": "org.apache.camel.model.dataformat.ZipFileDataFormat"
-  },
-  "properties": {
-    "id": { "index": 0, "kind": "attribute", "displayName": "Id", "required": 
false, "type": "string", "javaType": "java.lang.String", "deprecated": false, 
"autowired": false, "secret": false, "description": "The id of this node" },
-    "usingIterator": { "index": 1, "kind": "attribute", "displayName": "Using 
Iterator", "required": false, "type": "boolean", "javaType": 
"java.lang.Boolean", "deprecated": false, "autowired": false, "secret": false, 
"defaultValue": false, "description": "If the zip file has more than one entry, 
the setting this option to true, allows working with the splitter EIP, to split 
the data using an iterator in a streaming mode." },
-    "allowEmptyDirectory": { "index": 2, "kind": "attribute", "displayName": 
"Allow Empty Directory", "required": false, "type": "boolean", "javaType": 
"java.lang.Boolean", "deprecated": false, "autowired": false, "secret": false, 
"defaultValue": false, "description": "If the zip file has more than one entry, 
setting this option to true, allows to get the iterator even if the directory 
is empty" },
-    "preservePathElements": { "index": 3, "kind": "attribute", "displayName": 
"Preserve Path Elements", "required": false, "type": "boolean", "javaType": 
"java.lang.Boolean", "deprecated": false, "autowired": false, "secret": false, 
"defaultValue": false, "description": "If the file name contains path elements, 
setting this option to true, allows the path to be maintained in the zip file." 
},
-    "maxDecompressedSize": { "index": 4, "kind": "attribute", "displayName": 
"Max Decompressed Size", "label": "advanced", "required": false, "type": 
"integer", "javaType": "java.lang.Long", "deprecated": false, "autowired": 
false, "secret": false, "defaultValue": "1073741824", "description": "Set the 
maximum decompressed size of a zip file (in bytes). The default value if not 
specified corresponds to 1 gigabyte. An IOException will be thrown if the 
decompressed size exceeds this amount. [...]
-  }
-}
diff --git 
a/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
 
b/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
index 2584392cd66..06024741d42 100644
--- 
a/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
+++ 
b/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
@@ -180,46 +180,40 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
         File zipFile;
         Exchange answer = oldExchange;
 
+        boolean isFirstTimeInAggregation = oldExchange == null;
         // Guard against empty new exchanges
-        if (newExchange == null) {
+        if (newExchange.getIn().getBody() == null && 
!isFirstTimeInAggregation) {
             return oldExchange;
         }
 
-        // First time for this aggregation
-        if (oldExchange == null) {
-            try {
-                zipFile = FileUtil.createTempFile(this.filePrefix, 
this.fileSuffix, this.parentDir);
-                newZipFile(zipFile);
-            } catch (IOException | URISyntaxException e) {
-                throw new GenericFileOperationFailedException(e.getMessage(), 
e);
-            }
+        if (isFirstTimeInAggregation) {
+            zipFile = createZipFile();
             answer = newExchange;
             answer.getExchangeExtension().addOnCompletion(new 
DeleteZipFileOnCompletion(zipFile));
         } else {
             zipFile = oldExchange.getIn().getBody(File.class);
         }
         Object body = newExchange.getIn().getBody();
-        if (body instanceof WrappedFile) {
-            body = ((WrappedFile) body).getFile();
+        if (body instanceof WrappedFile wrappedFile) {
+            body = wrappedFile.getFile();
         }
 
         String charset = ExchangeHelper.getCharsetName(newExchange, true);
 
-        if (body instanceof File) {
-            try {
-                File appendFile = (File) body;
-                // try to append empty data only when explicit set
-                if (this.allowEmptyFiles || appendFile.length() > 0) {
-                    String entryName = preserveFolderStructure
-                            ? 
newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class)
-                            : newExchange.getIn().getMessageId();
-                    addFileToZip(zipFile, appendFile, 
this.preserveFolderStructure ? entryName : null);
-                }
-            } catch (Exception e) {
-                throw new GenericFileOperationFailedException(e.getMessage(), 
e);
-            }
+        if (body instanceof File appendFile) {
+            appendFileToZip(newExchange, appendFile, zipFile);
         } else {
-            // Handle all other messages
+            appendIncomingBodyAsBytesToZip(newExchange, zipFile, charset);
+        }
+
+        GenericFile<File> genericFile = 
FileConsumer.asGenericFile(zipFile.getParent(), zipFile, charset, false);
+        genericFile.bindToExchange(answer);
+
+        return answer;
+    }
+
+    private void appendIncomingBodyAsBytesToZip(Exchange newExchange, File 
zipFile, String charset) {
+        if (newExchange.getIn().getBody() != null) {
             try {
                 byte[] buffer = 
newExchange.getIn().getMandatoryBody(byte[].class);
                 // try to append empty data only when explicit set
@@ -233,11 +227,31 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
                 throw new GenericFileOperationFailedException(e.getMessage(), 
e);
             }
         }
+    }
 
-        GenericFile<File> genericFile = 
FileConsumer.asGenericFile(zipFile.getParent(), zipFile, charset, false);
-        genericFile.bindToExchange(answer);
+    private void appendFileToZip(Exchange newExchange, File appendFile, File 
zipFile) {
+        try {
+            // try to append empty data only when explicit set
+            if (this.allowEmptyFiles || appendFile.length() > 0) {
+                String entryName = preserveFolderStructure
+                        ? newExchange.getIn().getHeader(Exchange.FILE_NAME, 
String.class)
+                        : newExchange.getIn().getMessageId();
+                addFileToZip(zipFile, appendFile, this.preserveFolderStructure 
? entryName : null);
+            }
+        } catch (Exception e) {
+            throw new GenericFileOperationFailedException(e.getMessage(), e);
+        }
+    }
 
-        return answer;
+    private File createZipFile() {
+        File zipFile;
+        try {
+            zipFile = FileUtil.createTempFile(this.filePrefix, 
this.fileSuffix, this.parentDir);
+            newZipFile(zipFile);
+        } catch (IOException | URISyntaxException e) {
+            throw new GenericFileOperationFailedException(e.getMessage(), e);
+        }
+        return zipFile;
     }
 
     @Override
diff --git 
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyNullBodyTest.java
 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyNullBodyTest.java
new file mode 100644
index 00000000000..1970caebbca
--- /dev/null
+++ 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyNullBodyTest.java
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.aggregate.zipfile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ZipAggregationStrategyNullBodyTest extends CamelTestSupport {
+
+    private static final String TEST_DIR = 
"target/out_ZipAggregationStrategyNullBodyTest";
+    public static final String MOCK_AGGREGATE_TO_ZIP_ENTRY = 
"mock:aggregateToZipEntry";
+
+    @BeforeEach
+    public void deleteTestDirs() {
+        deleteDirectory(TEST_DIR);
+    }
+
+    @Test
+    public void testNullBodyLast() throws Exception {
+        MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello");
+        template.sendBody("direct:start", "Hello again");
+        template.sendBody("direct:start", null);
+
+        assertZipContainsFiles(2);
+    }
+
+    @Test
+    public void testNullBodyFirst() throws Exception {
+        MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", null);
+        template.sendBody("direct:start", "Hello");
+        template.sendBody("direct:start", "Hello again");
+
+        assertZipContainsFiles(2);
+    }
+
+    @Test
+    public void testNullBodyMiddle() throws Exception {
+        MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello");
+        template.sendBody("direct:start", null);
+        template.sendBody("direct:start", "Hello again");
+
+        assertZipContainsFiles(2);
+    }
+
+    @Test
+    public void testNullBodiesOnly() throws Exception {
+        MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", null);
+        template.sendBody("direct:start", null);
+        template.sendBody("direct:start", null);
+
+        assertZipContainsFiles(0);
+    }
+
+    @Test
+    public void testTwoNullBodies() throws Exception {
+        MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", null);
+        template.sendBody("direct:start", null);
+        template.sendBody("direct:start", "Hello");
+
+        assertZipContainsFiles(1);
+    }
+
+    private void assertZipContainsFiles(int expectedCount) throws 
InterruptedException, IOException {
+        MockEndpoint.assertIsSatisfied(context);
+
+        File[] files = new File(TEST_DIR).listFiles();
+        assertNotNull(files);
+        assertTrue(files.length > 0, "Should be a file in " + TEST_DIR + " 
directory");
+
+        File resultFile = files[0];
+
+        ZipInputStream zin = new ZipInputStream(new 
FileInputStream(resultFile));
+        try {
+            int fileCount = 0;
+            for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = 
zin.getNextEntry()) {
+                fileCount++;
+            }
+            assertEquals(expectedCount, fileCount,
+                    "Zip file should contains " + expectedCount + " files");
+        } finally {
+            IOHelper.close(zin);
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                        .aggregate(new ZipAggregationStrategy())
+                        .constant(true)
+                        .completionSize(3)
+                        .eagerCheckCompletion()
+                        .to("file:" + TEST_DIR)
+                        .to(MOCK_AGGREGATE_TO_ZIP_ENTRY)
+                        .log("Done processing zip file: 
${header.CamelFileName}");
+
+            }
+        };
+
+    }
+}

Reply via email to