87ce8d37c32d8acfcc38760dcde0b76d69def1ad
[sdc.git] /
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 org.apache.commons.io.IOUtils;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.tosca.services.YamlUtil;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.zip.ZipEntry;
35 import java.util.zip.ZipInputStream;
36
37 /**
38  * The type File utils.
39  */
40 public class FileUtils {
41
42   /**
43    * Gets file input stream.
44    *
45    * @param fileName the file name
46    * @return the file input stream
47    */
48   public static InputStream getFileInputStream(String fileName) {
49     URL urlFile = FileUtils.class.getClassLoader().getResource(fileName);
50     InputStream is;
51     try {
52       assert urlFile != null;
53       is = urlFile.openStream();
54     } catch (IOException exception) {
55       throw new RuntimeException(exception);
56     }
57     return is;
58   }
59
60   /**
61    * Gets file input stream.
62    *
63    * @param urlFile the url file
64    * @return the file input stream
65    */
66   public static InputStream getFileInputStream(URL urlFile) {
67     InputStream is;
68     try {
69       assert urlFile != null;
70       is = urlFile.openStream();
71     } catch (IOException exception) {
72       throw new RuntimeException(exception);
73     }
74     return is;
75   }
76
77   /**
78    * Gets file input streams.
79    *
80    * @param fileName the file name
81    * @return the file input streams
82    */
83   public static List<InputStream> getFileInputStreams(String fileName) {
84     Enumeration<URL> urlFiles;
85     List<InputStream> streams = new ArrayList<>();
86     InputStream is;
87     URL url;
88     try {
89       urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
90       while (urlFiles.hasMoreElements()) {
91         url = urlFiles.nextElement();
92         is = url.openStream();
93         streams.add(is);
94       }
95
96
97     } catch (IOException exception) {
98       throw new RuntimeException(exception);
99     }
100     return streams;
101   }
102
103   /**
104    * Convert to bytes byte [ ].
105    *
106    * @param object    the object
107    * @param extension the extension
108    * @return the byte [ ]
109    */
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();
114       } else {
115         return JsonUtil.object2Json(object).getBytes();
116       }
117     } else {
118       return new byte[]{};
119     }
120   }
121
122   /**
123    * Convert to input stream input stream.
124    *
125    * @param object    the object
126    * @param extension the extension
127    * @return the input stream
128    */
129   public static InputStream convertToInputStream(Object object, FileExtension extension) {
130     if (object != null) {
131
132       byte[] content;
133
134       if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
135         content = new YamlUtil().objectToYaml(object).getBytes();
136       } else {
137         content = JsonUtil.object2Json(object).getBytes();
138
139       }
140       return new ByteArrayInputStream(content);
141     } else {
142       return null;
143     }
144   }
145
146   /**
147    * Load file to input stream input stream.
148    *
149    * @param fileName the file name
150    * @return the input stream
151    */
152   public static InputStream loadFileToInputStream(String fileName) {
153     URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
154     try {
155       Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
156       while (en.hasMoreElements()) {
157         urlFile = en.nextElement();
158       }
159     } catch (IOException | NullPointerException exception) {
160       throw new RuntimeException(exception);
161     }
162     try {
163       if (urlFile != null) {
164         return urlFile.openStream();
165       } else {
166         throw new RuntimeException();
167       }
168     } catch (IOException | NullPointerException exception) {
169       throw new RuntimeException(exception);
170     }
171
172   }
173
174   /**
175    * To byte array byte [ ].
176    *
177    * @param input the input
178    * @return the byte [ ]
179    */
180   public static byte[] toByteArray(InputStream input) {
181     if (input == null) {
182       return new byte[0];
183     }
184     try {
185       return IOUtils.toByteArray(input);
186     } catch (IOException exception) {
187       throw new RuntimeException(
188           "error will convertion input stream to byte array:" + exception.getMessage());
189     }
190   }
191
192   /**
193    * Gets file without extention.
194    *
195    * @param fileName the file name
196    * @return the file without extention
197    */
198   public static String getFileWithoutExtention(String fileName) {
199     if (!fileName.contains(".")) {
200       return fileName;
201     }
202     return fileName.substring(0, fileName.lastIndexOf("."));
203   }
204
205   /**
206    * Gets file content map from zip.
207    *
208    * @param zipData the zip data
209    * @return the file content map from zip
210    * @throws IOException the io exception
211    */
212   public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
213     ZipEntry zipEntry;
214     FileContentHandler mapFileContent = new FileContentHandler();
215     try {
216       ZipInputStream inputZipStream;
217
218       byte[] fileByteContent;
219       String currentEntryName;
220       inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData));
221
222       while ((zipEntry = inputZipStream.getNextEntry()) != null) {
223         currentEntryName = zipEntry.getName();
224         fileByteContent = FileUtils.toByteArray(inputZipStream);
225         mapFileContent.addFile(currentEntryName, fileByteContent);
226       }
227
228     } catch (RuntimeException exception) {
229       throw new IOException(exception);
230     }
231     return mapFileContent;
232   }
233
234
235   /**
236    * The enum File extension.
237    */
238   public enum FileExtension {
239
240     /**
241      * Json file extension.
242      */
243     JSON("json"),
244     /**
245      * Yaml file extension.
246      */
247     YAML("yaml"),
248     /**
249      * Yml file extension.
250      */
251     YML("yml");
252
253     private String displayName;
254
255     FileExtension(String displayName) {
256       this.displayName = displayName;
257     }
258
259     /**
260      * Gets display name.
261      *
262      * @return the display name
263      */
264     public String getDisplayName() {
265       return displayName;
266     }
267   }
268
269
270 }