Implement CBA upload through REST
authorAlexis de Talhouët <adetalhouet89@gmail.com>
Tue, 15 Jan 2019 19:44:58 +0000 (14:44 -0500)
committerAlexis de Talhouët <adetalhouet89@gmail.com>
Fri, 18 Jan 2019 18:56:01 +0000 (13:56 -0500)
Change-Id: I417254c5107f8b0031932e6a7cf0599561ee9a3c
Issue-ID: CCSDK-910
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/BluePrintArchiveUtils.kt

index ab5175d..fe7929e 100755 (executable)
@@ -24,13 +24,18 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
 import org.apache.commons.io.IOUtils
 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
-import java.io.*
+import org.slf4j.LoggerFactory
+import java.io.BufferedInputStream
+import java.io.File
+import java.io.FileInputStream
+import java.io.IOException
 import java.nio.charset.Charset
 import java.util.zip.ZipFile
 
 class BluePrintArchiveUtils {
 
     companion object {
+        private val log = LoggerFactory.getLogger(BluePrintArchiveUtils::class.java)
 
         fun getFileContent(fileName: String): String = runBlocking {
             async {
@@ -51,50 +56,20 @@ class BluePrintArchiveUtils {
         /**
          * Create a new Zip from a root directory
          *
-         * @param directory the base directory
-         * @param filename the output filename
+         * @param source the base directory
+         * @param destination the output filename
          * @param absolute store absolute filepath (from directory) or only filename
          * @return True if OK
          */
         fun compress(source: File, destination: File, absolute: Boolean): Boolean {
-            // recursive call
-            val zaos: ZipArchiveOutputStream
             try {
-                zaos = ZipArchiveOutputStream(FileOutputStream(destination))
-            } catch (e: FileNotFoundException) {
-                return false
-            }
-
-            try {
-                recurseFiles(source, source, zaos, absolute)
-            } catch (e2: IOException) {
-                try {
-                    zaos.close()
-                } catch (e: IOException) {
-                    // ignore
+                ZipArchiveOutputStream(destination).use {
+                    recurseFiles(source, source, it, absolute)
                 }
-
+            } catch (e: Exception) {
+                log.error("Fail to compress folder(:$source) to path(${destination.path}", e)
                 return false
             }
-
-            try {
-                zaos.finish()
-            } catch (e1: IOException) {
-                // ignore
-            }
-
-            try {
-                zaos.flush()
-            } catch (e: IOException) {
-                // ignore
-            }
-
-            try {
-                zaos.close()
-            } catch (e: IOException) {
-                // ignore
-            }
-
             return true
         }
 
@@ -113,21 +88,19 @@ class BluePrintArchiveUtils {
             if (file.isDirectory) {
                 // recursive call
                 val files = file.listFiles()
-                for (file2 in files!!) {
-                    recurseFiles(root, file2, zaos, absolute)
+                for (fileChild in files!!) {
+                    recurseFiles(root, fileChild, zaos, absolute)
                 }
             } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) {
-                var filename: String? = null
-                if (absolute) {
-                    filename = file.absolutePath.substring(root.absolutePath.length)
+                val filename = if (absolute) {
+                    file.absolutePath.substring(root.absolutePath.length)
                 } else {
-                    filename = file.name
+                    file.name
                 }
                 val zae = ZipArchiveEntry(filename)
-                zae.setSize(file.length())
+                zae.size = file.length()
                 zaos.putArchiveEntry(zae)
-                val fis = FileInputStream(file)
-                IOUtils.copy(fis, zaos)
+                FileInputStream(file).use { IOUtils.copy(it, zaos) }
                 zaos.closeArchiveEntry()
             }
         }