Repository: camel
Updated Branches:
  refs/heads/camel-2.14.x 834118dce -> 5f17c37ac


added possibility to use filename header withing zip aggregation


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/79472f63
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/79472f63
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/79472f63

Branch: refs/heads/camel-2.14.x
Commit: 79472f637962f43635c8abf9ccd8f83616dc0a52
Parents: 834118d
Author: Ranil Wijeyratne <[email protected]>
Authored: Sat Jan 24 00:37:04 2015 +0100
Committer: Claus Ibsen <[email protected]>
Committed: Mon Jan 26 14:54:29 2015 +0100

----------------------------------------------------------------------
 .../zipfile/ZipAggregationStrategy.java         | 15 +++-
 ...gregationStrategyWithFilenameHeaderTest.java | 92 ++++++++++++++++++++
 ...AggregationStrategyWithPreservationTest.java |  2 +-
 3 files changed, 104 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/79472f63/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
----------------------------------------------------------------------
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 d8fe877..b1105fa 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
@@ -40,7 +40,8 @@ import org.apache.camel.util.IOHelper;
  * This aggregation strategy will aggregate all incoming messages into a ZIP 
file.
  * <p>If the incoming exchanges contain {@link GenericFileMessage} file name 
will 
  * be taken from the body otherwise the body content will be treated as a byte 
- * array and the ZIP entry will be named using the message id.</p>
+ * array and the ZIP entry will be named using the message id (unless the flag
+ * useFilenameHeader is set to true.</p>
  * <p><b>Note:</b> Please note that this aggregation strategy requires eager 
  * completion check to work properly.</p>
  */
@@ -49,17 +50,21 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
     private String filePrefix;
     private String fileSuffix = ".zip";
     private boolean preserveFolderStructure;
+    private boolean useFilenameHeader;
 
     public ZipAggregationStrategy() {
-        this(false);
+        this(false, false);
     }
     
     /**
      * @param preserveFolderStructure if true, the folder structure is 
preserved when the source is
      * a type of {@link GenericFileMessage}.  If used with a file, use 
recursive=true.
+     * @param useFilenameHeader if true, the filename header will be used to 
name aggregated byte arrays
+     * within the ZIP file.
      */
-    public ZipAggregationStrategy(boolean preserveFolderStructure) {
+    public ZipAggregationStrategy(boolean preserveFolderStructure, boolean 
useFilenameHeader) {
         this.preserveFolderStructure = preserveFolderStructure;
+        this.useFilenameHeader = useFilenameHeader;
     }
     
     /**
@@ -85,6 +90,7 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
     public String getFileSuffix() {
         return fileSuffix;
     }
+
     /**
      * Sets the suffix that will be used when creating the ZIP filename.
      * @param fileSuffix suffix to use on ZIP file.
@@ -136,7 +142,8 @@ public class ZipAggregationStrategy implements 
AggregationStrategy {
             // Handle all other messages
             try {
                 byte[] buffer = 
newExchange.getIn().getMandatoryBody(byte[].class);
-                addEntryToZip(zipFile, newExchange.getIn().getMessageId(), 
buffer, buffer.length);
+                String entryName = useFilenameHeader ? 
newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : 
newExchange.getIn().getMessageId();
+                addEntryToZip(zipFile, entryName, buffer, buffer.length);
                 GenericFile<File> genericFile = FileConsumer.asGenericFile(
                     zipFile.getParent(), zipFile, 
Charset.defaultCharset().toString());
                 genericFile.bindToExchange(answer);

http://git-wip-us.apache.org/repos/asf/camel/blob/79472f63/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
new file mode 100644
index 0000000..2d31722
--- /dev/null
+++ 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
@@ -0,0 +1,92 @@
+/**
+ * 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.util.Arrays;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.util.IOHelper;
+import org.junit.Test;
+
+public class AggregationStrategyWithFilenameHeaderTest extends 
CamelTestSupport {
+
+    private static final List<String> FILE_NAMES = Arrays.asList("foo", "bar");
+
+    @Override
+    public void setUp() throws Exception {
+        deleteDirectory("target/out");
+        super.setUp();
+    }
+
+    @Test
+    public void testSplitter() throws Exception {
+       MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
+        mock.expectedMessageCount(1);
+        
+        template.setDefaultEndpointUri("direct:start");
+        template.sendBodyAndHeader("foo", Exchange.FILE_NAME, 
FILE_NAMES.get(0));
+        template.sendBodyAndHeader("bar", Exchange.FILE_NAME, 
FILE_NAMES.get(1));
+        assertMockEndpointsSatisfied();
+
+        Thread.sleep(500);
+
+        File[] files = new File("target/out").listFiles();
+        assertTrue(files != null);
+        assertTrue("Should be a file in target/out directory", files.length > 
0);
+
+        File resultFile = files[0];
+
+        final ZipFile file = new ZipFile(resultFile);
+        try {
+            final Enumeration<? extends ZipEntry> entries = file.entries();
+            int fileCount = 0;
+            while (entries.hasMoreElements()) {
+                fileCount++;
+                final ZipEntry entry = entries.nextElement();
+                assertTrue("Zip entry file name should be on of: " + 
FILE_NAMES, FILE_NAMES.contains(entry.getName()));                
+            }
+            assertEquals("Zip file should contains " + FILE_NAMES.size() + " 
files", FILE_NAMES.size(), fileCount);
+        } finally {
+            IOHelper.close(file);
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                       .aggregate(new ZipAggregationStrategy(false, true))
+                       .constant(true)
+                       .completionTimeout(50)
+                    .to("file:target/out")
+                    .to("mock:aggregateToZipEntry")
+                    .log("Done processing zip file: ${header.CamelFileName}");
+            }
+        };
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/79472f63/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
index 9c40cad..4fc6ac7 100644
--- 
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
+++ 
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
@@ -78,7 +78,7 @@ public class AggregationStrategyWithPreservationTest extends 
CamelTestSupport {
             public void configure() throws Exception {
                 // Unzip file and Split it according to FileEntry
                 
from("file:src/test/resources/org/apache/camel/aggregate/zipfile/data?consumer.delay=1000&noop=true&recursive=true")
-                    .aggregate(new ZipAggregationStrategy(true))
+                    .aggregate(new ZipAggregationStrategy(true, false))
                         .constant(true)
                         .completionFromBatchConsumer()
                         .eagerCheckCompletion()

Reply via email to