From bcfcf7438c3dbe3209c95b51d325f90db05a65f0 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Mon, 13 Aug 2018 13:30:18 +0900 Subject: Fix blocker,critical sonar issue Fix NPE, use try-with-resources issues Issue-ID: PORTAL-342 Change-Id: I9aaf805c012076bad5d803319cb5e3577471f635 Signed-off-by: Parshad Patel --- .../org/onap/portalapp/widget/utils/UnzipUtil.java | 96 +++++++++++----------- 1 file changed, 49 insertions(+), 47 deletions(-) (limited to 'ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java') diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java index d699e5da..f20ed1b5 100644 --- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java +++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java @@ -46,49 +46,51 @@ public class UnzipUtil { File destDir = new File(destDirectory); if (!destDir.exists()) destDir.mkdir(); - ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); - ZipEntry entry = zipIn.getNextEntry(); - Map map = new HashMap<>(); - - String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION, - WidgetConstant.WIDGET_STYLE_LOCATION }; - for (String k : requiredKeys) - map.put(k, null); - - // iterates over entries in the zip file - Stack stack = new Stack<>(); - while (entry != null) { - String filePath = destDirectory + File.separator + widgetName + File.separator - + entry.getName().substring(entry.getName().indexOf("/") + 1); - final String entryShortName = entry.getName().substring(entry.getName().indexOf("/") + 1); - logger.debug("UnzipUtil.unzip_db: file path {}, short name {}", filePath, entryShortName); - if (!entry.isDirectory()) { - // if the entry is a file, extracts it - logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath); - stack.push(new File(filePath)); - extractFile(zipIn, filePath); - } else { - // if the entry is a directory, make the directory - logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath); - File dir = new File(filePath); - stack.push(new File(filePath)); - dir.mkdir(); + try(ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) + { + ZipEntry entry = zipIn.getNextEntry(); + Map map = new HashMap<>(); + + String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION, + WidgetConstant.WIDGET_STYLE_LOCATION }; + for (String k : requiredKeys) + map.put(k, null); + + // iterates over entries in the zip file + Stack stack = new Stack<>(); + while (entry != null) { + String filePath = destDirectory + File.separator + widgetName + File.separator + + entry.getName().substring(entry.getName().indexOf("/") + 1); + final String entryShortName = entry.getName().substring(entry.getName().indexOf("/") + 1); + logger.debug("UnzipUtil.unzip_db: file path {}, short name {}", filePath, entryShortName); + if (!entry.isDirectory()) { + // if the entry is a file, extracts it + logger.debug("UnzipUtil.unzip_db: unzip and save widget file {}", filePath); + stack.push(new File(filePath)); + extractFile(zipIn, filePath); + } else { + // if the entry is a directory, make the directory + logger.debug("UnzipUtil.unzip_db: unzip and create widget folder {}", filePath); + File dir = new File(filePath); + stack.push(new File(filePath)); + dir.mkdir(); + } + // Is this one we need? + if (map.containsKey(entryShortName)) + map.put(entryShortName, Files.readAllBytes(Paths.get(filePath))); + zipIn.closeEntry(); + entry = zipIn.getNextEntry(); } - // Is this one we need? - if (map.containsKey(entryShortName)) - map.put(entryShortName, Files.readAllBytes(Paths.get(filePath))); - zipIn.closeEntry(); - entry = zipIn.getNextEntry(); + + while (!stack.isEmpty()) + stack.pop().delete(); + + for (String k : requiredKeys) + if (!map.containsKey(k)) + logger.warn("UnzipUtil.unzip_db: no zip archive entry found for required key {}", k); + + return map; } - zipIn.close(); - while (!stack.isEmpty()) - stack.pop().delete(); - - for (String k : requiredKeys) - if (!map.containsKey(k)) - logger.warn("UnzipUtil.unzip_db: no zip archive entry found for required key {}", k); - - return map; } /** @@ -99,12 +101,12 @@ public class UnzipUtil { * @throws IOException */ private void extractFile(ZipInputStream zipIn, String filePath) throws IOException { - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); - byte[] bytesIn = new byte[BUFFER_SIZE]; - int read = 0; - while ((read = zipIn.read(bytesIn)) != -1) { - bos.write(bytesIn, 0, read); + try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))){ + byte[] bytesIn = new byte[BUFFER_SIZE]; + int read = 0; + while ((read = zipIn.read(bytesIn)) != -1) { + bos.write(bytesIn, 0, read); + } } - bos.close(); } } \ No newline at end of file -- cgit 1.2.3-korg