diff options
author | Krishnajinka <kris.jinka@samsung.com> | 2018-07-16 17:27:59 +0900 |
---|---|---|
committer | Krishnajinka <kris.jinka@samsung.com> | 2018-07-16 17:28:37 +0900 |
commit | 39191aaa6cf9f9d164c995e580c7df418b59b5c9 (patch) | |
tree | 6c6b05033b1bd63a8d95be2dc2ed30f6bb16f852 | |
parent | a96a3e49cd472aa902c22143358b87562603d47c (diff) |
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 <kris.jinka@samsung.com>
4 files changed, 39 insertions, 48 deletions
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 "); } } diff --git a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java index 8d797c37..1ce03146 100644 --- a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java +++ b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java @@ -589,27 +589,25 @@ public class PortalRestAPIProxy extends HttpServlet implements IPortalRestAPISer String body = null; StringBuilder stringBuilder = new StringBuilder(); - BufferedReader bufferedReader = null; - try { - InputStream inputStream = request.getInputStream(); + + try(InputStream inputStream = request.getInputStream()) { if (inputStream != null) { - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - char[] charBuffer = new char[1024]; - int bytesRead = -1; - while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { - stringBuilder.append(charBuffer, 0, bytesRead); + try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));){ + char[] charBuffer = new char[1024]; + int bytesRead = -1; + while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { + stringBuilder.append(charBuffer, 0, bytesRead); + } + } catch(IOException e) { + logger.error("readRequestBody", e); + throw e; } } else { stringBuilder.append(""); } - } finally { - if (bufferedReader != null) { - try { - bufferedReader.close(); - } catch (IOException ex) { - logger.error("readRequestBody", ex); - } - } + } catch(IOException e) { + logger.error("readRequestBody", e); + throw e; } body = stringBuilder.toString(); return body; |