diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java')
-rw-r--r-- | openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java index 07322c626b..1c5293004d 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java @@ -22,8 +22,11 @@ package org.openecomp.sdc.common.utils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; @@ -45,29 +48,42 @@ import java.util.zip.ZipInputStream; public class CommonUtil { - public static FileContentHandler validateAndUploadFileContent(byte[] uploadedFileData) + public static FileContentHandler validateAndUploadFileContent(OnboardingTypesEnum type, + byte[] uploadedFileData) throws IOException { - return getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders(uploadedFileData); + return getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders(type, uploadedFileData); } /** * Gets files out of the zip AND validates zip is flat (no folders) * + * + * @param type * @param uploadFileData zip file * @return FileContentHandler if input is valid and has no folders */ private static FileContentHandler getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders( - byte[] uploadFileData) + OnboardingTypesEnum type, byte[] uploadFileData) throws IOException { + Pair<FileContentHandler,List<String> > pair = getFileContentMapFromOrchestrationCandidateZip(uploadFileData); + + if(type.equals(OnboardingTypesEnum.ZIP)) { + validateNoFolders(pair.getRight()); + } + + return pair.getLeft(); + } + + public static Pair<FileContentHandler,List<String> > getFileContentMapFromOrchestrationCandidateZip( + byte[] uploadFileData) + throws IOException { ZipEntry zipEntry; List<String> folderList = new ArrayList<>(); FileContentHandler mapFileContent = new FileContentHandler(); - try { - ZipInputStream inputZipStream; - + try ( ByteArrayInputStream in = new ByteArrayInputStream(uploadFileData); + ZipInputStream inputZipStream = new ZipInputStream(in)){ byte[] fileByteContent; String currentEntryName; - inputZipStream = new ZipInputStream(new ByteArrayInputStream(uploadFileData)); while ((zipEntry = inputZipStream.getNextEntry()) != null) { currentEntryName = zipEntry.getName(); @@ -77,7 +93,8 @@ public class CommonUtil { int index = lastIndexFileSeparatorIndex(currentEntryName); if (index != -1) { //todo ? folderList.add(currentEntryName); - } else { + } + if(isFile(currentEntryName)) { mapFileContent.addFile(currentEntryName, fileByteContent); } } @@ -86,9 +103,11 @@ public class CommonUtil { throw new IOException(exception); } - validateNoFolders(folderList); + return new ImmutablePair<>(mapFileContent,folderList); + } - return mapFileContent; + private static boolean isFile(String currentEntryName) { + return !(currentEntryName.endsWith("\\") || currentEntryName.endsWith("/")); } private static void validateNoFolders(List<String> folderList) { |