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.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.common.errors.ErrorCategory;
28 import org.openecomp.sdc.common.errors.ErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.common.errors.Messages;
34 import java.io.ByteArrayInputStream;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipInputStream;
42 public class CommonUtil {
44 public static String getMethodName() {
45 return Thread.currentThread().getStackTrace()[2].getMethodName();
48 public static FileContentHandler loadUploadFileContent(byte[] uploadedFileData)
50 return getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
53 public static FileContentHandler getFileContentMapFromOrchestrationCandidateZip(byte[] uploadFileData)
56 List<String> folderList = new ArrayList<>();
57 FileContentHandler mapFileContent = new FileContentHandler();
59 ZipInputStream inputZipStream;
61 byte[] fileByteContent;
62 String currentEntryName;
63 inputZipStream = new ZipInputStream(new ByteArrayInputStream(uploadFileData));
65 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
66 currentEntryName = zipEntry.getName();
67 // else, get the file content (as byte array) and save it in a map.
68 fileByteContent = FileUtils.toByteArray(inputZipStream);
70 int index = lastIndexFileSeparatorIndex(currentEntryName);
71 if (index != -1) { //todo ?
72 folderList.add(currentEntryName);
74 mapFileContent.addFile(currentEntryName, fileByteContent);
79 } catch (RuntimeException exception) {
80 throw new IOException(exception);
83 if (CollectionUtils.isNotEmpty(folderList)) {
84 MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP);
85 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
86 .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
87 .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage())
88 .withCategory(ErrorCategory.APPLICATION).build());
91 return mapFileContent;
94 private static int lastIndexFileSeparatorIndex(String filePath) {
95 int length = filePath.length() - 1;
97 for (int i = length; i >= 0; i--) {
98 char currChar = filePath.charAt(i);
99 if (currChar == '/' || currChar == File.separatorChar || currChar == File.pathSeparatorChar) {
103 // if we've reached to the start of the string and didn't find file separator - return -1