Plugin to Generate Service ETSI NSD CSAR
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / main / java / org / openecomp / core / utilities / file / FileContentHandler.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.core.utilities.file;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.MapUtils;
31
32 /**
33  * Stores the content of files in a path:byte[] structure
34  */
35 public class FileContentHandler {
36
37     private final Map<String, byte[]> files = new HashMap<>();
38
39     public FileContentHandler() {
40     }
41
42     public FileContentHandler(final FileContentHandler other) {
43         addAll(other);
44     }
45
46     /**
47      * Gets file content as stream.
48      *
49      * @param fileName the file name
50      * @return if the file was found, its content as stream, otherwise {@code null}.
51      */
52     public InputStream getFileContentAsStream(final String fileName) {
53         byte[] content = files.get(fileName);
54         if (content == null || content.length == 0) {
55             return null;
56         }
57
58         return new ByteArrayInputStream(content);
59     }
60
61     /**
62      * Gets the content of a file.
63      *
64      * @param filePath the file path
65      * @return the content of the file
66      */
67     public byte[] getFileContent(final String filePath) {
68         return files.get(filePath);
69     }
70
71     /**
72      * Checks if the path is a folder.
73      *
74      * @param filePath the file path to verify
75      * @return {@code true} if the path is a folder, {@code false} otherwise
76      */
77     public boolean isFolder(final String filePath) {
78         return files.get(filePath) == null;
79     }
80
81     /**
82      * Checks if the path is a file.
83      *
84      * @param filePath the file path to verify
85      * @return {@code true} if the path is a file, {@code false} otherwise
86      */
87     public boolean isFile(final String filePath) {
88         return files.get(filePath) != null;
89     }
90
91     /**
92      * Adds a folder.
93      *
94      * @param folderPath the folder path to add
95      */
96     public void addFolder(final String folderPath) {
97         files.put(folderPath, null);
98     }
99
100     /**
101      * Adds a file.
102      *
103      * @param filePath the file path
104      * @param content the file content
105      */
106     public void addFile(final String filePath, final byte[] content) {
107         files.put(filePath, content == null ? new byte[0] : content);
108     }
109
110     /**
111      * Adds a file.
112      *
113      * @param filePath the file path
114      * @param fileInputStream the file input stream
115      */
116     public void addFile(final String filePath, final InputStream fileInputStream) {
117         files.put(filePath, FileUtils.toByteArray(fileInputStream));
118     }
119
120     /**
121      * Gets only the files, ignoring directories from the structure.
122      *
123      * @return a file path:content map
124      */
125     public Map<String, byte[]> getFiles() {
126         return files.entrySet().stream().filter(entry -> entry.getValue() != null)
127             .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
128     }
129
130     public void setFiles(final Map<String, byte[]> files) {
131         addAll(files);
132     }
133
134     /**
135      * Gets only the file paths, ignoring directories from the structure.
136      *
137      * @return a set of the file paths
138      */
139     public Set<String> getFileList() {
140         return files.keySet().stream().filter(this::isFile).collect(Collectors.toSet());
141     }
142
143     /**
144      * Gets only the folder paths, ignoring files from the structure.
145      *
146      * @return a set of the folder paths
147      */
148     public Set<String> getFolderList() {
149         return files.keySet().stream().filter(this::isFolder).collect(Collectors.toSet());
150     }
151
152     public void addAll(final FileContentHandler fileContentHandlerOther) {
153         if (CollectionUtils.isNotEmpty(fileContentHandlerOther.getFolderList())) {
154             fileContentHandlerOther.getFolderList().forEach(this::addFolder);
155         }
156         addAll(fileContentHandlerOther.getFiles());
157     }
158
159     private void addAll(final Map<String, byte[]> files) {
160         if (!MapUtils.isEmpty(files)) {
161             files.forEach(this::addFile);
162         }
163     }
164
165     /**
166      * Checks if the file structure is empty.
167      *
168      * @return {@code true} if the file structure is empty, {@code false} otherwise
169      */
170     public boolean isEmpty() {
171         return MapUtils.isEmpty(this.files);
172     }
173
174     /**
175      * Removes a file or folder from the file structure.
176      *
177      * @param filePath the file path to remove
178      * @return the removed file content
179      */
180     public byte[] remove(final String filePath) {
181         return files.remove(filePath);
182     }
183
184     /**
185      * Checks if the file structure contains the provided file.
186      *
187      * @param filePath the file path to search
188      * @return {@code true} if the file exists, {@code false} otherwise
189      */
190     public boolean containsFile(final String filePath) {
191         return files.containsKey(filePath);
192     }
193
194 }