2 * Copyright 2016 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.gvnfm.jujuvnfmadapter.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.gvnfm.jujuvnfmadapter.service.constant.Constant;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
43 * Utility class to download CSAR
46 * @version NFVO 0.5 Sep 5, 2016
49 public class DownloadCsarManager {
51 private static final Logger LOG = LoggerFactory.getLogger(DownloadCsarManager.class);
53 public static final int CACHE = 100 * 1024;
55 private DownloadCsarManager(){
60 * 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.
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 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);
98 status = Constant.DOWNLOADCSAR_SUCCESS;
100 } catch (Exception e) {
101 status = Constant.DOWNLOADCSAR_FAIL;
102 LOG.error("Download csar file failed! "+ e.getMessage(), e);
108 * Retrieve file path from given response.
109 * @param response HttpResponse
112 public static String getFilePath(HttpResponse response) {
113 String filepath = System.getProperty("java.home");
114 String filename = getFileName(response);
116 if (filename != null) {
117 filepath += filename;
119 filepath += getRandomFileName();
125 * Retrieve file name from given response.
126 * @param response HttpResponse
129 public static String getFileName(HttpResponse response) {
130 Header contentHeader = response.getFirstHeader("Content-Disposition");
131 String filename = null;
132 if (contentHeader != null) {
133 HeaderElement[] values = contentHeader.getElements();
134 if (values.length == 1) {
135 NameValuePair param = values[0].getParameterByName("filename");
138 filename = param.getValue();
139 } catch (Exception e) {
140 LOG.error("getting filename failed! "+ e.getMessage(), e);
149 * Provides random file name.
152 public static String getRandomFileName() {
153 return String.valueOf(System.currentTimeMillis());
158 * @param fileName filePath
161 public static int unzipCSAR(String fileName,String filePath){
162 final int BUFFER = 2048;
164 try (ZipFile zipFile = new ZipFile(fileName)){
165 Enumeration emu = zipFile.entries();
166 while(emu.hasMoreElements()){
167 ZipEntry entry = (ZipEntry)emu.nextElement();
168 //read directory as file first,so only need to create directory
169 if (entry.isDirectory())
171 new File(filePath + entry.getName()).mkdirs();
174 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
175 File file = new File(filePath + entry.getName());
176 //Because that is random to read zipfile,maybe the file is read first
177 //before the directory is read,so we need to create directory first.
178 File parent = file.getParentFile();
179 if(parent != null && (!parent.exists())){
182 try(FileOutputStream fos = new FileOutputStream(file);
183 BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER)){
185 byte[] data = new byte[BUFFER];
186 while ((count = bis.read(data, 0, BUFFER)) != -1)
188 bos.write(data, 0, count);
193 if(entry.getName().endsWith(".zip")){
194 File subFile = new File(filePath+entry.getName());
195 if(subFile.exists()){
196 int subStatus = unzipCSAR(filePath+entry.getName(),subFile.getParent()+"/");
198 LOG.error("sub file unzip fail!"+subFile.getName());
199 status=Constant.UNZIP_FAIL;
205 status=Constant.UNZIP_SUCCESS;
207 } catch (Exception e) {
208 status=Constant.UNZIP_FAIL;
209 LOG.error("unzipCSAR Exception: ",e);
215 private static String getImagesPath(String csarfilepath){
216 File imageFile = new File(csarfilepath+"SoftwareImages");
217 if(imageFile.exists()){
218 File[] charmFiles = imageFile.listFiles();
219 for(File file : charmFiles){
220 if(!file.getName().endsWith(".zip")){
221 return file.getAbsolutePath();
227 public static void main(String[] args) {
228 LOG.info("AbsolutePath: " + getImagesPath("e:/juju/csar2/"));