CAMEL-11016-add allowEmptyDirectory option added
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/47f863e1 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/47f863e1 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/47f863e1 Branch: refs/heads/master Commit: 47f863e12a14f4b500d3a77db1d150b03aa0cbb4 Parents: 82257c7 Author: onders86 <ondersez...@gmail.com> Authored: Sun Mar 19 00:03:30 2017 +0300 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Mar 22 21:23:44 2017 +0100 ---------------------------------------------------------------------- .../dataformat/tarfile/TarFileDataFormat.java | 15 ++- .../camel/dataformat/tarfile/TarIterator.java | 15 ++- .../tarfile/TarFileDataFormatTest.java | 93 ++++++++++++++++++- .../src/test/resources/data/hello.tar | Bin 0 -> 51200 bytes 4 files changed, 115 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/47f863e1/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java index 5465edc..1f17f30 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarFileDataFormat.java @@ -42,8 +42,9 @@ import static org.apache.camel.Exchange.FILE_NAME; */ public class TarFileDataFormat extends ServiceSupport implements DataFormat, DataFormatName { private boolean usingIterator; + private boolean allowEmptyDirectory; - @Override + @Override public String getDataFormatName() { return "tarfile"; } @@ -86,7 +87,9 @@ public class TarFileDataFormat extends ServiceSupport implements DataFormat, Dat @Override public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { if (usingIterator) { - return new TarIterator(exchange.getIn(), stream); + TarIterator tarIterator = new TarIterator(exchange.getIn(), stream); + tarIterator.setAllowEmptyDirectory(allowEmptyDirectory); + return tarIterator; } else { BufferedInputStream bis = new BufferedInputStream(stream); TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, bis); @@ -119,6 +122,14 @@ public class TarFileDataFormat extends ServiceSupport implements DataFormat, Dat public void setUsingIterator(boolean usingIterator) { this.usingIterator = usingIterator; } + + public boolean isAllowEmptyDirectory() { + return allowEmptyDirectory; + } + + public void setAllowEmptyDirectory(boolean allowEmptyDirectory) { + this.allowEmptyDirectory = allowEmptyDirectory; + } @Override protected void doStart() throws Exception { http://git-wip-us.apache.org/repos/asf/camel/blob/47f863e1/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java index 52332c1..fb1deda 100644 --- a/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java +++ b/components/camel-tarfile/src/main/java/org/apache/camel/dataformat/tarfile/TarIterator.java @@ -52,8 +52,9 @@ public class TarIterator implements Iterator<Message>, Closeable { private final Message inputMessage; private volatile TarArchiveInputStream tarInputStream; private volatile Message parent; + private boolean allowEmptyDirectory; - public TarIterator(Message inputMessage, InputStream inputStream) { + public TarIterator(Message inputMessage, InputStream inputStream) { this.inputMessage = inputMessage; //InputStream inputStream = inputMessage.getBody(InputStream.class); @@ -151,6 +152,10 @@ public class TarIterator implements Iterator<Message>, Closeable { while ((entry = tarInputStream.getNextTarEntry()) != null) { if (!entry.isDirectory()) { return entry; + } else { + if (allowEmptyDirectory) { + return entry; + } } } @@ -167,4 +172,12 @@ public class TarIterator implements Iterator<Message>, Closeable { IOHelper.close(tarInputStream); tarInputStream = null; } + + public boolean isAllowEmptyDirectory() { + return allowEmptyDirectory; + } + + public void setAllowEmptyDirectory(boolean allowEmptyDirectory) { + this.allowEmptyDirectory = allowEmptyDirectory; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/47f863e1/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java index 1ae7a58..47fffca 100644 --- a/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java +++ b/components/camel-tarfile/src/test/java/org/apache/camel/dataformat/tarfile/TarFileDataFormatTest.java @@ -16,27 +16,42 @@ */ package org.apache.camel.dataformat.tarfile; +import static org.apache.camel.Exchange.FILE_NAME; +import static org.apache.camel.dataformat.tarfile.TarUtils.TEXT; +import static org.apache.camel.dataformat.tarfile.TarUtils.getBytes; +import static org.apache.camel.dataformat.tarfile.TarUtils.getTaredText; + import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Iterator; import java.util.concurrent.TimeUnit; import org.apache.camel.Exchange; +import org.apache.camel.Processor; import org.apache.camel.builder.NotifyBuilder; 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.apache.camel.util.ObjectHelper; +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.junit.Test; -import static org.apache.camel.Exchange.FILE_NAME; -import static org.apache.camel.dataformat.tarfile.TarUtils.*; -import static org.apache.camel.dataformat.tarfile.TarUtils.getTaredText; - /** * Unit tests for {@link TarFileDataFormat}. */ public class TarFileDataFormatTest extends CamelTestSupport { private static final File TEST_DIR = new File("target/tar"); + private TarFileDataFormat tar; @Test public void testTarWithoutFileName() throws Exception { @@ -154,12 +169,55 @@ public class TarFileDataFormatTest extends CamelTestSupport { assertMockEndpointsSatisfied(); } + + @Test + public void testUntarWithEmptyDirectorySupported() throws Exception { + deleteDirectory(new File("hello_out")); + tar.setUsingIterator(true); + tar.setAllowEmptyDirectory(true); + template.sendBody("direct:untarWithEmptyDirectory", new File("src/test/resources/data/hello.tar")); + assertTrue(Files.exists(Paths.get("hello_out/Configurations2"))); + deleteDirectory(new File("hello_out")); + } + + @Test + public void testUntarWithEmptyDirectoryUnsupported() throws Exception { + deleteDirectory(new File("hello_out")); + tar.setUsingIterator(true); + tar.setAllowEmptyDirectory(false); + template.sendBody("direct:untarWithEmptyDirectory", new File("src/test/resources/data/hello.tar")); + assertTrue(!Files.exists(Paths.get("hello_out/Configurations2"))); + deleteDirectory(new File("hello_out")); + } @Override public void setUp() throws Exception { deleteDirectory(TEST_DIR); super.setUp(); } + + private static void copy(InputStream in, OutputStream out) throws IOException { + byte[] buffer = new byte[1024]; + while (true) { + int readCount = in.read(buffer); + if (readCount < 0) { + break; + } + out.write(buffer, 0, readCount); + } + } + + private static void copy(File file, OutputStream out) throws IOException { + try (InputStream in = new FileInputStream(file)) { + copy(in, out); + } + } + + private static void copy(InputStream in, File file) throws IOException { + try (OutputStream out = new FileOutputStream(file)) { + copy(in, out); + } + } @Override protected RouteBuilder createRouteBuilder() throws Exception { @@ -168,10 +226,35 @@ public class TarFileDataFormatTest extends CamelTestSupport { public void configure() throws Exception { interceptSendToEndpoint("file:*").to("mock:intercepted"); - TarFileDataFormat tar = new TarFileDataFormat(); + tar = new TarFileDataFormat(); from("direct:tar").marshal(tar).to("mock:tar"); from("direct:untar").unmarshal(tar).to("mock:untar"); + from("direct:untarWithEmptyDirectory").unmarshal(tar) + .split(body(Iterator.class)) + //.streaming() + //.to("file:hello_out?autoCreate=true") + .process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + InputStream is = new FileInputStream("src/test/resources/data/hello.tar"); + + TarArchiveEntry entry = new TarArchiveEntry((String)exchange.getIn().getHeader(Exchange.FILE_NAME)); + File outputFile = new File("hello_out", entry.getName()); + if (entry.isDirectory()) { + outputFile.mkdirs(); + } else { + outputFile.getParentFile().mkdirs(); + TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); + try { + copy(debInputStream, outputFile); + } finally { + debInputStream.close(); } + } + } + + }) + .end(); from("direct:tarAndUntar").marshal(tar).unmarshal(tar).to("mock:tarAndUntar"); from("direct:tarToFile").marshal(tar).to("file:" + TEST_DIR.getPath()).to("mock:tarToFile"); from("direct:dslTar").marshal(tar).to("mock:dslTar"); http://git-wip-us.apache.org/repos/asf/camel/blob/47f863e1/components/camel-tarfile/src/test/resources/data/hello.tar ---------------------------------------------------------------------- diff --git a/components/camel-tarfile/src/test/resources/data/hello.tar b/components/camel-tarfile/src/test/resources/data/hello.tar new file mode 100644 index 0000000..0bc978c Binary files /dev/null and b/components/camel-tarfile/src/test/resources/data/hello.tar differ