Remove the useless judgement
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / DownloadCsarManager.java
1 /*
2  * Copyright 2016-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
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common;
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.util.Enumeration;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27
28 import org.apache.http.Header;
29 import org.apache.http.HeaderElement;
30 import org.apache.http.HttpEntity;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.NameValuePair;
33 import org.apache.http.client.methods.CloseableHttpResponse;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.apache.http.impl.client.HttpClients;
37 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Utility class to download CSAR
43  *
44  * @author
45  * @version VFC 1.0 Sep 5, 2016
46  */
47 public class DownloadCsarManager {
48
49     private static final Logger LOG = LoggerFactory.getLogger(DownloadCsarManager.class);
50
51     public static final int CACHE = 100 * 1024;
52
53     private DownloadCsarManager() {
54         // private constructor
55     }
56
57     /**
58      * Download from given URL.
59      * 
60      * @param url String
61      * @return
62      */
63     public static String download(String url) {
64         return download(url, null);
65     }
66
67     /**
68      * Download from given URL to given file location.
69      * 
70      * @param url String
71      * @param filepath String
72      * @return
73      */
74     public static String download(String url, String filepath) {
75         String status = "";
76         try {
77             CloseableHttpClient client = HttpClients.createDefault();
78             HttpGet httpget = new HttpGet(url);
79             CloseableHttpResponse response = client.execute(httpget);
80
81             HttpEntity entity = response.getEntity();
82             InputStream is = entity.getContent();
83             if(filepath == null) {
84                 filepath = getFilePath(response); // NOSONAR
85             }
86
87             File file = new File(filepath);
88             file.getParentFile().mkdirs();
89             FileOutputStream fileout = new FileOutputStream(file);
90
91             byte[] buffer = new byte[CACHE];
92             int ch;
93             while((ch = is.read(buffer)) != -1) {
94                 fileout.write(buffer, 0, ch);
95             }
96             is.close();
97             fileout.flush();
98             fileout.close();
99             client.close();
100             status = Constant.DOWNLOADCSAR_SUCCESS;
101
102         } catch(Exception e) {
103             status = Constant.DOWNLOADCSAR_FAIL;
104             LOG.error("Download csar file failed! " + e.getMessage(), e);
105         }
106         return status;
107     }
108
109     /**
110      * Retrieve file path from given response.
111      * 
112      * @param response HttpResponse
113      * @return
114      */
115     public static String getFilePath(HttpResponse response) {
116         String filepath = System.getProperty("java.home");
117         String filename = getFileName(response);
118
119         if(filename != null) {
120             filepath += filename;
121         } else {
122             filepath += getRandomFileName();
123         }
124         return filepath;
125     }
126
127     /**
128      * Retrieve file name from given response.
129      * 
130      * @param response HttpResponse
131      * @return
132      */
133     public static String getFileName(HttpResponse response) {
134         Header contentHeader = response.getFirstHeader("Content-Disposition");
135         String filename = null;
136         if(contentHeader != null) {
137             HeaderElement[] values = contentHeader.getElements();
138             if(values.length == 1) {
139                 NameValuePair param = values[0].getParameterByName("filename");
140                 if(param != null) {
141                     try {
142                         filename = param.getValue();
143                     } catch(Exception e) {
144                         LOG.error("getting filename failed! " + e.getMessage(), e);
145                     }
146                 }
147             }
148         }
149         return filename;
150     }
151
152     /**
153      * Provides random file name.
154      * 
155      * @return
156      */
157     public static String getRandomFileName() {
158         return String.valueOf(System.currentTimeMillis());
159     }
160
161     /**
162      * unzip CSAR packge
163      * 
164      * @param fileName filePath
165      * @return
166      */
167     public static int unzipCSAR(String fileName, String filePath) {
168         final int BUFFER = 2048;
169         int status = 0;
170
171         try {
172             ZipFile zipFile = new ZipFile(fileName);
173             Enumeration emu = zipFile.entries();
174             int i = 0;
175             while(emu.hasMoreElements()) {
176                 ZipEntry entry = (ZipEntry)emu.nextElement();
177                 // read directory as file first,so only need to create directory
178                 if(entry.isDirectory()) {
179                     new File(filePath + entry.getName()).mkdirs();
180                     continue;
181                 }
182                 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
183                 File file = new File(filePath + entry.getName());
184                 // Because that is random to read zipfile,maybe the file is read first
185                 // before the directory is read,so we need to create directory first.
186                 File parent = file.getParentFile();
187                 if(parent != null && (!parent.exists())) {
188                     parent.mkdirs();
189                 }
190                 FileOutputStream fos = new FileOutputStream(file);
191                 BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
192
193                 int count;
194                 byte data[] = new byte[BUFFER];
195                 while((count = bis.read(data, 0, BUFFER)) != -1) {
196                     bos.write(data, 0, count);
197                 }
198                 bos.flush();
199                 bos.close();
200                 bis.close();
201             }
202             status = Constant.UNZIP_SUCCESS;
203             zipFile.close();
204         } catch(Exception e) {
205             status = Constant.UNZIP_FAIL;
206             e.printStackTrace();
207         }
208         return status;
209     }
210 }