c199a0e1f3022c828ef9634ebad1673651d187a2
[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
25 import org.onap.vnfsdk.marketplace.common.HttpServerPathConfig;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
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       if (!new File(destDirName).delete()) {
78         LOGGER.error("failed to delete file in createDestDir()");
79       }
80     } else if (destDir.exists() && !overlay) {
81         return false;
82     }
83
84     return destDir.mkdirs();
85   }
86
87   /**
88    * copy from directory.
89    * @param srcDirName source directory name
90    * @param destDirName destination directory name
91    * @param overlay overwrite or not
92    * @return boolean
93    * @throws IOException e
94    */
95   public static boolean copyDirectory(String srcDirName, String destDirName, boolean overlay)
96       throws IOException {
97     File srcDir = new File(srcDirName);
98     if (!srcDir.exists() || !srcDir.isDirectory()) {
99       return false;
100     }
101
102     String fullDestDirName = destDirName;
103     if (!fullDestDirName.endsWith(File.separator)) {
104       fullDestDirName += File.separator;
105     }
106
107     if (!createDestDir(fullDestDirName, overlay))
108     {
109       return false;
110     }
111
112     return simpleCopyDirectory(srcDirName, fullDestDirName);
113   }
114
115   /**
116    * copy byte.
117    * @param srcFileName source file name
118    * @param destFileName target file name
119    * @return boolean
120    */
121   public static boolean copyByte(File srcFile,  File destFile)
122   {
123     try (
124           InputStream in = new FileInputStream(srcFile);
125           OutputStream out = new FileOutputStream(destFile);
126       ) {
127
128       byte[] buffer = new byte[1024];
129
130       for (int byteread = 0; (byteread = in.read(buffer)) != -1;) {
131         out.write(buffer, 0, byteread);
132       }
133
134       return true;
135     } catch (IOException e) {
136       LOGGER.error("IOException in copyFile", e);
137       return false;
138     }
139   }
140
141   /**
142    * copy file.
143    * @param srcFileName source file name
144    * @param destFileName target file name
145    * @param overlay overwrite or not
146    * @return boolean
147    */
148   public static boolean copyFile(String srcFileName, String destFileName, boolean overlay) {
149     File srcFile = new File(srcFileName);
150
151     if (!srcFile.exists() || !srcFile.isFile()) {
152       String message = "Source file : " + srcFileName + " not exist !";
153       LOGGER.error(message);
154       return false;
155     }
156
157     File destFile = new File(destFileName);
158     if (destFile.exists() && overlay) {
159         if(!new File(destFileName).delete())
160           LOGGER.error("failed to delete file in copyFile()");
161     } else if (!destFile.exists() && !destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
162         return false;
163     }
164
165     return copyByte(srcFile,  destFile);
166   }
167
168   /**
169    * create directory.
170    * @param destDirName target directory name
171    * @return boolean
172    */
173   public static boolean createDir(String destDirName) {
174     String useDestDirName = destDirName;
175     if (!useDestDirName.endsWith(File.separator)) {
176       useDestDirName += File.separator;
177     }
178     File dir = new File(useDestDirName);
179     if (dir.exists()) {
180       if(!dir.delete())
181         LOGGER.error("failed to delete file in createDir()");
182     }
183
184     return dir.mkdirs();
185   }
186
187   public static String getHttpServerAbsolutePath() {
188     return Thread.currentThread().getContextClassLoader().getResource("/").getPath() +  HttpServerPathConfig.getHttpServerPath();
189   }
190
191   /**
192    * delete directory.
193    * @param dir file to delete
194    * @return boolean
195    */
196   public static boolean deleteDir(File dir) {
197     if (dir.isDirectory()) {
198       String[] children = dir.list();
199       for (int i = 0; i < children.length; i++) {
200         boolean success = deleteDir(new File(dir, children[i]));
201         if (!success) {
202           return false;
203         }
204       }
205     }
206     return dir.delete();
207   }
208
209   public static String getAppDeployPath()
210   {
211       return Thread.currentThread().getContextClassLoader().getResource("/").getPath();
212   }
213 }
214