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.function.Function;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipException;
34 import java.util.zip.ZipInputStream;
38 * The type File utils.
40 public class FileUtils {
43 * Allows to consume an input stream open against a resource with a given file name.
45 * @param fileName the file name
46 * @param function logic to be applied to the input stream
48 public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
50 Objects.requireNonNull(fileName);
52 // the leading slash doesn't make sense and doesn't work when used with a class loader
53 URL resource = FileUtils.class.getClassLoader().getResource(fileName.startsWith("/")
54 ? fileName.substring(1) : fileName);
55 if (resource == null) {
56 throw new IllegalArgumentException("Resource not found: " + fileName);
59 return readViaInputStream(resource, function);
63 * Allows to consume an input stream open against a resource with a given URL.
65 * @param urlFile the url file
66 * @param function logic to be applied to the input stream
68 public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) {
70 Objects.requireNonNull(urlFile);
71 try (InputStream is = urlFile.openStream()) {
72 return function.apply(is);
73 } catch (IOException exception) {
74 throw new RuntimeException(exception);
79 * Gets file input streams.
81 * @param fileName the file name
82 * @return the file input streams
84 public static List<URL> getAllLocations(String fileName) {
86 List<URL> urls = new LinkedList<>();
87 Enumeration<URL> urlFiles;
90 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
91 while (urlFiles.hasMoreElements()) {
92 urls.add(urlFiles.nextElement());
96 } catch (IOException exception) {
97 throw new RuntimeException(exception);
100 return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls);
104 * Convert to bytes byte [ ].
106 * @param object the object
107 * @param extension the extension
108 * @return the byte [ ]
110 public static byte[] convertToBytes(Object object, FileExtension extension) {
111 if (object != null) {
112 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
113 return new YamlUtil().objectToYaml(object).getBytes();
115 return JsonUtil.object2Json(object).getBytes();
123 * Convert to input stream input stream.
125 * @param object the object
126 * @param extension the extension
127 * @return the input stream
129 public static InputStream convertToInputStream(Object object, FileExtension extension) {
130 if (object != null) {
134 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
135 content = new YamlUtil().objectToYaml(object).getBytes();
137 content = JsonUtil.object2Json(object).getBytes();
140 return new ByteArrayInputStream(content);
147 * Load file to input stream input stream.
149 * @param fileName the file name
150 * @return the input stream
152 public static InputStream loadFileToInputStream(String fileName) {
153 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
155 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
156 while (en.hasMoreElements()) {
157 urlFile = en.nextElement();
159 } catch (IOException | NullPointerException exception) {
160 throw new RuntimeException(exception);
163 if (urlFile != null) {
164 return urlFile.openStream();
166 throw new RuntimeException();
168 } catch (IOException | NullPointerException exception) {
169 throw new RuntimeException(exception);
175 * To byte array byte [ ].
177 * @param input the input
178 * @return the byte [ ]
180 public static byte[] toByteArray(InputStream input) {
185 return IOUtils.toByteArray(input);
186 } catch (IOException exception) {
187 throw new RuntimeException(
188 "error while converting input stream to byte array", exception);
193 * Gets file without extention.
195 * @param fileName the file name
196 * @return the file without extention
198 public static String getFileWithoutExtention(String fileName) {
199 if (!fileName.contains(".")) {
202 return fileName.substring(0, fileName.lastIndexOf('.'));
205 public static String getFileExtension(String filename) {
206 return FilenameUtils.getExtension(filename);
209 public static String getNetworkPackageName(String filename) {
210 String[] split = filename.split("\\.");
212 if (split.length > 1) {
219 * Gets file content map from zip.
221 * @param zipData the zip data
222 * @return the file content map from zip
223 * @throws IOException the io exception
225 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
227 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) {
229 FileContentHandler mapFileContent = new FileContentHandler();
233 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
234 assertEntryNotVulnerable(zipEntry);
235 mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream));
238 return mapFileContent;
240 } catch (RuntimeException exception) {
241 throw new IOException(exception);
247 * The enum File extension.
249 public enum FileExtension {
252 * Json file extension.
256 * Yaml file extension.
260 * Yml file extension.
264 private final String displayName;
266 FileExtension(String displayName) {
267 this.displayName = displayName;
273 * @return the display name
275 public String getDisplayName() {
282 * Write files and folders map to disk in the given path
284 * @param fileContentHandler the file content handler
286 * @return a map containing file names and their absolute paths
287 * @throws IOException the io exception
289 public static Map<String, String> writeFilesFromFileContentHandler(FileContentHandler
295 File dirFile = dir.toFile();
296 Map<String, String> filePaths = new HashMap<>();
297 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
298 file = new File(dirFile, fileEntry.getKey());
299 file.getParentFile().mkdirs();
300 filePaths.put(fileEntry.getKey(), file.getAbsolutePath());
301 try (FileOutputStream fop = new FileOutputStream(file.getAbsolutePath());) {
302 fop.write(fileEntry.getValue());
311 * Verify whether the provided extension is valid Yaml/Yml extension or not.
313 * @param fileExtension the file extension
314 * @return the boolean
316 public static boolean isValidYamlExtension(String fileExtension){
317 return fileExtension.equalsIgnoreCase(FileExtension.YML.getDisplayName()) ||
318 fileExtension.equalsIgnoreCase(FileExtension.YAML.getDisplayName());
321 private static void assertEntryNotVulnerable(ZipEntry entry) throws ZipException {
322 if (entry.getName().contains("../")) {
323 throw new ZipException("Path traversal attempt discovered.");