94a5408446f575f5c0ab2da12e451df9eb1c1c91
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.core.utilities.file;
18
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;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.URL;
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;
36 import java.util.Map;
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;
42
43 /**
44  * The type File utils.
45  */
46 public class FileUtils {
47
48   /**
49    * Allows to consume an input stream open against a resource with a given file name.
50    *
51    * @param fileName the file name
52    * @param function logic to be applied to the input stream
53    */
54   public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
55
56     Objects.requireNonNull(fileName);
57
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);
63     }
64
65     return readViaInputStream(resource, function);
66   }
67
68   /**
69    * Allows to consume an input stream open against a resource with a given URL.
70    *
71    * @param urlFile the url file
72    * @param function logic to be applied to the input stream
73    */
74   public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) {
75
76     Objects.requireNonNull(urlFile);
77     try (InputStream is = urlFile.openStream()) {
78       return function.apply(is);
79     } catch (IOException exception) {
80       throw new RuntimeException(exception);
81     }
82   }
83
84   /**
85    * Gets file input streams.
86    *
87    * @param fileName the file name
88    * @return the file input streams
89    */
90   public static List<URL> getAllLocations(String fileName) {
91
92     List<URL> urls = new LinkedList<>();
93     Enumeration<URL> urlFiles;
94
95     try {
96       urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
97       while (urlFiles.hasMoreElements()) {
98         urls.add(urlFiles.nextElement());
99       }
100
101
102     } catch (IOException exception) {
103       throw new RuntimeException(exception);
104     }
105
106     return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls);
107   }
108
109   /**
110    * Convert to bytes byte [ ].
111    *
112    * @param object    the object
113    * @param extension the extension
114    * @return the byte [ ]
115    */
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();
120       } else {
121         return JsonUtil.object2Json(object).getBytes();
122       }
123     } else {
124       return new byte[]{};
125     }
126   }
127
128   /**
129    * Convert to input stream input stream.
130    *
131    * @param object    the object
132    * @param extension the extension
133    * @return the input stream
134    */
135   public static InputStream convertToInputStream(Object object, FileExtension extension) {
136     if (object != null) {
137
138       byte[] content;
139
140       if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
141         content = new YamlUtil().objectToYaml(object).getBytes();
142       } else {
143         content = JsonUtil.object2Json(object).getBytes();
144
145       }
146       return new ByteArrayInputStream(content);
147     } else {
148       return null;
149     }
150   }
151
152   /**
153    * Load file to input stream input stream.
154    *
155    * @param fileName the file name
156    * @return the input stream
157    */
158   public static InputStream loadFileToInputStream(String fileName) {
159     URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
160     try {
161       Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
162       while (en.hasMoreElements()) {
163         urlFile = en.nextElement();
164       }
165     } catch (IOException | NullPointerException exception) {
166       throw new RuntimeException(exception);
167     }
168     try {
169       if (urlFile != null) {
170         return urlFile.openStream();
171       } else {
172         throw new RuntimeException();
173       }
174     } catch (IOException | NullPointerException exception) {
175       throw new RuntimeException(exception);
176     }
177
178   }
179
180   /**
181    * To byte array byte [ ].
182    *
183    * @param input the input
184    * @return the byte [ ]
185    */
186   public static byte[] toByteArray(InputStream input) {
187     if (input == null) {
188       return new byte[0];
189     }
190     try {
191       return IOUtils.toByteArray(input);
192     } catch (IOException exception) {
193       throw new RuntimeException(
194           "error while converting input stream to byte array", exception);
195     }
196   }
197
198   /**
199    * Gets file without extention.
200    *
201    * @param fileName the file name
202    * @return the file without extention
203    */
204   public static String getFileWithoutExtention(String fileName) {
205     if (!fileName.contains(".")) {
206       return fileName;
207     }
208     return fileName.substring(0, fileName.lastIndexOf('.'));
209   }
210
211   public static String getFileExtension(String filename) {
212       return FilenameUtils.getExtension(filename);
213   }
214
215   public static String getNetworkPackageName(String filename) {
216     String[] split = filename.split("\\.");
217     String name = null;
218     if (split.length > 1) {
219       name = split[0];
220     }
221     return name;
222   }
223
224   /**
225    * Gets file content map from zip.
226    *
227    * @param zipData the zip data
228    * @return the file content map from zip
229    * @throws IOException the io exception
230    */
231   public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
232
233     try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) {
234
235       FileContentHandler mapFileContent = new FileContentHandler();
236
237       ZipEntry zipEntry;
238
239       while ((zipEntry = inputZipStream.getNextEntry()) != null) {
240         assertEntryNotVulnerable(zipEntry);
241         mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream));
242       }
243
244       return mapFileContent;
245
246     } catch (RuntimeException exception) {
247       throw new IOException(exception);
248     }
249   }
250
251
252   /**
253    * The enum File extension.
254    */
255   public enum FileExtension {
256
257     /**
258      * Json file extension.
259      */
260     JSON("json"),
261     /**
262      * Yaml file extension.
263      */
264     YAML("yaml"),
265     /**
266      * Yml file extension.
267      */
268     YML("yml");
269
270     private final String displayName;
271
272     FileExtension(String displayName) {
273       this.displayName = displayName;
274     }
275
276     /**
277      * Gets display name.
278      *
279      * @return the display name
280      */
281     public String getDisplayName() {
282       return displayName;
283     }
284   }
285
286
287   /**
288    * Write files and folders map to disk in the given path
289    *
290    * @param fileContentHandler the file content handler
291    * @param dir                the dir
292    * @return a map containing file names and their absolute paths
293    * @throws IOException the io exception
294    */
295   public static Map<String, String> writeFilesFromFileContentHandler(FileContentHandler
296                                                                          fileContentHandler,
297                                                                      Path dir)
298       throws IOException {
299
300     File file;
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());
309         fop.flush();
310       }
311     }
312
313     return filePaths;
314   }
315
316   /**
317    * Verify whether the provided extension is valid Yaml/Yml extension or not.
318    *
319    * @param fileExtension the file extension
320    * @return the boolean
321    */
322   public static boolean isValidYamlExtension(String fileExtension){
323     return fileExtension.equalsIgnoreCase(FileExtension.YML.getDisplayName()) ||
324         fileExtension.equalsIgnoreCase(FileExtension.YAML.getDisplayName());
325   }
326
327   private static void assertEntryNotVulnerable(ZipEntry entry) throws ZipException {
328     if (entry.getName().contains("../")) {
329       throw new ZipException("Path traversal attempt discovered.");
330     }
331   }
332
333 }