camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject git commit: CAMEL-7404 Zip Aggregation Strategy preserves folder structure with thanks to Matt
Date Wed, 30 Apr 2014 14:59:16 GMT
Repository: camel
Updated Branches:
  refs/heads/master 913c48d92 -> bb2539c2b


CAMEL-7404 Zip Aggregation Strategy preserves folder structure with thanks to Matt


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

Branch: refs/heads/master
Commit: bb2539c2b574f0f64f42680d680c5a9cb909e375
Parents: 913c48d
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Wed Apr 30 22:44:16 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Wed Apr 30 22:44:16 2014 +0800

----------------------------------------------------------------------
 .../zipfile/ZipAggregationStrategy.java         | 31 ++++---
 ...AggregationStrategyWithPreservationTest.java | 93 ++++++++++++++++++++
 .../aggregate/zipfile/data/another/hello.txt    |  1 +
 .../aggregate/zipfile/data/other/greetings.txt  |  1 +
 4 files changed, 116 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/bb2539c2/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 a028614..208f727 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
@@ -49,7 +49,20 @@ public class ZipAggregationStrategy implements AggregationStrategy {
 
     private String filePrefix;
     private String fileSuffix = ".zip";
+    private boolean preserveFolderStructure;
 
+    public ZipAggregationStrategy() {
+        this(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.
+     */
+    public ZipAggregationStrategy(boolean preserveFolderStructure) {
+        this.preserveFolderStructure = preserveFolderStructure;
+    }
+    
     /**
      * Gets the prefix used when creating the ZIP file name.
      * @return the prefix
@@ -110,7 +123,7 @@ public class ZipAggregationStrategy implements AggregationStrategy {
             try {
                 File appendFile =  newExchange.getIn().getBody(File.class);
                 if (appendFile != null) {
-                    addFilesToZip(zipFile, new File[]{appendFile});
+                    addFileToZip(zipFile, appendFile, this.preserveFolderStructure ? newExchange.getIn().toString()
: null);
                     GenericFile<File> genericFile = 
                         FileConsumer.asGenericFile(
                             zipFile.getParent(), 
@@ -139,7 +152,7 @@ public class ZipAggregationStrategy implements AggregationStrategy {
         return answer;
     }
     
-    private static void addFilesToZip(File source, File[] files) throws IOException {
+    private static void addFileToZip(File source, File file, String fileName) throws IOException
{
         File tmpZip = File.createTempFile(source.getName(), null);
         tmpZip.delete();
         if (!source.renameTo(tmpZip)) {
@@ -149,15 +162,13 @@ public class ZipAggregationStrategy implements AggregationStrategy {
         ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
 
-        for (int i = 0; i < files.length; i++) {
-            InputStream in = new FileInputStream(files[i]);
-            out.putNextEntry(new ZipEntry(files[i].getName()));
-            for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
-                out.write(buffer, 0, read);
-            }
-            out.closeEntry();
-            in.close();
+        InputStream in = new FileInputStream(file);
+        out.putNextEntry(new ZipEntry(fileName == null ? file.getName() : fileName));
+        for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
+            out.write(buffer, 0, read);
         }
+        out.closeEntry();
+        in.close();
 
         for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) {
             out.putNextEntry(ze);

http://git-wip-us.apache.org/repos/asf/camel/blob/bb2539c2/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
new file mode 100644
index 0000000..647faa5
--- /dev/null
+++ b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
@@ -0,0 +1,93 @@
+/**
+ * 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.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+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.junit4.CamelTestSupport;
+import org.apache.camel.util.IOHelper;
+import org.junit.Test;
+
+public class AggregationStrategyWithPreservationTest extends CamelTestSupport {
+
+    private static final int EXPECTED_NO_FILES = 5;
+
+    @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);
+
+        assertMockEndpointsSatisfied();
+
+        Thread.sleep(500);
+
+        File[] files = new File("target/out").listFiles();
+        assertTrue("Should be a file in target/out directory", files.length > 0);
+        
+        File resultFile = files[0];
+        Set<String> expectedZipFiles = new HashSet<String>(Arrays.asList("another"
+ File.separator + "hello.txt", 
+                                                                         "other" + File.separator
+ "greetings.txt",
+                                                                         "chiau.txt", "hi.txt",
"hola.txt"));
+        ZipInputStream zin = new ZipInputStream(new FileInputStream(resultFile));
+        try {
+            int fileCount = 0;
+            for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) {
+                System.out.println(ze.toString());
+                expectedZipFiles.remove(ze.toString());
+                fileCount++;
+            }
+            assertTrue("Zip file should contains " + AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES
+ " files",
+                       fileCount == AggregationStrategyWithPreservationTest.EXPECTED_NO_FILES);
+            assertEquals("Should have found all of the zip files in the file.", 0, expectedZipFiles.size());
+        } finally {
+            IOHelper.close(zin);
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            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))
+                        .constant(true)
+                        .completionFromBatchConsumer()
+                        .eagerCheckCompletion()
+                    .to("file:target/out")
+                    .to("mock:aggregateToZipEntry")
+                    .log("Done processing zip file: ${header.CamelFileName}");
+            }
+        };
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/bb2539c2/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/another/hello.txt
----------------------------------------------------------------------
diff --git a/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/another/hello.txt
b/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/another/hello.txt
new file mode 100644
index 0000000..b6fc4c6
--- /dev/null
+++ b/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/another/hello.txt
@@ -0,0 +1 @@
+hello
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/bb2539c2/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/other/greetings.txt
----------------------------------------------------------------------
diff --git a/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/other/greetings.txt
b/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/other/greetings.txt
new file mode 100644
index 0000000..9eaa11a
--- /dev/null
+++ b/components/camel-zipfile/src/test/resources/org/apache/camel/aggregate/zipfile/data/other/greetings.txt
@@ -0,0 +1 @@
+greetings
\ No newline at end of file


Mime
View raw message