8ffddc48d71d6e3a07730d42ace932407494fb41
[sdc.git] / openecomp-be / lib / openecomp-common-lib / src / main / java / org / openecomp / sdc / common / utils / CommonUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.common.utils;
22
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;
32 import org.slf4j.MDC;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.File;
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;
41
42 public class CommonUtil {
43
44   public static String getMethodName() {
45     return Thread.currentThread().getStackTrace()[2].getMethodName();
46   }
47
48   public static FileContentHandler loadUploadFileContent(byte[] uploadedFileData)
49           throws IOException {
50     return getFileContentMapFromOrchestrationCandidateZip(uploadedFileData);
51   }
52
53   public static FileContentHandler getFileContentMapFromOrchestrationCandidateZip(byte[] uploadFileData)
54           throws IOException {
55     ZipEntry zipEntry;
56     List<String> folderList = new ArrayList<>();
57     FileContentHandler mapFileContent = new FileContentHandler();
58     try {
59       ZipInputStream inputZipStream;
60
61       byte[] fileByteContent;
62       String currentEntryName;
63       inputZipStream = new ZipInputStream(new ByteArrayInputStream(uploadFileData));
64
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);
69
70         int index = lastIndexFileSeparatorIndex(currentEntryName);
71         if (index != -1) { //todo ?
72           folderList.add(currentEntryName);
73         } else {
74           mapFileContent.addFile(currentEntryName, fileByteContent);
75         }
76
77       }
78
79     } catch (RuntimeException exception) {
80       throw new IOException(exception);
81     }
82
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());
89     }
90
91     return mapFileContent;
92   }
93
94   private static int lastIndexFileSeparatorIndex(String filePath) {
95     int length = filePath.length() - 1;
96
97     for (int i = length; i >= 0; i--) {
98       char currChar = filePath.charAt(i);
99       if (currChar == '/' || currChar == File.separatorChar || currChar == File.pathSeparatorChar) {
100         return i;
101       }
102     }
103     // if we've reached to the start of the string and didn't find file separator - return -1
104     return -1;
105   }
106 }