From d3c7059189d25ecc20d1406d8aa31db6c73c0e7f Mon Sep 17 00:00:00 2001 From: ajay priyadarshi Date: Mon, 12 Mar 2018 15:14:21 +0530 Subject: sonar fix: resource & logging issues try-with resource & logging issues fixed file name: DownloadCsarManager.java Change-Id: I60f8f95601c7314dabdaf92ee5f6967488462e52 Issue-ID: VFC-800 Signed-off-by: ajay priyadarshi --- .../common/DownloadCsarManager.java | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'juju/juju-vnfmadapter') diff --git a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/DownloadCsarManager.java b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/DownloadCsarManager.java index 79f8032..d246705 100644 --- a/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/DownloadCsarManager.java +++ b/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/DownloadCsarManager.java @@ -78,8 +78,8 @@ public class DownloadCsarManager { */ public static String download(String url, String filepath) { String status = ""; - try { - CloseableHttpClient client = HttpClients.createDefault(); + try( + CloseableHttpClient client = HttpClients.createDefault()){ HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = client.execute(httpget); @@ -91,16 +91,17 @@ public class DownloadCsarManager { File file = new File(filepath); file.getParentFile().mkdirs(); - FileOutputStream fileout = new FileOutputStream(file); + try(FileOutputStream fileout = new FileOutputStream(file)){ byte[] buffer = new byte[CACHE]; int ch; while ((ch = is.read(buffer)) != -1) { fileout.write(buffer,0,ch); } + } is.close(); - fileout.flush(); - fileout.close(); + //fileout.flush(); + //fileout.close(); status = Constant.DOWNLOADCSAR_SUCCESS; } catch (Exception e) { @@ -168,8 +169,7 @@ public class DownloadCsarManager { final int BUFFER = 2048; int status=0; - try { - ZipFile zipFile = new ZipFile(fileName); + try ( ZipFile zipFile = new ZipFile(fileName)){ Enumeration emu = zipFile.entries(); int i=0; while(emu.hasMoreElements()){ @@ -188,8 +188,8 @@ public class DownloadCsarManager { if(parent != null && (!parent.exists())){ parent.mkdirs(); } - FileOutputStream fos = new FileOutputStream(file); - BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER); + try(FileOutputStream fos = new FileOutputStream(file); + BufferedOutputStream bos = new BufferedOutputStream(fos,BUFFER)){ int count; byte data[] = new byte[BUFFER]; @@ -197,8 +197,9 @@ public class DownloadCsarManager { { bos.write(data, 0, count); } - bos.flush(); - bos.close(); + } + //bos.flush(); + //bos.close(); bis.close(); if(entry.getName().endsWith(".zip")){ @@ -217,7 +218,9 @@ public class DownloadCsarManager { zipFile.close(); } catch (Exception e) { status=Constant.UNZIP_FAIL; - e.printStackTrace(); + //e.printStackTrace(); + LOG.error("unzipCSAR Exception: ",e); + } return status; } @@ -237,4 +240,4 @@ public class DownloadCsarManager { public static void main(String[] args) { System.out.println(getImagesPath("e:/juju/csar2/")); } -} \ No newline at end of file +} -- cgit 1.2.3-korg