diff --git a/src/main/java/io/vertx/mods/UnzipVerticle.java b/src/main/java/io/vertx/mods/UnzipVerticle.java index 699e5eb..02d66b6 100644 --- a/src/main/java/io/vertx/mods/UnzipVerticle.java +++ b/src/main/java/io/vertx/mods/UnzipVerticle.java @@ -1,121 +1,127 @@ -package io.vertx.mods; - -import org.vertx.java.core.Handler; -import org.vertx.java.core.eventbus.Message; -import org.vertx.java.core.json.JsonObject; -import org.vertx.java.platform.PlatformManagerException; -import org.vertx.java.platform.Verticle; - -import java.io.*; -import java.util.UUID; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -/* - * Copyright 2013 Red Hat, Inc. - * - * Red Hat 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. - * - * @author Tim Fox - */ -public class UnzipVerticle extends Verticle { - - private static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); - private static final String FILE_SEP = System.getProperty("file.separator"); - private static final int BUFFER_SIZE = 4096; - - @Override - public void start() { - - JsonObject conf = container.config(); - String address = conf.getString("address", "io.vertx.unzipper"); - - vertx.eventBus().registerHandler(address, new Handler>() { - @Override - public void handle(Message message) { - String zipFile = message.body().getString("zipFile"); - if (zipFile == null) { - sendError("Please specify zipFile field in message", message); - return; - } - String destDir = message.body().getString("destDir"); - if (destDir == null) { - // Generate a tmp dest dir - destDir = generateTmpFileName(); - } - File dest = new File(destDir); - if (!dest.exists()) { - if (!dest.mkdir()) { - sendError("Failed to create directory " + dest, message); - return; - } - } - boolean deleteZip = message.body().getBoolean("deleteZip", false); - try { - unzipModuleData(dest, zipFile); - if (deleteZip) { - if (!new File(zipFile).delete()) { - sendError("Failed to delete zip file " + dest, message); - return; - } - } - message.reply(new JsonObject().putString("status", "ok").putString("destDir", destDir)); - } catch (Exception e) { - sendError("Failed to unzip module: " + e.getMessage(), message); - } - } - }); - } - - private void sendError(String errMsg, Message msg) { - JsonObject reply = new JsonObject().putString("status", "error").putString("message", errMsg); - msg.reply(reply); - } - - private String generateTmpFileName() { - return TEMP_DIR + FILE_SEP + "vertx-" + UUID.randomUUID().toString(); - } - - private void unzipModuleData(final File directory, final String zipFileName) throws Exception { - try (InputStream is = new BufferedInputStream(new FileInputStream(zipFileName)); - ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is))) { - ZipEntry entry; - while ((entry = zis.getNextEntry()) != null) { - String entryName = entry.getName(); - if (!entryName.isEmpty()) { - if (entry.isDirectory()) { - if (!new File(directory, entryName).mkdir()) { - throw new PlatformManagerException("Failed to create directory"); - } - } else { - int count; - byte[] buff = new byte[BUFFER_SIZE]; - BufferedOutputStream dest = null; - try { - OutputStream fos = new FileOutputStream(new File(directory, entryName)); - dest = new BufferedOutputStream(fos, BUFFER_SIZE); - while ((count = zis.read(buff, 0, BUFFER_SIZE)) != -1) { - dest.write(buff, 0, count); - } - dest.flush(); - } finally { - if (dest != null) { - dest.close(); - } - } - } - } - } - } - } -} +package io.vertx.mods; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.vertx.java.core.Handler; +import org.vertx.java.core.eventbus.Message; +import org.vertx.java.core.json.JsonObject; +import org.vertx.java.platform.Verticle; + +/* + * Copyright 2013 Red Hat, Inc. + * + * Red Hat 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. + * + * @author Tim Fox + */ +public class UnzipVerticle extends Verticle { + + private static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); + private static final String FILE_SEP = System.getProperty("file.separator"); + private static final int BUFFER_SIZE = 4096; + + @Override + public void start() { + + JsonObject conf = container.config(); + String address = conf.getString("address", "io.vertx.unzipper"); + + vertx.eventBus().registerHandler(address, new Handler>() { + @Override + public void handle(Message message) { + String zipFile = message.body().getString("zipFile"); + if (zipFile == null) { + sendError("Please specify zipFile field in message", message); + return; + } + String destDir = message.body().getString("destDir"); + if (destDir == null) { + // Generate a tmp dest dir + destDir = generateTmpFileName(); + } + + Path dest; + try { + dest = Files.createDirectories(Paths.get(destDir)); + } catch (Exception e) { + sendError("Failed to create directory " + destDir + " (" + e.getMessage() + ")", message); + return; + } + try { + unzipData(dest.toString(), zipFile); + } catch (Exception e) { + sendError("Failed to unzip file: " + destDir + " (" + e.getMessage() + ")", message); + return; + } + try { + boolean deleteZip = message.body().getBoolean("deleteZip", false); + if (deleteZip) + Files.delete(Paths.get(zipFile)); + } catch (Exception e) { + sendError("Failed to delete zip file " + destDir + " (" + e.getMessage() + ")", message); + return; + } + message.reply(new JsonObject().putString("status", "ok").putString("destDir", destDir)); + } + }); + } + + private void sendError(String errMsg, Message msg) { + JsonObject reply = new JsonObject().putString("status", "error").putString("message", errMsg); + msg.reply(reply); + } + + private String generateTmpFileName() { + return TEMP_DIR + FILE_SEP + "vertx-" + UUID.randomUUID().toString(); + } + + private void unzipData(final String directory, final String zipFileName) throws Exception { + try (InputStream is = new BufferedInputStream(new FileInputStream(zipFileName)); + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is))) { + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + String entryName = entry.getName(); + if (!entryName.isEmpty()) { + if (entry.isDirectory()) { + Files.createDirectories(Paths.get(directory, entryName)); + continue; + } + Files.createDirectories(Paths.get(directory, entryName).getParent()); + + int count; + byte[] buff = new byte[BUFFER_SIZE]; + try (OutputStream fos = new FileOutputStream(Paths.get(directory, entryName).toFile()); + BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);) { + + while ((count = zis.read(buff, 0, BUFFER_SIZE)) != -1) { + dest.write(buff, 0, count); + } + dest.flush(); + } catch (Exception e) { + } + } + } + } + } +} diff --git a/src/test/java/io/vertx/mods/test/integration/ModuleIntegrationTest.java b/src/test/java/io/vertx/mods/test/integration/ModuleIntegrationTest.java index d4df3cf..67e30ad 100644 --- a/src/test/java/io/vertx/mods/test/integration/ModuleIntegrationTest.java +++ b/src/test/java/io/vertx/mods/test/integration/ModuleIntegrationTest.java @@ -1,110 +1,113 @@ -package io.vertx.mods.test.integration;/* - * Copyright 2013 Red Hat, Inc. - * - * Red Hat 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. - * - * @author Tim Fox - */ - -import org.junit.Test; -import org.vertx.java.core.AsyncResult; -import org.vertx.java.core.AsyncResultHandler; -import org.vertx.java.core.Handler; -import org.vertx.java.core.eventbus.Message; -import org.vertx.java.core.http.HttpClientResponse; -import org.vertx.java.core.http.HttpServerRequest; -import org.vertx.java.core.json.JsonObject; -import org.vertx.testtools.TestVerticle; -import org.vertx.testtools.VertxAssert; - -import static org.vertx.testtools.VertxAssert.*; - -/** - * Example Java integration test that deploys the module that this project builds. - * - * Quite often in integration tests you want to deploy the same module for all tests and you don't want tests - * to start before the module has been deployed. - * - * This test demonstrates how to do that. - */ -public class ModuleIntegrationTest extends TestVerticle { - - private static final String destDir = "src/test/resources/destDir"; - - @Test - public void testUnzipSpecifyDir() { - JsonObject msg = new JsonObject().putString("zipFile", "src/test/resources/testfile.zip").putString("destDir", destDir); - vertx.eventBus().send("io.vertx.unzipper", msg, new Handler>() { - @Override - public void handle(Message reply) { - assertEquals("ok", reply.body().getString("status")); - String dest = reply.body().getString("destDir"); - assertEquals(destDir, dest); - assertUnzipped(dest); - testComplete(); - } - }); - } - - @Test - public void testUnzipTempDir() { - JsonObject msg = new JsonObject().putString("zipFile", "src/test/resources/testfile.zip"); - vertx.eventBus().send("io.vertx.unzipper", msg, new Handler>() { - @Override - public void handle(Message reply) { - assertEquals("ok", reply.body().getString("status")); - String dest = reply.body().getString("destDir"); - assertNotNull(dest); - assertUnzipped(dest); - testComplete(); - } - }); - } - - private void assertUnzipped(String dest) { - assertTrue(vertx.fileSystem().existsSync(dest + "/some-dir")); - assertTrue(vertx.fileSystem().existsSync(dest + "/some-dir/textfile.txt")); - } - - @Override - public void start() { - cleanup(); - // Make sure we call initialize() - this sets up the assert stuff so assert functionality works correctly - initialize(); - // Deploy the module - the System property `vertx.modulename` will contain the name of the module so you - // don't have to hardecode it in your tests - container.deployModule(System.getProperty("vertx.modulename"), new AsyncResultHandler() { - @Override - public void handle(AsyncResult asyncResult) { - // Deployment is asynchronous and this this handler will be called when it's complete (or failed) - assertTrue(asyncResult.succeeded()); - assertNotNull("deploymentID should not be null", asyncResult.result()); - // If deployed correctly then start the tests! - startTests(); - } - }); - } - - @Override - public void stop() { - cleanup(); - } - - private void cleanup() { - try { - vertx.fileSystem().deleteSync(destDir, true); - } catch (Exception ignore) { - } - } - -} +package io.vertx.mods.test.integration;/* + * Copyright 2013 Red Hat, Inc. + * + * Red Hat 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. + * + * @author Tim Fox + */ + +import static org.vertx.testtools.VertxAssert.assertEquals; +import static org.vertx.testtools.VertxAssert.assertNotNull; +import static org.vertx.testtools.VertxAssert.assertTrue; +import static org.vertx.testtools.VertxAssert.testComplete; + +import org.junit.Test; +import org.vertx.java.core.AsyncResult; +import org.vertx.java.core.AsyncResultHandler; +import org.vertx.java.core.Handler; +import org.vertx.java.core.eventbus.Message; +import org.vertx.java.core.json.JsonObject; +import org.vertx.testtools.TestVerticle; + +/** + * Example Java integration test that deploys the module that this project builds. + * + * Quite often in integration tests you want to deploy the same module for all tests and you don't want tests to start + * before the module has been deployed. + * + * This test demonstrates how to do that. + */ +public class ModuleIntegrationTest extends TestVerticle { + + private static final String destDir = "src/test/resources/destDir"; + + @Test + public void testUnzipSpecifyDir() { + JsonObject msg = new JsonObject().putString("zipFile", "src/test/resources/testfile.zip").putString("destDir", + destDir); + vertx.eventBus().send("io.vertx.unzipper", msg, new Handler>() { + @Override + public void handle(Message reply) { + assertEquals("ok", reply.body().getString("status")); + String dest = reply.body().getString("destDir"); + assertEquals(destDir, dest); + assertUnzipped(dest); + testComplete(); + } + }); + } + + @Test + public void testUnzipTempDir() { + JsonObject msg = new JsonObject().putString("zipFile", "src/test/resources/testfile.zip"); + vertx.eventBus().send("io.vertx.unzipper", msg, new Handler>() { + @Override + public void handle(Message reply) { + assertEquals("ok", reply.body().getString("status")); + String dest = reply.body().getString("destDir"); + assertNotNull(dest); + assertUnzipped(dest); + testComplete(); + } + }); + } + + private void assertUnzipped(String dest) { + assertTrue(vertx.fileSystem().existsSync(dest + "/some-dir")); + assertTrue(vertx.fileSystem().existsSync(dest + "/some-dir/textfile.txt")); + assertTrue(vertx.fileSystem().existsSync(dest + "/some-dir/some-dir2")); + assertTrue(vertx.fileSystem().existsSync(dest + "/some-dir/some-dir2/textfile2.txt")); + } + + @Override + public void start() { + cleanup(); + // Make sure we call initialize() - this sets up the assert stuff so assert functionality works correctly + initialize(); + // Deploy the module - the System property `vertx.modulename` will contain the name of the module so you + // don't have to hardecode it in your tests + container.deployModule(System.getProperty("vertx.modulename"), new AsyncResultHandler() { + @Override + public void handle(AsyncResult asyncResult) { + // Deployment is asynchronous and this this handler will be called when it's complete (or failed) + assertTrue(asyncResult.succeeded()); + assertNotNull("deploymentID should not be null", asyncResult.result()); + // If deployed correctly then start the tests! + startTests(); + } + }); + } + + @Override + public void stop() { + cleanup(); + } + + private void cleanup() { + try { + vertx.fileSystem().deleteSync(destDir, true); + } catch (Exception ignore) { + } + } + +} diff --git a/src/test/resources/some-dir/some-dir2/textfile2.txt b/src/test/resources/some-dir/some-dir2/textfile2.txt new file mode 100644 index 0000000..f13499f --- /dev/null +++ b/src/test/resources/some-dir/some-dir2/textfile2.txt @@ -0,0 +1 @@ +blah blah blah diff --git a/src/test/resources/testfile.zip b/src/test/resources/testfile.zip index 5c981c8..f93267f 100644 Binary files a/src/test/resources/testfile.zip and b/src/test/resources/testfile.zip differ