You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/10/09 08:21:53 UTC
(camel) branch main updated: CAMEL-21329: camel-zipfile and camel-tarfile - Fix NPE in split empty body with ZipAggregationStrategy
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 5303d6f4543 CAMEL-21329: camel-zipfile and camel-tarfile - Fix NPE in split empty body with ZipAggregationStrategy
5303d6f4543 is described below
commit 5303d6f45431ccdc31b6b83752599246418f5c95
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Oct 9 10:21:17 2024 +0200
CAMEL-21329: camel-zipfile and camel-tarfile - Fix NPE in split empty body with ZipAggregationStrategy
---
.../tarfile/TarAggregationStrategyConfigurer.java | 12 ++++++
.../aggregate/tarfile/TarAggregationStrategy.java | 26 ++++++++++-
.../aggregate/zipfile/ZipAggregationStrategy.java | 16 +------
.../ZipAggregationStrategySplitEmptyBodyTest.java | 50 ++++++++++++++++++++++
4 files changed, 88 insertions(+), 16 deletions(-)
diff --git a/components/camel-tarfile/src/generated/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategyConfigurer.java b/components/camel-tarfile/src/generated/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategyConfigurer.java
index 4cfbb04ddeb..87b5086456e 100644
--- a/components/camel-tarfile/src/generated/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategyConfigurer.java
+++ b/components/camel-tarfile/src/generated/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategyConfigurer.java
@@ -29,6 +29,10 @@ public class TarAggregationStrategyConfigurer extends org.apache.camel.support.c
case "fileSuffix": target.setFileSuffix(property(camelContext, java.lang.String.class, value)); return true;
case "parentdir":
case "parentDir": target.setParentDir(property(camelContext, java.io.File.class, value)); return true;
+ case "preservefolderstructure":
+ case "preserveFolderStructure": target.setPreserveFolderStructure(property(camelContext, boolean.class, value)); return true;
+ case "usefilenameheader":
+ case "useFilenameHeader": target.setUseFilenameHeader(property(camelContext, boolean.class, value)); return true;
default: return false;
}
}
@@ -42,6 +46,10 @@ public class TarAggregationStrategyConfigurer extends org.apache.camel.support.c
case "fileSuffix": return java.lang.String.class;
case "parentdir":
case "parentDir": return java.io.File.class;
+ case "preservefolderstructure":
+ case "preserveFolderStructure": return boolean.class;
+ case "usefilenameheader":
+ case "useFilenameHeader": return boolean.class;
default: return null;
}
}
@@ -56,6 +64,10 @@ public class TarAggregationStrategyConfigurer extends org.apache.camel.support.c
case "fileSuffix": return target.getFileSuffix();
case "parentdir":
case "parentDir": return target.getParentDir();
+ case "preservefolderstructure":
+ case "preserveFolderStructure": return target.isPreserveFolderStructure();
+ case "usefilenameheader":
+ case "useFilenameHeader": return target.isUseFilenameHeader();
default: return null;
}
}
diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java b/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java
index 3b734bfc7c2..018bb600eed 100644
--- a/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java
+++ b/components/camel-tarfile/src/main/java/org/apache/camel/processor/aggregate/tarfile/TarAggregationStrategy.java
@@ -146,6 +146,28 @@ public class TarAggregationStrategy implements AggregationStrategy {
this.parentDir = new File(parentDir);
}
+ public boolean isPreserveFolderStructure() {
+ return preserveFolderStructure;
+ }
+
+ /**
+ * If the incoming message is from a file, then the folder structure of said file can be preserved
+ */
+ public void setPreserveFolderStructure(boolean preserveFolderStructure) {
+ this.preserveFolderStructure = preserveFolderStructure;
+ }
+
+ public boolean isUseFilenameHeader() {
+ return useFilenameHeader;
+ }
+
+ /**
+ * Whether to use CamelFileName header for the filename instead of using unique message id
+ */
+ public void setUseFilenameHeader(boolean useFilenameHeader) {
+ this.useFilenameHeader = useFilenameHeader;
+ }
+
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
File tarFile;
@@ -171,7 +193,7 @@ public class TarAggregationStrategy implements AggregationStrategy {
}
Object body = newExchange.getIn().getBody();
- if (body instanceof WrappedFile wrappedFile) {
+ if (body instanceof WrappedFile<?> wrappedFile) {
body = wrappedFile.getFile();
}
@@ -220,7 +242,7 @@ public class TarAggregationStrategy implements AggregationStrategy {
@Override
public void onCompletion(Exchange exchange, Exchange inputExchange) {
// this aggregation strategy added onCompletion which we should handover when we are complete
- if (inputExchange != null) {
+ if (exchange != null && inputExchange != null) {
exchange.getExchangeExtension().handoverCompletions(inputExchange);
}
}
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 650099e6084..47713868811 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
@@ -132,8 +132,6 @@ public class ZipAggregationStrategy implements AggregationStrategy {
/**
* Gets the prefix used when creating the ZIP file name.
- *
- * @return the prefix
*/
public String getFilePrefix() {
return filePrefix;
@@ -141,8 +139,6 @@ public class ZipAggregationStrategy implements AggregationStrategy {
/**
* Sets the prefix that will be used when creating the ZIP filename.
- *
- * @param filePrefix prefix to use on ZIP file.
*/
public void setFilePrefix(String filePrefix) {
this.filePrefix = filePrefix;
@@ -150,8 +146,6 @@ public class ZipAggregationStrategy implements AggregationStrategy {
/**
* Gets the suffix used when creating the ZIP file name.
- *
- * @return the suffix
*/
public String getFileSuffix() {
return fileSuffix;
@@ -159,8 +153,6 @@ public class ZipAggregationStrategy implements AggregationStrategy {
/**
* Sets the suffix that will be used when creating the ZIP filename.
- *
- * @param fileSuffix suffix to use on ZIP file.
*/
public void setFileSuffix(String fileSuffix) {
this.fileSuffix = fileSuffix;
@@ -235,7 +227,7 @@ public class ZipAggregationStrategy implements AggregationStrategy {
zipFile = oldExchange.getIn().getBody(File.class);
}
Object body = newExchange.getIn().getBody();
- if (body instanceof WrappedFile wrappedFile) {
+ if (body instanceof WrappedFile<?> wrappedFile) {
body = wrappedFile.getFile();
}
@@ -298,7 +290,7 @@ public class ZipAggregationStrategy implements AggregationStrategy {
@Override
public void onCompletion(Exchange exchange, Exchange inputExchange) {
// this aggregation strategy added onCompletion which we should handover when we are complete
- if (inputExchange != null) {
+ if (exchange != null && inputExchange != null) {
exchange.getExchangeExtension().handoverCompletions(inputExchange);
}
}
@@ -325,8 +317,6 @@ public class ZipAggregationStrategy implements AggregationStrategy {
if (parent != null) {
Files.createDirectories(parent);
Files.copy(file.toPath(), dest, StandardCopyOption.REPLACE_EXISTING);
- } else {
- // TODO do some logging
}
}
}
@@ -342,8 +332,6 @@ public class ZipAggregationStrategy implements AggregationStrategy {
if (parent != null) {
Files.createDirectories(parent);
Files.write(dest, buffer, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
- } else {
- // TODO do some logging
}
}
}
diff --git a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategySplitEmptyBodyTest.java b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategySplitEmptyBodyTest.java
new file mode 100644
index 00000000000..5102ccbeda0
--- /dev/null
+++ b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategySplitEmptyBodyTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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 org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class ZipAggregationStrategySplitEmptyBodyTest extends CamelTestSupport {
+
+ @Test
+ public void testSplitEmptyBody() throws Exception {
+ getMockEndpoint("mock:split").expectedMessageCount(0);
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start", null);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .setBody().constant("")
+ .split(body(), new ZipAggregationStrategy())
+ .to("mock:split")
+ .end()
+ .to("mock:result");
+ }
+ };
+ }
+}