2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common;
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Enumeration;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipFile;
29 import org.apache.http.Header;
30 import org.apache.http.HeaderElement;
31 import org.apache.http.HttpEntity;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.NameValuePair;
34 import org.apache.http.client.methods.CloseableHttpResponse;
35 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.impl.client.CloseableHttpClient;
37 import org.apache.http.impl.client.HttpClients;
38 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Utility class to download CSAR
46 * @version VFC 1.0 Sep 5, 2016
48 public class DownloadCsarManager {
50 private static final Logger LOG = LoggerFactory.getLogger(DownloadCsarManager.class);
52 public static final int CACHE = 100 * 1024;
54 private DownloadCsarManager() {
55 // private constructor
59 * Download from given URL.
64 public static String download(String url) {
65 return download(url, null);
69 * Download from given URL to given file location.
72 * @param filepath String
75 public static String download(String url, String filepath) {
78 CloseableHttpClient client = HttpClients.createDefault();
79 HttpGet httpget = new HttpGet(url);
80 CloseableHttpResponse response = client.execute(httpget);
82 HttpEntity entity = response.getEntity();
83 InputStream is = entity.getContent();
84 if(filepath == null) {
85 filepath = getFilePath(response); // NOSONAR
88 File file = new File(filepath);
89 file.getParentFile().mkdirs();
90 FileOutputStream fileout = new FileOutputStream(file);
92 byte[] buffer = new byte[CACHE];
94 while((ch = is.read(buffer)) != -1) {
95 fileout.write(buffer, 0, ch);
101 status = Constant.DOWNLOADCSAR_SUCCESS;
103 } catch(Exception e) {
104 status = Constant.DOWNLOADCSAR_FAIL;
105 LOG.error("Download csar file failed! " + e.getMessage(), e);
111 * Retrieve file path from given response.
113 * @param response HttpResponse
116 public static String getFilePath(HttpResponse response) {
117 String filepath = System.getProperty("java.home");
118 String filename = getFileName(response);
120 if(filename != null) {
121 filepath += filename;
123 filepath += getRandomFileName();
129 * Retrieve file name from given response.
131 * @param response HttpResponse
134 public static String getFileName(HttpResponse response) {
135 Header contentHeader = response.getFirstHeader("Content-Disposition");
136 String filename = null;
137 if(contentHeader != null) {
138 HeaderElement[] values = contentHeader.getElements();
139 if(values.length == 1) {
140 NameValuePair param = values[0].getParameterByName("filename");
143 filename = param.getValue();
144 } catch(Exception e) {
145 LOG.error("getting filename failed! " + e.getMessage(), e);
154 * Provides random file name.
158 public static String getRandomFileName() {
159 return String.valueOf(System.currentTimeMillis());
165 * @param fileName filePath
167 * @throws IOException
169 public static int unzipCSAR(String fileName, String filePath) {
170 final int BUFFER = 2048;
172 ZipFile zipFile = null;
174 zipFile = new ZipFile(fileName);
175 Enumeration emu = zipFile.entries();
176 while(emu.hasMoreElements()) {
177 ZipEntry entry = (ZipEntry)emu.nextElement();
178 // read directory as file first,so only need to create directory
179 if(entry.isDirectory()) {
180 new File(filePath + entry.getName()).mkdirs();
183 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
184 File file = new File(filePath + entry.getName());
185 // Because that is random to read zipfile,maybe the file is read first
186 // before the directory is read,so we need to create directory first.
187 File parent = file.getParentFile();
188 if(parent != null && (!parent.exists())) {
191 FileOutputStream fos = new FileOutputStream(file);
192 BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
195 byte data[] = new byte[BUFFER];
196 while((count = bis.read(data, 0, BUFFER)) != -1) {
197 bos.write(data, 0, count);
204 status = Constant.UNZIP_SUCCESS;
206 } catch(Exception e) {
207 status = Constant.UNZIP_FAIL;
208 LOG.error("Exception: " + e);
210 if(zipFile != null) {
213 } catch(IOException e) {
214 LOG.error("IOException: " + e);