2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.core.utilities.file;
19 import org.apache.commons.io.FilenameUtils;
20 import org.apache.commons.io.IOUtils;
21 import org.onap.sdc.tosca.services.YamlUtil;
22 import org.openecomp.core.utilities.json.JsonUtil;
24 import java.io.ByteArrayInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
30 import java.nio.file.Path;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.HashMap;
34 import java.util.LinkedList;
35 import java.util.List;
37 import java.util.Objects;
38 import java.util.function.Function;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipException;
41 import java.util.zip.ZipInputStream;
44 * The type File utils.
46 public class FileUtils {
49 * Allows to consume an input stream open against a resource with a given file name.
51 * @param fileName the file name
52 * @param function logic to be applied to the input stream
54 public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
56 Objects.requireNonNull(fileName);
58 // the leading slash doesn't make sense and doesn't work when used with a class loader
59 URL resource = FileUtils.class.getClassLoader().getResource(fileName.startsWith("/")
60 ? fileName.substring(1) : fileName);
61 if (resource == null) {
62 throw new IllegalArgumentException("Resource not found: " + fileName);
65 return readViaInputStream(resource, function);
69 * Allows to consume an input stream open against a resource with a given URL.
71 * @param urlFile the url file
72 * @param function logic to be applied to the input stream
74 public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) {
76 Objects.requireNonNull(urlFile);
77 try (InputStream is = urlFile.openStream()) {
78 return function.apply(is);
79 } catch (IOException exception) {
80 throw new RuntimeException(exception);
85 * Gets file input streams.
87 * @param fileName the file name
88 * @return the file input streams
90 public static List<URL> getAllLocations(String fileName) {
92 List<URL> urls = new LinkedList<>();
93 Enumeration<URL> urlFiles;
96 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
97 while (urlFiles.hasMoreElements()) {
98 urls.add(urlFiles.nextElement());
102 } catch (IOException exception) {
103 throw new RuntimeException(exception);
106 return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls);
110 * Convert to bytes byte [ ].
112 * @param object the object
113 * @param extension the extension
114 * @return the byte [ ]
116 public static byte[] convertToBytes(Object object, FileExtension extension) {
117 if (object != null) {
118 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
119 return new YamlUtil().objectToYaml(object).getBytes();
121 return JsonUtil.object2Json(object).getBytes();
129 * Convert to input stream input stream.
131 * @param object the object
132 * @param extension the extension
133 * @return the input stream
135 public static InputStream convertToInputStream(Object object, FileExtension extension) {
136 if (object != null) {
140 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
141 content = new YamlUtil().objectToYaml(object).getBytes();
143 content = JsonUtil.object2Json(object).getBytes();
146 return new ByteArrayInputStream(content);
153 * Load file to input stream input stream.
155 * @param fileName the file name
156 * @return the input stream
158 public static InputStream loadFileToInputStream(String fileName) {
159 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
161 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
162 while (en.hasMoreElements()) {
163 urlFile = en.nextElement();
165 } catch (IOException | NullPointerException exception) {
166 throw new RuntimeException(exception);
169 if (urlFile != null) {
170 return urlFile.openStream();
172 throw new RuntimeException();
174 } catch (IOException | NullPointerException exception) {
175 throw new RuntimeException(exception);
181 * To byte array byte [ ].
183 * @param input the input
184 * @return the byte [ ]
186 public static byte[] toByteArray(InputStream input) {
191 return IOUtils.toByteArray(input);
192 } catch (IOException exception) {
193 throw new RuntimeException(
194 "error while converting input stream to byte array", exception);
199 * Gets file without extention.
201 * @param fileName the file name
202 * @return the file without extention
204 public static String getFileWithoutExtention(String fileName) {
205 if (!fileName.contains(".")) {
208 return fileName.substring(0, fileName.lastIndexOf('.'));
211 public static String getFileExtension(String filename) {
212 return FilenameUtils.getExtension(filename);
215 public static String getNetworkPackageName(String filename) {
216 String[] split = filename.split("\\.");
218 if (split.length > 1) {
225 * Gets file content map from zip.
227 * @param zipData the zip data
228 * @return the file content map from zip
229 * @throws IOException the io exception
231 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
233 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) {
235 FileContentHandler mapFileContent = new FileContentHandler();
239 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
240 assertEntryNotVulnerable(zipEntry);
241 mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream));
244 return mapFileContent;
246 } catch (RuntimeException exception) {
247 throw new IOException(exception);
253 * The enum File extension.
255 public enum FileExtension {
258 * Json file extension.
262 * Yaml file extension.
266 * Yml file extension.
270 private final String displayName;
272 FileExtension(String displayName) {
273 this.displayName = displayName;
279 * @return the display name
281 public String getDisplayName() {
288 * Write files and folders map to disk in the given path
290 * @param fileContentHandler the file content handler
292 * @return a map containing file names and their absolute paths
293 * @throws IOException the io exception
295 public static Map<String, String> writeFilesFromFileContentHandler(FileContentHandler
301 File dirFile = dir.toFile();
302 Map<String, String> filePaths = new HashMap<>();
303 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
304 file = new File(dirFile, fileEntry.getKey());
305 file.getParentFile().mkdirs();
306 filePaths.put(fileEntry.getKey(), file.getAbsolutePath());
307 try (FileOutputStream fop = new FileOutputStream(file.getAbsolutePath());) {
308 fop.write(fileEntry.getValue());
317 * Verify whether the provided extension is valid Yaml/Yml extension or not.
319 * @param fileExtension the file extension
320 * @return the boolean
322 public static boolean isValidYamlExtension(String fileExtension){
323 return fileExtension.equalsIgnoreCase(FileExtension.YML.getDisplayName()) ||
324 fileExtension.equalsIgnoreCase(FileExtension.YAML.getDisplayName());
327 private static void assertEntryNotVulnerable(ZipEntry entry) throws ZipException {
328 if (entry.getName().contains("../")) {
329 throw new ZipException("Path traversal attempt discovered.");