/*
- * Copyright 2016 Huawei Technologies Co., Ltd.
+ * Copyright 2016-2017 Huawei Technologies Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
-import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import net.sf.json.JSONArray;
-import net.sf.json.JSONObject;
-
/**
* Utility class to download CSAR
*
* @author
- * @version VFC 1.0 Sep 5, 2016
- *
+ * @version VFC 1.0 Sep 5, 2016
*/
public class DownloadCsarManager {
public static final int CACHE = 100 * 1024;
- private DownloadCsarManager(){
- //private constructor
+ private DownloadCsarManager() {
+ // private constructor
}
/**
* Download from given URL.
+ *
* @param url String
* @return
*/
/**
* Download from given URL to given file location.
+ *
* @param url String
* @param filepath String
* @return
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
- if (filepath == null){
- filepath = getFilePath(response); //NOSONAR
+ if(filepath == null) {
+ filepath = getFilePath(response); // NOSONAR
}
File file = new File(filepath);
byte[] buffer = new byte[CACHE];
int ch;
- while ((ch = is.read(buffer)) != -1) {
- fileout.write(buffer,0,ch);
+ while((ch = is.read(buffer)) != -1) {
+ fileout.write(buffer, 0, ch);
}
is.close();
fileout.flush();
fileout.close();
status = Constant.DOWNLOADCSAR_SUCCESS;
- } catch (Exception e) {
+ } catch(Exception e) {
status = Constant.DOWNLOADCSAR_FAIL;
- LOG.error("Download csar file failed! "+ e.getMessage(), e);
+ LOG.error("Download csar file failed! " + e.getMessage(), e);
}
return status;
}
/**
* Retrieve file path from given response.
+ *
* @param response HttpResponse
* @return
*/
String filepath = System.getProperty("java.home");
String filename = getFileName(response);
- if (filename != null) {
+ if(filename != null) {
filepath += filename;
} else {
filepath += getRandomFileName();
/**
* Retrieve file name from given response.
+ *
* @param response HttpResponse
* @return
*/
public static String getFileName(HttpResponse response) {
Header contentHeader = response.getFirstHeader("Content-Disposition");
String filename = null;
- if (contentHeader != null) {
+ if(contentHeader != null) {
HeaderElement[] values = contentHeader.getElements();
- if (values.length == 1) {
+ if(values.length == 1) {
NameValuePair param = values[0].getParameterByName("filename");
- if (param != null) {
+ if(param != null) {
try {
filename = param.getValue();
- } catch (Exception e) {
- LOG.error("getting filename failed! "+ e.getMessage(), e);
+ } catch(Exception e) {
+ LOG.error("getting filename failed! " + e.getMessage(), e);
}
}
}
/**
* Provides random file name.
+ *
* @return
*/
public static String getRandomFileName() {
return String.valueOf(System.currentTimeMillis());
}
-
+
/**
* unzip CSAR packge
+ *
* @param fileName filePath
* @return
*/
- public static int unzipCSAR(String fileName,String filePath){
- final int BUFFER = 2048;
- int status=0;
-
+ public static int unzipCSAR(String fileName, String filePath) {
+ final int BUFFER = 2048;
+ int status = 0;
+
try {
ZipFile zipFile = new ZipFile(fileName);
Enumeration emu = zipFile.entries();
- int i=0;
- while(emu.hasMoreElements()){
+ int i = 0;
+ while(emu.hasMoreElements()) {
ZipEntry entry = (ZipEntry)emu.nextElement();
- //read directory as file first,so only need to create directory
- if (entry.isDirectory())
- {
+ // read directory as file first,so only need to create directory
+ if(entry.isDirectory()) {
new File(filePath + entry.getName()).mkdirs();
continue;
}
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
File file = new File(filePath + entry.getName());
- //Because that is random to read zipfile,maybe the file is read first
- //before the directory is read,so we need to create directory first.
+ // Because that is random to read zipfile,maybe the file is read first
+ // before the directory is read,so we need to create directory first.
File parent = file.getParentFile();
- if(parent != null && (!parent.exists())){
+ if(parent != null && (!parent.exists())) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file);
- BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER);
-
+ BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
+
int count;
byte data[] = new byte[BUFFER];
- while ((count = bis.read(data, 0, BUFFER)) != -1)
- {
+ while((count = bis.read(data, 0, BUFFER)) != -1) {
bos.write(data, 0, count);
}
bos.flush();
bos.close();
bis.close();
}
- status=Constant.UNZIP_SUCCESS;
+ status = Constant.UNZIP_SUCCESS;
zipFile.close();
- } catch (Exception e) {
- status=Constant.UNZIP_FAIL;
+ } catch(Exception e) {
+ status = Constant.UNZIP_FAIL;
e.printStackTrace();
}
return status;
}
-}
\ No newline at end of file
+}
import java.util.Map;
import org.apache.commons.lang.StringUtils;
+import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
-import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
if(StringUtils.isEmpty(type) || StringUtils.isEmpty(requestType) || StringUtils.isEmpty(vnfName)
|| StringUtils.isEmpty(vnfId)) {
LOG.error("function=grantVnfResource, msg=grant basic params error");
- resultJson.put("errorMsg", "basic params error");
+ resultJson.put(Constant.ERRORMSG, "basic params error");
return resultJson;
}
if(null == resMap) {
LOG.error("function=grantVnfResource, msg=grant resource params error");
- resultJson.put("errorMsg", "resource params error");
+ resultJson.put(Constant.ERRORMSG, "resource params error");
return resultJson;
}
LOG.error("function=grantVnfResource, resultJson={}.", resultJson);
} catch(JSONException e) {
LOG.error("function=grantVnfResource, msg=parse params occoured JSONException e={}.", e);
- resultJson.put("errorMsg", "params parse exception");
+ resultJson.put(Constant.ERRORMSG, "params parse exception");
}
return resultJson;