Fix sonar issues 63/57663/2
authorParshad Patel <pars.patel@samsung.com>
Thu, 26 Jul 2018 08:26:56 +0000 (17:26 +0900)
committerParshad Patel <pars.patel@samsung.com>
Fri, 3 Aug 2018 07:01:19 +0000 (16:01 +0900)
Fix use try-with-resource issue in portal

Issue-ID: PORTAL-342
Change-Id: Ieda3384feddae688c66890743978eaf4deda41e0
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
ecomp-portal-BE-common/src/main/java/jarutil/ExtractJar.java
ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/WidgetsCatalogController.java
ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/InitializationServiceImpl.java

index 7d3bfee..b550863 100644 (file)
@@ -72,47 +72,50 @@ public class ExtractJar {
                String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
                URL jarUrl = clazz.getProtectionDomain().getCodeSource().getLocation();
 
-               JarInputStream entryStream = new JarInputStream(jarUrl.openStream());
-               JarEntry entry;
-               while (true) {
-                       entry = entryStream.getNextJarEntry();
-                       if (entry == null)
-                               break;
-                       if (entry.getName().indexOf("jarutil") < 0) {
-                               logger.info(entry.getName());
-                               File file = new File(directory, entry.getName());
-                               if (entry.isDirectory()) {
-                                       if (!file.exists())
-                                               file.mkdirs();
-                               } else {
-                                       // make directory (some jars don't list dirs)
-                                       File dir = new File(file.getParent());
-                                       if (!dir.exists())
-                                               dir.mkdirs();
-                                       if (file.exists())
-                                               file.delete();
-                                       // Make file
-                                       FileOutputStream fout = new FileOutputStream(file);
-                                       copy(entryStream, fout);
-                                       fout.close();
-
-                                       // touch the file.
-                                       if (entry.getTime() >= 0)
-                                               file.setLastModified(entry.getTime());
+               try(JarInputStream entryStream = new JarInputStream(jarUrl.openStream())){
+                       JarEntry entry;
+                       while (true) {
+                               entry = entryStream.getNextJarEntry();
+                               if (entry == null)
+                                       break;
+                               if (entry.getName().indexOf("jarutil") < 0) {
+                                       logger.info(entry.getName());
+                                       File file = new File(directory, entry.getName());
+                                       if (entry.isDirectory()) {
+                                               if (!file.exists())
+                                                       file.mkdirs();
+                                       } else {
+                                               // make directory (some jars don't list dirs)
+                                               File dir = new File(file.getParent());
+                                               if (!dir.exists())
+                                                       dir.mkdirs();
+                                               if (file.exists())
+                                                       file.delete();
+                                               // Make file
+                                               FileOutputStream fout = new FileOutputStream(file);
+                                               copy(entryStream, fout);
+                                               fout.close();
+       
+                                               // touch the file.
+                                               if (entry.getTime() >= 0)
+                                                       file.setLastModified(entry.getTime());
+                                       }
+       
                                }
-
+                               entryStream.closeEntry();
                        }
-                       entryStream.closeEntry();
+                       System.out.println("************************************************");
+                       System.out.println("*                                              *");
+                       System.out.println("*                                              *");
+                       System.out.println("*          RAPTOR SETUP COMPLETE.              *");
+                       System.out.println("*                                              *");
+                       System.out.println("*         Thank you for upgrading.             *");
+                       System.out.println("*                                              *");
+                       System.out.println("************************************************");
+               }catch(Exception e) {
+                       logger.error("Exception in extractFilesFromJar",e);
                }
-               entryStream.close();
-               System.out.println("************************************************");
-               System.out.println("*                                              *");
-               System.out.println("*                                              *");
-               System.out.println("*          RAPTOR SETUP COMPLETE.              *");
-               System.out.println("*                                              *");
-               System.out.println("*         Thank you for upgrading.             *");
-               System.out.println("*                                              *");
-               System.out.println("************************************************");
+               
        }
 
        public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException {
index d05fe01..a7ce355 100644 (file)
@@ -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)
index cbc0c9d..c9365c4 100644 (file)
@@ -114,18 +114,13 @@ public class InitializationServiceImpl implements InitializationService {
                        long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
 
                        File tmpZipFile = new File("/tmp/" + fileLocation);
-                       InputStream fileInputStream = null;
-                       OutputStream outputStream = null;
-                       try {
-                               fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
-                               outputStream = new FileOutputStream(tmpZipFile);
+                       try(OutputStream outputStream = new FileOutputStream(tmpZipFile);
+                               InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation)) {
                                int read = 0;
                                byte[] bytes = new byte[4096];
                                while ((read = fileInputStream.read(bytes)) != -1) {
                                        outputStream.write(bytes, 0, read);
                                }
-                               outputStream.close();
-                               fileInputStream.close();
                        } catch (Exception e) {
                                logger.error(
                                                "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);