"java.nio.Files#delete" should be preferred
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / filemanage / http / ToolUtil.java
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
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 package org.onap.vnfsdk.marketplace.filemanage.http;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import org.onap.vnfsdk.marketplace.common.HttpServerPathConfig;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29
30 public class ToolUtil {
31   private static final Logger LOGGER = LoggerFactory.getLogger(ToolUtil.class);
32
33   private ToolUtil() {
34   }
35
36   /**
37    * simple copy directory
38    * @param srcDirName source directory name
39    * @param destDirName destination directory name
40    * @param files file list in directory
41    * @param overlay overwrite or not
42    * @return boolean
43    * @throws IOException e
44    */
45   public static boolean simpleCopyDirectory(String srcDirName, String destDirName)
46       throws IOException
47   {
48     File srcDir = new File(srcDirName);
49     File[] files = srcDir.listFiles();
50     for (int i = 0; i < files.length; i++) {
51         boolean flag = false;
52         if (files[i].isFile()) {
53           flag = copyFile(files[i].getAbsolutePath(), destDirName + files[i].getName(), true);
54         }
55         if (files[i].isDirectory()) {
56           flag = simpleCopyDirectory(files[i].getAbsolutePath(), destDirName + files[i].getName());
57         }
58         if (!flag) {
59           String message = "Copy catagory " + srcDirName + " to " + destDirName + " failed!";
60           LOGGER.error(message);
61           return false;
62         }
63      }
64      return true;
65   }
66
67   /**
68    * create destDir
69    * @param destDirName destination directory name
70    * @param overlay overwrite or not
71    * @return boolean
72    */
73   public static boolean createDestDir(String destDirName, boolean overlay)
74   {
75     File destDir = new File(destDirName);
76     if (destDir.exists() && overlay) {
77       String fileAbsPath = destDir.getAbsolutePath();
78       try {
79         Files.delete(Paths.get(destDirName));
80       } catch (IOException e) {
81         LOGGER.error("fail to delete {} {} " , fileAbsPath, e);
82       }
83     } else if (destDir.exists() && !overlay) {
84         return false;
85     }
86
87     return destDir.mkdirs();
88   }
89
90   /**
91    * copy from directory.
92    * @param srcDirName source directory name
93    * @param destDirName destination directory name
94    * @param overlay overwrite or not
95    * @return boolean
96    * @throws IOException e
97    */
98   public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
99       throws IOException {
100     File srcDir = new File(srcDirName);
101     if (!srcDir.exists() || !srcDir.isDirectory()) {
102       return false;
103     }
104
105     String fullDestDirName = destDirName;
106     if (!fullDestDirName.endsWith(File.separator)) {
107       fullDestDirName += File.separator;
108     }
109
110     if (!createDestDir(fullDestDirName, overlay))
111     {
112       return false;
113     }
114
115     return simpleCopyDirectory(srcDirName, fullDestDirName);
116   }
117
118   /**
119    * copy byte.
120    * @param srcFileName source file name
121    * @param destFileName target file name
122    * @return boolean
123    */
124   public static boolean copyByte(File srcFile,  File destFile)
125   {
126     try (
127           InputStream in = new FileInputStream(srcFile);
128           OutputStream out = new FileOutputStream(destFile);
129       ) {
130
131       byte[] buffer = new byte[1024];
132
133       for (int byteread = 0; (byteread = in.read(buffer)) != -1;) {
134         out.write(buffer, 0, byteread);
135       }
136
137       return true;
138     } catch (IOException e) {
139       LOGGER.error("IOException in copyFile", e);
140       return false;
141     }
142   }
143
144   /**
145    * copy file.
146    * @param srcFileName source file name
147    * @param destFileName target file name
148    * @param overlay overwrite or not
149    * @return boolean
150    */
151   public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
152     File srcFile = new File(srcFileName);
153
154     if (!srcFile.exists() || !srcFile.isFile()) {
155       String message = "Source file : " + srcFileName + " not exist !";
156       LOGGER.error(message);
157       return false;
158     }
159
160     File destFile = new File(destFileName);
161     if (destFile.exists() && overlay) {
162       String fileAbsPath = destFile.getAbsolutePath();
163       try {
164         Files.delete(Paths.get(destFileName));
165       } catch (IOException e) {
166         LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
167       }
168     } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
169         return false;
170     }
171
172     return copyByte(srcFile,  destFile);
173   }
174
175   /**
176    * create directory.
177    * @param destDirName target directory name
178    * @return boolean
179    */
180   public static boolean createDir(String destDirName) {
181     String useDestDirName = destDirName;
182     if (!useDestDirName.endsWith(File.separator)) {
183       useDestDirName += File.separator;
184     }
185     File dir = new File(useDestDirName);
186     if (dir.exists()) {
187       String fileAbsPath = dir.getAbsolutePath();
188       try {
189         Files.delete(Paths.get(useDestDirName));
190       } catch (IOException e) {
191         LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
192       }
193     }
194
195     return dir.mkdirs();
196   }
197
198   public static String getHttpServerAbsolutePath() {
199     return Thread.currentThread().getContextClassLoader().getResource("/").getPath() +  HttpServerPathConfig.getHttpServerPath();
200   }
201
202   /**
203    * delete directory.
204    * @param dir file to delete
205    * @return boolean
206    */
207   public static boolean deleteDir(File dir) {
208     if (dir.isDirectory()) {
209       String[] children = dir.list();
210       for (int i = 0; i < children.length; i++) {
211         boolean success = deleteDir(new File(dir, children[i]));
212         if (!success) {
213           return false;
214         }
215       }
216     }
217     boolean isFileDeleted=false;
218     String fileAbsPath = dir.getAbsolutePath();
219     try {
220       Files.delete(Paths.get(dir.getPath()));
221       isFileDeleted=true;
222     } catch (IOException e) {
223       LOGGER.error("fail to delete {} {} ", fileAbsPath, e);
224     }
225     return isFileDeleted;
226   }
227
228   public static String getAppDeployPath()
229   {
230       return Thread.currentThread().getContextClassLoader().getResource("/").getPath();
231   }
232 }
233