fix try block 67/56367/1
authorKrishnajinka <kris.jinka@samsung.com>
Fri, 13 Jul 2018 08:54:49 +0000 (17:54 +0900)
committerKrishnajinka <kris.jinka@samsung.com>
Fri, 13 Jul 2018 09:11:36 +0000 (18:11 +0900)
Issue-ID: POLICY-961

Change-Id: Ia1f388368007a4a82a57520dc6ddd99cc484a393
Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/PAPRestConfig.java
ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java
ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateNewMicroServiceModel.java
ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java
ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java
POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java
POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java

index 1b9afe5..7e514ef 100644 (file)
@@ -57,9 +57,8 @@ public class PAPRestConfig extends WebMvcConfigurerAdapter {
        @PostConstruct
        public void init(){
                Properties prop = new Properties();
-               InputStream input = null;
-               try {
-                       input = new FileInputStream("xacml.pap.properties");
+
+               try(InputStream input = new FileInputStream("xacml.pap.properties")) {
                        // load a properties file
                        prop.load(input);
                        setDbDriver(prop.getProperty("javax.persistence.jdbc.driver"));
@@ -68,14 +67,6 @@ public class PAPRestConfig extends WebMvcConfigurerAdapter {
                        setDbPassword( CryptoUtils.decryptTxtNoExStr(prop.getProperty("javax.persistence.jdbc.password", "")));
                }catch(Exception e){
                        LOGGER.error("Exception Occured while loading properties file"+e);
-               }finally{
-                       if(input != null){
-                               try {
-                                       input.close();
-                               } catch (IOException e) {
-                                       LOGGER.error("Exception Occured while clsoing the stream"+e);
-                               }
-                       }
                }
        }
        
index 48eb784..b808e58 100644 (file)
@@ -80,24 +80,15 @@ public class ConfigPolicy extends Policy {
        
        // Saving the Configurations file at server location for config policy.
        protected void saveConfigurations(String policyName) {
-               BufferedWriter bw = null;
-               try {
-                       String fileName = getConfigFile(policyName);
-                       bw = new BufferedWriter(new FileWriter(CONFIG_HOME + File.separator + fileName));
+
+               String fileName = getConfigFile(policyName);
+               try(BufferedWriter bw = new BufferedWriter(new FileWriter(CONFIG_HOME + File.separator + fileName))) {
                        bw.write(configBodyData);
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Configuration is succesfully saved");
                        }
                } catch (IOException e) {
                        LOGGER.error("Exception Occured while writing Configuration Data"+e);
-               } finally {
-                   if(bw != null){
-                    try {
-                        bw.close();
-                    } catch (Exception e) {
-                        LOGGER.error("Exception Occured while closing the BufferedWriter"+e);
-                    }
-               }
                }
        }
 
index 7757c2f..0679e79 100644 (file)
@@ -185,9 +185,8 @@ public class CreateNewMicroServiceModel {
            int BUFFER = 2048;
            File file = new File(zipFile);
 
-           ZipFile zip = null;
-               try {
-                       zip = new ZipFile("ExtractDir" + File.separator +file);
+
+               try(ZipFile zip = new ZipFile("ExtractDir" + File.separator +file)) {
                    String newPath =  zipFile.substring(0, zipFile.length() - 4);
                    new File(newPath).mkdir();
                    Enumeration zipFileEntries = zip.entries();
@@ -225,14 +224,6 @@ public class CreateNewMicroServiceModel {
                    }
            } catch (IOException e) {
                        logger.error("Failed to unzip model file " + zipFile + e);
-               }finally{
-                       if(zip != null){
-                               try {
-                                       zip.close();
-                               } catch (Exception e) {
-                                       logger.error("Exception Occured while closing the zip file"+e);
-                               }
-                       }
                }
        }
 
index a8449e3..60b35fb 100644 (file)
@@ -1923,18 +1923,15 @@ public class PolicyDBDao {
                
                private String readConfigFile(String configPath){
                        String configDataString = null;
-                       InputStream configContentStream = null;
-                       try {
-                               configContentStream = new FileInputStream(configPath);
+
+                       try(InputStream configContentStream = new FileInputStream(configPath)) {
                                configDataString = IOUtils.toString(configContentStream);
                        } catch (FileNotFoundException e) {
                                logger.error("Caught FileNotFoundException on new FileInputStream("+configPath+")",e);
                                throw new IllegalArgumentException("The config file path does not exist");
                        } catch(IOException e2){
-                               logger.error("Caught IOException on newIOUtils.toString("+configContentStream+")",e2);
+                               logger.error("Caught IOException on newIOUtils.toString(configContentStream)",e2);
                                throw new IllegalArgumentException("The config file path cannot be read");
-                       } finally {
-                               IOUtils.closeQuietly(configContentStream);
                        }
                        if(configDataString == null){
                                throw new IllegalArgumentException("The config file path cannot be read");
index af7112e..8722de4 100644 (file)
@@ -340,10 +340,9 @@ public class PDPServices {
         if(pdpConfigLocation.contains("/")){
             pdpConfigLocation = pdpConfigLocation.replace("/", File.separator);
         }
-        InputStream inputStream = null;
+
         JsonReader jsonReader = null;
-        try {
-            inputStream = new FileInputStream(new File(pdpConfigLocation));
+        try(InputStream inputStream = new FileInputStream(new File(pdpConfigLocation))) {
             try {
                 if (pdpConfigLocation.endsWith("json")) {
                     pdpResponse.setType(PolicyType.JSON);
@@ -413,10 +412,6 @@ public class PDPServices {
         } catch (FileNotFoundException e) {
             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
             throw new PDPException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in ConfigURL", e);
-        }finally{
-               if(inputStream != null){
-               inputStream.close();
-               }
         }
     }
 
index 8df9d1b..21f15bb 100644 (file)
@@ -338,10 +338,17 @@ public class PolicyRestController extends RestrictedBaseController{
                                // get the response content into a String
                                String responseJson = null;
                                // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
-                               java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream());
-                               scanner.useDelimiter("\\A");
-                               responseJson =  scanner.hasNext() ? scanner.next() : "";
-                               scanner.close();
+                               try(java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream())) {
+                                       scanner.useDelimiter("\\A");
+                                       responseJson = scanner.hasNext() ? scanner.next() : "";
+                               } catch (Exception e) {
+                                       //Reason for rethrowing the exception is if any exception occurs during reading of inputsteam
+                                       //then the exception handling is done by the outer block without returning the response immediately
+                                       //Also finally block is existing only in outer block and not here so all exception handling is
+                                       //done in only one place
+                                       policyLogger.error("Exception Occured"+e);
+                                       throw e;
+                               }
                                policyLogger.info("JSON response from PAP: " + responseJson);
                                return responseJson;
                        }
index a8831ea..b14a881 100644 (file)
@@ -455,10 +455,13 @@ public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAP
                        // get the response content into a String
                        String json = null;
                                // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
-                           java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream());
-                           scanner.useDelimiter("\\A");
-                           json =  scanner.hasNext() ? scanner.next() : "";
-                           scanner.close();
+                           try(java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream())) {
+                                               scanner.useDelimiter("\\A");
+                                               json = scanner.hasNext() ? scanner.next() : "";
+                                       }catch(Exception e) {
+                                               LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to read inputStream from connection: " + e, e);
+                               throw e;
+                                       }
                            LOGGER.info("JSON response from PAP: " + json);
                        
                        // convert Object sent as JSON into local object