From 39191aaa6cf9f9d164c995e580c7df418b59b5c9 Mon Sep 17 00:00:00 2001 From: Krishnajinka Date: Mon, 16 Jul 2018 17:27:59 +0900 Subject: FIX TO USE TRY WITH RESOURCES FIX SONAR ISSUES RELATED WITH USE OF TRY WITH RESOURCES INSTEAD OF JUST TRY Issue-ID: PORTAL-336 Change-Id: I143c3d6167b101e5113faa3c70f57bfb8638e8de Signed-off-by: Krishnajinka --- .../analytics/model/definition/ReportSchedule.java | 19 +++++++++++-------- .../onap/portalsdk/analytics/scheduler/SendEmail.java | 19 ++++++++++--------- .../fusion/controller/FileServletController.java | 19 ++++--------------- 3 files changed, 25 insertions(+), 32 deletions(-) (limited to 'ecomp-sdk/epsdk-analytics/src/main/java') diff --git a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/definition/ReportSchedule.java b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/definition/ReportSchedule.java index b3ec072d..d56391ef 100644 --- a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/definition/ReportSchedule.java +++ b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/definition/ReportSchedule.java @@ -1223,16 +1223,19 @@ public class ReportSchedule extends RaptorObject implements Serializable{ int len = 0; char[] buffer = new char[512]; - Reader in = null; - in = new InputStreamReader(clob.getAsciiStream()); - // if(obj instanceof oracle.sql.CLOB) { - // in = ((oracle.sql.CLOB) obj).getCharacterStream(); - // } else if (obj instanceof weblogic.jdbc.wrapper.Clob) { - // in = ((weblogic.jdbc.base.BaseClob) obj).getCharacterStream(); - // } + try(Reader in = new InputStreamReader(clob.getAsciiStream())) { + // if(obj instanceof oracle.sql.CLOB) { + // in = ((oracle.sql.CLOB) obj).getCharacterStream(); + // } else if (obj instanceof weblogic.jdbc.wrapper.Clob) { + // in = ((weblogic.jdbc.base.BaseClob) obj).getCharacterStream(); + // } while ((len = in.read(buffer)) != -1) sb.append(buffer, 0, len); - in.close(); + } catch(Exception e) { + //if any error while operating the input stream, just throw the error out + //so that outer try/catch block could handle it + throw e; + } } else if (Globals.isPostgreSQL() || Globals.isMySQL()) { String clob= null; Object obj = null; diff --git a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/scheduler/SendEmail.java b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/scheduler/SendEmail.java index 7722d9ed..1f6b4917 100644 --- a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/scheduler/SendEmail.java +++ b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/scheduler/SendEmail.java @@ -171,9 +171,12 @@ SchedulerUtil schedulerUtil; params, types ); - FileInputStream fileStream = new FileInputStream(readFile); - schedulerUtil.updateBinaryStream("update cr_report_file_history set file_blob = ? where hist_id = ?", v_hist_rec.hist_id, fileStream, v_hist_rec.file_size); - fileStream.close(); + try(FileInputStream fileStream = new FileInputStream(readFile)) { + schedulerUtil.updateBinaryStream("update cr_report_file_history set file_blob = ? where hist_id = ?", v_hist_rec.hist_id, fileStream, v_hist_rec.file_size); + }catch (Exception e){ + //throw the exception to outer block for handling it + throw e; + } String userAddRecSql = Globals.getSchedulerUserEmails().replace("[p_schedule_id]", p_schedule_id+""); @@ -401,18 +404,16 @@ SchedulerUtil schedulerUtil; InputStream in = con.getInputStream(); try { - - FileOutputStream out = new FileOutputStream(outputFolder + java.io.File.separator + fileName ); - try { + + try (FileOutputStream out = new FileOutputStream(outputFolder + java.io.File.separator + fileName )) { int inputLine; while ((inputLine = in.read()) != -1) { out.write(inputLine); } out.flush(); - } - finally { - out.close(); + } catch(Exception e){ + throw e; } } diff --git a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java index d54e0579..a03c9539 100644 --- a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java +++ b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java @@ -141,12 +141,10 @@ public class FileServletController { private void serveFile(HttpServletResponse response, File inFile) throws Exception { - OutputStream os = null; - InputStream is = null; - try { + + try(InputStream is = new BufferedInputStream(new FileInputStream(inFile)); + OutputStream os = new BufferedOutputStream(response.getOutputStream());) { response.reset(); - is = new BufferedInputStream(new FileInputStream(inFile)); - os = new BufferedOutputStream(response.getOutputStream()); response.setContentLength((int) inFile.length()); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment; filename=\"" @@ -154,16 +152,7 @@ public class FileServletController { copyStream(is, os); os.flush(); } catch (Exception ex) { - if (os == null) - throw new Exception("Could not open output stream for file "); - if (is == null) - throw new Exception("Could not open input stream for file "); - } finally { - if (os != null) { - os.close(); - } - if (is != null) - is.close(); + throw new Exception("Could not open input or output stream for file "); } } -- cgit 1.2.3-korg