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.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.common.errors.ErrorCategory;
29 import org.openecomp.sdc.common.errors.ErrorCode;
30 import org.openecomp.sdc.common.errors.Messages;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
35 import java.io.ByteArrayInputStream;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashSet;
41 import java.util.List;
43 import java.util.zip.ZipEntry;
44 import java.util.zip.ZipInputStream;
46 public class CommonUtil {
48 public static FileContentHandler validateAndUploadFileContent(byte[] uploadedFileData)
50 return getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders(uploadedFileData);
54 * Gets files out of the zip AND validates zip is flat (no folders)
56 * @param uploadFileData zip file
57 * @return FileContentHandler if input is valid and has no folders
59 private static FileContentHandler getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders(
60 byte[] uploadFileData)
63 List<String> folderList = new ArrayList<>();
64 FileContentHandler mapFileContent = new FileContentHandler();
66 ZipInputStream inputZipStream;
68 byte[] fileByteContent;
69 String currentEntryName;
70 inputZipStream = new ZipInputStream(new ByteArrayInputStream(uploadFileData));
72 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
73 currentEntryName = zipEntry.getName();
74 // else, get the file content (as byte array) and save it in a map.
75 fileByteContent = FileUtils.toByteArray(inputZipStream);
77 int index = lastIndexFileSeparatorIndex(currentEntryName);
78 if (index != -1) { //todo ?
79 folderList.add(currentEntryName);
81 mapFileContent.addFile(currentEntryName, fileByteContent);
85 } catch (RuntimeException exception) {
86 throw new IOException(exception);
89 validateNoFolders(folderList);
91 return mapFileContent;
94 private static void validateNoFolders(List<String> folderList) {
95 if (CollectionUtils.isNotEmpty(folderList)) {
96 MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
97 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
98 .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
99 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
100 .withCategory(ErrorCategory.APPLICATION).build());
104 private static int lastIndexFileSeparatorIndex(String filePath) {
105 int length = filePath.length() - 1;
107 for (int i = length; i >= 0; i--) {
108 char currChar = filePath.charAt(i);
109 if (currChar == '/' || currChar == File.separatorChar || currChar == File.pathSeparatorChar) {
113 // if we've reached to the start of the string and didn't find file separator - return -1
117 public static boolean validateFilesExtensions(Set<String> allowedExtensions, FileContentHandler
119 for (String fileName : files.getFileList()) {
120 if (!allowedExtensions.contains(FilenameUtils.getExtension(fileName))) {
127 public static boolean validateAllFilesYml(FileContentHandler files) {
128 Set<String> allowedExtensions = new HashSet<>(Arrays.asList("yml", "yaml"));
129 return validateFilesExtensions(allowedExtensions, files);