diff options
author | vasraz <vasyl.razinkov@est.tech> | 2021-03-22 15:33:06 +0000 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-03-24 06:59:47 +0000 |
commit | d378c37fbd1ecec7b43394926f1ca32a695e07de (patch) | |
tree | 5c8a085f8732f980d871d966ac49361644efa698 /openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java | |
parent | a6ae7294ecd336d7e88f915710b08e2658eaee00 (diff) |
Reformat openecomp-be
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Issue-ID: SDC-3449
Change-Id: I13e02322f8e00820cc5a1d85752caaeda9bf10d1
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 | 188 |
1 files changed, 87 insertions, 101 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 a564928820..66a295eba3 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 @@ -16,7 +16,6 @@ * Modifications copyright (c) 2019 Nokia * ================================================================================ */ - package org.openecomp.sdc.common.utils; import com.google.common.collect.Multimap; @@ -44,118 +43,105 @@ import org.openecomp.sdc.common.zip.exception.ZipException; public class CommonUtil { - private CommonUtil() { - // prevent instantiation - } - - /** - * Reads the files from the zip AND validates zip is flat (no folders). - * - * @param type the onboarding type - * @param uploadedFileData zip file bytes - * @return FileContentHandler if input is valid and has no folders - * @throws IOException when the zip could not be read - */ - public static FileContentHandler validateAndUploadFileContent(final OnboardingTypesEnum type, - final byte[] uploadedFileData) throws IOException { - final Pair<FileContentHandler, List<String>> pair; - try { - pair = getFileContentMapFromOrchestrationCandidateZip(uploadedFileData); - } catch (final ZipException e) { - throw new IOException(e); + private CommonUtil() { + // prevent instantiation } - if (isFileOriginFromZip(type.toString())) { - validateNoFolders(pair.getRight()); - } - - return pair.getLeft(); - } - - /** - * Extracts the zip in memory and build a pair of {@link FileContentHandler} and the zip folder list. The {@link - * FileContentHandler} will only contain the files, not the folders. - * - * @param uploadFileData the zip file to extract - * @return a pair of {@link FileContentHandler} only with the zip files and a list of the zip folders. - * @throws ZipException when there was a problem during the zip reading - */ - public static Pair<FileContentHandler, List<String>> getFileContentMapFromOrchestrationCandidateZip( - byte[] uploadFileData) throws ZipException { - final Map<String, byte[]> zipFileMap = ZipUtils.readZip(uploadFileData, true); - final List<String> folderList = new ArrayList<>(); - final FileContentHandler mapFileContent = new FileContentHandler(); - - zipFileMap.forEach((key, value) -> { - if (value == null) { - folderList.add(key); - } else { - mapFileContent.addFile(key, value); - } - }); - - return new ImmutablePair<>(mapFileContent, folderList); - } - /** - * Extracts the zip in memory and build the {@link FileContentHandler}. - * - * @param zipFile the zip file to extract - * @return The {@link FileContentHandler} based on the zip content - * @throws ZipException when there was a problem during the zip reading - */ - public static FileContentHandler getZipContent(final byte[] zipFile) throws ZipException { - final Map<String, byte[]> zipFileMap = ZipUtils.readZip(zipFile, true); - final FileContentHandler fileContentHandler = new FileContentHandler(); - zipFileMap.forEach((key, value) -> { - if (value == null) { - fileContentHandler.addFolder(key); - } else { - fileContentHandler.addFile(key, value); - } - }); - return fileContentHandler; - } + /** + * Reads the files from the zip AND validates zip is flat (no folders). + * + * @param type the onboarding type + * @param uploadedFileData zip file bytes + * @return FileContentHandler if input is valid and has no folders + * @throws IOException when the zip could not be read + */ + public static FileContentHandler validateAndUploadFileContent(final OnboardingTypesEnum type, final byte[] uploadedFileData) throws IOException { + final Pair<FileContentHandler, List<String>> pair; + try { + pair = getFileContentMapFromOrchestrationCandidateZip(uploadedFileData); + } catch (final ZipException e) { + throw new IOException(e); + } + if (isFileOriginFromZip(type.toString())) { + validateNoFolders(pair.getRight()); + } + return pair.getLeft(); + } - private static void validateNoFolders(List<String> folderList) { - if (CollectionUtils.isNotEmpty(folderList)) { - throw new CoreException((new ErrorCode.ErrorCodeBuilder()) - .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()) - .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()) - .withCategory(ErrorCategory.APPLICATION).build()); + /** + * Extracts the zip in memory and build a pair of {@link FileContentHandler} and the zip folder list. The {@link FileContentHandler} will only + * contain the files, not the folders. + * + * @param uploadFileData the zip file to extract + * @return a pair of {@link FileContentHandler} only with the zip files and a list of the zip folders. + * @throws ZipException when there was a problem during the zip reading + */ + public static Pair<FileContentHandler, List<String>> getFileContentMapFromOrchestrationCandidateZip(byte[] uploadFileData) throws ZipException { + final Map<String, byte[]> zipFileMap = ZipUtils.readZip(uploadFileData, true); + final List<String> folderList = new ArrayList<>(); + final FileContentHandler mapFileContent = new FileContentHandler(); + zipFileMap.forEach((key, value) -> { + if (value == null) { + folderList.add(key); + } else { + mapFileContent.addFile(key, value); + } + }); + return new ImmutablePair<>(mapFileContent, folderList); } - } - private static boolean validateFilesExtensions(Set<String> allowedExtensions, FileContentHandler - files) { - for (String fileName : files.getFileList()) { - if (!allowedExtensions.contains(FilenameUtils.getExtension(fileName))) { - return false; - } + /** + * Extracts the zip in memory and build the {@link FileContentHandler}. + * + * @param zipFile the zip file to extract + * @return The {@link FileContentHandler} based on the zip content + * @throws ZipException when there was a problem during the zip reading + */ + public static FileContentHandler getZipContent(final byte[] zipFile) throws ZipException { + final Map<String, byte[]> zipFileMap = ZipUtils.readZip(zipFile, true); + final FileContentHandler fileContentHandler = new FileContentHandler(); + zipFileMap.forEach((key, value) -> { + if (value == null) { + fileContentHandler.addFolder(key); + } else { + fileContentHandler.addFile(key, value); + } + }); + return fileContentHandler; } - return true; - } - public static boolean validateAllFilesYml(FileContentHandler files) { - Set<String> allowedExtensions = new HashSet<>(Arrays.asList("yml", "yaml")); - return validateFilesExtensions(allowedExtensions, files); - } + private static void validateNoFolders(List<String> folderList) { + if (CollectionUtils.isNotEmpty(folderList)) { + throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()) + .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()).withCategory(ErrorCategory.APPLICATION).build()); + } + } - public static boolean isFileOriginFromZip(String fileOrigin) { - return Objects.nonNull(fileOrigin) - && fileOrigin.equalsIgnoreCase(OnboardingTypesEnum.ZIP.toString()); - } + private static boolean validateFilesExtensions(Set<String> allowedExtensions, FileContentHandler files) { + for (String fileName : files.getFileList()) { + if (!allowedExtensions.contains(FilenameUtils.getExtension(fileName))) { + return false; + } + } + return true; + } + public static boolean validateAllFilesYml(FileContentHandler files) { + Set<String> allowedExtensions = new HashSet<>(Arrays.asList("yml", "yaml")); + return validateFilesExtensions(allowedExtensions, files); + } - public static <T> Optional<T> createObjectUsingSetters(Object objectCandidate, - Class<T> classToCreate) - throws Exception { - return org.onap.sdc.tosca.services.CommonUtil.createObjectUsingSetters(objectCandidate, classToCreate); - } + public static boolean isFileOriginFromZip(String fileOrigin) { + return Objects.nonNull(fileOrigin) && fileOrigin.equalsIgnoreCase(OnboardingTypesEnum.ZIP.toString()); + } + public static <T> Optional<T> createObjectUsingSetters(Object objectCandidate, Class<T> classToCreate) throws Exception { + return org.onap.sdc.tosca.services.CommonUtil.createObjectUsingSetters(objectCandidate, classToCreate); + } - public static Map<String, Object> getObjectAsMap(Object obj) { - return org.onap.sdc.tosca.services.CommonUtil.getObjectAsMap(obj); - } + public static Map<String, Object> getObjectAsMap(Object obj) { + return org.onap.sdc.tosca.services.CommonUtil.getObjectAsMap(obj); + } public static <K, V> boolean isMultimapEmpty(Multimap<K, V> obj) { return Objects.isNull(obj) || obj.isEmpty(); |