2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.utils;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.io.FilenameUtils;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.openecomp.core.utilities.file.FileContentHandler;
28 import org.openecomp.core.utilities.file.FileUtils;
29 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.errors.ErrorCategory;
32 import org.openecomp.sdc.common.errors.ErrorCode;
33 import org.openecomp.sdc.common.errors.Messages;
34 import org.openecomp.sdc.logging.types.LoggerConstants;
35 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
38 import java.io.ByteArrayInputStream;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.HashSet;
44 import java.util.List;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipInputStream;
49 public class CommonUtil {
51 public static FileContentHandler validateAndUploadFileContent(OnboardingTypesEnum type,
52 byte[] uploadedFileData)
54 return getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders(type, uploadedFileData);
58 * Gets files out of the zip AND validates zip is flat (no folders)
62 * @param uploadFileData zip file
63 * @return FileContentHandler if input is valid and has no folders
65 private static FileContentHandler getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders(
66 OnboardingTypesEnum type, byte[] uploadFileData)
68 Pair<FileContentHandler,List<String> > pair = getFileContentMapFromOrchestrationCandidateZip(uploadFileData);
70 if(type.equals(OnboardingTypesEnum.ZIP)) {
71 validateNoFolders(pair.getRight());
74 return pair.getLeft();
77 public static Pair<FileContentHandler,List<String> > getFileContentMapFromOrchestrationCandidateZip(
78 byte[] uploadFileData)
81 List<String> folderList = new ArrayList<>();
82 FileContentHandler mapFileContent = new FileContentHandler();
83 try ( ByteArrayInputStream in = new ByteArrayInputStream(uploadFileData);
84 ZipInputStream inputZipStream = new ZipInputStream(in)){
85 byte[] fileByteContent;
86 String currentEntryName;
88 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
89 currentEntryName = zipEntry.getName();
90 // else, get the file content (as byte array) and save it in a map.
91 fileByteContent = FileUtils.toByteArray(inputZipStream);
93 int index = lastIndexFileSeparatorIndex(currentEntryName);
94 if (index != -1) { //todo ?
95 folderList.add(currentEntryName);
97 if(isFile(currentEntryName)) {
98 mapFileContent.addFile(currentEntryName, fileByteContent);
102 } catch (RuntimeException exception) {
103 throw new IOException(exception);
106 return new ImmutablePair<>(mapFileContent,folderList);
109 private static boolean isFile(String currentEntryName) {
110 return !(currentEntryName.endsWith("\\") || currentEntryName.endsWith("/"));
113 private static void validateNoFolders(List<String> folderList) {
114 if (CollectionUtils.isNotEmpty(folderList)) {
115 MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
116 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
117 .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
118 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
119 .withCategory(ErrorCategory.APPLICATION).build());
123 private static int lastIndexFileSeparatorIndex(String filePath) {
124 int length = filePath.length() - 1;
126 for (int i = length; i >= 0; i--) {
127 char currChar = filePath.charAt(i);
128 if (currChar == '/' || currChar == File.separatorChar || currChar == File.pathSeparatorChar) {
132 // if we've reached to the start of the string and didn't find file separator - return -1
136 public static boolean validateFilesExtensions(Set<String> allowedExtensions, FileContentHandler
138 for (String fileName : files.getFileList()) {
139 if (!allowedExtensions.contains(FilenameUtils.getExtension(fileName))) {
146 public static boolean validateAllFilesYml(FileContentHandler files) {
147 Set<String> allowedExtensions = new HashSet<>(Arrays.asList("yml", "yaml"));
148 return validateFilesExtensions(allowedExtensions, files);