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.InputStream;
24 import java.util.Enumeration;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
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;
42 * Utility class to download CSAR
45 * @version VFC 1.0 Sep 5, 2016
47 public class DownloadCsarManager {
49 private static final Logger LOG = LoggerFactory.getLogger(DownloadCsarManager.class);
51 public static final int CACHE = 100 * 1024;
53 private DownloadCsarManager() {
54 // private constructor
58 * Download from given URL.
63 public static String download(String url) {
64 return download(url, null);
68 * Download from given URL to given file location.
71 * @param filepath String
74 public static String download(String url, String filepath) {
77 CloseableHttpClient client = HttpClients.createDefault();
78 HttpGet httpget = new HttpGet(url);
79 CloseableHttpResponse response = client.execute(httpget);
81 HttpEntity entity = response.getEntity();
82 InputStream is = entity.getContent();
83 if(filepath == null) {
84 filepath = getFilePath(response); // NOSONAR
87 File file = new File(filepath);
88 file.getParentFile().mkdirs();
89 FileOutputStream fileout = new FileOutputStream(file);
91 byte[] buffer = new byte[CACHE];
93 while((ch = is.read(buffer)) != -1) {
94 fileout.write(buffer, 0, ch);
100 status = Constant.DOWNLOADCSAR_SUCCESS;
102 } catch(Exception e) {
103 status = Constant.DOWNLOADCSAR_FAIL;
104 LOG.error("Download csar file failed! " + e.getMessage(), e);
110 * Retrieve file path from given response.
112 * @param response HttpResponse
115 public static String getFilePath(HttpResponse response) {
116 String filepath = System.getProperty("java.home");
117 String filename = getFileName(response);
119 if(filename != null) {
120 filepath += filename;
122 filepath += getRandomFileName();
128 * Retrieve file name from given response.
130 * @param response HttpResponse
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");
142 filename = param.getValue();
143 } catch(Exception e) {
144 LOG.error("getting filename failed! " + e.getMessage(), e);
153 * Provides random file name.
157 public static String getRandomFileName() {
158 return String.valueOf(System.currentTimeMillis());
164 * @param fileName filePath
167 public static int unzipCSAR(String fileName, String filePath) {
168 final int BUFFER = 2048;
172 ZipFile zipFile = new ZipFile(fileName);
173 Enumeration emu = zipFile.entries();
174 while(emu.hasMoreElements()) {
175 ZipEntry entry = (ZipEntry)emu.nextElement();
176 // read directory as file first,so only need to create directory
177 if(entry.isDirectory()) {
178 new File(filePath + entry.getName()).mkdirs();
181 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
182 File file = new File(filePath + entry.getName());
183 // Because that is random to read zipfile,maybe the file is read first
184 // before the directory is read,so we need to create directory first.
185 File parent = file.getParentFile();
186 if(parent != null && (!parent.exists())) {
189 FileOutputStream fos = new FileOutputStream(file);
190 BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
193 byte data[] = new byte[BUFFER];
194 while((count = bis.read(data, 0, BUFFER)) != -1) {
195 bos.write(data, 0, count);
201 status = Constant.UNZIP_SUCCESS;
203 } catch(Exception e) {
204 status = Constant.UNZIP_FAIL;