Fix all bugs reported by Sonar in policy/engine
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / PDPServices.java
index e495c99..af7112e 100644 (file)
@@ -21,10 +21,10 @@ package org.onap.policy.pdp.rest.api.services;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
-import java.net.MalformedURLException;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -37,6 +37,7 @@ import javax.json.JsonReader;
 import javax.xml.XMLConstants;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
@@ -65,6 +66,7 @@ import com.att.research.xacml.api.Request;
 import com.att.research.xacml.api.Response;
 import com.att.research.xacml.api.Result;
 import com.att.research.xacml.api.pdp.PDPEngine;
+import com.att.research.xacml.api.pdp.PDPException;
 import com.att.research.xacml.std.json.JSONRequest;
 import com.att.research.xacml.std.json.JSONResponse;
 import com.att.research.xacml.util.XACMLProperties;
@@ -126,7 +128,7 @@ public class PDPServices {
                 LOGGER.info("Decision not a Permit. "  + result.getDecision().toString());
                 PDPResponse pdpResponse = new PDPResponse();
                 if (decide) {
-                       String indeterminatePropValue = XACMLProperties.getProperty("decision.inStringdeterminate.response");
+                       String indeterminatePropValue = XACMLProperties.getProperty("decision.indeterminate.response");
                        if(result.getDecision().equals(Decision.INDETERMINATE)&& indeterminatePropValue != null){
                                if("PERMIT".equalsIgnoreCase(indeterminatePropValue)){
                                        pdpResponse.setDecision(PolicyDecision.PERMIT);
@@ -333,18 +335,19 @@ public class PDPServices {
        return treatment;
     }
 
-    private PDPResponse configCall(String pdpConfigLocation) throws Exception{
+    private PDPResponse configCall(String pdpConfigLocation) throws PDPException, IOException{
         PDPResponse pdpResponse = new PDPResponse();
         if(pdpConfigLocation.contains("/")){
             pdpConfigLocation = pdpConfigLocation.replace("/", File.separator);
         }
         InputStream inputStream = null;
+        JsonReader jsonReader = null;
         try {
             inputStream = new FileInputStream(new File(pdpConfigLocation));
             try {
                 if (pdpConfigLocation.endsWith("json")) {
                     pdpResponse.setType(PolicyType.JSON);
-                    JsonReader jsonReader = Json.createReader(inputStream);
+                    jsonReader = Json.createReader(inputStream);
                     pdpResponse.setConfig(jsonReader.readObject().toString());
                     jsonReader.close();
                 } else if (pdpConfigLocation.endsWith("xml")) {
@@ -366,7 +369,7 @@ public class PDPServices {
                         pdpResponse.setConfig(writer.toString());
                     } catch (Exception e) {
                         LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID+ e);
-                        throw new Exception(XACMLErrorConstants.ERROR_SCHEMA_INVALID+ "Unable to parse the XML config", e);
+                        throw new PDPException(XACMLErrorConstants.ERROR_SCHEMA_INVALID+ "Unable to parse the XML config", e);
                     }
                 } else if (pdpConfigLocation.endsWith("properties")) {
                     pdpResponse.setType(PolicyType.PROPERTIES);
@@ -394,14 +397,22 @@ public class PDPServices {
                         PolicyResponseStatus.NO_ACTION_REQUIRED,
                         PolicyConfigStatus.CONFIG_RETRIEVED);
                 return pdpResponse;
-            } catch (IOException e) {
+            } catch (IOException | ParserConfigurationException e) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
-                throw new Exception(XACMLErrorConstants.ERROR_PROCESS_FLOW +
+                throw new PDPException(XACMLErrorConstants.ERROR_PROCESS_FLOW +
                         "Cannot open a connection to the configURL", e);
+            } finally {
+                if(jsonReader != null) {
+                    try {
+                        jsonReader.close();
+                    } catch (Exception e) {
+                        LOGGER.error("Exception Occured while closing the JsonReader"+e);
+                    }
+                }
             }
-        } catch (MalformedURLException e) {
+        } catch (FileNotFoundException e) {
             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
-            throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in ConfigURL", e);
+            throw new PDPException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in ConfigURL", e);
         }finally{
                if(inputStream != null){
                inputStream.close();
@@ -409,8 +420,7 @@ public class PDPServices {
         }
     }
 
-    private Response callPDP(Request request,
-            UUID requestID) throws Exception{
+    private Response callPDP(Request request, UUID requestID){
         Response response = null;
         // Get the PDPEngine
         if (requestID == null) {