From 2f3daf7701f9d0a6a4388c9391eeeb91bbdfb3b6 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Thu, 26 Jul 2018 17:26:56 +0900 Subject: Fix sonar issues Fix use try-with-resource issue in portal Issue-ID: PORTAL-342 Change-Id: Ieda3384feddae688c66890743978eaf4deda41e0 Signed-off-by: Parshad Patel --- .../controller/WidgetsCatalogController.java | 52 ++++++++++++---------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'ecomp-portal-BE-common/src/main/java/org/onap/portalapp') diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java index d05fe014..a7ce3557 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java @@ -377,32 +377,38 @@ public class WidgetsCatalogController extends EPRestrictedBaseController { .getBody(); File downloadFile = File.createTempFile("temp", ".zip"); - FileOutputStream stream = new FileOutputStream(downloadFile.getPath()); - stream.write(byteFile); - stream.close(); - - FileInputStream inputStream = new FileInputStream(downloadFile); - String mimeType = context.getMimeType(downloadFile.getPath()); - if (mimeType == null) { - mimeType = "application/octet-stream"; + try(FileOutputStream stream = new FileOutputStream(downloadFile.getPath())){ + stream.write(byteFile); + }catch(Exception e) + { + logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e); + throw e; } - response.setContentType(mimeType); - response.setContentLength((int) downloadFile.length()); - String headerKey = "Content-Disposition"; - String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); - downloadFile.delete(); - response.setHeader(headerKey, headerValue); - - OutputStream outStream = response.getOutputStream(); - byte[] buffer = new byte[32 * 1024]; - int bytesRead; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outStream.write(buffer, 0, bytesRead); - } + try(FileInputStream inputStream = new FileInputStream(downloadFile); + OutputStream outStream = response.getOutputStream()){ + String mimeType = context.getMimeType(downloadFile.getPath()); + if (mimeType == null) { + mimeType = "application/octet-stream"; + } - inputStream.close(); - outStream.close(); + response.setContentType(mimeType); + response.setContentLength((int) downloadFile.length()); + String headerKey = "Content-Disposition"; + String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); + downloadFile.delete(); + response.setHeader(headerKey, headerValue); + + byte[] buffer = new byte[32 * 1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outStream.write(buffer, 0, bytesRead); + } + }catch(Exception e) + { + logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e); + throw e; + } } @RequestMapping(value = { "/portalApi/microservices/parameters" }, method = RequestMethod.POST) -- cgit 1.2.3-korg