summaryrefslogtreecommitdiffstats
path: root/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java')
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java242
1 files changed, 123 insertions, 119 deletions
diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java
index c2d6048..7d48b06 100644
--- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java
+++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java
@@ -32,127 +32,131 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Map;
-public class CsarUtil {
-
- private static final Logger logger = LoggerFactory.getLogger(CsarUtil.class);
-
- public static String getUnzipDir(String dirName) {
- File tmpDir = new File(File.separator + dirName);
- return tmpDir.getAbsolutePath().replace(".csar", "");
- }
-
- /**
- * unzip zip file.
- *
- * @param zipFileName
- * file name to zip
- * @param extPlace
- * extPlace
- * @return unzip file names in zip
- * @throws IOException
- * e1
- * @throws ValidationException
- */
- public static Map<String, String> unzip(String zipFileName, String extPlace) throws IOException {
- HashMap<String, String> unzipFileNames = new HashMap<>();
-
- try(ZipFile zipFile = new ZipFile(zipFileName)) {
-
- Enumeration<?> fileEn = zipFile.entries();
- byte[] buffer = new byte[CommonConstants.BUFFER_SIZE];
-
- while (fileEn.hasMoreElements()) {
- InputStream input = null;
- BufferedOutputStream bos = null;
- ZipEntry entry = (ZipEntry) fileEn.nextElement();
- if (!entry.isDirectory()) {
- try {
- input = zipFile.getInputStream(entry);
- File file = new File(extPlace, entry.getName());
-
- //Currently it does not support xml based VNF descriptors.
- //So skip and proceed to YAML defined files validation only.
- if (file.getAbsolutePath().contains("xml"+System.getProperty("file.separator"))) {
- continue;
- }
-
- if (!file.getParentFile().exists()) {
- FileUtil.createDirectory(file.getParentFile().getAbsolutePath());
- }
-
- bos = new BufferedOutputStream(new FileOutputStream(file));
- while (true) {
- int length = input.read(buffer);
- if (length == -1) {
- break;
- }
- bos.write(buffer, 0, length);
- }
-
- unzipFileNames.put(file.getName(), file.getAbsolutePath());
-
- } finally {
- closeOutputStream(bos);
- closeInputStream(input);
- }
- }
+public final class CsarUtil {
+
+ private static final Logger logger = LoggerFactory.getLogger(CsarUtil.class);
+
+ private CsarUtil(){
+ //It is made private in order to resolve: Utility classes should not have public constructors
+ }
+ public static String getUnzipDir(String dirName) {
+ File tmpDir = new File(File.separator + dirName);
+ return tmpDir.getAbsolutePath().replace(".csar", "");
+ }
+
+ /**
+ * unzip zip file.
+ *
+ * @param zipFileName
+ * file name to zip
+ * @param extPlace
+ * extPlace
+ * @return unzip file names in zip
+ * @throws IOException
+ * e1
+ * @throws ValidationException
+ */
+ public static Map<String, String> unzip(String zipFileName, String extPlace) throws IOException {
+ HashMap<String, String> unzipFileNames = new HashMap<>();
+ InputStream input = null;
+ try(ZipFile zipFile = new ZipFile(zipFileName)) {
+
+ Enumeration<?> fileEn = zipFile.entries();
+ byte[] buffer = new byte[CommonConstants.BUFFER_SIZE];
+
+ while (fileEn.hasMoreElements()) {
+ ZipEntry entry = (ZipEntry) fileEn.nextElement();
+ if (entry.isDirectory()) {
+ continue;
}
- }
- return unzipFileNames;
- }
-
- /**
- * close InputStream.
- *
- * @param inputStream
- * the inputstream to close
- * @throws ValidationException
- */
- public static void closeInputStream(InputStream inputStream) {
- try {
- if (inputStream != null) {
- inputStream.close();
+
+ input = zipFile.getInputStream(entry);
+ File file = new File(extPlace, entry.getName());
+
+ //Currently it does not support xml based VNF descriptors.
+ //So skip and proceed to YAML defined files validation only.
+ if (file.getAbsolutePath().contains("xml"+System.getProperty("file.separator"))) {
+ continue;
}
- } catch (Exception e1) {
- logger.error("FILE_IO" + ":" + "close InputStream error! " +ErrorCodes.FILE_IO+" "+ e1.getMessage(), e1);
- throw new ValidationException(ErrorCodes.FILE_IO);
- }
- }
-
- /**
- * close OutputStream.
- *
- * @param outputStream
- * the output stream to close
- * @throws ValidationException
- */
- public static void closeOutputStream(OutputStream outputStream) {
- try {
- if (outputStream != null) {
- outputStream.close();
+
+ updateUnzipFileNames(input, buffer, unzipFileNames, file);
+ }
+ } finally {
+ closeInputStream(input);
+ }
+ return unzipFileNames;
+ }
+
+ private static void updateUnzipFileNames(InputStream input, byte[] buffer, HashMap<String, String> unzipFileNames, File file) throws IOException {
+ if (!file.getParentFile().exists()) {
+ FileUtil.createDirectory(file.getParentFile().getAbsolutePath());
+ }
+ try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){
+ while (true) {
+ int length = input.read(buffer);
+ if (length == -1) {
+ break;
}
- } catch (Exception e1) {
- logger.error("FILE_IO" + ":" + "close OutputStream error! " +ErrorCodes.FILE_IO, e1);
- throw new ValidationException(ErrorCodes.FILE_IO);
-
- }
- }
-
- /**
- *
- * @param filePath
- * @return HashMap<String, String>
- */
- public static Map<String, String> csarExtract(String filePath) {
-
- try {
- String tempfolder = CsarUtil.getUnzipDir(filePath);
- return CsarUtil.unzip(filePath, tempfolder);
-
- } catch (IOException e1) {
- logger.error("CSAR_EXTRACTION" + ":" + "CSAR extraction error ! " +ErrorCodes.FILE_IO+" "+ e1.getMessage(), e1);
- throw new ValidationException(ErrorCodes.FILE_IO);
- }
- }
+ bos.write(buffer, 0, length);
+ }
+
+ unzipFileNames.put(file.getName(), file.getAbsolutePath());
+ }
+ }
+
+ /**
+ * close InputStream.
+ *
+ * @param inputStream
+ * the inputstream to close
+ * @throws ValidationException
+ */
+ public static void closeInputStream(InputStream inputStream) {
+ try {
+ if (inputStream != null) {
+ inputStream.close();
+ }
+ } catch (Exception e1) {
+ String errCodeMessage = ErrorCodes.FILE_IO+" "+ e1.getMessage();
+ logger.error("FILE_IO:close InputStream error! {} {}", errCodeMessage, e1);
+ throw new ValidationException(ErrorCodes.FILE_IO);
+ }
+ }
+
+ /**
+ * close OutputStream.
+ *
+ * @param outputStream
+ * the output stream to close
+ * @throws ValidationException
+ */
+ public static void closeOutputStream(OutputStream outputStream) {
+ try {
+ if (outputStream != null) {
+ outputStream.close();
+ }
+ } catch (Exception e1) {
+ logger.error("FILE_IO:close OutputStream error! {} {}", ErrorCodes.FILE_IO, e1);
+ throw new ValidationException(ErrorCodes.FILE_IO);
+
+ }
+ }
+ /**
+ *
+ * @param filePath
+ * @return HashMap<String, String>
+ */
+ public static Map<String, String> csarExtract(String filePath) {
+
+ try {
+ String tempfolder = CsarUtil.getUnzipDir(filePath);
+ return CsarUtil.unzip(filePath, tempfolder);
+
+ } catch (IOException e1) {
+ String errCodeMessage = ErrorCodes.FILE_IO+" "+ e1.getMessage();
+ logger.error("CSAR_EXTRACTION:CSAR extraction error ! {} {}", errCodeMessage, e1);
+ throw new ValidationException(ErrorCodes.FILE_IO);
+ }
+ }
}