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.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Enumeration;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipFile;
31 import org.apache.http.Header;
32 import org.apache.http.HeaderElement;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.NameValuePair;
36 import org.apache.http.client.methods.CloseableHttpResponse;
37 import org.apache.http.client.methods.HttpGet;
38 import org.apache.http.impl.client.CloseableHttpClient;
39 import org.apache.http.impl.client.HttpClients;
40 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import net.sf.json.JSONArray;
45 import net.sf.json.JSONObject;
48 * Utility class to download CSAR
51 * @version NFVO 0.5 Sep 5, 2016
54 public class DownloadCsarManager {
56 private static final Logger LOG = LoggerFactory.getLogger(DownloadCsarManager.class);
58 public static final int CACHE = 100 * 1024;
60 private DownloadCsarManager(){
65 * Download from given URL.
69 public static String download(String url) {
70 return download(url, null);
74 * Download from given URL to given file location.
76 * @param filepath String
79 public static String download(String url, String filepath) {
82 CloseableHttpClient client = HttpClients.createDefault();
83 HttpGet httpget = new HttpGet(url);
84 CloseableHttpResponse response = client.execute(httpget);
86 HttpEntity entity = response.getEntity();
87 InputStream is = entity.getContent();
88 if (filepath == null){
89 filepath = getFilePath(response); //NOSONAR
92 File file = new File(filepath);
93 file.getParentFile().mkdirs();
94 FileOutputStream fileout = new FileOutputStream(file);
96 byte[] buffer = new byte[CACHE];
98 while ((ch = is.read(buffer)) != -1) {
99 fileout.write(buffer,0,ch);
104 status = Constant.DOWNLOADCSAR_SUCCESS;
106 } catch (Exception e) {
107 status = Constant.DOWNLOADCSAR_FAIL;
108 LOG.error("Download csar file failed! "+ e.getMessage(), e);
114 * Retrieve file path from given response.
115 * @param response HttpResponse
118 public static String getFilePath(HttpResponse response) {
119 String filepath = System.getProperty("java.home");
120 String filename = getFileName(response);
122 if (filename != null) {
123 filepath += filename;
125 filepath += getRandomFileName();
131 * Retrieve file name from given response.
132 * @param response HttpResponse
135 public static String getFileName(HttpResponse response) {
136 Header contentHeader = response.getFirstHeader("Content-Disposition");
137 String filename = null;
138 if (contentHeader != null) {
139 HeaderElement[] values = contentHeader.getElements();
140 if (values.length == 1) {
141 NameValuePair param = values[0].getParameterByName("filename");
144 filename = param.getValue();
145 } catch (Exception e) {
146 LOG.error("getting filename failed! "+ e.getMessage(), e);
155 * Provides random file name.
158 public static String getRandomFileName() {
159 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();
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())
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)
198 bos.write(data, 0, count);
204 if(entry.getName().endsWith(".zip")){
205 File subFile = new File(filePath+entry.getName());
206 if(subFile.exists()){
207 int subStatus = unzipCSAR(filePath+entry.getName(),subFile.getParent()+"/");
209 LOG.error("sub file unzip fail!"+subFile.getName());
210 status=Constant.UNZIP_FAIL;
216 status=Constant.UNZIP_SUCCESS;
218 } catch (Exception e) {
219 status=Constant.UNZIP_FAIL;
225 private static String getImagesPath(String csarfilepath){
226 File imageFile = new File(csarfilepath+"SoftwareImages");
227 if(imageFile.exists()){
228 File[] charmFiles = imageFile.listFiles();
229 for(File file : charmFiles){
230 if(!file.getName().endsWith(".zip")){
231 return file.getAbsolutePath();
237 public static void main(String[] args) {
238 System.out.println(getImagesPath("e:/juju/csar2/"));