FIX TO USE TRY WITH RESOURCES 87/56487/1
authorKrishnajinka <kris.jinka@samsung.com>
Mon, 16 Jul 2018 08:27:59 +0000 (17:27 +0900)
committerKrishnajinka <kris.jinka@samsung.com>
Mon, 16 Jul 2018 08:28:37 +0000 (17:28 +0900)
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>
ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/definition/ReportSchedule.java
ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/scheduler/SendEmail.java
ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java
ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java

index b3ec072..d56391e 100644 (file)
@@ -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;
index 7722d9e..1f6b491 100644 (file)
@@ -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;
                        }
                        
                }
index d54e057..a03c953 100644 (file)
@@ -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 ");
                }
        }
 
index 8d797c3..1ce0314 100644 (file)
@@ -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;