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) {
77 try (CloseableHttpClient client = HttpClients.createDefault()){
78 HttpGet httpget = new HttpGet(url);
79 CloseableHttpResponse response = client.execute(httpget);
81 HttpEntity entity = response.getEntity();
82 try(InputStream is = entity.getContent()) {
83 if(filepath == null) {
84 filepath = getFilePath(response); // NOSONAR
87 File file = new File(filepath);
88 file.getParentFile().mkdirs();
89 try(FileOutputStream fileout = new FileOutputStream(file)){
91 byte[] buffer = new byte[CACHE];
93 while((ch = is.read(buffer)) != -1) {
94 fileout.write(buffer, 0, ch);
97 status = Constant.DOWNLOADCSAR_SUCCESS;
98 } catch(Exception e) {
99 status = Constant.DOWNLOADCSAR_FAIL;
100 LOG.error("Download csar file failed! " + e.getMessage(), e);
102 } catch(Exception e) {
103 status = Constant.DOWNLOADCSAR_FAIL;
104 LOG.error("Download csar file failed! " + e.getMessage(), e);
106 } catch(Exception e) {
107 status = Constant.DOWNLOADCSAR_FAIL;
108 LOG.error("Download csar file failed! " + e.getMessage(), e);
115 * Retrieve file path from given response.
117 * @param response HttpResponse
120 public static String getFilePath(HttpResponse response) {
121 String filepath = System.getProperty("java.home");
122 String filename = getFileName(response);
124 if(filename != null) {
125 filepath += filename;
127 filepath += getRandomFileName();
133 * Retrieve file name from given response.
135 * @param response HttpResponse
138 public static String getFileName(HttpResponse response) {
139 Header contentHeader = response.getFirstHeader("Content-Disposition");
140 String filename = null;
141 if(contentHeader != null) {
142 HeaderElement[] values = contentHeader.getElements();
143 if(values.length == 1) {
144 NameValuePair param = values[0].getParameterByName("filename");
147 filename = param.getValue();
148 } catch(Exception e) {
149 LOG.error("getting filename failed! " + e.getMessage(), e);
158 * Provides random file name.
162 public static String getRandomFileName() {
163 return String.valueOf(System.currentTimeMillis());
169 * @param fileName filePath
171 * @throws IOException
173 public static int unzipCSAR(String fileName, String filePath) {
174 final int BUFFER = 2048;
175 int status = Constant.UNZIP_SUCCESS;
177 try(ZipFile zipFile = new ZipFile(fileName);) {
178 Enumeration emu = zipFile.entries();
179 while(emu.hasMoreElements()) {
180 ZipEntry entry = (ZipEntry)emu.nextElement();
181 // read directory as file first,so only need to create directory
182 if(entry.isDirectory()) {
183 new File(filePath + entry.getName()).mkdirs();
186 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
187 File file = new File(filePath + entry.getName());
188 // Because that is random to read zipfile,maybe the file is read first
189 // before the directory is read,so we need to create directory first.
190 File parent = file.getParentFile();
191 if(parent != null && (!parent.exists())) {
194 try(FileOutputStream fos = new FileOutputStream(file);
195 BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);){
197 byte data[] = new byte[BUFFER];
198 while((count = bis.read(data, 0, BUFFER)) != -1) {
199 bos.write(data, 0, count);
204 } catch(Exception e) {
205 status = Constant.UNZIP_FAIL;
206 LOG.error("Exception: " + e);